Commit | Line | Data |
---|---|---|
c55f394e IJ |
1 | #!/usr/bin/python3 |
2 | ||
3 | from hippotat import * | |
4 | ||
c0c90673 IJ |
5 | import twisted.web |
6 | import twisted.web.client | |
7 | ||
dd6665ee IJ |
8 | import io |
9 | ||
034284c3 | 10 | client_cs = None |
88487243 IJ |
11 | |
12 | def 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 |
22 | def 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 |
48 | outstanding = 0 |
49 | ||
50 | def 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 | 56 | def outbound(packet, saddr, daddr): |
ca732796 IJ |
57 | #print('OUT ', saddr, daddr, repr(packet)) |
58 | queue.append(packet) | |
59 | check_outbound() | |
60 | ||
62b51bcf | 61 | class ResponseConsumer(twisted.internet.protocol.Protocol): |
8b62cd2c | 62 | def __init__(self, req): |
8b62cd2c | 63 | self._req = req |
db6ba584 | 64 | self._ssd = SlipStreamDecoder('req %s' % id(req), 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: | |
eedc8b30 | 75 | self._handleexception() |
ccd371b3 IJ |
76 | |
77 | def connectionMade(self): | |
78 | self._log(DBG.HTTP_CTRL, 'connectionMade') | |
79 | ||
62b51bcf | 80 | def connectionLost(self, reason): |
15407d80 | 81 | self._log(DBG.HTTP_CTRL, 'connectionLost ' + str(reason)) |
765aba55 | 82 | if not reason.check(twisted.web.client.ResponseDone): |
33932420 | 83 | self._asyncfailure(reason) |
765aba55 IJ |
84 | return |
85 | try: | |
86 | self._ssd.flush() | |
87 | except Exception as e: | |
eedc8b30 IJ |
88 | self._handleexception() |
89 | ||
90 | def _handleexception(self): | |
91 | self._asyncfailure(traceback.format_exc()) | |
33932420 IJ |
92 | |
93 | def _asyncfailure(self, reason): | |
15407d80 | 94 | self._log(DBG.HTTP_CTRL, '_asyncFailure ' + str(reason)) |
fd87d3f3 | 95 | req_err(self._req, reason) |
bd9e77fb | 96 | |
6e4af0a2 IJ |
97 | class ErrorResponseConsumer(twisted.internet.protocol.Protocol): |
98 | def __init__(self, req, resp): | |
99 | self._req = req | |
100 | self._resp = resp | |
101 | ||
102 | try: | |
103 | self._phrase = resp.phrase.decode('utf-8') | |
104 | except Exception: | |
105 | self._phrase = repr(resp.phrase) | |
106 | ||
107 | self._log(DBG.HTTP_CTRL, '__init__ %d %s' % (resp.code, self._phrase)) | |
108 | ||
109 | def _log(self, dflag, msg, **kwargs): | |
110 | log_debug(dflag,'ERROR-RC '+msg, idof=self._req, **kwargs) | |
111 | ||
765aba55 IJ |
112 | def connectionMade(self): |
113 | self._m = b'' | |
114 | ||
115 | def dataReceived(self, data): | |
116 | self._log(DBG.HTTP_CTRL, 'dataReceived ' + repr(data)) | |
117 | self._m += data | |
118 | ||
6e4af0a2 IJ |
119 | def connectionLost(self, reason): |
120 | try: | |
121 | mbody = self._m.decode('utf-8') | |
122 | except Exception: | |
123 | mbody = repr(self._m) | |
765aba55 IJ |
124 | if not reason.check(twisted.web.client.ResponseDone): |
125 | mbody += ' || ' + str(reason) | |
126 | req_err(self._req, | |
127 | "FAILED %d %s | %s" | |
128 | % (self._resp.code, self._phrase, mbody)) | |
6e4af0a2 | 129 | |
8b62cd2c | 130 | def req_ok(req, resp): |
5dd3275b IJ |
131 | log_debug(DBG.HTTP_CTRL, |
132 | 'req_ok %d %s %s' % (resp.code, repr(resp.phrase), str(resp)), | |
133 | idof=req) | |
6e4af0a2 IJ |
134 | if resp.code == 200: |
135 | rc = ResponseConsumer(req) | |
136 | else: | |
137 | rc = ErrorResponseConsumer(req, resp) | |
5dd3275b | 138 | |
8b62cd2c | 139 | resp.deliverBody(rc) |
7b07f0b5 | 140 | |
fd87d3f3 | 141 | def req_err(req, err): |
3a7aaa41 | 142 | log_debug(DBG.HTTP_CTRL, 'req_err ' + str(err), idof=req) |
d7b4e25b IJ |
143 | if isinstance(err, twisted.python.failure.Failure): |
144 | err = err.getTraceback() | |
c0c90673 | 145 | print(err, file=sys.stderr) |
60b58030 | 146 | reactor.callLater(c.http_retry, (lambda: req_fin(req))) |
7b07f0b5 | 147 | |
60b58030 | 148 | def req_fin(req): |
84e763c7 | 149 | global outstanding |
c0c90673 | 150 | outstanding -= 1 |
6e4af0a2 | 151 | log_debug(DBG.HTTP_CTRL, 'req_fin OS=%d' % outstanding, idof=req) |
4edf77a3 IJ |
152 | check_outbound() |
153 | ||
ca732796 | 154 | def check_outbound(): |
84e763c7 | 155 | global outstanding |
4edf77a3 | 156 | |
ca732796 | 157 | while True: |
c0c90673 IJ |
158 | if outstanding >= c.max_outstanding : break |
159 | if not queue.nonempty() and outstanding >= c.target_outstanding: break | |
7b07f0b5 IJ |
160 | |
161 | d = b'' | |
84e763c7 | 162 | def moredata(s): nonlocal d; d += s |
7b07f0b5 | 163 | queue.process((lambda: len(d)), |
c0c90673 | 164 | moredata, |
7b07f0b5 | 165 | c.max_batch_up) |
7b07f0b5 | 166 | |
fc0ba433 IJ |
167 | d = mime_translate(d) |
168 | ||
7b07f0b5 | 169 | crlf = b'\r\n' |
60dc70f9 | 170 | lf = b'\n' |
5e234983 IJ |
171 | mime = (b'--b' + crlf + |
172 | b'Content-Type: text/plain; charset="utf-8"' + crlf + | |
173 | b'Content-Disposition: form-data; name="m"' + crlf + crlf + | |
174 | str(c.client) .encode('ascii') + crlf + | |
175 | password + crlf + | |
176 | str(c.target_outstanding) .encode('ascii') + crlf + | |
60dc70f9 | 177 | (( |
5e234983 IJ |
178 | b'--b' + crlf + |
179 | b'Content-Type: application/octet-stream' + crlf + | |
180 | b'Content-Disposition: form-data; name="d"' + crlf + crlf + | |
fc0ba433 | 181 | d + crlf |
60dc70f9 | 182 | ) if len(d) else b'') + |
5e234983 | 183 | b'--b--' + crlf) |
ca732796 | 184 | |
a518aa4b IJ |
185 | #df = open('data.dump.dbg', mode='wb') |
186 | #df.write(mime) | |
187 | #df.close() | |
534f07df | 188 | # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg |
60dc70f9 | 189 | |
297b3ebf | 190 | log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime)) |
4edf77a3 | 191 | |
7b07f0b5 | 192 | hh = { 'User-Agent': ['hippotat'], |
b37c6b53 IJ |
193 | 'Content-Type': ['multipart/form-data; boundary="b"'], |
194 | 'Content-Length': [str(len(mime))] } | |
dd6665ee IJ |
195 | |
196 | bytesreader = io.BytesIO(mime) | |
197 | producer = twisted.web.client.FileBodyProducer(bytesreader) | |
198 | ||
3dbadade | 199 | req = agent.request(b'POST', |
7b07f0b5 | 200 | c.url, |
b37c6b53 IJ |
201 | twisted.web.client.Headers(hh), |
202 | producer) | |
47191df1 | 203 | |
6e4af0a2 IJ |
204 | outstanding += 1 |
205 | log_debug(DBG.HTTP_CTRL, 'request OS=%d' % outstanding, idof=req, d=d) | |
84e763c7 | 206 | req.addTimeout(c.http_timeout, reactor) |
8b62cd2c | 207 | req.addCallback((lambda resp: req_ok(req, resp))) |
fd87d3f3 | 208 | req.addErrback((lambda err: req_err(req, err))) |
034284c3 | 209 | |
1321ad5f | 210 | common_startup() |
87a7c0c7 | 211 | process_cfg() |
7b07f0b5 | 212 | start_client() |
034284c3 | 213 | start_ipif(c.ipif_command, outbound) |
4edf77a3 | 214 | check_outbound() |
034284c3 | 215 | common_run() |