From ce25bbf650107441be067146840688e79b97e730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=B4=E5=A8=83=E9=85=B1?= Date: Tue, 22 Nov 2016 07:23:30 +0800 Subject: [PATCH] catch bind error --- shadowsocks/tcprelay.py | 5 ++++- shadowsocks/udprelay.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index d65833c..441931d 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -548,7 +548,10 @@ class TCPRelayHandler(object): local_addrs = socket.getaddrinfo(bind_addr, 0, 0, socket.SOCK_STREAM, socket.SOL_TCP) if local_addrs[0][0] == af: logging.debug("bind %s" % (bind_addr,)) - sock.bind((bind_addr, 0)) + try: + sock.bind((bind_addr, 0)) + except Exception as e: + logging.warn("bind %s fail" % (bind_addr,)) def _create_remote_socket(self, ip, port): if self._remote_udp: diff --git a/shadowsocks/udprelay.py b/shadowsocks/udprelay.py index 30649a6..78b915d 100644 --- a/shadowsocks/udprelay.py +++ b/shadowsocks/udprelay.py @@ -1025,10 +1025,13 @@ class UDPRelay(object): if bind_addr in self._ignore_bind_list: bind_addr = None if bind_addr: - local_addrs = socket.getaddrinfo(bind_addr, 0, 0, socket.SOCK_STREAM, socket.SOL_TCP) + local_addrs = socket.getaddrinfo(bind_addr, 0, 0, socket.SOCK_DGRAM, socket.SOL_UDP) if local_addrs[0][0] == af: logging.debug("bind %s" % (bind_addr,)) - sock.bind((bind_addr, 0)) + try: + sock.bind((bind_addr, 0)) + except Exception as e: + logging.warn("bind %s fail" % (bind_addr,)) def _handle_server(self): server = self._server_socket