diff --git a/juicepassproxy.py b/juicepassproxy.py index b091f44..5b42b65 100755 --- a/juicepassproxy.py +++ b/juicepassproxy.py @@ -264,6 +264,15 @@ def basic_message_try_parse(self, data): ) return message + def pyproxy_oserror_message_try_parse(self, data): + message = {"type": "pyproxy_oserror"} + err_data = str(data).split("|") + message["status"] = "unavailable" + message[ + "debug_message" + ] = f"PyProxy {err_data[1].title()} OSError {err_data[3]} [{err_data[2]}]: {err_data[4]}" + return message + def debug_message_try_parse(self, data): message = {"type": "debug"} dbg_data = ( @@ -303,7 +312,9 @@ def remote_data_handler(self, data): def local_data_handler(self, data): logging.debug("local: {}".format(data)) - if ":DBG," in str(data): + if "PYPROXY_OSERROR" in str(data): + message = self.pyproxy_oserror_message_try_parse(data) + elif ":DBG," in str(data): message = self.debug_message_try_parse(data) else: message = self.basic_message_try_parse(data) diff --git a/pyproxy/pyproxy.py b/pyproxy/pyproxy.py index 05177c6..4cb4f28 100644 --- a/pyproxy/pyproxy.py +++ b/pyproxy/pyproxy.py @@ -16,6 +16,7 @@ import logging import select import socket +import errno FORMAT = '%(asctime)-15s %(levelname)-10s %(message)s' @@ -30,7 +31,7 @@ def udp_proxy(src, dst): """Run UDP proxy. - + Arguments: src -- Source IP address and port string. I.e.: '127.0.0.1:8000' dst -- Destination IP address and port. I.e.: '127.0.0.1:8888' @@ -38,34 +39,42 @@ def udp_proxy(src, dst): LOGGER.debug('Starting UDP proxy...') LOGGER.debug('Src: {}'.format(src)) LOGGER.debug('Dst: {}'.format(dst)) - + proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) proxy_socket.bind(ip_to_tuple(src)) - + client_address = None server_address = ip_to_tuple(dst) - + LOGGER.debug('Looping proxy (press Ctrl-Break to stop)...') while True: data, address = proxy_socket.recvfrom(BUFFER_SIZE) - + if address[0] != server_address[0]: client_address = address if address == client_address: data = LOCAL_DATA_HANDLER(data) - proxy_socket.sendto(data, server_address) + try: + proxy_socket.sendto(data, server_address) + except OSError as e: + LOGGER.warning(f"PyProxy Server OSError {errno.errorcode[e.errno]} [{server_address}]: {e}") + LOCAL_DATA_HANDLER(f"PYPROXY_OSERROR|server|{server_address}|{errno.errorcode[e.errno]}|{e}") elif address == server_address: data = REMOTE_DATA_HANDLER(data) - proxy_socket.sendto(data, client_address) + try: + proxy_socket.sendto(data, client_address) + except OSError as e: + LOGGER.warning(f"PyProxy Client OSError {errno.errorcode[e.errno]} [{client_address}]: {e}") + LOCAL_DATA_HANDLER(f"PYPROXY_OSERROR|client|{client_address}|{errno.errorcode[e.errno]}|{e}") else: LOGGER.warning('Unknown address: {}'.format(str(address))) -# end-of-function udp_proxy - - +# end-of-function udp_proxy + + def tcp_proxy(src, dst): """Run TCP proxy. - + Arguments: src -- Source IP address and port string. I.e.: '127.0.0.1:8000' dst -- Destination IP address and port. I.e.: '127.0.0.1:8888' @@ -73,9 +82,9 @@ def tcp_proxy(src, dst): LOGGER.debug('Starting TCP proxy...') LOGGER.debug('Src: {}'.format(src)) LOGGER.debug('Dst: {}'.format(dst)) - + sockets = [] - + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(ip_to_tuple(src)) s.listen(1) @@ -83,29 +92,29 @@ def tcp_proxy(src, dst): s_src, _ = s.accept() s_dst = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s_dst.connect(ip_to_tuple(dst)) - + s_dst.connect(ip_to_tuple(dst)) + sockets.append(s_src) sockets.append(s_dst) - + while True: s_read, _, _ = select.select(sockets, [], []) - + for s in s_read: data = s.recv(BUFFER_SIZE) - + if s == s_src: d = LOCAL_DATA_HANDLER(data) s_dst.sendall(d) elif s == s_dst: d = REMOTE_DATA_HANDLER(data) s_src.sendall(d) -# end-of-function tcp_proxy +# end-of-function tcp_proxy def ip_to_tuple(ip): """Parse IP string and return (ip, port) tuple. - + Arguments: ip -- IP address:port string. I.e.: '127.0.0.1:8000'. """ @@ -117,32 +126,32 @@ def ip_to_tuple(ip): def main(): """Main method.""" parser = argparse.ArgumentParser(description='TCP/UPD proxy.') - + # TCP UPD groups proto_group = parser.add_mutually_exclusive_group(required=True) proto_group.add_argument('--tcp', action='store_true', help='TCP proxy') proto_group.add_argument('--udp', action='store_true', help='UDP proxy') - + parser.add_argument('-s', '--src', required=True, help='Source IP and port, i.e.: 127.0.0.1:8000') parser.add_argument('-d', '--dst', required=True, help='Destination IP and port, i.e.: 127.0.0.1:8888') - + output_group = parser.add_mutually_exclusive_group() output_group.add_argument('-q', '--quiet', action='store_true', help='Be quiet') output_group.add_argument('-v', '--verbose', action='store_true', help='Be loud') - + args = parser.parse_args() - + if args.quiet: LOGGER.setLevel(logging.CRITICAL) if args.verbose: LOGGER.setLevel(logging.NOTSET) - + if args.udp: udp_proxy(args.src, args.dst) elif args.tcp: tcp_proxy(args.src, args.dst) -# end-of-function main +# end-of-function main if __name__ == '__main__': - main() \ No newline at end of file + main()