Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

allow specifying https:// proxy #9119

Closed
wants to merge 11 commits into from
Closed

Conversation

Bubu
Copy link
Contributor

@Bubu Bubu commented Jan 14, 2021

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
  • Pull request includes a sign off
  • Code style is correct (run the linters)

synapse/http/proxyagent.py Outdated Show resolved Hide resolved
@Bubu Bubu marked this pull request as draft January 14, 2021 18:39
@Bubu
Copy link
Contributor Author

Bubu commented Jan 14, 2021

I'm trying to adapt the proxyagent tests for this... but it's not that easy to see what's going on and now having to nest two ssl connections inside each other isn't helping at all.

@clokep
Copy link
Contributor

clokep commented Jan 22, 2021

I'm trying to adapt the proxyagent tests for this... but it's not that easy to see what's going on and now having to nest two ssl connections inside each other isn't helping at all.

Please feel free to ask, probably best in #synapse-dev:matrix.org.

synapse/http/proxyagent.py Outdated Show resolved Hide resolved
@Bubu Bubu force-pushed the https_proxy branch 2 times, most recently from f01e490 to 8818309 Compare March 10, 2021 18:49
@Bubu Bubu marked this pull request as ready for review March 10, 2021 18:52
@Bubu Bubu requested a review from clokep March 10, 2021 18:52
@Bubu
Copy link
Contributor Author

Bubu commented Mar 11, 2021

Sorry for this being stale so long, I've rebased this on develop again (meanwhile #9372 has changed things up somewhat, so that required some cosmetic changes here as well).

The blocker preciously were missing tests, I think I found a good way to add tests for this now..

Copy link
Contributor

@clokep clokep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable overall! I left a few comments though.

synapse/http/proxyagent.py Outdated Show resolved Hide resolved
synapse/http/proxyagent.py Show resolved Hide resolved
synapse/http/proxyagent.py Show resolved Hide resolved
tests/http/test_proxyagent.py Show resolved Hide resolved
@clokep clokep self-requested a review March 11, 2021 16:20
clokep
clokep previously requested changes Mar 12, 2021
synapse/http/proxyagent.py Show resolved Hide resolved
synapse/http/proxyagent.py Outdated Show resolved Hide resolved
tests/http/test_proxyagent.py Show resolved Hide resolved
@f1-outsourcing
Copy link

f1-outsourcing commented Apr 20, 2021

I have this issue[1], I assume it is related to half implemented proxy support. Is it possible to get some patch or so, that I can apply to the pip package matrix-synapse. I would like to try matrix chat.

Or is there some indication when proxy support will be fully implemented?

[1]
#9852

@clokep
Copy link
Contributor

clokep commented Apr 21, 2021

I think #9119 (comment) is still an oustanding comment, by the way.

@erikjohnston
Copy link
Member

I think we're still waiting for #9119 (comment) to be dealt with, so removing review request for now. (There also seems to be some conflicts that need to be resolved)

@erikjohnston erikjohnston removed the request for review from a team April 28, 2021 14:02
@f1-outsourcing
Copy link

I tried this bubu https_proxy branch, but I am still getting these

matrix-synapse synapse.http.federation.matrix_federation_agent - 288 - INFO - GET-23 - Failed to connect to matrix-federation.matrix.org.cdn.cloudflare.net:8443: No route to host: 101: Network unreachable.

@anoadragon453
Copy link
Member

Note that the context for @f1-outsourcing's setup is at #9852.

@f1-outsourcing
Copy link

@anoadragon453 @Bubu

Note that the context for @f1-outsourcing's setup is at #9852.

Will this merge make it possible to use matrix without setting a default gateway and only use a forward and reverse proxy? Because this is still not clear to me. Or is this changing just a little part of the code, and then we have to change another little part, and then we have to change another little part. etc.
The reason why I am asking is, is it worth waiting for this merge, or should I just try an find an xmpp solution?

@f1-outsourcing
Copy link

@anoadragon453 @Bubu

Note that the context for @f1-outsourcing's setup is at #9852.

Will this merge make it possible to use matrix without setting a default gateway and only use a forward and reverse proxy? Because this is still not clear to me. Or is this changing just a little part of the code, and then we have to change another little part, and then we have to change another little part. etc.
The reason why I am asking is, is it worth waiting for this merge, or should I just try an find an xmpp solution?

If you do not know, would you mind stating this then, because I do not know what to expect now.

@erikjohnston erikjohnston added the X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged label May 20, 2021
@clokep clokep dismissed their stale review June 17, 2021 13:52

stale review

@richvdh richvdh removed the X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged label Jun 17, 2021
@f1-outsourcing

This comment has been minimized.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks generally good to me! A few small comments here.

