Skip to content

Commit 1b0bce0

Browse files
committed
Allow connecting to localhost without SSL
This makes it possible to use https://github.com/matrix-org/pantalaimon for end-to-end encryption.
1 parent a04e007 commit 1b0bce0

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

Diff for: miniirc_matrix.py

+29-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import miniirc, requests, traceback # type: ignore
1313

1414

15-
ver = (0, 0, 2)
15+
ver = (0, 0, 3)
1616
__version__ = '.'.join(map(str, ver))
1717

1818

@@ -388,24 +388,44 @@ def __init__(self, ip: str, port: int = 0, nick: str = '', *args,
388388
self.__session.headers['Authorization'] = f'Bearer {token}'
389389
if auto_connect:
390390
self.connect()
391+
else:
392+
self._update_baseurl()
393+
394+
def _update_baseurl(self) -> None:
395+
hostname = f'{self.ip}:{self.port}'
396+
if (not self.ssl and self.ssl is not None and
397+
self.ip in ('localhost', '127.0.0.1', '::1', '0::1')):
398+
# Non-SSL localhost connections are probably to
399+
# https://github.com/matrix-org/pantalaimon which doesn't support
400+
# the "v3" URLs yet.
401+
protocol = 'http'
402+
api_version = 'r0'
403+
else:
404+
protocol = 'https'
405+
api_version = 'v3'
406+
407+
# Use a shorter hostname if possible
408+
if self.port == 443:
409+
hostname = self.ip
391410

392-
def _url_for(self, endpoint: str) -> str:
393-
return f'https://{self.ip}:{self.port}/_matrix/client/v3/{endpoint}'
411+
matrix_url = f'{protocol}://{hostname}/_matrix'
412+
self._baseurl = f'{matrix_url}/client/{api_version}'
413+
self._media_baseurl = f'{matrix_url}/media/{api_version}'
394414

395415
def __get(self, endpoint: str, timeout: int = 5, /,
396416
**params: Optional[str | int]) -> Any:
397417
self.debug('GET', endpoint, params)
398-
return self.__session.get(self._url_for(endpoint), params=params,
418+
return self.__session.get(f'{self._baseurl}/{endpoint}', params=params,
399419
timeout=timeout).json()
400420

401421
def __post(self, endpoint: str, /, **params: Any) -> Any:
402422
self.debug('POST', endpoint, params)
403-
return self.__session.post(self._url_for(endpoint), json=params,
423+
return self.__session.post(f'{self._baseurl}/{endpoint}', json=params,
404424
timeout=5).json()
405425

406426
def __put(self, endpoint: str, /, **params: Any) -> Any:
407427
self.debug('PUT', endpoint, params)
408-
return self.__session.put(self._url_for(endpoint), json=params,
428+
return self.__session.put(f'{self._baseurl}/{endpoint}', json=params,
409429
timeout=5).json()
410430

411431
def _get_room_url_no_cache(self, room_id: str) -> str:
@@ -427,6 +447,7 @@ def connect(self) -> None:
427447
if self.connected is not None:
428448
return
429449
with self._send_lock:
450+
self._update_baseurl()
430451
self.active_caps = self.ircv3_caps & {
431452
'account-tag', 'echo-message', 'message-tags',
432453
}
@@ -610,8 +631,7 @@ def _message_event(self, room_id: str, event: _Event) -> None:
610631
if 'url' in content:
611632
msg = content.url[str]
612633
if msg.startswith('mxc://'):
613-
msg = (f'https://{self.ip}:{self.port}/_matrix/media/v3/'
614-
f'download/{msg[6:]}')
634+
msg = f'{self._media_baseurl}/download/{msg[6:]}'
615635
else:
616636
msg, html_parsed_ok = _matrix_html_to_irc(content)
617637

@@ -697,7 +717,7 @@ def _logout(cls, homeserver: str, token: str) -> None:
697717
def _logout_all(cls, homeserver: str, username: str,
698718
password: str) -> None:
699719
""" Logs out everywhere / voids all tokens for the user. """
700-
token = cls.login(homeserver, username, password)
720+
token = cls._login(homeserver, username, password)
701721
matrix = Matrix(homeserver, token=token, auto_connect=False)
702722
res = matrix.__post('logout/all')
703723
assert 'error' not in res, res

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='miniirc_matrix',
8-
version='0.0.2',
8+
version='0.0.3',
99
py_modules=['miniirc_matrix'],
1010
author='luk3yx',
1111
description='A Matrix wrapper for miniirc.',

0 commit comments

Comments
 (0)