sys.exit(1) server.listen(1) try: while True: client, _ = server.accept() # Go ahead and assume a command, since the client # will never send a lone True/False msgtype, params = socketproto.read_message(client) if msgtype == socketproto.Messages.AddProxy: src, dest = params try: portforward.add_mapping(src, dest) socketproto.write_message(client, True) logger.debug("Done") except socket.error as e: socketproto.write_message(client, False) logger.debug("Fail\n\t-%s", e) elif msgtype == socketproto.Messages.DelProxy: src = params try: portforward.del_mapping(src) socketproto.write_message(client, True) logger.debug("Done") except KeyError: socketproto.write_message(client, False) logger.debug("Fail")
sys.exit(1) if sys.argv[1] == 'add': src = portspec(sys.argv[2]) dest = portspec(sys.argv[3]) elif sys.argv[1] == 'del': src = portspec(sys.argv[2]) except IndexError: print(__doc__) sys.exit(1) client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) client.connect("/tmp/.proxy-socket") if sys.argv[1] == 'add': socketproto.write_message(client, (socketproto.Messages.AddProxy, (src, dest))) if socketproto.read_message(client) is not True: print('[Unable to map port - is it taken already?]') sys.exit(1) elif sys.argv[1] == 'del': socketproto.write_message(client, (socketproto.Messages.DelProxy, src)) if socketproto.read_message(client) is not True: print('[Unable to unmap port - does it have a proxy?]') sys.exit(1) elif sys.argv[1] == 'list': socketproto.write_message(client, (socketproto.Messages.GetProxies, [])) msg, proxies = socketproto.read_message(client) if msg != socketproto.Messages.GetProxies: print('[Protocol error]')
test_socket_recv.bind('/tmp/proxy-sockets-test') test_socket_recv.listen(1) client, _ = test_socket_recv.accept() try: # The first message is a bind from :8000 to www.google.com:80. Succeed at this. msgtype, params = socketproto.read_message(client) assert_eq(msgtype, socketproto.Messages.AddProxy) assert_eq(params[0][0], '') assert_eq(params[0][1], 8000) assert_eq(params[0][2], socket.SOCK_DGRAM) assert_eq(params[1][0], 'www.google.com') assert_eq(params[1][1], 80) assert_eq(params[1][2], socket.SOCK_STREAM) socketproto.write_message(client, True) print("[AddProxy] Success") # The second message removes the bind at :8000. Fail at this. msgtype, params = socketproto.read_message(client) assert_eq(msgtype, socketproto.Messages.DelProxy) assert_eq(params[0], '') assert_eq(params[1], 8000) assert_eq(params[2], socket.SOCK_DGRAM) socketproto.write_message(client, False) print("[DelProxy] Success") # The third message lists the proxies (say that there is only one) msgtype, params = socketproto.read_message(client) assert_eq(msgtype, socketproto.Messages.GetProxies)
import socket import socketproto import time def assert_eq(a, b): print(repr(a), "==", repr(b), "...") assert a == b test_socket_send = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) test_socket_send.connect('/tmp/proxy-sockets-test') try: socketproto.write_message(test_socket_send, (socketproto.Messages.AddProxy, (('', 8000, socket.SOCK_DGRAM), ('www.google.com', 80, socket.SOCK_STREAM)))) msg = socketproto.read_message(test_socket_send) assert type(msg) is bool assert_eq(msg, True) print("[AddProxy] Success") socketproto.write_message(test_socket_send, (socketproto.Messages.DelProxy, ('', 8000, socket.SOCK_DGRAM))) msg = socketproto.read_message(test_socket_send) assert type(msg) is bool assert_eq(msg, False) print("[DelProxy] Success") socketproto.write_message(test_socket_send,
import socket import socketproto import time def assert_eq(a, b): print(repr(a), "==", repr(b), "...") assert a == b test_socket_send = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) test_socket_send.connect('/tmp/proxy-sockets-test') try: socketproto.write_message(test_socket_send, (socketproto.Messages.AddProxy, (('', 8000, socket.SOCK_DGRAM), ('www.google.com', 80, socket.SOCK_STREAM)))) msg = socketproto.read_message(test_socket_send) assert type(msg) is bool assert_eq(msg, True) print("[AddProxy] Success") socketproto.write_message(test_socket_send, (socketproto.Messages.DelProxy, ('', 8000, socket.SOCK_DGRAM))) msg = socketproto.read_message(test_socket_send) assert type(msg) is bool assert_eq(msg, False) print("[DelProxy] Success") socketproto.write_message(test_socket_send, (socketproto.Messages.GetProxies, [])) msgtype, param = socketproto.read_message(test_socket_send) assert_eq(msgtype, socketproto.Messages.GetProxies) assert_eq(param, [(('', 8000, socket.SOCK_DGRAM), ('www.google.com', 80, socket.SOCK_STREAM))]) print("[GetProxies] Success") finally: test_socket_send.close()