Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

proxy support: add support for proxy creds and no_proxy config #70

Open
MatMaul opened this issue Dec 4, 2020 · 8 comments
Open

proxy support: add support for proxy creds and no_proxy config #70

MatMaul opened this issue Dec 4, 2020 · 8 comments

Comments

@MatMaul
Copy link
Contributor

MatMaul commented Dec 4, 2020

Currently the credentials to use the proxy are injected through a local patch in the deploy machine.
Would be nice to parse that from the https_proxy string instead.

Also we would need some no_proxy support, since we want to be able to both reach internal Tchap Sygnal (no proxy) and matrix.org Sygnal (proxy needed).

@anoadragon453
Copy link
Member

Currently the credentials to use the proxy are injected through a local patch in the deploy machine.
Would be nice to parse that from the https_proxy string instead.

@MatMaul could you post the contents of the patch (without the credentials of course) so we could see what exactly you're changing?

Otherwise in terms of additional parsing, what we currently parse is done here:

def _http_proxy_endpoint(proxy, reactor, **kwargs):
"""Parses an http proxy setting and returns an endpoint for the proxy
Args:
proxy (bytes|None): the proxy setting
reactor: reactor to be used to connect to the proxy
kwargs: other args to be passed to HostnameEndpoint
Returns:
interfaces.IStreamClientEndpoint|None: endpoint to use to connect to the proxy,
or None
"""
if proxy is None:
return None
# currently we only support hostname:port. Some apps also support
# protocol://<host>[:port], which allows a way of requiring a TLS connection to the
# proxy.
host, port = parse_host_port(proxy, default_port=1080)
return HostnameEndpoint(reactor, host, port, **kwargs)
def parse_host_port(hostport, default_port=None):
# could have sworn we had one of these somewhere else...
if b":" in hostport:
host, port = hostport.rsplit(b":", 1)
try:
port = int(port)
return host, port
except ValueError:
# the thing after the : wasn't a valid port; presumably this is an
# IPv6 address.
pass
return hostport, default_port

Also we would need some no_proxy support, since we want to be able to both reach internal Tchap Sygnal (no proxy) and matrix.org Sygnal (proxy needed).

NO_PROXY is an environment variables consisting of a comma-separated list of domains that the proxy should not be used for.

This would involve adding a conditional to using the proxy for (for right now) requests to the Identity Service. There's a limited number of places that Synapse attempts to contact an identity server. It usually makes use of SimpleHttpClient to do so. However, I'm not currently sure how difficult it will be to turn the proxy on or off for different domains.

@richvdh
Copy link
Member

richvdh commented Jan 4, 2021

related: matrix-org/synapse#9000

@anoadragon453
Copy link
Member

The NO_PROXY side of this is being handled by this community PR: matrix-org/synapse#9372

@anoadragon453
Copy link
Member

The above PR has been merged 🎉

@MatMaul Still waiting on that patch for the proxy creds so we can upstream it (with some changes possibly)? 🙂

@MatMaul
Copy link
Contributor Author

MatMaul commented Mar 3, 2021

diff -u -r synapse-dinsic-dinsic12_2020-04-28/synapse/http/connectproxyclient.py synapse-dinsic-dinsic12_2020-04-28_hotfix1/synapse/http/connectproxyclient.py
--- synapse-dinsic-dinsic12_2020-04-28/synapse/http/connectproxyclient.py       2020-04-28 12:53:21.000000000 +0200
+++ synapse-dinsic-dinsic12_2020-04-28_hotfix1/synapse/http/connectproxyclient.py       2020-04-28 16:16:06.056143846 +0200
@@ -180,6 +180,7 @@
     def connectionMade(self):
         logger.debug("Connected to proxy, sending CONNECT")
         self.sendCommand(b"CONNECT", b"%s:%d" % (self.host, self.port))
+        self.sendHeader(b"Proxy-Authorization", b"basic secret==")
         self.endHeaders()

     def handleStatus(self, version, status, message):

@anoadragon453
Copy link
Member

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization#directives describes the method of transforming username/password information into a base64 encoded string.

It's just base64(username:password).

@anoadragon453
Copy link
Member

NO_PROXY support has been ported from mainline to dinsic in #93.

@anoadragon453
Copy link
Member

Opened a PR on mainline to address proxy credentials: matrix-org/synapse#9657

anoadragon453 added a commit to matrix-org/synapse that referenced this issue Mar 22, 2021
Addresses matrix-org/synapse-dinsic#70

This PR causes `ProxyAgent` to attempt to extract credentials from an `HTTPS_PROXY` env var. If credentials are found, a `Proxy-Authorization` header ([details](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization)) is sent to the proxy server to authenticate against it. The headers are *not* passed to the remote server.

Also added some type hints.
anoadragon453 added a commit that referenced this issue Mar 22, 2021
Addresses #70

This PR causes `ProxyAgent` to attempt to extract credentials from an `HTTPS_PROXY` env var. If credentials are found, a `Proxy-Authorization` header ([details](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization)) is sent to the proxy server to authenticate against it. The headers are *not* passed to the remote server.

Also added some type hints.
MatMaul pushed a commit that referenced this issue Mar 23, 2021
…ne (#95)

* Allow providing credentials to HTTPS_PROXY (#9657)

Addresses #70

This PR causes `ProxyAgent` to attempt to extract credentials from an `HTTPS_PROXY` env var. If credentials are found, a `Proxy-Authorization` header ([details](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization)) is sent to the proxy server to authenticate against it. The headers are *not* passed to the remote server.

Also added some type hints.

* lint
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants