3 # Hippotat - Asinine IP Over HTTP program
4 # ./hippotatd - server main program
6 # Copyright 2017 Ian Jackson
10 # This program is free software: you can redistribute it and/or
11 # modify it under the terms of the GNU Affero General Public
12 # License as published by the Free Software Foundation, either
13 # version 3 of the License, or (at your option) any later version,
14 # with the "CAF Login Exception" as published by Ian Jackson
15 # (version 2, or at your option any later version) as an Additional
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # Affero General Public License for more details.
23 # You should have received a copy of the GNU Affero General Public
24 # License and the CAF Login Exception along with this program, in
25 # the file AGPLv3+CAFv2. If not, email Ian Jackson
26 # <ijackson@chiark.greenend.org.uk>.
29 from hippotatlib
import *
37 import twisted
.internet
38 from twisted
.web
.server
import NOT_DONE_YET
40 import twisted
.web
.static
41 import twisted
.python
.syslog
43 import hippotatlib
.ownsource
44 from hippotatlib
.ownsource
import SourceShipmentPreparer
46 #import twisted.web.server import Site
47 #from twisted.web.resource import Resource
55 #---------- "router" ----------
57 def route(packet
, iface
, saddr
, daddr
):
59 log_debug(DBG
.ROUTE
, 'route: %s -> %s: %s' %
(saddr
,daddr
,dest
), d
=packet
)
60 try: dclient
= clients
[daddr
]
61 except KeyError: dclient
= None
62 if dclient
is not None:
64 dclient
.queue_outbound(packet
)
65 elif daddr
== c
.vaddr
or daddr
not in c
.vnetwork
:
67 queue_inbound(ipif
, packet
)
68 elif daddr
== c
.relay
:
70 log_discard(packet
, iface
, saddr
, daddr
, 'relay')
72 lt('discard no-client')
73 log_discard(packet
, iface
, saddr
, daddr
, 'no-client')
75 #---------- client ----------
78 def __init__(self
, ip
, cc
):
79 # instance data members
82 self
._rq
= collections
.deque() # requests
83 self
._pq
= PacketQueue(str(ip
), self
.cc
.max_queue_time
)
85 if ip
not in c
.vnetwork
:
86 raise ValueError('client %s not in vnetwork' % ip
)
89 raise ValueError('multiple client cfg sections for %s' % ip
)
92 self
._log(DBG
.INIT
, 'new')
94 def _log(self
, dflag
, msg
, **kwargs
):
95 log_debug(dflag
, ('client %s: ' % self
._ip
)+msg
, **kwargs
)
97 def process_arriving_data(self
, d
):
98 self
._log(DBG
.FLOW
, "req data (enc'd)", d
=d
)
100 for packet
in slip
.decode(d
):
101 (saddr
, daddr
) = packet_addrs(packet
)
102 if saddr
!= self
._ip
:
103 raise ValueError('wrong source address %s' % saddr
)
104 route(packet
, self
._ip
, saddr
, daddr
)
106 def _req_cancel(self
, request
):
107 self
._log(DBG
.HTTP_CTRL
, 'cancel', idof
=request
)
110 def _req_error(self
, err
, request
):
111 self
._log(DBG
.HTTP_CTRL
, 'error %s' % err
, idof
=request
)
112 self
._req_cancel(request
)
114 def queue_outbound(self
, packet
):
115 self
._pq
.append(packet
)
116 self
._check_outbound()
118 def _req_fin(self
, dummy
, request
, cl
):
119 self
._log(DBG
.HTTP_CTRL
, '_req_fin ' + repr(dummy
), idof
=request
)
121 except twisted
.internet
.error
.AlreadyCalled
: pass
123 def new_request(self
, request
):
124 request
.setHeader('Content-Type','application/octet-stream')
125 cl
= reactor
.callLater(self
.cc
.http_timeout
, self
._req_cancel
, request
)
126 nf
= request
.notifyFinish()
127 nf
.addErrback(self
._req_error
, request
)
128 nf
.addCallback(self
._req_fin
, request
, cl
)
129 self
._rq
.append(request
)
130 self
._check_outbound()
132 def _req_write(self
, req
, d
):
133 self
._log(DBG
.HTTP
, 'req_write ', idof
=req
, d
=d
)
136 def _check_outbound(self
):
137 log_debug(DBG
.HTTP_CTRL
, 'CHKO')
139 try: request
= self
._rq
[0]
140 except IndexError: request
= None
141 if request
and request
.finished
:
142 self
._log(DBG
.HTTP_CTRL
, 'CHKO req finished, discard', idof
=request
)
146 if not self
._pq
.nonempty():
147 # no packets, oh well
148 self
._log(DBG
.HTTP_CTRL
, 'CHKO no packets, OUT-DONE', idof
=request
)
153 self
._log(DBG
.HTTP_CTRL
, 'CHKO no request, OUT-DONE', idof
=request
)
156 self
._log(DBG
.HTTP_CTRL
, 'CHKO processing', idof
=request
)
157 # request, and also some non-expired packets
158 self
._pq
.process((lambda: request
.sentLength
),
159 (lambda d
: self
._req_write(request
, d
)),
160 self
.cc
.max_batch_down
)
162 assert(request
.sentLength
)
165 self
._log(DBG
.HTTP
, 'complete', idof
=request
)
166 # round again, looking for more to do
168 while len(self
._rq
) > self
.cc
.target_requests_outstanding
:
169 request
= self
._rq
.popleft()
170 self
._log(DBG
.HTTP
, 'CHKO above target, returning empty', idof
=request
)
173 def process_request(request
, desca
):
174 # find client, update config, etc.
175 metadata
= request
.args
[b
'm'][0]
176 metadata
= metadata
.split(b
'\r\n')
177 (ci_s
, pw
, tro
, cto
) = metadata
[0:4]
178 desca
['m[0,2:3]'] = [ci_s
, tro
, cto
]
179 ci_s
= ci_s
.decode('utf-8')
180 tro
= int(tro
); desca
['tro']= tro
181 cto
= int(cto
); desca
['cto']= cto
185 if pw
!= cl
.cc
.password
: raise ValueError('bad password')
188 if tro
!= cl
.cc
.target_requests_outstanding
:
189 raise ValueError('tro must be %d' % cl
.cc
.target_requests_outstanding
)
191 if cto
< cl
.cc
.http_timeout
:
192 raise ValueError('cto must be >= %d' % cl
.cc
.http_timeout
)
195 d
= request
.args
[b
'd'][0]
197 desca
['dlen'] = len(d
)
202 log_http(desca
, 'processing', idof
=id(request
), d
=d
)
204 d
= mime_translate(d
)
206 cl
.process_arriving_data(d
)
207 cl
.new_request(request
)
209 def log_http(desca
, msg
, **kwargs
):
211 kwargs
['d'] = desca
['d']
215 log_debug(DBG
.HTTP
, msg
+ repr(desca
), **kwargs
)
217 class NotStupidResource(twisted
.web
.resource
.Resource
):
218 # why this is not the default is a mystery!
219 def getChild(self
, name
, request
):
220 if name
== b
'': return self
221 else: return twisted
.web
.resource
.Resource
.getChild(name
, request
)
223 class IphttpResource(NotStupidResource
):
224 def render_POST(self
, request
):
225 log_debug(DBG
.HTTP_FULL
,
226 'req recv: ' + repr(request
) + ' ' + repr(request
.args
),
229 try: process_request(request
, desca
)
230 except Exception as e
:
231 emsg
= traceback
.format_exc()
232 log_http(desca
, 'RETURNING EXCEPTION ' + emsg
)
233 request
.setHeader('Content-Type','text/plain; charset="utf-8"')
234 request
.setResponseCode(400)
235 return (emsg
+ ' # ' + repr(desca
) + '\r\n').encode('utf-8')
236 log_debug(DBG
.HTTP_CTRL
, '...', idof
=id(request
))
239 # instantiator should set
240 # self.hippotat_sources = (source_names[0], source_names[1])
242 self
.hippotat_sources
= [None, None]
245 def render_GET(self
, request
):
246 log_debug(DBG
.HTTP
, 'GET request')
247 s
= '<html><body>hippotat\n'
248 (s0
,s1
) = self
.hippotat_sources
250 s
+= '<p><a href="%s">source</a>\n' % s0
251 if self
.hippotat_sources
[1]:
252 s
+= ('(and that of dependency <a href="%s">packages</a>)\n' % s
1)
256 s
+= '</body></html>'
257 return s
.encode('utf-8')
260 resource
= IphttpResource()
261 site
= twisted
.web
.server
.Site(resource
)
264 ep
= sa
.make_endpoint()
265 crash_on_defer(ep
.listen(site
))
266 log_debug(DBG
.INIT
, 'listening on %s' % sa
)
268 td
= tempfile
.mkdtemp()
271 try: shutil
.rmtree(td
)
272 except FileNotFoundError
: pass
274 cleanups
.append(cleanup
)
276 ssp
= SourceShipmentPreparer(td
)
277 ssp
.logger
= partial(log_debug
, DBG
.OWNSOURCE
)
278 if DBG
.OWNSOURCE
in debug_set
: ssp
.stream_debug
= sys
.stdout
279 ssp
.download_packages
= opts
.ownsource
>= 2
280 if opts
.ownsource
>= 1: ssp
.generate()
283 bn
= ssp
.output_names
[ix
]
284 op
= ssp
.output_paths
[ix
]
285 if op
is None: continue
286 resource
.hippotat_sources
[ix
] = bn
287 subresource
=twisted
.web
.static
.File(op
)
288 resource
.putChild(bn
.encode('utf-8'), subresource
)
290 reactor
.callLater(0.1, (lambda: log
.info('hippotatd started', dflag
=False)))
292 #---------- config and setup ----------
294 def process_cfg(_opts
, putative_servers
, putative_clients
):
300 c
.server
= cfg
.get('SERVER','server')
302 cfg_process_common(c
, c
.server
)
303 cfg_process_saddrs(c
, c
.server
)
304 cfg_process_vnetwork(c
, c
.server
)
305 cfg_process_vaddr(c
, c
.server
)
307 for (ci
,cs
) in putative_clients
.items():
309 sections
= cfg_process_client_common(cc
,c
.server
,cs
,ci
)
310 if not sections
: continue
311 cfg_process_client_limited(cc
,c
.server
,sections
, 'max_batch_down')
312 cfg_process_client_limited(cc
,c
.server
,sections
, 'max_queue_time')
316 c
.vrelay
= cfg
.get(c
.server
, 'vrelay')
317 except NoOptionError
:
318 for search
in c
.vnetwork
.hosts():
319 if search
== c
.vaddr
: continue
324 [c
.server
, 'DEFAULT'],
327 ('rnets','vnetwork')))
329 def catch_termination():
331 for cleanup
in cleanups
:
334 atexit
.register(run_cleanups
)
336 def signal_handler(name
, sig
, *args
):
337 signal
.signal(sig
, signal
.SIG_DFL
)
338 print('exiting due to %s' % name
, file=sys
.stderr
)
340 os
.kill(os
.getpid(), sig
)
341 raise RuntimeError('did not die due to signal %s !' % name
)
343 for sig
in (signal
.SIGINT
, signal
.SIGTERM
):
344 signal
.signal(sig
, partial(signal_handler
, sig
.name
))
347 global syslogfacility
348 if opts
.daemon
and opts
.syslogfacility
is None:
349 opts
.syslogfacility
= 'daemon'
351 if opts
.syslogfacility
is not None:
352 facilnum
= syslog
.__dict__
['LOG_' + opts
.syslogfacility
.upper()]
353 syslog
.openlog('hippotatd',
355 logoption
=syslog
.LOG_PID
)
357 m
= twisted
.logger
.formatEvent(event
)
358 #print(repr(event), m, file=org_stderr)
359 level
= event
.get('log_level')
360 if event
.get('dflag',None) is not None: sl
= syslog
.LOG_DEBUG
361 elif level
== LogLevel
.critical
: sl
= syslog
.LOG_CRIT
362 elif level
== LogLevel
.error
: sl
= syslog
.LOG_ERR
363 elif level
== LogLevel
.warn
: sl
= syslog
.LOG_WARNING
364 else : sl
= syslog
.LOG_INFO
366 glp
= twisted
.logger
.globalLogPublisher
367 glp
.addObserver(emit
)
368 log_debug(DBG
.INIT
, 'starting to log to syslog')
370 #log.crit('daemonic hippotatd crashed', dflag=False)
372 daemonic_reactor
= (twisted
.internet
.interfaces
.IReactorDaemonize
373 .providedBy(reactor
))
374 if daemonic_reactor
: reactor
.beforeDaemonize()
385 log
.critical('daemonic hippotatd crashed', dflag
=False)
389 grandchildpid
= os
.fork()
391 # we are the intermediate child
395 if opts
.pidfile
is not None:
396 pfh
= open(opts
.pidfile
, 'w')
397 print(mypid
, file=pfh
)
400 logger
= subprocess
.Popen(['logger','-d',
401 '-t','hippotat(stderr)',
403 '-p',opts
.syslogfacility
+ '.err'],
404 stdin
=subprocess
.PIPE
,
405 stdout
=subprocess
.DEVNULL
,
406 stderr
=subprocess
.DEVNULL
,
407 restore_signals
=True)
409 nullfd
= os
.open('/dev/null', os
.O_RDWR
)
412 os
.dup2(logger
.stdin
.fileno(), 2)
414 if daemonic_reactor
: reactor
.afterDaemonize()
415 log_debug(DBG
.INIT
, 'daemonised')
419 if opts
.syslogfacility
is not None:
420 glp
.removeObserver(hippotatlib
.file_log_observer
)
422 optparser
.add_option('--ownsource', default
=2,
423 action
='store_const', dest
='ownsource', const
=2,
424 help='source download fully enabled (default)')
426 optparser
.add_option('--ownsource-local',
427 action
='store_const', dest
='ownsource', const
=1,
428 help='source download is local source code only')
430 optparser
.add_option('--no-ownsource',
431 action
='store_const', dest
='ownsource', const
=0,
432 help='source download disabled (for testing only)')
434 optparser
.add_option('--daemon',
435 action
='store_true', dest
='daemon', default
=False,
436 help='daemonize (and log to syslog)')
438 optparser
.add_option('--pidfile',
439 nargs
=1, type='string',
440 action
='store', dest
='pidfile', default
=None,
441 help='write pid to this file')
443 optparser
.add_option('--syslog-facility',
444 nargs
=1, type='string',action
='store',
445 metavar
='FACILITY', dest
='syslogfacility',
447 help='log to syslog, with specified facility')
449 common_startup(process_cfg
)
453 ipif
= start_ipif(c
.ipif_command
, (lambda p
,s
,d
: route(p
,"[ipif]",s
,d
)))