Skip to content

Commit

Permalink
Move contents of socket_utils into protocol module
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Jan 13, 2025
1 parent b2220f8 commit b1b1446
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions source/remoteClient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from .connection_info import ConnectionInfo, ConnectionMode
from .localMachine import LocalMachine
from .menu import RemoteMenu
from .protocol import RemoteMessageType
from .protocol import RemoteMessageType, addressToHostPort
from .secureDesktop import SecureDesktopHandler
from .session import MasterSession, SlaveSession
from .socket_utils import addressToHostPort, hostPortToAddress
from .protocol import hostPortToAddress
from .transport import RelayTransport

# Type aliases
Expand Down
4 changes: 2 additions & 2 deletions source/remoteClient/connection_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse

from . import socket_utils
from . import protocol
from .protocol import SERVER_PORT, URL_PREFIX


Expand Down Expand Up @@ -62,7 +62,7 @@ def getAddress(self):

def _build_url(self, mode: ConnectionMode):
# Build URL components
netloc = socket_utils.hostPortToAddress((self.hostname, self.port))
netloc = protocol.hostPortToAddress((self.hostname, self.port))
params = {
"key": self.key,
"mode": mode if isinstance(mode, str) else mode.value,
Expand Down
6 changes: 3 additions & 3 deletions source/remoteClient/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from logHandler import log
from utils.alwaysCallAfter import alwaysCallAfter

from . import configuration, serializer, server, socket_utils, transport
from . import configuration, serializer, server, protocol, transport
from .connection_info import ConnectionInfo, ConnectionMode
from .protocol import SERVER_PORT, RemoteMessageType

Expand Down Expand Up @@ -52,7 +52,7 @@ def onGenerateKey(self, evt: wx.CommandEvent) -> None:
self.generateKeyCommand()

def generateKeyCommand(self, insecure: bool = False) -> None:
address = socket_utils.addressToHostPort(self.host.GetValue())
address = protocol.addressToHostPort(self.host.GetValue())
self.keyConnector = transport.RelayTransport(
address=address,
serializer=serializer.JSONSerializer(),
Expand Down Expand Up @@ -288,7 +288,7 @@ def getKey(self) -> str:
def getConnectionInfo(self) -> ConnectionInfo:
if self.clientOrServer.GetSelection() == 0: # client
host = self.panel.host.GetValue()
serverAddr, port = socket_utils.addressToHostPort(host)
serverAddr, port = protocol.addressToHostPort(host)
mode = ConnectionMode.MASTER if self.connectionType.GetSelection() == 0 else ConnectionMode.SLAVE
return ConnectionInfo(
hostname=serverAddr,
Expand Down
18 changes: 18 additions & 0 deletions source/remoteClient/protocol.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import urllib
from enum import Enum

PROTOCOL_VERSION: int = 2
Expand Down Expand Up @@ -43,3 +44,20 @@ class RemoteMessageType(Enum):

SERVER_PORT = 6837
URL_PREFIX = "nvdaremote://"


def addressToHostPort(addr):
"""Converts an address such as google.com:80 into a tuple of (address, port).
If no port is given, use SERVER_PORT."""
addr = urllib.parse.urlparse("//" + addr)
port = addr.port or SERVER_PORT
return (addr.hostname, port)


def hostPortToAddress(hostPort):
host, port = hostPort
if ":" in host:
host = "[" + host + "]"
if port != SERVER_PORT:
return host + ":" + str(port)
return host
20 changes: 0 additions & 20 deletions source/remoteClient/socket_utils.py

This file was deleted.

2 changes: 1 addition & 1 deletion source/remoteClient/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from .connection_info import ConnectionInfo
from .protocol import PROTOCOL_VERSION, RemoteMessageType
from .serializer import Serializer
from .socket_utils import hostPortToAddress
from .protocol import hostPortToAddress

log = getLogger("transport")

Expand Down

0 comments on commit b1b1446

Please sign in to comment.