Auto-detect homeserver scheme://
This commit is contained in:
		@@ -2,7 +2,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
import logging as log
 | 
					import logging as log
 | 
				
			||||||
import math
 | 
					 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
@@ -11,6 +10,7 @@ import traceback
 | 
				
			|||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
from typing import Any, DefaultDict, Dict, List, Optional, Tuple
 | 
					from typing import Any, DefaultDict, Dict, List, Optional, Tuple
 | 
				
			||||||
 | 
					from urllib.parse import urlparse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import aiohttp
 | 
					import aiohttp
 | 
				
			||||||
from appdirs import AppDirs
 | 
					from appdirs import AppDirs
 | 
				
			||||||
@@ -148,16 +148,44 @@ class Backend:
 | 
				
			|||||||
        Possible login methods include `m.login.password` or `m.login.sso`.
 | 
					        Possible login methods include `m.login.password` or `m.login.sso`.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if not re.match(r"https?://", homeserver):
 | 
				
			||||||
 | 
					            homeserver = f"http://{homeserver}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client   = MatrixClient(self, homeserver=homeserver)
 | 
					        client   = MatrixClient(self, homeserver=homeserver)
 | 
				
			||||||
 | 
					        http_re  = re.compile("^http://")
 | 
				
			||||||
 | 
					        is_local = urlparse(client.homeserver).netloc.split(":")[0] in (
 | 
				
			||||||
 | 
					            "localhost", "127.0.0.1", "::1",
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            client.homeserver = (await client.discovery_info()).homeserver_url
 | 
					            client.homeserver = (await client.discovery_info()).homeserver_url
 | 
				
			||||||
        except (MatrixNotFound, MatrixForbidden):
 | 
					        except MatrixError:
 | 
				
			||||||
            # This is either already the real URL, or an invalid URL.
 | 
					            # This is either already the real URL, or an invalid URL.
 | 
				
			||||||
            pass
 | 
					            pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            return (client.homeserver, (await client.login_info()).flows)
 | 
					            try:
 | 
				
			||||||
 | 
					                login_response = await client.login_info()
 | 
				
			||||||
 | 
					            except (asyncio.TimeoutError, MatrixError):
 | 
				
			||||||
 | 
					                # Maybe we still have a http URL and server only supports https
 | 
				
			||||||
 | 
					                client.homeserver = http_re.sub("https://", client.homeserver)
 | 
				
			||||||
 | 
					                login_response    = await client.login_info()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # If we still have a http URL and server redirected to https
 | 
				
			||||||
 | 
					            if login_response.transport_response.real_url.scheme == "https":
 | 
				
			||||||
 | 
					                client.homeserver = http_re.sub("https://", client.homeserver)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # If we still have a http URL and server accept both http and https
 | 
				
			||||||
 | 
					            if http_re.match(client.homeserver) and not is_local:
 | 
				
			||||||
 | 
					                original          = client.homeserver
 | 
				
			||||||
 | 
					                client.homeserver = http_re.sub("https://", client.homeserver)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                try:
 | 
				
			||||||
 | 
					                    await asyncio.wait_for(client.login_info(), timeout=6)
 | 
				
			||||||
 | 
					                except (asyncio.TimeoutError, MatrixError):
 | 
				
			||||||
 | 
					                    client.homeserver = original
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return (client.homeserver, login_response.flows)
 | 
				
			||||||
        finally:
 | 
					        finally:
 | 
				
			||||||
            await client.close()
 | 
					            await client.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -160,11 +160,6 @@ class MatrixClient(nio.AsyncClient):
 | 
				
			|||||||
         device_id:  Optional[str] = None,
 | 
					         device_id:  Optional[str] = None,
 | 
				
			||||||
    ) -> None:
 | 
					    ) -> None:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not urlparse(homeserver).scheme:
 | 
					 | 
				
			||||||
            raise ValueError(
 | 
					 | 
				
			||||||
                f"homeserver is missing scheme (e.g. https://): {homeserver}",
 | 
					 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        store = Path(backend.appdirs.user_data_dir) / "encryption"
 | 
					        store = Path(backend.appdirs.user_data_dir) / "encryption"
 | 
				
			||||||
        store.mkdir(parents=True, exist_ok=True)
 | 
					        store.mkdir(parents=True, exist_ok=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -253,6 +248,9 @@ class MatrixClient(nio.AsyncClient):
 | 
				
			|||||||
        response = await super()._send(*args, **kwargs)
 | 
					        response = await super()._send(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if isinstance(response, nio.ErrorResponse):
 | 
					        if isinstance(response, nio.ErrorResponse):
 | 
				
			||||||
 | 
					            for a in args:
 | 
				
			||||||
 | 
					                if isinstance(a, str) and "cyberia" in a:
 | 
				
			||||||
 | 
					                    import remote_pdb; remote_pdb.RemotePdb("127.0.0.1", 4444).set_trace()
 | 
				
			||||||
            raise MatrixError.from_nio(response)
 | 
					            raise MatrixError.from_nio(response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return response
 | 
					        return response
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -64,6 +64,8 @@ HBox {
 | 
				
			|||||||
            accepted()
 | 
					            accepted()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }, (type, args, error, traceback, uuid) => {
 | 
					        }, (type, args, error, traceback, uuid) => {
 | 
				
			||||||
 | 
					            console.error(traceback)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            connectTimeout.stop()
 | 
					            connectTimeout.stop()
 | 
				
			||||||
            connectFuture = null
 | 
					            connectFuture = null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -73,7 +75,7 @@ HBox {
 | 
				
			|||||||
            text = qsTr("Invalid homeserver address") :
 | 
					            text = qsTr("Invalid homeserver address") :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            type.startsWith("Matrix") ?
 | 
					            type.startsWith("Matrix") ?
 | 
				
			||||||
            text = qsTr("Error contacting server: %1").arg(type) :
 | 
					            text = qsTr("Connection failed: %1(%2)").arg(type).arg(args) :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            py.showError(type, traceback, uuid)
 | 
					            py.showError(type, traceback, uuid)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -130,11 +132,11 @@ HBox {
 | 
				
			|||||||
                readonly property string cleanText:
 | 
					                readonly property string cleanText:
 | 
				
			||||||
                    text.toLowerCase().trim().replace(/\/+$/, "")
 | 
					                    text.toLowerCase().trim().replace(/\/+$/, "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                error: text && ! /https?:\/\/.+/.test(cleanText)
 | 
					                inputMethodHints: Qt.ImhUrlCharactersOnly
 | 
				
			||||||
                defaultText: window.getState(
 | 
					                defaultText: window.getState(
 | 
				
			||||||
                    box, "acceptedUserUrl", "",
 | 
					                    box, "acceptedUserUrl", "",
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
                placeholderText: "https://example.org"
 | 
					                placeholderText: "example.org"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                Layout.fillWidth: true
 | 
					                Layout.fillWidth: true
 | 
				
			||||||
                Layout.fillHeight: true
 | 
					                Layout.fillHeight: true
 | 
				
			||||||
@@ -202,7 +204,9 @@ HBox {
 | 
				
			|||||||
            width: serverList.width
 | 
					            width: serverList.width
 | 
				
			||||||
            loadingIconStep: box.loadingIconStep
 | 
					            loadingIconStep: box.loadingIconStep
 | 
				
			||||||
            onClicked: {
 | 
					            onClicked: {
 | 
				
			||||||
                serverField.item.field.text = model.id
 | 
					                serverField.item.field.text =
 | 
				
			||||||
 | 
					                    model.id.replace(/^https:\/\//, "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                serverField.item.apply.clicked()
 | 
					                serverField.item.apply.clicked()
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user