This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
support federation queries through http connect proxy #9306
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add support for sending federation requests through a proxy. Contributed by @Bubu. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -279,6 +279,7 @@ def __init__(self, hs, tls_client_options_factory): | |
tls_client_options_factory, | ||
user_agent, | ||
hs.config.federation_ip_range_blacklist, | ||
proxy_reactor=hs.get_reactor(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given that we already pass the ip blacklist into |
||
) | ||
|
||
# Use a BlacklistingAgentWrapper to prevent circumventing the IP | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||
# -*- coding: utf-8 -*- | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is no longer needed. See #9786 |
||||
# Copyright 2019 New Vector Ltd | ||||
# | ||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|
@@ -12,10 +13,11 @@ | |||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
import logging | ||||
from typing import Optional | ||||
from unittest.mock import Mock | ||||
import os | ||||
from unittest.mock import patch | ||||
|
||||
import treq | ||||
from mock import Mock | ||||
from netaddr import IPSet | ||||
from service_identity import VerificationError | ||||
from zope.interface import implementer | ||||
|
@@ -109,7 +111,7 @@ def setUp(self): | |||
_well_known_resolver=self.well_known_resolver, | ||||
) | ||||
|
||||
def _make_connection(self, client_factory, expected_sni): | ||||
def _make_connection(self, client_factory, expected_sni=None): | ||||
"""Builds a test server, and completes the outgoing client connection | ||||
|
||||
Returns: | ||||
|
@@ -146,12 +148,13 @@ def _make_connection(self, client_factory, expected_sni): | |||
self.reactor.pump((0.1,)) | ||||
|
||||
# check the SNI | ||||
server_name = server_tls_connection.get_servername() | ||||
self.assertEqual( | ||||
server_name, | ||||
expected_sni, | ||||
"Expected SNI %s but got %s" % (expected_sni, server_name), | ||||
) | ||||
if expected_sni is not None: | ||||
server_name = server_tls_connection.get_servername() | ||||
self.assertEqual( | ||||
server_name, | ||||
expected_sni, | ||||
"Expected SNI %s but got %s" % (expected_sni, server_name), | ||||
) | ||||
|
||||
return http_protocol | ||||
|
||||
|
@@ -179,11 +182,7 @@ def _make_get_request(self, uri): | |||
_check_logcontext(context) | ||||
|
||||
def _handle_well_known_connection( | ||||
self, | ||||
client_factory, | ||||
expected_sni, | ||||
content, | ||||
response_headers: Optional[dict] = None, | ||||
self, client_factory, expected_sni, content, response_headers={} | ||||
): | ||||
"""Handle an outgoing HTTPs connection: wire it up to a server, check that the | ||||
request is for a .well-known, and send the response. | ||||
|
@@ -205,20 +204,18 @@ def _handle_well_known_connection( | |||
self.assertEqual( | ||||
request.requestHeaders.getRawHeaders(b"user-agent"), [b"test-agent"] | ||||
) | ||||
self._send_well_known_response(request, content, headers=response_headers or {}) | ||||
self._send_well_known_response(request, content, headers=response_headers) | ||||
return well_known_server | ||||
|
||||
def _send_well_known_response( | ||||
self, request, content, headers: Optional[dict] = None | ||||
): | ||||
def _send_well_known_response(self, request, content, headers={}): | ||||
"""Check that an incoming request looks like a valid .well-known request, and | ||||
send back the response. | ||||
""" | ||||
self.assertEqual(request.method, b"GET") | ||||
self.assertEqual(request.path, b"/.well-known/matrix/server") | ||||
self.assertEqual(request.requestHeaders.getRawHeaders(b"host"), [b"testserv"]) | ||||
# send back a response | ||||
for k, v in (headers or {}).items(): | ||||
for k, v in headers.items(): | ||||
request.setHeader(k, v) | ||||
request.write(content) | ||||
request.finish() | ||||
|
@@ -282,6 +279,76 @@ def test_get(self): | |||
json = self.successResultOf(treq.json_content(response)) | ||||
self.assertEqual(json, {"a": 1}) | ||||
|
||||
@patch.dict(os.environ, {"https_proxy": "proxy.com", "no_proxy": "unused.com"}) | ||||
def test_get_via_proxy(self): | ||||
""" | ||||
test for federation request through proxy | ||||
""" | ||||
# recreate the agent with patched env | ||||
self.agent = MatrixFederationAgent( | ||||
reactor=self.reactor, | ||||
tls_client_options_factory=self.tls_factory, | ||||
user_agent="test-agent", # Note that this is unused since _well_known_resolver is provided. | ||||
ip_blacklist=IPSet(), | ||||
_srv_resolver=self.mock_resolver, | ||||
_well_known_resolver=self.well_known_resolver, | ||||
) | ||||
|
||||
self.reactor.lookups["testserv"] = "1.2.3.4" | ||||
self.reactor.lookups["proxy.com"] = "9.9.9.9" | ||||
test_d = self._make_get_request(b"matrix://testserv:8448/foo/bar") | ||||
|
||||
# Nothing happened yet | ||||
self.assertNoResult(test_d) | ||||
|
||||
# Make sure treq is trying to connect | ||||
clients = self.reactor.tcpClients | ||||
self.assertEqual(len(clients), 1) | ||||
(host, port, client_factory, _timeout, _bindAddress) = clients[0] | ||||
# make sure we are connecting to the proxy | ||||
self.assertEqual(host, "9.9.9.9") | ||||
self.assertEqual(port, 1080) | ||||
|
||||
# make a test server, and wire up the client | ||||
http_server = self._make_connection(client_factory) | ||||
|
||||
self.assertEqual(len(http_server.requests), 1) | ||||
request = http_server.requests[0] | ||||
self.assertEqual(request.method, b"GET") | ||||
self.assertEqual(request.path, b"/foo/bar") | ||||
self.assertEqual( | ||||
request.requestHeaders.getRawHeaders(b"host"), [b"testserv:8448"] | ||||
) | ||||
self.assertEqual( | ||||
request.requestHeaders.getRawHeaders(b"user-agent"), [b"test-agent"] | ||||
) | ||||
content = request.content.read() | ||||
self.assertEqual(content, b"") | ||||
|
||||
# Deferred is still without a result | ||||
self.assertNoResult(test_d) | ||||
|
||||
# send the headers | ||||
request.responseHeaders.setRawHeaders(b"Content-Type", [b"application/json"]) | ||||
request.write("") | ||||
|
||||
self.reactor.pump((0.1,)) | ||||
|
||||
response = self.successResultOf(test_d) | ||||
|
||||
# that should give us a Response object | ||||
self.assertEqual(response.code, 200) | ||||
|
||||
# Send the body | ||||
request.write('{ "a": 1 }'.encode("ascii")) | ||||
request.finish() | ||||
|
||||
self.reactor.pump((0.1,)) | ||||
|
||||
# check it can be read | ||||
json = self.successResultOf(treq.json_content(response)) | ||||
self.assertEqual(json, {"a": 1}) | ||||
|
||||
def test_get_ip_address(self): | ||||
""" | ||||
Test the behaviour when the server name contains an explicit IP (with no port) | ||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add new params to the docstring (on the class, in this case). As well as making the code maintainable in future, it's important to help with review.