forked from neubot/neubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
274 lines (231 loc) · 9.04 KB
/
server.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
# neubot/server.py
#
# Copyright (c) 2011 Simone Basso <bassosimone@gmail.com>,
# NEXA Center for Internet & Society at Politecnico di Torino
#
# This file is part of Neubot <http://www.neubot.org/>.
#
# Neubot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Neubot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Neubot. If not, see <http://www.gnu.org/licenses/>.
#
"""
This module implements the command that manages server-side
components, including the rendezvous server and, for each test,
the negotiate server and the test server.
"""
import gc
import sys
if __name__ == "__main__":
sys.path.insert(0, ".")
from neubot.http.message import Message
from neubot.http.server import HTTP_SERVER
from neubot.http.server import ServerHTTP
from neubot.net.poller import POLLER
from neubot.negotiate.server import NEGOTIATE_SERVER
from neubot.negotiate.server_speedtest import NEGOTIATE_SERVER_SPEEDTEST
from neubot.negotiate.server_bittorrent import NEGOTIATE_SERVER_BITTORRENT
from neubot.net.dns import DNS_CACHE
from neubot.notify import NOTIFIER
from neubot.state import STATE
from neubot.compat import json
from neubot.debug import objgraph
from neubot.config import CONFIG
from neubot.log import LOG
from neubot.main import common
from neubot import bittorrent
from neubot import negotiate
from neubot import system
#from neubot import rendezvous # Not yet
import neubot.rendezvous.server
#from neubot import speedtest # Not yet
import neubot.speedtest.wrapper
class DebugAPI(ServerHTTP):
''' Implements the debugging API '''
def process_request(self, stream, request):
''' Process HTTP request and return response '''
response = Message()
if request.uri == '/debugmem/collect':
body = gc.collect(2)
elif request.uri == '/debugmem/count':
counts = gc.get_count()
body = {
'len_gc_objects': len(gc.get_objects()),
'len_gc_garbage': len(gc.garbage),
'gc_count0': counts[0],
'gc_count1': counts[1],
'gc_count2': counts[2],
# Add the length of the most relevant globals
'NEGOTIATE_SERVER.queue': len(NEGOTIATE_SERVER.queue),
'NEGOTIATE_SERVER.known': len(NEGOTIATE_SERVER.known),
'NEGOTIATE_SERVER_BITTORRENT.peers': \
len(NEGOTIATE_SERVER_BITTORRENT.peers),
'NEGOTIATE_SERVER_SPEEDTEST.clients': \
len(NEGOTIATE_SERVER_SPEEDTEST.clients),
'DNS_CACHE': len(DNS_CACHE),
'POLLER.tasks': len(POLLER.tasks),
'POLLER.pending': len(POLLER.pending),
'POLLER.readset': len(POLLER.readset),
'POLLER.writeset': len(POLLER.writeset),
'LOG._queue': len(LOG._queue),
'CONFIG.conf': len(CONFIG.conf),
'NOTIFIER._timestamps': len(NOTIFIER._timestamps),
'NOTIFIER._subscribers': len(NOTIFIER._subscribers),
'NOTIFIER._tofire': len(NOTIFIER._tofire),
'STATE._events': len(STATE._events),
}
elif request.uri == '/debugmem/garbage':
body = [str(obj) for obj in gc.garbage]
elif request.uri == '/debugmem/saveall':
enable = json.load(request.body)
flags = gc.get_debug()
if enable:
flags |= gc.DEBUG_SAVEALL
else:
flags &= ~gc.DEBUG_SAVEALL
gc.set_debug(flags)
body = enable
elif request.uri.startswith('/debugmem/types'):
if request.uri.startswith('/debugmem/types/'):
typename = request.uri.replace('/debugmem/types/', '')
objects = objgraph.by_type(typename)
body = [str(obj) for obj in objects]
else:
body = objgraph.typestats()
else:
body = None
if body is not None:
body = json.dumps(body, indent=4, sort_keys=True)
response.compose(code="200", reason="Ok", body=body,
mimetype="application/json")
else:
response.compose(code="404", reason="Not Found")
stream.send_response(request, response)
class ServerSideAPI(ServerHTTP):
""" Implements server-side API for Nagios plugin """
def process_request(self, stream, request):
""" Process HTTP request and return response """
if request.uri == "/sapi":
request.uri = "/sapi/"
response = Message()
if request.uri == "/sapi/":
body = '["/sapi/", "/sapi/state"]'
response.compose(code="200", reason="Ok", body=body,
mimetype="application/json")
elif request.uri == "/sapi/state":
body = '{"queue_len_cur": %d}' % len(NEGOTIATE_SERVER.queue)
response.compose(code="200", reason="Ok", body=body,
mimetype="application/json")
else:
response.compose(code="404", reason="Not Found")
stream.send_response(request, response)
#
# Register default values in the global scope so that
# their variable names are always defined and the rules
# for casting the types apply.
#
CONFIG.register_defaults({
"server.bittorrent": True,
"server.daemonize": True,
'server.debug': False,
"server.negotiate": True,
"server.rendezvous": False, # Not needed on the random server
"server.sapi": True,
"server.speedtest": True,
})
def main(args):
""" Starts the server module """
#
# Register descriptions in main() only so that
# we don't advertise the name of knobs that aren't
# relevant in the current context.
#
CONFIG.register_descriptions({
"server.bittorrent": "Start up BitTorrent test and negotiate server",
"server.daemonize": "Become a daemon and run in background",
'server.debug': 'Run the localhost-only debug server',
"server.negotiate": "Turn on negotiation infrastructure",
"server.rendezvous": "Start up rendezvous server",
"server.sapi": "Turn on Server-side API",
"server.speedtest": "Start up Speedtest test and negotiate server",
})
common.main("server", "Neubot server-side component", args)
conf = CONFIG.copy()
#
# Configure our global HTTP server and make
# sure that we don't provide filesystem access
# even by mistake.
#
conf["http.server.rootdir"] = ""
HTTP_SERVER.configure(conf)
#
# New-style modules are started just setting a
# bunch of conf[] variables and then invoking
# their run() method in order to kick them off.
#
if conf["server.negotiate"]:
negotiate.run(POLLER, conf)
if conf["server.bittorrent"]:
conf["bittorrent.listen"] = True
conf["bittorrent.negotiate"] = True
bittorrent.run(POLLER, conf)
if conf['server.speedtest']:
#conf['speedtest.listen'] = 1 # Not yet
#conf['speedtest.negotiate'] = 1 # Not yet
neubot.speedtest.wrapper.run(POLLER, conf)
# Migrating from old style to new style
if conf["server.rendezvous"]:
#conf["rendezvous.listen"] = True # Not yet
neubot.rendezvous.server.run(POLLER, conf)
#
# Historically Neubot runs on port 9773 and
# 8080 but we would like to switch to port 80
# in the long term period, because it's rare
# that they filter it.
# OTOH it looks like it's not possible to
# do that easily w/ M-Lab because the port
# is already taken.
#
address = "0.0.0.0"
ports = (80, 8080, 9773)
for port in ports:
HTTP_SERVER.listen((address, port))
#
# Start server-side API for Nagios plugin
# to query the state of the server.
# functionalities.
#
if conf["server.sapi"]:
server = ServerSideAPI(POLLER)
server.configure(conf)
HTTP_SERVER.register_child(server, "/sapi")
#
# Create localhost-only debug server
#
if CONFIG['server.debug']:
LOG.info('server: Starting debug server at 127.0.0.1:9774')
server = DebugAPI(POLLER)
server.configure(conf)
server.listen(('127.0.0.1', 9774))
#
# Go background and drop privileges,
# then enter into the main loop.
#
if conf["server.daemonize"]:
system.change_dir()
system.go_background()
system.write_pidfile()
LOG.redirect()
system.drop_privileges(LOG.error)
POLLER.loop()
if __name__ == "__main__":
main(sys.argv)