-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetSplit.py
More file actions
394 lines (334 loc) · 14.6 KB
/
netSplit.py
File metadata and controls
394 lines (334 loc) · 14.6 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
"""Lightweight TCP proxy with a tiny selection protocol (NetSplit).
This module implements a small proxy server that speaks a minimal
selection protocol allowing a client to request one of several backend
servers by index or name. It forwards raw TCP data between the client
and the selected backend and contains small helpers for HTTP detection
and error reporting used by both client and server flows.
The public entry point is the module-level runner at the bottom which
loads a JSON configuration and starts listening on TCP port 8080.
"""
from typing import Tuple, Optional, List, Dict
import socket as _socket
import threading
import selectors
import time
import json
import sys
VERSION = "NetSplit-2"
# Backwards-compatibility: list of supported protocol version tokens (newest first).
# This list is used during protocol negotiation so older clients can still connect.
SUPPORTED_VERSIONS: List[str] = [
"NetSplit-2",
"NetSplit-1",
]
# Protocol error payloads sent to clients when the proxy fails early.
PROTOCOL_ERRORS: Dict[str, bytes] = {
"no_support": b"E\x00",
"client_error": b"E\x01",
"proxy_error": b"E\x02",
"invalid_server": b"E\x03",
}
class socket(_socket.socket):
"""Small _socket subclass that contains proxy helper methods.
Intentionally lightweight: it mainly centralizes protocol error
replies and implements helpers used by the client/server flows.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# allow quick restarts while developing
self.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
def _send_protocol_error(self, sock: _socket.socket, code: str, err: bytes = b"") -> str:
"""Send a protocol error payload to `sock` (best-effort) and close it.
Returns a human-readable description suitable for logging.
"""
payload = PROTOCOL_ERRORS.get(code, b"Unknown")
if err:
sock.sendall(payload + b"[" + err + b"]")
else:
sock.sendall(payload)
sock.close()
if err:
decoded = err.decode(errors="ignore")
return f"Protocol error: {code} [{decoded}]"
return f"Protocol error: {code}"
def bind(self, address: Tuple[str, int]) -> None:
"""Bind and start listening on ``address``.
This wrapper sets a conservative listen backlog so the socket is
ready to accept connections after binding.
:param address: (host, port) tuple to bind the socket to.
"""
super().bind(address)
# default listen backlog
self.listen(5)
def recv_exact(self, sock: _socket.socket, n: int) -> bytes:
"""Read exactly ``n`` bytes from ``sock`` or return b'' on EOF.
This helper avoids partial reads when the protocol expects fixed
length fields. It will block until ``n`` bytes are received or the
peer closes the connection.
:param sock: socket to read from
:param n: number of bytes to read
:return: bytes read (length ``n``) or b'' on EOF
"""
buf = b""
while len(buf) < n:
chunk = sock.recv(n - len(buf))
if not chunk:
return b""
buf += chunk
return buf
# Client-side API
def connect(self, address: Tuple[str, int], server: object) -> Optional[str]:
"""Connect to an upstream proxy listener and request a backend.
`server` may be an integer index/key or a string name.
Returns None on success or a string error message on failure.
"""
super().connect(address)
# send VERSION
self.sendall(VERSION.encode())
resp = self.recv(128)
if resp != b"OK":
return self._send_protocol_error(self, "no_support", resp)
raw = self.recv_exact(self, 4)
if not raw:
return self._send_protocol_error(self, "proxy_error", b"missing_server_count")
# server count is ignored by the client, but read for protocol sync
_ = int.from_bytes(raw, "big")
self.sendall(b"OK")
# Support integer index or string key
if isinstance(server, int):
self.sendall(b"i" + int(server).to_bytes(4, "big"))
else:
name = str(server).encode()
self.sendall(b"s" + len(name).to_bytes(4, "big") + name)
resp = self.recv(128)
if resp == b"OK":
return None
return self._send_protocol_error(self, "proxy_error", resp)
# Server-side helpers
def accept(self) -> Tuple[_socket.socket, Tuple[str, int]]:
"""Accept a new connection and return (conn, address).
This signature mirrors ``socket.socket.accept`` but provides an
explicit return type for the local subclass.
"""
return super().accept()
def handle_client(self, cs: _socket.socket, addr: Tuple[str, int]) -> Optional[str]:
"""Handle an incoming client connection.
This method performs a small amount of protocol detection (very
basic HTTP detection by method token) and either forwards the
connection to an HTTP backend or speaks the tiny selection
protocol to choose a backend and proxy raw TCP traffic.
:param cs: client-side socket object
:param addr: client address tuple (host, port)
:return: optional human-readable error string for logging, or
``None`` on success/normal termination.
"""
# Read an initial buffer to help detect HTTP requests without
# fragmenting the request data that should go to an HTTP backend.
initial = cs.recv(4096)
if not initial:
return self._send_protocol_error(cs, "client_error", b"empty")
# Rough HTTP detection by common methods
http_methods = (b"GET ", b"POST ", b"HEAD ", b"PUT ", b"DELETE ", b"OPTIONS ")
if any(initial.startswith(m) for m in http_methods):
# If an 'http' backend exists, forward; otherwise respond with a
# small HTML message.
http_backend = config.get("http") if isinstance(config, dict) else None
if http_backend is None:
body = b"<h1>This is not a website!</h1>"
headers = (
b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html; charset=utf-8\r\n"
+ f"Content-Length: {len(body)}\r\n".encode()
+ b"\r\n"
)
cs.sendall(headers + body)
time.sleep(0.5)
cs.close()
return "HTTP Client Tried to connect (no http backend)"
sock = _socket.socket()
sock.settimeout(10)
sock.connect((http_backend["host"], http_backend["port"]))
sock.sendall(initial)
return self._proxy_between(cs, sock)
# Not HTTP — treat the initial buffer as the start of the proxy protocol.
if len(initial) < len(VERSION):
rest = self.recv_exact(cs, len(VERSION) - len(initial))
if not rest:
return self._send_protocol_error(cs, "no_support", initial)
ver = initial + rest
else:
ver = initial[: len(VERSION)]
# Accept any supported protocol version token (not just the current
# `VERSION`). This allows older clients to connect if we still support
# their protocol layout.
if ver not in [v.encode() for v in SUPPORTED_VERSIONS]:
return self._send_protocol_error(cs, "no_support", ver)
cs.sendall(b"OK")
cs.sendall(len(config).to_bytes(4, "big"))
ack = self.recv_exact(cs, len(b"OK"))
if ack != b"OK":
return self._send_protocol_error(cs, "client_error", ack)
# Read server selection marker
marker = cs.recv(1)
if not marker:
return self._send_protocol_error(cs, "client_error", b"missing_id")
# Resolve selected backend based on marker
if marker == b"i":
raw = self.recv_exact(cs, 4)
if not raw:
return self._send_protocol_error(cs, "client_error", b"missing_id")
sid = int.from_bytes(raw, "big")
if isinstance(config, dict):
if sid in config:
server = config[sid]
else:
try:
server = list(config.values())[sid]
except IndexError:
return self._send_protocol_error(cs, "invalid_server", raw)
else:
try:
server = config[sid]
except (KeyError, IndexError):
return self._send_protocol_error(cs, "invalid_server", raw)
elif marker == b"s":
llen = self.recv_exact(cs, 4)
if not llen:
return self._send_protocol_error(cs, "client_error", b"missing_name_len")
ln = int.from_bytes(llen, "big")
name = cs.recv(ln).decode()
if isinstance(config, dict) and name in config:
server = config[name]
else:
return self._send_protocol_error(cs, "invalid_server", name.encode())
else:
# Legacy path: first byte was part of a 4-byte integer id
rest = self.recv_exact(cs, 3)
if not rest:
return self._send_protocol_error(cs, "client_error", b"missing_id")
raw = marker + rest
sid = int.from_bytes(raw, "big")
if isinstance(config, dict):
if sid in config:
server = config[sid]
else:
try:
server = list(config.values())[sid]
except IndexError:
return self._send_protocol_error(cs, "invalid_server", raw)
else:
try:
server = config[sid]
except (KeyError, IndexError):
return self._send_protocol_error(cs, "invalid_server", raw)
cs.sendall(b"OK")
sock = _socket.socket()
sock.settimeout(10)
sock.connect((server["host"], server["port"]))
sock.setblocking(False)
cs.setblocking(False)
return self._proxy_between(cs, sock)
def _proxy_between(self, cs: _socket.socket, sock: _socket.socket) -> Optional[str]:
"""Proxy data between client socket ``cs`` and backend ``sock``.
Uses ``selectors.DefaultSelector`` to non-blockingly forward data
between the two sockets until one end closes. This function is a
low-level forwarding loop and returns an optional error string if
something notable occurred (currently used for logging only).
:param cs: client socket
:param sock: backend socket
:return: optional error message string or ``None``
"""
# Print peer names (allow exception to propagate if it fails)
print(f"Proxying {':'.join(map(str, cs.getpeername()))} -> {':'.join(map(str, sock.getpeername()))}")
sock.setblocking(False)
cs.setblocking(False)
sel = selectors.DefaultSelector()
sel.register(cs, selectors.EVENT_READ, "client")
sel.register(sock, selectors.EVENT_READ, "server")
while True:
events = sel.select(timeout=1)
if not events:
continue
for key, _ in events:
s = key.fileobj
role = key.data
try:
data = s.recv(4096)
except BlockingIOError:
continue
except (ConnectionResetError, ConnectionAbortedError):
# Treat aborted/reset connections as a clean shutdown
return None
if DEBUG:
print(data)
if not data:
# peer closed; clean up both sides and exit
_cleanup_pair(sel, cs, sock)
return None
# Forward data; let send failures propagate
if role == "client":
sock.sendall(data)
else:
cs.sendall(data)
def _safe_close(sock: Optional[_socket.socket]) -> None:
"""Close a _socket if not None, ignoring all exceptions."""
if sock is None:
return
sock.close()
def _safe_unregister(sel: selectors.BaseSelector, sock: _socket.socket) -> None:
"""Unregister a file object from ``sel`` ignoring any errors.
Selectors may raise if the file object is not registered; this helper
swallows those errors to allow best-effort cleanup.
"""
try:
sel.unregister(sock)
except Exception:
# ignore errors during cleanup
pass
def _cleanup_pair(sel: selectors.BaseSelector, cs: _socket.socket, sock: _socket.socket) -> None:
"""Unregister and close two _sockets, best-effort."""
_safe_unregister(sel, cs)
_safe_unregister(sel, sock)
_safe_close(cs)
_safe_close(sock)
def _accept_loop(listener: _socket, stop_event: threading.Event, workers: List[threading.Thread]) -> None:
"""Accept loop that spawns a worker thread for each incoming client.
Runs until ``stop_event`` is set or the listener raises ``OSError``
(usually because it was closed during shutdown).
"""
while not stop_event.is_set():
try:
cs, addr = listener.accept()
except OSError:
break
t = threading.Thread(target=_client_worker, args=(listener, cs, addr), daemon=True)
workers.append(t)
t.start()
def _client_worker(listener: _socket, cs: _socket.socket, addr: Tuple[str, int]) -> None:
"""Thread target that delegates a client connection to ``listener``.
:param listener: the proxy listener socket (provides helper methods)
:param cs: client socket
:param addr: client address tuple
"""
err = listener.handle_client(cs, addr)
if err:
print(f'client {addr} error: {err}')
# This just displays all traffic
DEBUG = False
if __name__ == '__main__':
config = json.load(open(sys.argv[1]))
with socket(_socket.AF_INET, _socket.SOCK_STREAM) as s:
s.bind(('0.0.0.0', 8080))
print('Proxy listening on 0.0.0.0:8080')
stop_event = threading.Event()
workers: List[threading.Thread] = []
accept_thread = threading.Thread(target=_accept_loop, args=(s, stop_event, workers), daemon=False)
accept_thread.start()
try:
while not stop_event.is_set():
time.sleep(1)
except KeyboardInterrupt:
print('Shutting down...')
stop_event.set()
s.close()
accept_thread.join(timeout=5)