chiark / gitweb /
wip
[hippotat] / client
CommitLineData
c55f394e
IJ
1#!/usr/bin/python3
2
3from hippotat import *
4
c0c90673
IJ
5import twisted.web
6import twisted.web.client
7
dd6665ee
IJ
8import io
9
034284c3 10client_cs = None
88487243
IJ
11
12def set_client(ci,cs,pw):
034284c3 13 global client_cs
88487243 14 global password
034284c3
IJ
15 assert(client_cs is None)
16 client_cs = cs
17 c.client = ci
88487243 18 c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
7b07f0b5 19 c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
88487243
IJ
20 password = pw
21
87a7c0c7
IJ
22def process_cfg():
23 global url
24 global max_requests_outstanding
c55f394e 25
87a7c0c7 26 process_cfg_common_always()
88487243
IJ
27 process_cfg_server()
28
29 try:
30 c.url = cfg.get('server','url')
31 except NoOptionError:
32 process_cfg_saddrs()
84e763c7 33 c.url = c.saddrs[0].url()
88487243
IJ
34
35 process_cfg_clients(set_client)
87a7c0c7 36
ca732796 37 c.routes = cfg.get('virtual','routes')
7b07f0b5
IJ
38 c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
39 c.max_batch_up = cfg.getint(client_cs, 'max_batch_up')
ff613365 40 c.http_timeout = cfg.getint(client_cs, 'http_timeout')
4edf77a3 41 c.http_retry = cfg.getint(client_cs, 'http_retry')
034284c3
IJ
42
43 process_cfg_ipif(client_cs,
44 (('local', 'client'),
45 ('peer', 'server'),
46 ('rnets', 'routes')))
47
ca732796
IJ
48outstanding = 0
49
50def start_client():
51 global queue
7b07f0b5 52 global agent
297b3ebf 53 queue = PacketQueue('up', c.max_queue_time)
7b07f0b5 54 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
ca732796 55
034284c3 56def outbound(packet, saddr, daddr):
ca732796
IJ
57 #print('OUT ', saddr, daddr, repr(packet))
58 queue.append(packet)
59 check_outbound()
60
62b51bcf 61class ResponseConsumer(twisted.internet.protocol.Protocol):
8b62cd2c 62 def __init__(self, req):
8b62cd2c 63 self._req = req
62b51bcf 64 self._ssd = SlipStreamDecoder(queue_inbound)
14c6d55c
IJ
65 self._log(DBG.HTTP_CTRL, '__init__')
66
67 def _log(self, dflag, msg, **kwargs):
68 log_debug(dflag, 'RC ' + msg, idof=self._req, **kwargs)
bd9e77fb 69
62b51bcf 70 def dataReceived(self, data):
9b65cdd4
IJ
71 self._log(DBG.HTTP_CTRL, 'dataReceived', d=data)
72 try:
73 self._ssd.inputdata(mime_translate(data))
74 except Exception as e:
75 self._asyncfailure(e)
ccd371b3
IJ
76
77 def connectionMade(self):
78 self._log(DBG.HTTP_CTRL, 'connectionMade')
79
62b51bcf 80 def connectionLost(self, reason):
15407d80
IJ
81 self._log(DBG.HTTP_CTRL, 'connectionLost ' + str(reason))
82 if reason.check(twisted.web.client.ResponseDone):
4edf77a3 83 try: self._ssd.flush()
082d973e
IJ
84 except Exception as e:
85 self._asyncfailure(e)
62b51bcf 86 else:
33932420
IJ
87 self._asyncfailure(reason)
88
89 def _asyncfailure(self, reason):
15407d80 90 self._log(DBG.HTTP_CTRL, '_asyncFailure ' + str(reason))
33932420
IJ
91 global outstanding
92 outstanding += 1
fd87d3f3 93 req_err(self._req, reason)
bd9e77fb 94
8b62cd2c 95def req_ok(req, resp):
5dd3275b
IJ
96 log_debug(DBG.HTTP_CTRL,
97 'req_ok %d %s %s' % (resp.code, repr(resp.phrase), str(resp)),
98 idof=req)
99 if resp.code != 200:
100 try:
101 phrase = resp.phrase.decode('utf-8')
102 except UnicodeDecodeError:
103 phrase = repr(resp.phrase)
104 req_err(req, "FAILED %d %s" % (resp.code, phrase))
105 return
106
8b62cd2c
IJ
107 rc = ResponseConsumer(req)
108 resp.deliverBody(rc)
60b58030 109 req_fin(req)
7b07f0b5 110
fd87d3f3 111def req_err(req, err):
3a7aaa41 112 log_debug(DBG.HTTP_CTRL, 'req_err ' + str(err), idof=req)
c0c90673 113 print(err, file=sys.stderr)
60b58030 114 reactor.callLater(c.http_retry, (lambda: req_fin(req)))
7b07f0b5 115
60b58030 116def req_fin(req):
47191df1 117 log_debug(DBG.HTTP_CTRL, 'req_fin', idof=req)
84e763c7 118 global outstanding
c0c90673 119 outstanding -= 1
4edf77a3
IJ
120 check_outbound()
121
ca732796 122def check_outbound():
84e763c7 123 global outstanding
4edf77a3 124
ca732796 125 while True:
c0c90673
IJ
126 if outstanding >= c.max_outstanding : break
127 if not queue.nonempty() and outstanding >= c.target_outstanding: break
7b07f0b5
IJ
128
129 d = b''
84e763c7 130 def moredata(s): nonlocal d; d += s
7b07f0b5 131 queue.process((lambda: len(d)),
c0c90673 132 moredata,
7b07f0b5 133 c.max_batch_up)
7b07f0b5 134
fc0ba433
IJ
135 d = mime_translate(d)
136
7b07f0b5 137 crlf = b'\r\n'
60dc70f9 138 lf = b'\n'
5e234983
IJ
139 mime = (b'--b' + crlf +
140 b'Content-Type: text/plain; charset="utf-8"' + crlf +
141 b'Content-Disposition: form-data; name="m"' + crlf + crlf +
142 str(c.client) .encode('ascii') + crlf +
143 password + crlf +
144 str(c.target_outstanding) .encode('ascii') + crlf +
60dc70f9 145 ((
5e234983
IJ
146 b'--b' + crlf +
147 b'Content-Type: application/octet-stream' + crlf +
148 b'Content-Disposition: form-data; name="d"' + crlf + crlf +
fc0ba433 149 d + crlf
60dc70f9 150 ) if len(d) else b'') +
5e234983 151 b'--b--' + crlf)
ca732796 152
a518aa4b
IJ
153 #df = open('data.dump.dbg', mode='wb')
154 #df.write(mime)
155 #df.close()
534f07df 156 # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg
60dc70f9 157
297b3ebf 158 log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime))
4edf77a3 159
7b07f0b5 160 hh = { 'User-Agent': ['hippotat'],
b37c6b53
IJ
161 'Content-Type': ['multipart/form-data; boundary="b"'],
162 'Content-Length': [str(len(mime))] }
dd6665ee
IJ
163
164 bytesreader = io.BytesIO(mime)
165 producer = twisted.web.client.FileBodyProducer(bytesreader)
166
3dbadade 167 req = agent.request(b'POST',
7b07f0b5 168 c.url,
b37c6b53
IJ
169 twisted.web.client.Headers(hh),
170 producer)
47191df1
IJ
171
172 log_debug(DBG.HTTP_CTRL, 'request', idof=req, d=d)
84e763c7 173 req.addTimeout(c.http_timeout, reactor)
8b62cd2c 174 req.addCallback((lambda resp: req_ok(req, resp)))
fd87d3f3 175 req.addErrback((lambda err: req_err(req, err)))
c0c90673 176 outstanding += 1
034284c3 177
1321ad5f 178common_startup()
87a7c0c7 179process_cfg()
7b07f0b5 180start_client()
034284c3 181start_ipif(c.ipif_command, outbound)
4edf77a3 182check_outbound()
034284c3 183common_run()