/
httpserver.py
176 lines (131 loc) · 6.05 KB
/
httpserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from twisted.web.server import Site
from twisted.web.resource import Resource
import logging
import twisted.internet.reactor
import twisted.internet.ssl
from sydent.http.servlets.authenticated_bind_threepid_servlet import (
AuthenticatedBindThreePidServlet,
)
logger = logging.getLogger(__name__)
class ClientApiHttpServer:
def __init__(self, sydent):
self.sydent = sydent
root = Resource()
matrix = Resource()
identity = Resource()
api = Resource()
v1 = self.sydent.servlets.v1
v2 = self.sydent.servlets.v2
validate = Resource()
email = Resource()
msisdn = Resource()
emailReqCode = self.sydent.servlets.emailRequestCode
emailValCode = self.sydent.servlets.emailValidate
msisdnReqCode = self.sydent.servlets.msisdnRequestCode
msisdnValCode = self.sydent.servlets.msisdnValidate
getValidated3pid = self.sydent.servlets.getValidated3pid
lookup = self.sydent.servlets.lookup
bulk_lookup = self.sydent.servlets.bulk_lookup
hash_details = self.sydent.servlets.hash_details
lookup_v2 = self.sydent.servlets.lookup_v2
threepid = Resource()
bind = self.sydent.servlets.threepidBind
unbind = self.sydent.servlets.threepidUnbind
pubkey = Resource()
ephemeralPubkey = Resource()
pk_ed25519 = self.sydent.servlets.pubkey_ed25519
root.putChild('_matrix', matrix)
matrix.putChild('identity', identity)
identity.putChild('api', api)
identity.putChild('v2', v2)
api.putChild('v1', v1)
v1.putChild('validate', validate)
validate.putChild('email', email)
validate.putChild('msisdn', msisdn)
v1.putChild('lookup', lookup)
v1.putChild('bulk_lookup', bulk_lookup)
v1.putChild('pubkey', pubkey)
pubkey.putChild('isvalid', self.sydent.servlets.pubkeyIsValid)
pubkey.putChild('ed25519:0', pk_ed25519)
pubkey.putChild('ephemeral', ephemeralPubkey)
ephemeralPubkey.putChild('isvalid', self.sydent.servlets.ephemeralPubkeyIsValid)
v1.putChild('3pid', threepid)
threepid.putChild('bind', bind)
threepid.putChild('unbind', unbind)
threepid.putChild('getValidated3pid', getValidated3pid)
email.putChild('requestToken', emailReqCode)
email.putChild('submitToken', emailValCode)
msisdn.putChild('requestToken', msisdnReqCode)
msisdn.putChild('submitToken', msisdnValCode)
v1.putChild('store-invite', self.sydent.servlets.storeInviteServlet)
v1.putChild('sign-ed25519', self.sydent.servlets.blindlySignStuffServlet)
v2.putChild('lookup', lookup_v2)
v2.putChild('hash_details', hash_details)
self.factory = Site(root)
self.factory.displayTracebacks = False
def setup(self):
httpPort = int(self.sydent.cfg.get('http', 'clientapi.http.port'))
interface = self.sydent.cfg.get('http', 'clientapi.http.bind_address')
logger.info("Starting Client API HTTP server on %s:%d", interface, httpPort)
twisted.internet.reactor.listenTCP(
httpPort, self.factory, interface=interface,
)
class InternalApiHttpServer(object):
def __init__(self, sydent):
self.sydent = sydent
def setup(self, interface, port):
logger.info("Starting Internal API HTTP server on %s:%d", interface, port)
root = Resource()
matrix = Resource()
root.putChild('_matrix', matrix)
identity = Resource()
matrix.putChild('identity', identity)
internal = Resource()
identity.putChild('internal', internal)
authenticated_bind = AuthenticatedBindThreePidServlet(self.sydent)
internal.putChild('bind', authenticated_bind)
factory = Site(root)
factory.displayTracebacks = False
twisted.internet.reactor.listenTCP(port, factory, interface=interface)
class ReplicationHttpsServer:
def __init__(self, sydent):
self.sydent = sydent
root = Resource()
matrix = Resource()
identity = Resource()
root.putChild('_matrix', matrix)
matrix.putChild('identity', identity)
replicate = Resource()
replV1 = Resource()
identity.putChild('replicate', replicate)
replicate.putChild('v1', replV1)
replV1.putChild('push', self.sydent.servlets.replicationPush)
self.factory = Site(root)
self.factory.displayTracebacks = False
def setup(self):
httpPort = int(self.sydent.cfg.get('http', 'replication.https.port'))
interface = self.sydent.cfg.get('http', 'replication.https.bind_address')
if self.sydent.sslComponents.myPrivateCertificate:
# We will already have logged a warn if this is absent, so don't do it again
cert = self.sydent.sslComponents.myPrivateCertificate
certOptions = twisted.internet.ssl.CertificateOptions(privateKey=cert.privateKey.original,
certificate=cert.original,
trustRoot=self.sydent.sslComponents.trustRoot)
logger.info("Loaded server private key and certificate!")
logger.info("Starting Replication HTTPS server on %s:%d", interface, httpPort)
twisted.internet.reactor.listenSSL(httpPort, self.factory, certOptions, interface=interface)