Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
51958df7
Commit
51958df7
authored
6 years ago
by
Richard van der Hoff
Browse files
Options
Downloads
Patches
Plain Diff
MatrixFederationAgent: factor out routing logic
This is going to get too big and unmanageable.
parent
d8400191
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/http/federation/matrix_federation_agent.py
+62
-18
62 additions, 18 deletions
synapse/http/federation/matrix_federation_agent.py
with
62 additions
and
18 deletions
synapse/http/federation/matrix_federation_agent.py
+
62
−
18
View file @
51958df7
...
...
@@ -14,6 +14,7 @@
# limitations under the License.
import
logging
import
attr
from
zope.interface
import
implementer
from
twisted.internet
import
defer
...
...
@@ -85,9 +86,11 @@ class MatrixFederationAgent(object):
response from being received (including problems that prevent the request
from being sent).
"""
parsed_uri
=
URI
.
fromBytes
(
uri
,
defaultPort
=-
1
)
res
=
yield
self
.
_route_matrix_uri
(
parsed_uri
)
# set up the TLS connection params
#
# XXX disabling TLS is really only supported here for the benefit of the
# unit tests. We should make the UTs cope with TLS rather than having to make
# the code support the unit tests.
...
...
@@ -95,22 +98,9 @@ class MatrixFederationAgent(object):
tls_options
=
None
else
:
tls_options
=
self
.
_tls_client_options_factory
.
get_options
(
parsed_uri
.
host
.
decode
(
"
ascii
"
)
res
.
tls_server_name
.
decode
(
"
ascii
"
)
)
if
parsed_uri
.
port
!=
-
1
:
# there was an explicit port in the URI
target
=
parsed_uri
.
host
,
parsed_uri
.
port
else
:
service_name
=
b
"
_matrix._tcp.%s
"
%
(
parsed_uri
.
host
,
)
server_list
=
yield
self
.
_srv_resolver
.
resolve_service
(
service_name
)
if
not
server_list
:
target
=
(
parsed_uri
.
host
,
8448
)
logger
.
debug
(
"
No SRV record for %s, using %s
"
,
service_name
,
target
)
else
:
target
=
pick_server_from_list
(
server_list
)
# make sure that the Host header is set correctly
if
headers
is
None
:
headers
=
Headers
()
...
...
@@ -118,13 +108,13 @@ class MatrixFederationAgent(object):
headers
=
headers
.
copy
()
if
not
headers
.
hasHeader
(
b
'
host
'
):
headers
.
addRawHeader
(
b
'
host
'
,
parsed_uri
.
netloc
)
headers
.
addRawHeader
(
b
'
host
'
,
res
.
host_header
)
class
EndpointFactory
(
object
):
@staticmethod
def
endpointForURI
(
_uri
):
logger
.
info
(
"
Connecting to %s:%s
"
,
target
[
0
],
target
[
1
]
)
ep
=
HostnameEndpoint
(
self
.
_reactor
,
host
=
target
[
0
],
port
=
target
[
1
]
)
logger
.
info
(
"
Connecting to %s:%s
"
,
res
.
target
_host
,
res
.
target
_port
)
ep
=
HostnameEndpoint
(
self
.
_reactor
,
res
.
target
_host
,
res
.
target
_port
)
if
tls_options
is
not
None
:
ep
=
wrapClientTLS
(
tls_options
,
ep
)
return
ep
...
...
@@ -134,3 +124,57 @@ class MatrixFederationAgent(object):
agent
.
request
(
method
,
uri
,
headers
,
bodyProducer
)
)
defer
.
returnValue
(
res
)
@defer.inlineCallbacks
def
_route_matrix_uri
(
self
,
parsed_uri
):
"""
Helper for `request`: determine the routing for a Matrix URI
Args:
parsed_uri (twisted.web.client.URI): uri to route. Note that it should be
parsed with URI.fromBytes(uri, defaultPort=-1) to set the `port` to -1
if there is no explicit port given.
Returns:
Deferred[_RoutingResult]
"""
if
parsed_uri
.
port
!=
-
1
:
# there is an explicit port
defer
.
returnValue
(
_RoutingResult
(
host_header
=
parsed_uri
.
netloc
,
tls_server_name
=
parsed_uri
.
host
,
target_host
=
parsed_uri
.
host
,
target_port
=
parsed_uri
.
port
,
))
# try a SRV lookup
service_name
=
b
"
_matrix._tcp.%s
"
%
(
parsed_uri
.
host
,)
server_list
=
yield
self
.
_srv_resolver
.
resolve_service
(
service_name
)
if
not
server_list
:
target_host
=
parsed_uri
.
host
port
=
8448
logger
.
debug
(
"
No SRV record for %s, using %s:%i
"
,
parsed_uri
.
host
.
decode
(
"
ascii
"
),
target_host
.
decode
(
"
ascii
"
),
port
,
)
else
:
target_host
,
port
=
pick_server_from_list
(
server_list
)
logger
.
debug
(
"
Picked %s:%i from SRV records for %s
"
,
target_host
.
decode
(
"
ascii
"
),
port
,
parsed_uri
.
host
.
decode
(
"
ascii
"
),
)
defer
.
returnValue
(
_RoutingResult
(
host_header
=
parsed_uri
.
netloc
,
tls_server_name
=
parsed_uri
.
host
,
target_host
=
target_host
,
target_port
=
port
,
))
@attr.s
class
_RoutingResult
(
object
):
host_header
=
attr
.
ib
()
tls_server_name
=
attr
.
ib
()
target_host
=
attr
.
ib
()
target_port
=
attr
.
ib
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment