Fix VPN internet connectivity and simplify client setup
Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Isolated LAN SOCKS5 fixture for android_proxy_test.dart; no user profiles.
|
||||
|
||||
Only the explicitly supplied device IP is accepted. Documentation addresses
|
||||
serve deterministic TCP/UDP replies; other TCP destinations use real sockets
|
||||
to verify default DoH and certificate-validated HTTPS through the proxy.
|
||||
"""
|
||||
import argparse
|
||||
import ipaddress
|
||||
import select
|
||||
import socket
|
||||
import socketserver
|
||||
import struct
|
||||
|
||||
|
||||
def read_exact(sock, length):
|
||||
result = b""
|
||||
while len(result) < length:
|
||||
chunk = sock.recv(length - len(result))
|
||||
if not chunk:
|
||||
raise EOFError()
|
||||
result += chunk
|
||||
return result
|
||||
|
||||
|
||||
def address(sock, kind):
|
||||
if kind == 1:
|
||||
host = socket.inet_ntop(socket.AF_INET, read_exact(sock, 4))
|
||||
elif kind == 4:
|
||||
host = socket.inet_ntop(socket.AF_INET6, read_exact(sock, 16))
|
||||
elif kind == 3:
|
||||
host = read_exact(sock, read_exact(sock, 1)[0]).decode("ascii")
|
||||
else:
|
||||
raise ValueError("Invalid SOCKS address")
|
||||
return host, struct.unpack("!H", read_exact(sock, 2))[0]
|
||||
|
||||
|
||||
class Handler(socketserver.BaseRequestHandler):
|
||||
def handle(self):
|
||||
if self.client_address[0] != self.server.device:
|
||||
return
|
||||
client = self.request
|
||||
client.settimeout(45)
|
||||
try:
|
||||
version, count = read_exact(client, 2)
|
||||
methods = read_exact(client, count)
|
||||
if version != 5 or 0 not in methods:
|
||||
return
|
||||
client.sendall(b"\x05\x00")
|
||||
version, command, _, kind = read_exact(client, 4)
|
||||
host, port = address(client, kind)
|
||||
if command == 3:
|
||||
self.udp(client)
|
||||
return
|
||||
if version != 5 or command != 1:
|
||||
return
|
||||
if host == "198.51.100.45" and port == 18082:
|
||||
client.sendall(b"\x05\x00\x00\x01" + b"\x00" * 6)
|
||||
data = b""
|
||||
while b"\r\n\r\n" not in data and len(data) < 8192:
|
||||
data += client.recv(4096)
|
||||
body = b"MeshPort proxy fixture"
|
||||
client.sendall(b"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: "
|
||||
+ str(len(body)).encode() + b"\r\n\r\n" + body)
|
||||
print("FIXTURE_TCP_PASS", flush=True)
|
||||
return
|
||||
# No connections to private hosts, other devices, or host services.
|
||||
destinations = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
|
||||
public = [x for x in destinations if ipaddress.ip_address(x[4][0]).is_global]
|
||||
if not public:
|
||||
raise ValueError("Only public upstreams allowed")
|
||||
remote = None
|
||||
for family, kind, proto, _, destination in public:
|
||||
candidate = socket.socket(family, kind, proto)
|
||||
candidate.settimeout(10)
|
||||
try:
|
||||
candidate.connect(destination)
|
||||
remote = candidate
|
||||
break
|
||||
except OSError:
|
||||
candidate.close()
|
||||
if remote is None:
|
||||
raise OSError("No reachable public upstream")
|
||||
with remote:
|
||||
client.sendall(b"\x05\x00\x00\x01" + b"\x00" * 6)
|
||||
print("FIXTURE_PUBLIC_TCP_CONNECTED", flush=True)
|
||||
while True:
|
||||
readable, _, _ = select.select([client, remote], [], [], 45)
|
||||
if not readable:
|
||||
return
|
||||
for source in readable:
|
||||
data = source.recv(65536)
|
||||
if not data:
|
||||
return
|
||||
(remote if source is client else client).sendall(data)
|
||||
except (OSError, ValueError, EOFError) as error:
|
||||
print("FIXTURE_CONNECTION_ERROR", type(error).__name__, flush=True)
|
||||
|
||||
def udp(self, client):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp:
|
||||
udp.bind((self.server.server_address[0], 0))
|
||||
endpoint = udp.getsockname()
|
||||
client.sendall(b"\x05\x00\x00\x01" + socket.inet_aton(endpoint[0])
|
||||
+ struct.pack("!H", endpoint[1]))
|
||||
while True:
|
||||
readable, _, _ = select.select([client, udp], [], [], 45)
|
||||
if not readable or client in readable:
|
||||
return
|
||||
data, peer = udp.recvfrom(65536)
|
||||
if peer[0] != self.server.device:
|
||||
continue
|
||||
if data[:4] == b"\x00\x00\x00\x01" and len(data) >= 10:
|
||||
target = socket.inet_ntoa(data[4:8])
|
||||
port = struct.unpack("!H", data[8:10])[0]
|
||||
if target == "198.51.100.45" and port == 18083:
|
||||
udp.sendto(data, peer)
|
||||
print("FIXTURE_UDP_PASS", flush=True)
|
||||
|
||||
|
||||
class Server(socketserver.ThreadingTCPServer):
|
||||
allow_reuse_address = True
|
||||
daemon_threads = True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--host", required=True)
|
||||
parser.add_argument("--device", required=True)
|
||||
args = parser.parse_args()
|
||||
with Server((args.host, 18084), Handler) as server:
|
||||
server.device = args.device
|
||||
print("Android proxy fixture ready", flush=True)
|
||||
server.serve_forever()
|
||||
Reference in New Issue
Block a user