Initial commit.

This commit is contained in:
Damir Jelić 2019-03-21 11:05:12 +01:00
commit d51d47334b
2 changed files with 118 additions and 0 deletions

95
main.py Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env python3
import attr
import asyncio
from aiohttp import web, ClientSession
from nio import AsyncClient, LoginResponse
HOMESERVER = "https://localhost:8448"
@attr.s
class ProxyDaemon:
homeserver = attr.ib()
proxy = attr.ib(default=None)
ssl = attr.ib(default=None)
client_sessions = attr.ib(init=False, default=attr.Factory(dict))
default_session = attr.ib(init=False, default=None)
async def router(self, request):
path = request.path
method = request.method
data = await request.text()
print(method, path, data)
if not self.default_session:
self.default_session = ClientSession()
async with self.default_session.request(
method,
HOMESERVER + path,
data=data,
proxy=self.proxy,
ssl=False
) as resp:
return(web.Response(text=await resp.text()))
async def login(self, request):
json = await request.json()
user = json.get("user", "")
password = json.get("password", "")
device_id = json.get("device_id", "")
device_name = json.get("initial_device_display_name", "")
client = AsyncClient(
HOMESERVER,
user,
device_id,
ssl=self.ssl,
proxy=self.proxy
)
print("Logging in")
response = await client.login(password, device_name)
if isinstance(response, LoginResponse):
self.client_sessions[response.access_token] = client
return web.Response(
status=response.transport_response.status,
text=await response.transport_response.text()
)
async def sync(self, request):
return web.Response(
status=405,
text="Not implemented"
)
async def init():
"""Initialize the proxy and the http server."""
proxy = ProxyDaemon(HOMESERVER, proxy="http://localhost:8080", ssl=False)
app = web.Application()
app.add_routes([
web.post("/_matrix/client/r0/login", proxy.login),
web.get("/_matrix/client/r0/sync", proxy.sync),
])
app.router.add_route('*', "/" + '{proxyPath:.*}', proxy.router)
return proxy, app
def main():
loop = asyncio.get_event_loop()
proxy, app = loop.run_until_complete(init())
web.run_app(app, host="127.0.0.1", port=8081)
if __name__ == "__main__":
main()

23
setup.py Normal file
View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(
name="pantalaimon",
version="0.1",
url="https://github.com/poljar/pantalaimon",
author="The Matrix.org Team",
author_email="poljar@termina.org.uk",
description=("A Matrix proxy daemon that adds E2E encryption "
"capabilities."),
license="Apache License, Version 2.0",
packages=find_packages(),
install_requires=[
"attrs",
"aiohttp",
"aiohttp-socks",
"typing;python_version<'3.5'",
"matrix-nio @ git+https://github.com/poljar/matrix-nio.git@master#egg=matrix-nio-0"
],
zip_safe=False
)