Skip to content

Commit

Permalink
Clients: use certifi (ArchipelagoMW#1879)
Browse files Browse the repository at this point in the history
* Clients: use certifi for wss

On Windows, the local cert store might be outdated and refuse connection to some servers.

* Clients: lazily create ssl_context
  • Loading branch information
black-sliver authored Jun 21, 2023
1 parent b04b105 commit a939f50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from Utils import Version, stream_input, async_start
from worlds import network_data_package, AutoWorldRegister
import os
import ssl

if typing.TYPE_CHECKING:
import kvui
Expand All @@ -33,6 +34,12 @@
gui_enabled = not sys.stdout or "--nogui" not in sys.argv


@Utils.cache_argsless
def get_ssl_context():
import certifi
return ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=certifi.where())


class ClientCommandProcessor(CommandProcessor):
def __init__(self, ctx: CommonContext):
self.ctx = ctx
Expand Down Expand Up @@ -589,7 +596,8 @@ def reconnect_hint() -> str:

logger.info(f'Connecting to Archipelago server at {address}')
try:
socket = await websockets.connect(address, port=port, ping_timeout=None, ping_interval=None)
socket = await websockets.connect(address, port=port, ping_timeout=None, ping_interval=None,
ssl=get_ssl_context() if address.startswith("wss://") else None)
if ctx.ui is not None:
ctx.ui.update_address_bar(server_url.netloc)
ctx.server = Endpoint(socket)
Expand All @@ -604,6 +612,7 @@ def reconnect_hint() -> str:
except websockets.InvalidMessage:
# probably encrypted
if address.startswith("ws://"):
# try wss
await server_loop(ctx, "ws" + address[1:])
else:
ctx.handle_connection_loss(f"Lost connection to the multiworld server due to InvalidMessage"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ schema>=0.7.5
kivy>=2.2.0
bsdiff4>=1.2.3
platformdirs>=3.5.1
certifi>=2023.5.7

0 comments on commit a939f50

Please sign in to comment.