From 860c17a1bda97eb246891cc57cabbaccc3c16455 Mon Sep 17 00:00:00 2001 From: Thomas Ieong Date: Wed, 13 Dec 2023 15:14:45 +0100 Subject: [PATCH] Add proxy support --- maubot/cli/cliq/cliq.py | 4 ++-- maubot/client.py | 2 +- maubot/management/api/client_proxy.py | 5 ++++- maubot/standalone/__main__.py | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/maubot/cli/cliq/cliq.py b/maubot/cli/cliq/cliq.py index 2883441..91533ce 100644 --- a/maubot/cli/cliq/cliq.py +++ b/maubot/cli/cliq/cliq.py @@ -35,7 +35,7 @@ from .validators import ClickValidator, Required def with_http(func): @functools.wraps(func) async def wrapper(*args, **kwargs): - async with aiohttp.ClientSession() as sess: + async with aiohttp.ClientSession(trust_env=True) as sess: try: return await func(*args, sess=sess, **kwargs) except aiohttp.ClientError as e: @@ -50,7 +50,7 @@ def with_authenticated_http(func): server, token = get_token(server) if not token: return - async with aiohttp.ClientSession(headers={"Authorization": f"Bearer {token}"}) as sess: + async with aiohttp.ClientSession(headers={"Authorization": f"Bearer {token}"}, trust_env=True) as sess: try: return await func(*args, sess=sess, server=server, **kwargs) except aiohttp.ClientError as e: diff --git a/maubot/client.py b/maubot/client.py index bdb76fc..d09f9a9 100644 --- a/maubot/client.py +++ b/maubot/client.py @@ -139,7 +139,7 @@ class Client(DBClient): self._postinited = True self.cache[self.id] = self self.log = self.log.getChild(self.id) - self.http_client = ClientSession(loop=self.maubot.loop) + self.http_client = ClientSession(loop=self.maubot.loop, trust_env=True) self.references = set() self.started = False self.sync_ok = True diff --git a/maubot/management/api/client_proxy.py b/maubot/management/api/client_proxy.py index 3fa682b..d39f140 100644 --- a/maubot/management/api/client_proxy.py +++ b/maubot/management/api/client_proxy.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from aiohttp import client as http, web +from urllib.request import getproxies from ...client import Client from .base import routes @@ -45,8 +46,10 @@ async def proxy(request: web.Request) -> web.StreamResponse: headers["X-Forwarded-For"] = f"{host}:{port}" data = await request.read() + proxies = getproxies() async with http.request( - request.method, f"{client.homeserver}/{path}", headers=headers, params=query, data=data + request.method, f"{client.homeserver}/{path}", headers=headers, params=query, data=data, + proxy=proxies["https"] if "https" in proxies else None ) as proxy_resp: response = web.StreamResponse(status=proxy_resp.status, headers=proxy_resp.headers) await response.prepare(request) diff --git a/maubot/standalone/__main__.py b/maubot/standalone/__main__.py index 6d3150d..a72cb08 100644 --- a/maubot/standalone/__main__.py +++ b/maubot/standalone/__main__.py @@ -235,7 +235,7 @@ if appservice_listener: async def main(): - http_client = ClientSession(loop=loop) + http_client = ClientSession(loop=loop, trust_env=True) global client, bot