fake-userv from secnet abf665fc6ca2974e13acede83e0a77967fdccf9a
[hippotat] / client
CommitLineData
c55f394e
IJ
1#!/usr/bin/python3
2
3from hippotat import *
4
034284c3 5client_cs = None
88487243
IJ
6
7def set_client(ci,cs,pw):
034284c3 8 global client_cs
88487243 9 global password
034284c3
IJ
10 assert(client_cs is None)
11 client_cs = cs
12 c.client = ci
88487243 13 c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
7b07f0b5 14 c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
88487243
IJ
15 password = pw
16
87a7c0c7
IJ
17def process_cfg():
18 global url
19 global max_requests_outstanding
c55f394e 20
87a7c0c7 21 process_cfg_common_always()
88487243
IJ
22 process_cfg_server()
23
24 try:
25 c.url = cfg.get('server','url')
26 except NoOptionError:
27 process_cfg_saddrs()
1d023c89 28 sa = c.saddrs[0].url()
88487243
IJ
29
30 process_cfg_clients(set_client)
87a7c0c7 31
ca732796 32 c.routes = cfg.get('virtual','routes')
7b07f0b5
IJ
33 c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
34 c.max_batch_up = cfg.getint(client_cs, 'max_batch_up')
034284c3
IJ
35
36 process_cfg_ipif(client_cs,
37 (('local', 'client'),
38 ('peer', 'server'),
39 ('rnets', 'routes')))
40
ca732796
IJ
41outstanding = 0
42
43def start_client():
44 global queue
7b07f0b5 45 global agent
ca732796 46 queue = PacketQueue(c.max_queue_time)
7b07f0b5 47 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
ca732796 48
034284c3 49def outbound(packet, saddr, daddr):
ca732796
IJ
50 #print('OUT ', saddr, daddr, repr(packet))
51 queue.append(packet)
52 check_outbound()
53
62b51bcf
IJ
54class ResponseConsumer(twisted.internet.protocol.Protocol):
55 def __init__(self):
56 self._ssd = SlipStreamDecoder(queue_inbound)
57 def dataReceived(self, data):
58 self._ssd.inputdata(mime_translate(data))
59 def connectionMade(self): pass
60 def connectionLost(self, reason):
61 if isinstance(reason, twisted.internet.error.ConnectionDone):
62 self._ssd.flush()
63 else:
64 print(reason, file=sys.stderr)
65
3dbadade 66def req_ok(resp):
62b51bcf 67 resp.deliverBody(ResponseConsumer())
7b07f0b5
IJ
68
69def req_err(err):
70 print(err, >>sys.stderr)
7b07f0b5
IJ
71
72def req_fin(*args):
3dbadade 73 outstanding--
7b07f0b5 74
ca732796
IJ
75def check_outbound():
76 while True:
7b07f0b5
IJ
77 if outstanding >= c.max_outstanding : break
78 if not queue.nonempty() && outstanding >= c.target_outstanding: break
79
80 d = b''
81 queue.process((lambda: len(d)),
82 (lambda s: d += s),
83 c.max_batch_up)
84 assert(len(d))
85
86 crlf = b'\r\n'
87 mime = (b'--b' + crlf +
88 b'Content-Disposition: form-data; name="m"' + crlf +
89 password + crlf +
90 c.client + crlf +
91 c.target_outstanding + crlf +
92 b'--b' + crlf +
93 b'Content-Disposition: form-data; name="d"' + crlf +
94 mime_translate(d) + crlf +
95 b'--b--' + crlf)
ca732796 96
7b07f0b5
IJ
97 hh = { 'User-Agent': ['hippotat'],
98 'Content-Type': ['multipart/form-data; boundary="b"'] }
3dbadade 99 req = agent.request(b'POST',
7b07f0b5
IJ
100 c.url,
101 twisted.web.client.Headers(hh))
102 req.addTimeout(c.http_timeout,
103 req.addCallbacks(req_ok, req_err)
104 req.addBoth(req_fin)
105 outstanding++
034284c3 106
1321ad5f 107common_startup()
87a7c0c7 108process_cfg()
7b07f0b5 109start_client()
034284c3
IJ
110start_ipif(c.ipif_command, outbound)
111common_run()