Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#!/usr/bin/env python3"""Record one connection between a git client and git daemon.
record.py LISTEN TARGET NAME listens on 127.0.0.1:LISTEN, says "ready" onstandard output, takes one connection, relays it to 127.0.0.1:TARGET andwrites what each side sent: NAME.client, the client's bytes, NAME.server,the server's, and NAME.eof, how many of the server's bytes the client hadbeen sent when it ended its stream. Each side's end of stream is passed onto the other as a shutdown of the writing half, so neither sees an end theother did not send."""
import socketimport sysimport threading
def relay(src, dst, sink, side, ends, lock): while True: data = src.recv(65536) if not data: break dst.sendall(data) with lock: sink.append(data) with lock: ends[side] = sum(map(len, ends["received"])) try: dst.shutdown(socket.SHUT_WR) except OSError: pass
def main(): listen, target, name = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3] server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(("127.0.0.1", listen)) server.listen(1) print("ready", flush=True) client, _ = server.accept() upstream = socket.create_connection(("127.0.0.1", target)) sent, received, lock = [], [], threading.Lock() ends = {"received": received} threads = [ threading.Thread( target=relay, args=(client, upstream, sent, "client", ends, lock) ), threading.Thread( target=relay, args=(upstream, client, received, "server", ends, lock) ), ] for t in threads: t.start() for t in threads: t.join(60) if t.is_alive(): sys.exit("record.py: a side did not end its stream in 60 s") client.close() upstream.close() with open(name + ".client", "wb") as f: f.write(b"".join(sent)) with open(name + ".server", "wb") as f: f.write(b"".join(received)) with open(name + ".eof", "w") as f: f.write("%d\n" % ends["client"])
if __name__ == "__main__": main()