-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdomain_fronter.py
More file actions
813 lines (767 loc) · 32.7 KB
/
domain_fronter.py
File metadata and controls
813 lines (767 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import asyncio
import base64
import gzip
import json
import logging
import os
import re
import ssl
import time
from urllib.parse import urlparse
from ws import ws_encode, ws_decode
log = logging.getLogger("Fronter")
class DomainFronter:
def __init__(self, config: dict):
mode = config.get("mode", "domain_fronting")
if mode == "custom_domain":
domain = config["custom_domain"]
self.connect_host = domain
self.sni_host = domain
self.http_host = domain
elif mode == "google_fronting":
self.connect_host = config.get("google_ip", "216.239.38.120")
self.sni_host = config.get("front_domain", "www.google.com")
self.http_host = config["worker_host"]
elif mode == "apps_script":
self.connect_host = config.get("google_ip", "216.239.38.120")
self.sni_host = config.get("front_domain", "www.google.com")
self.http_host = "script.google.com"
script = config.get("script_ids") or config.get("script_id")
self._script_ids = script if isinstance(script, list) else [script]
self._script_idx = 0
self.script_id = self._script_ids[0]
self._dev_available = False
else:
self.connect_host = config["front_domain"]
self.sni_host = config["front_domain"]
self.http_host = config["worker_host"]
self.mode = mode
self.worker_path = config.get("worker_path", "")
self.auth_key = config.get("auth_key", "")
self.verify_ssl = config.get("verify_ssl", True)
self._pool: list[tuple[asyncio.StreamReader, asyncio.StreamWriter, float]] = []
self._pool_lock = asyncio.Lock()
self._pool_max = 50
self._conn_ttl = 45.0
self._semaphore = asyncio.Semaphore(50)
self._warmed = False
self._refilling = False
self._pool_min_idle = 15
self._maintenance_task: asyncio.Task | None = None
self._batch_lock = asyncio.Lock()
self._batch_pending: list[tuple[dict, asyncio.Future]] = []
self._batch_task: asyncio.Task | None = None
self._batch_window_micro = 0.005
self._batch_window_macro = 0.050
self._batch_max = 50
self._batch_enabled = True
self._coalesce: dict[str, list[asyncio.Future]] = {}
self._h2 = None
if mode == "apps_script":
try:
from h2_transport import H2Transport, H2_AVAILABLE
if H2_AVAILABLE:
self._h2 = H2Transport(self.connect_host, self.sni_host, self.verify_ssl)
except ImportError:
pass
def _ssl_ctx(self) -> ssl.SSLContext:
ctx = ssl.create_default_context()
if not self.verify_ssl:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return ctx
async def _open(self):
return await asyncio.open_connection(self.connect_host, 443, ssl=self._ssl_ctx(), server_hostname=self.sni_host)
async def _acquire(self):
now = asyncio.get_event_loop().time()
async with self._pool_lock:
while self._pool:
reader, writer, created = self._pool.pop()
if (now - created) < self._conn_ttl and not reader.at_eof():
asyncio.create_task(self._add_conn_to_pool())
return reader, writer, created
try:
writer.close()
except Exception:
pass
reader, writer = await asyncio.wait_for(self._open(), timeout=10)
if not self._refilling:
self._refilling = True
asyncio.create_task(self._refill_pool())
return reader, writer, asyncio.get_event_loop().time()
async def _release(self, reader, writer, created):
now = asyncio.get_event_loop().time()
if (now - created) >= self._conn_ttl or reader.at_eof():
try:
writer.close()
except Exception:
pass
return
async with self._pool_lock:
if len(self._pool) < self._pool_max:
self._pool.append((reader, writer, created))
else:
try:
writer.close()
except Exception:
pass
def _next_script_id(self) -> str:
sid = self._script_ids[self._script_idx % len(self._script_ids)]
self._script_idx += 1
return sid
def _exec_path(self) -> str:
sid = self._next_script_id()
return f"/macros/s/{sid}/{'dev' if self._dev_available else 'exec'}"
async def _flush_pool(self):
async with self._pool_lock:
for _, writer, _ in self._pool:
try:
writer.close()
except Exception:
pass
self._pool.clear()
async def _refill_pool(self):
try:
coros = [self._add_conn_to_pool() for _ in range(8)]
await asyncio.gather(*coros, return_exceptions=True)
finally:
self._refilling = False
async def _add_conn_to_pool(self):
try:
r, w = await asyncio.wait_for(self._open(), timeout=5)
t = asyncio.get_event_loop().time()
async with self._pool_lock:
if len(self._pool) < self._pool_max:
self._pool.append((r, w, t))
else:
try:
w.close()
except Exception:
pass
except Exception:
pass
async def _pool_maintenance(self):
while True:
try:
await asyncio.sleep(3)
now = asyncio.get_event_loop().time()
async with self._pool_lock:
alive = []
for r, w, t in self._pool:
if (now - t) < self._conn_ttl and not r.at_eof():
alive.append((r, w, t))
else:
try:
w.close()
except Exception:
pass
self._pool = alive
idle = len(self._pool)
needed = max(0, self._pool_min_idle - idle)
if needed > 0:
coros = [self._add_conn_to_pool() for _ in range(min(needed, 5))]
await asyncio.gather(*coros, return_exceptions=True)
except asyncio.CancelledError:
break
except Exception:
pass
async def _warm_pool(self):
if self._warmed:
return
self._warmed = True
asyncio.create_task(self._do_warm())
if self._maintenance_task is None:
self._maintenance_task = asyncio.create_task(self._pool_maintenance())
if self._h2:
asyncio.create_task(self._h2_connect_and_warm())
async def _h2_connect(self):
try:
await self._h2.ensure_connected()
except Exception:
pass
async def _h2_connect_and_warm(self):
await self._h2_connect()
if self._h2 and self._h2.is_connected:
asyncio.create_task(self._prewarm_script())
asyncio.create_task(self._keepalive_loop())
async def _prewarm_script(self):
payload = json.dumps({"m": "HEAD", "u": "http://example.com/", "k": self.auth_key}).encode()
hdrs = {"content-type": "application/json"}
sid = self._script_ids[0]
try:
dev_path = f"/macros/s/{sid}/dev"
status, _, body = await asyncio.wait_for(self._h2.request(method="POST", path=dev_path, host=self.http_host, headers=hdrs, body=payload), timeout=15)
data = json.loads(body.decode(errors="replace"))
if "s" in data:
self._dev_available = True
return
except Exception:
pass
try:
exec_path = f"/macros/s/{sid}/exec"
await asyncio.wait_for(self._h2.request(method="POST", path=exec_path, host=self.http_host, headers=hdrs, body=payload), timeout=15)
except Exception:
pass
async def _keepalive_loop(self):
while True:
try:
await asyncio.sleep(240)
if not self._h2 or not self._h2.is_connected:
try:
await self._h2.reconnect()
except Exception:
continue
await self._h2.ping()
payload = {"m": "HEAD", "u": "http://example.com/", "k": self.auth_key}
path = self._exec_path()
await asyncio.wait_for(self._h2.request(method="POST", path=path, host=self.http_host, headers={"content-type": "application/json"}, body=json.dumps(payload).encode()), timeout=20)
except asyncio.CancelledError:
break
except Exception:
pass
async def _do_warm(self):
count = 30
coros = [self._add_conn_to_pool() for _ in range(count)]
await asyncio.gather(*coros, return_exceptions=True)
def _auth_header(self) -> str:
return f"X-Auth-Key: {self.auth_key}\r\n" if self.auth_key else ""
async def tunnel(self, target_host: str, target_port: int, client_r: asyncio.StreamReader, client_w: asyncio.StreamWriter):
try:
remote_r, remote_w = await self._open()
except Exception:
return
try:
ws_key = base64.b64encode(os.urandom(16)).decode()
path = f"{self.worker_path}/tunnel?host={target_host}&port={target_port}"
handshake = (
f"GET {path} HTTP/1.1\r\n"
f"Host: {self.http_host}\r\n"
f"Upgrade: websocket\r\n"
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Key: {ws_key}\r\n"
f"Sec-WebSocket-Version: 13\r\n"
f"{self._auth_header()}"
f"\r\n"
)
remote_w.write(handshake.encode())
await remote_w.drain()
resp = b""
while b"\r\n\r\n" not in resp:
chunk = await asyncio.wait_for(remote_r.read(4096), timeout=15)
if not chunk:
raise ConnectionError("No response")
resp += chunk
status_line = resp.split(b"\r\n")[0]
if b"101" not in status_line:
raise ConnectionError("Upgrade rejected")
await asyncio.gather(self._client_to_ws(client_r, remote_w), self._ws_to_client(remote_r, client_w))
except Exception:
pass
finally:
try:
remote_w.close()
except Exception:
pass
async def _client_to_ws(self, src: asyncio.StreamReader, dst: asyncio.StreamWriter):
try:
while True:
data = await src.read(16384)
if not data:
dst.write(ws_encode(b"", opcode=0x08))
await dst.drain()
break
dst.write(ws_encode(data))
await dst.drain()
except (ConnectionError, asyncio.CancelledError):
pass
async def _ws_to_client(self, src: asyncio.StreamReader, dst: asyncio.StreamWriter):
buf = b""
try:
while True:
chunk = await src.read(16384)
if not chunk:
break
buf += chunk
while buf:
result = ws_decode(buf)
if result is None:
break
opcode, payload, consumed = result
buf = buf[consumed:]
if opcode == 0x08:
return
if payload:
dst.write(payload)
await dst.drain()
except (ConnectionError, asyncio.CancelledError):
pass
async def forward(self, raw_request: bytes) -> bytes:
try:
reader, writer, created = await self._acquire()
request = (
f"POST {self.worker_path}/forward HTTP/1.1\r\n"
f"Host: {self.http_host}\r\n"
f"Content-Type: application/octet-stream\r\n"
f"Content-Length: {len(raw_request)}\r\n"
f"Connection: keep-alive\r\n"
f"{self._auth_header()}"
f"\r\n"
)
writer.write(request.encode() + raw_request)
await writer.drain()
status, resp_headers, resp_body = await self._read_http_response(reader)
await self._release(reader, writer, created)
return resp_body
except Exception:
return b"HTTP/1.1 502 Bad Gateway\r\n\r\nDomain fronting request failed\r\n"
async def relay(self, method: str, url: str, headers: dict, body: bytes = b"") -> bytes:
if not self._warmed:
await self._warm_pool()
payload = self._build_payload(method, url, headers, body)
has_range = False
if headers:
for k in headers:
if k.lower() == "range":
has_range = True
break
if method == "GET" and not body and not has_range:
return await self._coalesced_submit(url, payload)
return await self._batch_submit(payload)
async def _coalesced_submit(self, url: str, payload: dict) -> bytes:
if url in self._coalesce:
future = asyncio.get_event_loop().create_future()
self._coalesce[url].append(future)
return await future
self._coalesce[url] = []
try:
result = await self._batch_submit(payload)
for f in self._coalesce.get(url, []):
if not f.done():
f.set_result(result)
return result
except Exception as e:
for f in self._coalesce.get(url, []):
if not f.done():
f.set_exception(e)
raise
finally:
self._coalesce.pop(url, None)
async def relay_parallel(self, method: str, url: str, headers: dict, body: bytes = b"", chunk_size: int = 256 * 1024, max_parallel: int = 16) -> bytes:
if method != "GET" or body:
return await self.relay(method, url, headers, body)
range_headers = dict(headers) if headers else {}
range_headers["Range"] = f"bytes=0-{chunk_size - 1}"
first_resp = await self.relay("GET", url, range_headers, b"")
status, resp_hdrs, resp_body = self._split_raw_response(first_resp)
if status != 206:
return first_resp
content_range = resp_hdrs.get("content-range", "")
m = re.search(r"/(\d+)", content_range)
if not m:
return self._rewrite_206_to_200(first_resp)
total_size = int(m.group(1))
if total_size <= chunk_size or len(resp_body) >= total_size:
return self._rewrite_206_to_200(first_resp)
ranges = []
start = len(resp_body)
while start < total_size:
end = min(start + chunk_size - 1, total_size - 1)
ranges.append((start, end))
start = end + 1
sem = asyncio.Semaphore(max_parallel)
async def fetch_range(s, e, max_tries: int = 3):
async with sem:
rh_base = dict(headers) if headers else {}
rh_base["Range"] = f"bytes={s}-{e}"
expected = e - s + 1
for attempt in range(max_tries):
try:
raw = await self.relay("GET", url, rh_base, b"")
_, _, chunk_body = self._split_raw_response(raw)
if len(chunk_body) == expected:
return chunk_body
except Exception:
pass
await asyncio.sleep(0.3 * (attempt + 1))
raise RuntimeError("chunk failed")
results = await asyncio.gather(*[fetch_range(s, e) for s, e in ranges], return_exceptions=True)
parts = [resp_body]
for r in results:
if isinstance(r, Exception):
return self._error_response(502, f"Parallel download failed")
parts.append(r)
full_body = b"".join(parts)
result = f"HTTP/1.1 200 OK\r\n"
skip = {"transfer-encoding", "connection", "keep-alive", "content-length", "content-encoding", "content-range"}
for k, v in resp_hdrs.items():
if k.lower() not in skip:
result += f"{k}: {v}\r\n"
result += f"Content-Length: {len(full_body)}\r\n\r\n"
return result.encode() + full_body
@staticmethod
def _rewrite_206_to_200(raw: bytes) -> bytes:
sep = b"\r\n\r\n"
if sep not in raw:
return raw
header_section, body = raw.split(sep, 1)
lines = header_section.decode(errors="replace").split("\r\n")
if not lines:
return raw
first = lines[0]
if " 206" in first:
lines[0] = first.replace(" 206 Partial Content", " 200 OK").replace(" 206", " 200 OK")
filtered = [lines[0]]
for ln in lines[1:]:
low = ln.lower()
if low.startswith("content-range:") or low.startswith("content-length:"):
continue
filtered.append(ln)
filtered.append(f"Content-Length: {len(body)}")
return ("\r\n".join(filtered) + "\r\n\r\n").encode() + body
def _build_payload(self, method, url, headers, body):
payload = {"m": method, "u": url, "r": True}
if headers:
filt = {k: v for k, v in headers.items() if k.lower() != "accept-encoding"}
payload["h"] = filt if filt else headers
if body:
payload["b"] = base64.b64encode(body).decode()
ct = headers.get("Content-Type") or headers.get("content-type")
if ct:
payload["ct"] = ct
return payload
async def _batch_submit(self, payload: dict) -> bytes:
if not self._batch_enabled:
return await self._relay_with_retry(payload)
future = asyncio.get_event_loop().create_future()
async with self._batch_lock:
self._batch_pending.append((payload, future))
if len(self._batch_pending) >= self._batch_max:
batch = self._batch_pending[:]
self._batch_pending.clear()
if self._batch_task and not self._batch_task.done():
self._batch_task.cancel()
self._batch_task = None
asyncio.create_task(self._batch_send(batch))
elif self._batch_task is None or self._batch_task.done():
self._batch_task = asyncio.create_task(self._batch_timer())
return await future
async def _batch_timer(self):
await asyncio.sleep(self._batch_window_micro)
async with self._batch_lock:
if len(self._batch_pending) <= 1:
if self._batch_pending:
batch = self._batch_pending[:]
self._batch_pending.clear()
self._batch_task = None
asyncio.create_task(self._batch_send(batch))
return
await asyncio.sleep(self._batch_window_macro - self._batch_window_micro)
async with self._batch_lock:
if self._batch_pending:
batch = self._batch_pending[:]
self._batch_pending.clear()
self._batch_task = None
asyncio.create_task(self._batch_send(batch))
async def _batch_send(self, batch: list):
if len(batch) == 1:
payload, future = batch[0]
try:
result = await self._relay_with_retry(payload)
if not future.done():
future.set_result(result)
except Exception as e:
if not future.done():
future.set_result(self._error_response(502, str(e)))
else:
try:
results = await self._relay_batch([p for p, _ in batch])
for (_, future), result in zip(batch, results):
if not future.done():
future.set_result(result)
except Exception as e:
self._batch_enabled = False
tasks = []
for payload, future in batch:
tasks.append(self._relay_fallback(payload, future))
await asyncio.gather(*tasks)
async def _relay_fallback(self, payload, future):
try:
result = await self._relay_with_retry(payload)
if not future.done():
future.set_result(result)
except Exception as e:
if not future.done():
future.set_result(self._error_response(502, str(e)))
async def _relay_with_retry(self, payload: dict) -> bytes:
if self._h2 and self._h2.is_connected:
for attempt in range(2):
try:
return await asyncio.wait_for(self._relay_single_h2(payload), timeout=25)
except Exception:
if attempt == 0:
try:
await self._h2.reconnect()
except Exception:
break
else:
raise
async with self._semaphore:
for attempt in range(2):
try:
return await asyncio.wait_for(self._relay_single(payload), timeout=25)
except Exception:
if attempt == 0:
await self._flush_pool()
else:
raise
async def _relay_single_h2(self, payload: dict) -> bytes:
full_payload = dict(payload)
full_payload["k"] = self.auth_key
json_body = json.dumps(full_payload).encode()
path = self._exec_path()
status, headers, body = await self._h2.request(method="POST", path=path, host=self.http_host, headers={"content-type": "application/json"}, body=json_body)
return self._parse_relay_response(body)
async def _relay_single(self, payload: dict) -> bytes:
full_payload = dict(payload)
full_payload["k"] = self.auth_key
json_body = json.dumps(full_payload).encode()
path = self._exec_path()
reader, writer, created = await self._acquire()
try:
request = (
f"POST {path} HTTP/1.1\r\n"
f"Host: {self.http_host}\r\n"
f"Content-Type: application/json\r\n"
f"Content-Length: {len(json_body)}\r\n"
f"Accept-Encoding: gzip\r\n"
f"Connection: keep-alive\r\n\r\n"
)
writer.write(request.encode() + json_body)
await writer.drain()
status, resp_headers, resp_body = await self._read_http_response(reader)
for _ in range(5):
if status not in (301, 302, 303, 307, 308):
break
location = resp_headers.get("location")
if not location:
break
parsed = urlparse(location)
rpath = parsed.path + ("?" + parsed.query if parsed.query else "")
request = (
f"GET {rpath} HTTP/1.1\r\n"
f"Host: {parsed.netloc}\r\n"
f"Accept-Encoding: gzip\r\n"
f"Connection: keep-alive\r\n\r\n"
)
writer.write(request.encode())
await writer.drain()
status, resp_headers, resp_body = await self._read_http_response(reader)
await self._release(reader, writer, created)
return self._parse_relay_response(resp_body)
except Exception:
try:
writer.close()
except Exception:
pass
raise
async def _relay_batch(self, payloads: list[dict]) -> list[bytes]:
batch_payload = {"k": self.auth_key, "q": payloads}
json_body = json.dumps(batch_payload).encode()
path = self._exec_path()
if self._h2 and self._h2.is_connected:
try:
status, headers, body = await asyncio.wait_for(self._h2.request(method="POST", path=path, host=self.http_host, headers={"content-type": "application/json"}, body=json_body), timeout=30)
return self._parse_batch_body(body, payloads)
except Exception:
pass
async with self._semaphore:
reader, writer, created = await self._acquire()
try:
request = (
f"POST {path} HTTP/1.1\r\n"
f"Host: {self.http_host}\r\n"
f"Content-Type: application/json\r\n"
f"Content-Length: {len(json_body)}\r\n"
f"Accept-Encoding: gzip\r\n"
f"Connection: keep-alive\r\n\r\n"
)
writer.write(request.encode() + json_body)
await writer.drain()
status, resp_headers, resp_body = await self._read_http_response(reader)
for _ in range(5):
if status not in (301, 302, 303, 307, 308):
break
location = resp_headers.get("location")
if not location:
break
parsed = urlparse(location)
rpath = parsed.path + ("?" + parsed.query if parsed.query else "")
request = (
f"GET {rpath} HTTP/1.1\r\n"
f"Host: {parsed.netloc}\r\n"
f"Accept-Encoding: gzip\r\n"
f"Connection: keep-alive\r\n\r\n"
)
writer.write(request.encode())
await writer.drain()
status, resp_headers, resp_body = await self._read_http_response(reader)
await self._release(reader, writer, created)
except Exception:
try:
writer.close()
except Exception:
pass
raise
return self._parse_batch_body(resp_body, payloads)
def _parse_batch_body(self, resp_body: bytes, payloads: list[dict]) -> list[bytes]:
text = resp_body.decode(errors="replace").strip()
try:
data = json.loads(text)
except json.JSONDecodeError:
m = re.search(r'\{.*\}', text, re.DOTALL)
data = json.loads(m.group()) if m else None
if not data:
raise RuntimeError("Bad batch response")
if "e" in data:
raise RuntimeError(f"Batch error: {data['e']}")
items = data.get("q", [])
if len(items) != len(payloads):
raise RuntimeError("Batch size mismatch")
results = []
for item in items:
results.append(self._parse_relay_json(item))
return results
async def _read_http_response(self, reader: asyncio.StreamReader):
raw = b""
while b"\r\n\r\n" not in raw:
chunk = await asyncio.wait_for(reader.read(8192), timeout=8)
if not chunk:
break
raw += chunk
if b"\r\n\r\n" not in raw:
return 0, {}, b""
header_section, body = raw.split(b"\r\n\r\n", 1)
lines = header_section.split(b"\r\n")
status_line = lines[0].decode(errors="replace")
m = re.search(r"\d{3}", status_line)
status = int(m.group()) if m else 0
headers = {}
for line in lines[1:]:
if b":" in line:
k, v = line.decode(errors="replace").split(":", 1)
headers[k.strip().lower()] = v.strip()
content_length = headers.get("content-length")
transfer_encoding = headers.get("transfer-encoding", "")
if "chunked" in transfer_encoding:
body = await self._read_chunked(reader, body)
elif content_length:
remaining = int(content_length) - len(body)
while remaining > 0:
chunk = await asyncio.wait_for(reader.read(min(remaining, 65536)), timeout=20)
if not chunk:
break
body += chunk
remaining -= len(chunk)
else:
while True:
try:
chunk = await asyncio.wait_for(reader.read(65536), timeout=2)
if not chunk:
break
body += chunk
except asyncio.TimeoutError:
break
if headers.get("content-encoding", "").lower() == "gzip":
try:
body = gzip.decompress(body)
except Exception:
pass
return status, headers, body
async def _read_chunked(self, reader, buf=b""):
result = b""
while True:
while b"\r\n" not in buf:
data = await asyncio.wait_for(reader.read(8192), timeout=20)
if not data:
return result
buf += data
end = buf.find(b"\r\n")
size_str = buf[:end].decode(errors="replace").strip()
buf = buf[end + 2:]
if not size_str:
continue
try:
size = int(size_str, 16)
except ValueError:
break
if size == 0:
break
while len(buf) < size + 2:
data = await asyncio.wait_for(reader.read(65536), timeout=20)
if not data:
result += buf[:size]
return result
buf += data
result += buf[:size]
buf = buf[size + 2:]
return result
def _parse_relay_response(self, body: bytes) -> bytes:
text = body.decode(errors="replace").strip()
if not text:
return self._error_response(502, "Empty response from relay")
try:
data = json.loads(text)
except json.JSONDecodeError:
m = re.search(r'\{.*\}', text, re.DOTALL)
if m:
try:
data = json.loads(m.group())
except json.JSONDecodeError:
return self._error_response(502, "Bad JSON")
else:
return self._error_response(502, "No JSON")
return self._parse_relay_json(data)
def _parse_relay_json(self, data: dict) -> bytes:
if "e" in data:
return self._error_response(502, f"Relay error: {data['e']}")
status = data.get("s", 200)
resp_headers = data.get("h", {})
resp_body = base64.b64decode(data.get("b", ""))
status_text = {200: "OK", 206: "Partial Content", 301: "Moved", 302: "Found", 304: "Not Modified", 400: "Bad Request", 403: "Forbidden", 404: "Not Found", 500: "Internal Server Error"}.get(status, "OK")
result = f"HTTP/1.1 {status} {status_text}\r\n"
skip = {"transfer-encoding", "connection", "keep-alive", "content-length", "content-encoding"}
for k, v in resp_headers.items():
if k.lower() in skip:
continue
values = v if isinstance(v, list) else [v]
if k.lower() == "set-cookie":
expanded = []
for item in values:
expanded.extend(self._split_set_cookie(str(item)))
values = expanded
for val in values:
result += f"{k}: {val}\r\n"
result += f"Content-Length: {len(resp_body)}\r\n\r\n"
return result.encode() + resp_body
@staticmethod
def _split_set_cookie(blob: str) -> list[str]:
if not blob:
return []
parts = re.split(r",\s*(?=[A-Za-z0-9!#$%&'*+\-.^_`|~]+=)", blob)
return [p.strip() for p in parts if p.strip()]
def _split_raw_response(self, raw: bytes):
if b"\r\n\r\n" not in raw:
return 0, {}, raw
header_section, body = raw.split(b"\r\n\r\n", 1)
lines = header_section.split(b"\r\n")
m = re.search(r"\d{3}", lines[0].decode(errors="replace"))
status = int(m.group()) if m else 0
headers = {}
for line in lines[1:]:
if b":" in line:
k, v = line.decode(errors="replace").split(":", 1)
headers[k.strip().lower()] = v.strip()
return status, headers, body
def _error_response(self, status: int, message: str) -> bytes:
body = f"<html><body><h1>{status}</h1><p>{message}</p></body></html>"
return (f"HTTP/1.1 {status} Error\r\nContent-Type: text/html\r\nContent-Length: {len(body)}\r\n\r\n{body}").encode()