GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vm2api/scripts/http_to_socks.py at dev · dofastted/vm2api

Expand file tree

/

http_to_socks.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
143 lines (125 loc) · 4.63 KB
/

http_to_socks.py

Copy path

File metadata and controls

143 lines (125 loc) · 4.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python3
"""HTTP CONNECT bridge → one SOCKS5 egress.
Claude CLI treats HTTPS_PROXY as HTTP CONNECT. It cannot dial socks5://.
Point KIN_HTTPS_PROXY at this listener; keep KIN_SOCKS5 as the real egress
so refresh (Go) and inference (CLI) share one path.
KIN_SOCKS5=socks5h://user:pass@host:port
KIN_HTTP_BRIDGE_ADDR=127.0.0.1:18080
python3 scripts/http_to_socks.py
"""
from __future__ import annotations
import os
import select
import socket
import struct
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import unquote, urlparse
def parse_socks5(raw: str) -> tuple[str, int, str | None, str | None]:
value = raw.strip()
if "://" not in value:
value = "socks5h://" + value
parsed = urlparse(value)
if parsed.scheme not in {"socks5", "socks5h"}:
raise SystemExit("KIN_SOCKS5 must be socks5h://user:pass@host:port")
if not parsed.hostname or not parsed.port:
raise SystemExit("KIN_SOCKS5 host:port required")
user = unquote(parsed.username) if parsed.username else None
password = unquote(parsed.password) if parsed.password else None
return parsed.hostname, parsed.port, user, password
def socks5_connect(
socks_host: str,
socks_port: int,
user: str | None,
password: str | None,
target: str,
port: int,
timeout: float = 20,
) -> socket.socket:
sock = socket.create_connection((socks_host, socks_port), timeout=timeout)
if user is not None:
sock.sendall(b"\x05\x01\x02")
if sock.recv(2) != b"\x05\x02":
sock.close()
raise OSError("socks5 method rejected")
u, p = user.encode(), (password or "").encode()
sock.sendall(b"\x01" + bytes([len(u)]) + u + bytes([len(p)]) + p)
if sock.recv(2) != b"\x01\x00":
sock.close()
raise OSError("socks5 auth failed")
else:
sock.sendall(b"\x05\x01\x00")
if sock.recv(2) != b"\x05\x00":
sock.close()
raise OSError("socks5 method rejected")
host = target.encode()
sock.sendall(b"\x05\x01\x00\x03" + bytes([len(host)]) + host + struct.pack("!H", port))
reply = sock.recv(10)
if len(reply) < 2 or reply[1] != 0:
sock.close()
raise OSError("socks5 connect failed")
sock.settimeout(None)
return sock
def pipe(left: socket.socket, right: socket.socket) -> None:
sockets = [left, right]
try:
while True:
ready, _, _ = select.select(sockets, [], [], 300)
if not ready:
break
for src in ready:
data = src.recv(65536)
if not data:
return
dst = right if src is left else left
dst.sendall(data)
except OSError:
return
finally:
for sock in sockets:
try:
sock.shutdown(socket.SHUT_RDWR)
except OSError:
pass
try:
sock.close()
except OSError:
pass
def main() -> None:
socks = parse_socks5(os.environ.get("KIN_SOCKS5", ""))
listen = os.environ.get("KIN_HTTP_BRIDGE_ADDR", "127.0.0.1:18080")
host, _, port = listen.partition(":")
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, fmt: str, *args) -> None:
msg = fmt % args
if "sk-ant-" in msg or (socks[3] and socks[3] in msg):
return
print(msg, flush=True)
def do_CONNECT(self) -> None:
target, _, target_port = self.path.partition(":")
try:
remote = socks5_connect(
socks[0], socks[1], socks[2], socks[3], target, int(target_port or "443")
)
except Exception as exc:
self.send_error(502, str(exc))
return
self.send_response(200, "Connection Established")
self.end_headers()
pipe(self.connection, remote)
def do_GET(self) -> None:
if self.path in {"/", "/health"}:
body = b"ok"
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return
self.send_error(405)
httpd = ThreadingHTTPServer((host, int(port or "18080")), Handler)
print(f"http-connect -> socks5h {socks[0]}:{socks[1]} listen {listen}", flush=True)
httpd.serve_forever()
if __name__ == "__main__":
main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.