synapse/http/proxyagent.py Outdated Show resolved Hide resolved
@@ -121,11 +122,11 @@ def __init__(
self.https_proxy_creds, https_proxy = parse_username_password(https_proxy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break for https://username:password@host, I think? Probably best to call parse_proxy here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why only credentials were parsed for https proxy? Only security reason or anything else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break for https://username:password@host, I think? Probably best to call parse_proxy here

I would suggest a solution like this.
This change can make parse_username_password compatible to https://username:password@host

diff --git a/synapse/http/proxyagent.py b/synapse/http/proxyagent.py
index c747b99..3c4fee6 100644
--- a/synapse/http/proxyagent.py
+++ b/synapse/http/proxyagent.py
@@ -293,7 +293,7 @@
 def parse_username_password(proxy: bytes) -> Tuple[Optional[ProxyCredentials], bytes]:
     """
     Parses the username and password from a proxy declaration e.g
-    username:password@hostname:port.
+    username:password@hostname:port or https://username:password@hostname:port
 
     Args:
         proxy: The proxy connection string.
@@ -304,9 +304,15 @@
         ProxyCredentials instance is replaced with None.
     """
     if proxy and b"@" in proxy:
+        scheme, host, port = parse_proxy(proxy)
         # We use rsplit here as the password could contain an @ character
-        credentials, proxy_without_credentials = proxy.rsplit(b"@", 1)
-        return ProxyCredentials(credentials), proxy_without_credentials
+        credentials, proxy_without_credentials = host.rsplit(b"@", 1)
+        return (
+            ProxyCredentials(credentials),
+            b"".join(
+                [scheme, b"://", proxy_without_credentials, b":", str(port).encode()]
+            ),
+        )
 
     return None, proxy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why only credentials were parsed for https proxy? Only security reason or anything else?

I don't think there is much reason at all. Support for credentials was added in #9657: perhaps http_proxy was just fogotten?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a solution like this.

seems sensible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why only credentials were parsed for https proxy? Only security reason or anything else?

I don't think there is much reason at all. Support for credentials was added in #9657: perhaps http_proxy was just fogotten?

In addition the issue for this #9000

tests/http/test_proxyagent.py Show resolved Hide resolved
synapse/http/proxyagent.py Outdated Show resolved Hide resolved
synapse/http/proxyagent.py Show resolved Hide resolved
tests/http/test_proxyagent.py Show resolved Hide resolved
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs support for <scheme>://<username>:<password>@<host>

@richvdh richvdh added the X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged label Jun 23, 2021
@richvdh
Copy link
Member

richvdh commented Jun 23, 2021

While I'm here, I'll just complain that it's a real shame that this code has been cut-and-pasted into sygnal, at https://github.com/matrix-org/sygnal/blob/main/sygnal/helper/proxy/proxyagent_twisted.py rather than factored out to a separate library. Now we have two versions to maintain :(.

@f1-outsourcing

This comment has been minimized.

synapse/http/proxyagent.py Outdated Show resolved Hide resolved
@richvdh
Copy link
Member

richvdh commented Jul 7, 2021

oh I hadn't realised that @Bubu had updated it since my last review. Let me stick it back in the queue for review.

Co-authored-by: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com>
@richvdh richvdh self-requested a review July 7, 2021 09:50
@richvdh richvdh removed the X-Awaiting-Changes A contributed PR which needs changes and re-review before it can be merged label Jul 7, 2021
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this just needs a couple of minor tweaks.

@Bubu are you still keen to work on this? It sounds like @dklimpel would be happy to finish it up if you're busy.

Comment on lines +85 to +86
This currently supports http:// and https:// proxies.
A hostname without scheme is assumed to be http.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit confusing. A hostname where?

... and yes, of course it supports http and https proxies - what other sorts of proxies would an http/https agent possibly care about?

Suggest removing these lines.

@@ -121,11 +122,11 @@ def __init__(
self.https_proxy_creds, https_proxy = parse_username_password(https_proxy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why only credentials were parsed for https proxy? Only security reason or anything else?

I don't think there is much reason at all. Support for credentials was added in #9657: perhaps http_proxy was just fogotten?

@@ -121,11 +122,11 @@ def __init__(
self.https_proxy_creds, https_proxy = parse_username_password(https_proxy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a solution like this.

seems sensible.

Comment on lines +329 to +330
if b"://" in proxy:
scheme, host = proxy.split(b"://", 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have thought about the problem with Python 3.9 and urlparse. See: #9119 (comment)

I would suggest to work with urlparse after all. For compatibility reasons we can check if there is a sheme and if not we add this. Somthing like this

Suggested change
if b"://" in proxy:
scheme, host = proxy.split(b"://", 1)
if not b"://" in proxy:
proxy = b"".join([default_scheme, b"://", proxy])

@richvdh
Copy link
Member

richvdh commented Jul 20, 2021

ok, I'm going to close this in favour of #10411. Thanks @dklimpel for taking up the baton.

@richvdh richvdh closed this Jul 20, 2021
@Bubu
Copy link
Contributor Author

Bubu commented Jul 20, 2021

Thanks for taking over! ❤️

@Bubu Bubu deleted the https_proxy branch July 22, 2021 12:42
@Bubu Bubu restored the https_proxy branch July 22, 2021 12:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for https traffic to the proxy
7 participants