Skip to content

Commit

Permalink
Allow connecting to localhost without SSL
Browse files Browse the repository at this point in the history
This makes it possible to use https://github.com/matrix-org/pantalaimon 
for end-to-end encryption.
  • Loading branch information
luk3yx committed Apr 19, 2022
1 parent a04e007 commit 1b0bce0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 29 additions & 9 deletions miniirc_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import miniirc, requests, traceback # type: ignore


ver = (0, 0, 2)
ver = (0, 0, 3)
__version__ = '.'.join(map(str, ver))


Expand Down Expand Up @@ -388,24 +388,44 @@ def __init__(self, ip: str, port: int = 0, nick: str = '', *args,
self.__session.headers['Authorization'] = f'Bearer {token}'
if auto_connect:
self.connect()
else:
self._update_baseurl()

def _update_baseurl(self) -> None:
hostname = f'{self.ip}:{self.port}'
if (not self.ssl and self.ssl is not None and
self.ip in ('localhost', '127.0.0.1', '::1', '0::1')):
# Non-SSL localhost connections are probably to
# https://github.com/matrix-org/pantalaimon which doesn't support
# the "v3" URLs yet.
protocol = 'http'
api_version = 'r0'
else:
protocol = 'https'
api_version = 'v3'

# Use a shorter hostname if possible
if self.port == 443:
hostname = self.ip

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

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

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

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

def _get_room_url_no_cache(self, room_id: str) -> str:
Expand All @@ -427,6 +447,7 @@ def connect(self) -> None:
if self.connected is not None:
return
with self._send_lock:
self._update_baseurl()
self.active_caps = self.ircv3_caps & {
'account-tag', 'echo-message', 'message-tags',
}
Expand Down Expand Up @@ -610,8 +631,7 @@ def _message_event(self, room_id: str, event: _Event) -> None:
if 'url' in content:
msg = content.url[str]
if msg.startswith('mxc://'):
msg = (f'https://{self.ip}:{self.port}/_matrix/media/v3/'
f'download/{msg[6:]}')
msg = f'{self._media_baseurl}/download/{msg[6:]}'
else:
msg, html_parsed_ok = _matrix_html_to_irc(content)

Expand Down Expand Up @@ -697,7 +717,7 @@ def _logout(cls, homeserver: str, token: str) -> None:
def _logout_all(cls, homeserver: str, username: str,
password: str) -> None:
""" Logs out everywhere / voids all tokens for the user. """
token = cls.login(homeserver, username, password)
token = cls._login(homeserver, username, password)
matrix = Matrix(homeserver, token=token, auto_connect=False)
res = matrix.__post('logout/all')
assert 'error' not in res, res
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='miniirc_matrix',
version='0.0.2',
version='0.0.3',
py_modules=['miniirc_matrix'],
author='luk3yx',
description='A Matrix wrapper for miniirc.',
Expand Down

0 comments on commit 1b0bce0

Please sign in to comment.