This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
/
main.py
290 lines (243 loc) · 9.48 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""autopush/autoendpoint daemon scripts"""
import os
from argparse import Namespace # noqa
from twisted.application.internet import (
TCPServer,
TimerService,
SSLServer,
StreamServerEndpointService,
)
from twisted.application.service import MultiService
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.internet.protocol import ServerFactory # noqa
from twisted.logger import Logger
from typing import ( # noqa
Any,
Dict,
Optional,
Sequence,
)
from autopush.http import (
InternalRouterHTTPFactory,
EndpointHTTPFactory,
MemUsageHTTPFactory,
agent_from_settings
)
import autopush.utils as utils
import autopush.logging as logging
from autopush.config import AutopushConfig
from autopush.db import DatabaseManager
from autopush.exceptions import InvalidSettings
from autopush.haproxy import HAProxyServerEndpoint
from autopush.logging import PushLogger
from autopush.main_argparse import parse_connection, parse_endpoint
from autopush.router import routers_from_settings
from autopush.websocket import (
ConnectionWSSite,
PushServerFactory,
)
from autopush.websocket import PushServerProtocol # noqa
log = Logger()
class AutopushMultiService(MultiService):
shared_config_files = (
'/etc/autopush_shared.ini',
'configs/autopush_shared.ini',
'~/.autopush_shared.ini',
'.autopush_shared.ini',
)
config_files = None # type: Sequence[str]
logger_name = None # type: str
THREAD_POOL_SIZE = 50
def __init__(self, settings):
# type: (AutopushConfig) -> None
super(AutopushMultiService, self).__init__()
self.settings = settings
self.db = DatabaseManager.from_settings(settings)
self.agent = agent_from_settings(settings)
@staticmethod
def parse_args(config_files, args):
# type: (Sequence[str], Sequence[str]) -> Namespace
"""Parse command line args via argparse"""
raise NotImplementedError # pragma: nocover
def setup(self, rotate_tables=True):
# type: (bool) -> None
"""Initialize the services"""
raise NotImplementedError # pragma: nocover
def add_maybe_ssl(self, port, factory, ssl_cf):
# type: (int, ServerFactory, Optional[Any]) -> None
"""Add a Service from factory, optionally behind TLS"""
self.addService(
SSLServer(port, factory, contextFactory=ssl_cf, reactor=reactor)
if ssl_cf else
TCPServer(port, factory, reactor=reactor)
)
def add_timer(self, *args, **kwargs):
"""Add a TimerService"""
self.addService(TimerService(*args, **kwargs))
def add_memusage(self):
"""Add the memusage Service"""
factory = MemUsageHTTPFactory(self.settings, None)
self.addService(
TCPServer(self.settings.memusage_port, factory, reactor=reactor))
def run(self):
"""Start the services and run the reactor"""
reactor.suggestThreadPoolSize(self.THREAD_POOL_SIZE)
self.startService()
reactor.run()
@inlineCallbacks
def stopService(self):
yield self.agent._pool.closeCachedConnections()
yield super(AutopushMultiService, self).stopService()
@classmethod
def _from_argparse(cls, ns, **kwargs):
# type: (Namespace, **Any) -> AutopushMultiService
"""Create an instance from argparse/additional kwargs"""
# Add some entropy to prevent potential conflicts.
postfix = os.urandom(4).encode('hex').ljust(8, '0')
settings = AutopushConfig.from_argparse(
ns,
debug=ns.debug,
preflight_uaid="deadbeef000000000deadbeef" + postfix,
**kwargs
)
return cls(settings)
@classmethod
def main(cls, args=None, use_files=True):
# type: (Sequence[str], bool) -> Any
"""Entry point to autopush's main command line scripts.
aka autopush/autoendpoint.
"""
ns = cls.parse_args(cls.config_files if use_files else [], args)
if not ns.no_aws:
logging.HOSTNAME = utils.get_ec2_instance_id()
PushLogger.setup_logging(
cls.logger_name,
log_level=ns.log_level or ("debug" if ns.debug else "info"),
log_format="text" if ns.human_logs else "json",
log_output=ns.log_output,
sentry_dsn=bool(os.environ.get("SENTRY_DSN")),
firehose_delivery_stream=ns.firehose_stream_name
)
try:
app = cls.from_argparse(ns)
except InvalidSettings as e:
log.critical(str(e))
return 1
app.setup()
app.run()
class EndpointApplication(AutopushMultiService):
"""The autoendpoint application"""
config_files = AutopushMultiService.shared_config_files + (
'/etc/autopush_endpoint.ini',
'configs/autopush_endpoint.ini',
'~/.autopush_endpoint.ini',
'.autopush_endpoint.ini'
)
parse_args = staticmethod(parse_endpoint) # type: ignore
logger_name = "Autoendpoint"
endpoint_factory = EndpointHTTPFactory
def __init__(self, settings):
# type: (AutopushConfig) -> None
super(EndpointApplication, self).__init__(settings)
self.routers = routers_from_settings(settings, self.db, self.agent)
def setup(self, rotate_tables=True):
self.db.setup(self.settings.preflight_uaid)
self.add_endpoint()
if self.settings.memusage_port:
self.add_memusage()
# Start the table rotation checker/updater
if rotate_tables:
self.add_timer(60, self.db.update_rotating_tables)
def add_endpoint(self):
"""Start the Endpoint HTTP router"""
settings = self.settings
factory = self.endpoint_factory(settings, self.db, self.routers)
factory.protocol.maxData = settings.max_data
factory.add_health_handlers()
ssl_cf = factory.ssl_cf()
self.add_maybe_ssl(settings.port, factory, ssl_cf)
if settings.proxy_protocol_port:
ep = HAProxyServerEndpoint(
reactor,
settings.proxy_protocol_port,
ssl_cf
)
self.addService(StreamServerEndpointService(ep, factory))
@classmethod
def from_argparse(cls, ns):
# type: (Namespace) -> AutopushMultiService
return super(EndpointApplication, cls)._from_argparse(
ns,
port=ns.port,
endpoint_scheme=ns.endpoint_scheme,
endpoint_hostname=ns.endpoint_hostname or ns.hostname,
endpoint_port=ns.endpoint_port,
cors=not ns.no_cors,
bear_hash_key=ns.auth_key,
proxy_protocol_port=ns.proxy_protocol_port,
)
class ConnectionApplication(AutopushMultiService):
"""The autopush application"""
config_files = AutopushMultiService.shared_config_files + (
'/etc/autopush_connection.ini',
'configs/autopush_connection.ini',
'~/.autopush_connection.ini',
'.autopush_connection.ini'
)
parse_args = staticmethod(parse_connection) # type: ignore
logger_name = "Autopush"
internal_router_factory = InternalRouterHTTPFactory
websocket_factory = PushServerFactory
websocket_site_factory = ConnectionWSSite
def __init__(self, settings):
# type: (AutopushConfig) -> None
super(ConnectionApplication, self).__init__(settings)
self.clients = {} # type: Dict[str, PushServerProtocol]
def setup(self, rotate_tables=True):
self.db.setup(self.settings.preflight_uaid)
self.add_internal_router()
if self.settings.memusage_port:
self.add_memusage()
self.add_websocket()
# Start the table rotation checker/updater
if rotate_tables:
self.add_timer(60, self.db.update_rotating_tables)
def add_internal_router(self):
"""Start the internal HTTP notification router"""
factory = self.internal_router_factory(
self.settings, self.db, self.clients)
factory.add_health_handlers()
self.add_maybe_ssl(self.settings.router_port, factory,
factory.ssl_cf())
def add_websocket(self):
"""Start the public WebSocket server"""
settings = self.settings
ws_factory = self.websocket_factory(settings, self.db, self.agent,
self.clients)
site_factory = self.websocket_site_factory(settings, ws_factory)
self.add_maybe_ssl(settings.port, site_factory, site_factory.ssl_cf())
@classmethod
def from_argparse(cls, ns):
# type: (Namespace) -> AutopushMultiService
return super(ConnectionApplication, cls)._from_argparse(
ns,
port=ns.port,
endpoint_scheme=ns.endpoint_scheme,
endpoint_hostname=ns.endpoint_hostname,
endpoint_port=ns.endpoint_port,
router_scheme="https" if ns.router_ssl_key else "http",
router_hostname=ns.router_hostname,
router_port=ns.router_port,
env=ns.env,
hello_timeout=ns.hello_timeout,
router_ssl=dict(
key=ns.router_ssl_key,
cert=ns.router_ssl_cert,
dh_param=ns.ssl_dh_param
),
auto_ping_interval=ns.auto_ping_interval,
auto_ping_timeout=ns.auto_ping_timeout,
max_connections=ns.max_connections,
close_handshake_timeout=ns.close_handshake_timeout,
)