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): |
9aa11043 | 62 | def __init__(self): |
62b51bcf | 63 | self._ssd = SlipStreamDecoder(queue_inbound) |
bd9e77fb | 64 | |
62b51bcf | 65 | def dataReceived(self, data): |
9aa11043 IJ |
66 | try: self._ssd.inputdata(mime_translate(data)) |
67 | except Exception as e: asyncfailure(e) | |
68 | def connectionMade(self): pass | |
62b51bcf IJ |
69 | def connectionLost(self, reason): |
70 | if isinstance(reason, twisted.internet.error.ConnectionDone): | |
4edf77a3 | 71 | try: self._ssd.flush() |
9aa11043 | 72 | except Exception as e: asyncfailure(e) |
62b51bcf | 73 | else: |
9aa11043 | 74 | asyncfailure(reason) |
bd9e77fb | 75 | |
9aa11043 IJ |
76 | def req_ok(resp): |
77 | resp.deliverBody(ResponseConsumer()) | |
78 | req_fin() | |
7b07f0b5 | 79 | |
9aa11043 | 80 | def req_err(err): |
c0c90673 | 81 | print(err, file=sys.stderr) |
9aa11043 | 82 | reactor.callLater(c.http_retry, req_fin) |
7b07f0b5 | 83 | |
9aa11043 | 84 | def req_fin(*args): |
84e763c7 | 85 | global outstanding |
c0c90673 | 86 | outstanding -= 1 |
4edf77a3 IJ |
87 | check_outbound() |
88 | ||
9aa11043 IJ |
89 | def asyncfailure(reason): |
90 | global outstanding | |
91 | outstanding += 1 | |
92 | req_err(reason) | |
93 | ||
ca732796 | 94 | def check_outbound(): |
84e763c7 | 95 | global outstanding |
4edf77a3 | 96 | |
ca732796 | 97 | while True: |
c0c90673 IJ |
98 | if outstanding >= c.max_outstanding : break |
99 | if not queue.nonempty() and outstanding >= c.target_outstanding: break | |
7b07f0b5 IJ |
100 | |
101 | d = b'' | |
84e763c7 | 102 | def moredata(s): nonlocal d; d += s |
7b07f0b5 | 103 | queue.process((lambda: len(d)), |
c0c90673 | 104 | moredata, |
7b07f0b5 | 105 | c.max_batch_up) |
7b07f0b5 | 106 | |
fc0ba433 IJ |
107 | d = mime_translate(d) |
108 | ||
7b07f0b5 | 109 | crlf = b'\r\n' |
60dc70f9 | 110 | lf = b'\n' |
5e234983 IJ |
111 | mime = (b'--b' + crlf + |
112 | b'Content-Type: text/plain; charset="utf-8"' + crlf + | |
113 | b'Content-Disposition: form-data; name="m"' + crlf + crlf + | |
114 | str(c.client) .encode('ascii') + crlf + | |
115 | password + crlf + | |
116 | str(c.target_outstanding) .encode('ascii') + crlf + | |
60dc70f9 | 117 | (( |
5e234983 IJ |
118 | b'--b' + crlf + |
119 | b'Content-Type: application/octet-stream' + crlf + | |
120 | b'Content-Disposition: form-data; name="d"' + crlf + crlf + | |
fc0ba433 | 121 | d + crlf |
60dc70f9 | 122 | ) if len(d) else b'') + |
5e234983 | 123 | b'--b--' + crlf) |
ca732796 | 124 | |
a518aa4b IJ |
125 | #df = open('data.dump.dbg', mode='wb') |
126 | #df.write(mime) | |
127 | #df.close() | |
534f07df | 128 | # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg |
60dc70f9 | 129 | |
297b3ebf | 130 | log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime)) |
4edf77a3 | 131 | |
7b07f0b5 | 132 | hh = { 'User-Agent': ['hippotat'], |
b37c6b53 IJ |
133 | 'Content-Type': ['multipart/form-data; boundary="b"'], |
134 | 'Content-Length': [str(len(mime))] } | |
dd6665ee IJ |
135 | |
136 | bytesreader = io.BytesIO(mime) | |
137 | producer = twisted.web.client.FileBodyProducer(bytesreader) | |
138 | ||
3dbadade | 139 | req = agent.request(b'POST', |
7b07f0b5 | 140 | c.url, |
b37c6b53 IJ |
141 | twisted.web.client.Headers(hh), |
142 | producer) | |
84e763c7 | 143 | req.addTimeout(c.http_timeout, reactor) |
9aa11043 | 144 | req.addCallbacks(req_ok, req_err) |
c0c90673 | 145 | outstanding += 1 |
034284c3 | 146 | |
1321ad5f | 147 | common_startup() |
87a7c0c7 | 148 | process_cfg() |
7b07f0b5 | 149 | start_client() |
034284c3 | 150 | start_ipif(c.ipif_command, outbound) |
4edf77a3 | 151 | check_outbound() |
034284c3 | 152 | common_run() |