Browse Source

log UDP bind port

dev
破娃酱 8 years ago
parent
commit
f3a3ab4b84
  1. 19
      shadowsocks/tcprelay.py
  2. 11
      shadowsocks/udprelay.py

19
shadowsocks/tcprelay.py

@ -223,6 +223,8 @@ class TCPRelayHandler(object):
self.speed_tester_u = SpeedTester(config.get("speed_limit_per_con", 0)) self.speed_tester_u = SpeedTester(config.get("speed_limit_per_con", 0))
self.speed_tester_d = SpeedTester(config.get("speed_limit_per_con", 0)) self.speed_tester_d = SpeedTester(config.get("speed_limit_per_con", 0))
self._recv_pack_id = 0 self._recv_pack_id = 0
self._udp_send_pack_id = 0
self._udpv6_send_pack_id = 0
def __hash__(self): def __hash__(self):
# default __hash__ is id / 16 # default __hash__ is id / 16
@ -338,14 +340,23 @@ class TCPRelayHandler(object):
connecttype, dest_addr, dest_port, header_length = header_result connecttype, dest_addr, dest_port, header_length = header_result
addrs = socket.getaddrinfo(dest_addr, dest_port, 0, addrs = socket.getaddrinfo(dest_addr, dest_port, 0,
socket.SOCK_DGRAM, socket.SOL_UDP) socket.SOCK_DGRAM, socket.SOL_UDP)
#logging.info('UDP over TCP sendto %s:%d %d bytes from %s:%d' % (dest_addr, dest_port, len(data), self._client_address[0], self._client_address[1]))
if addrs: if addrs:
af, socktype, proto, canonname, server_addr = addrs[0] af, socktype, proto, canonname, server_addr = addrs[0]
data = data[header_length:] data = data[header_length:]
if af == socket.AF_INET6: if af == socket.AF_INET6:
self._remote_sock_v6.sendto(data, (server_addr[0], dest_port)) self._remote_sock_v6.sendto(data, (server_addr[0], dest_port))
if self._udpv6_send_pack_id == 0:
addr, port = self._remote_sock_v6.getsockname()[:2]
common.connect_log('UDPv6 sendto %s:%d from %s:%d by user %d' %
(server_addr[0], dest_port, addr, port, self._user_id))
self._udpv6_send_pack_id += 1
else: else:
sock.sendto(data, (server_addr[0], dest_port)) sock.sendto(data, (server_addr[0], dest_port))
if self._udp_send_pack_id == 0:
addr, port = sock.getsockname()[:2]
common.connect_log('UDP sendto %s:%d from %s:%d by user %d' %
(server_addr[0], dest_port, addr, port, self._user_id))
self._udp_send_pack_id += 1
except Exception as e: except Exception as e:
#trace = traceback.format_exc() #trace = traceback.format_exc()
@ -597,8 +608,9 @@ class TCPRelayHandler(object):
server_info.buffer_size = self._recv_buffer_size server_info.buffer_size = self._recv_buffer_size
connecttype, remote_addr, remote_port, header_length = header_result connecttype, remote_addr, remote_port, header_length = header_result
if connecttype != 0: if connecttype != 0:
common.connect_log('UDP over TCP by user %d' % pass
(self._user_id, )) #common.connect_log('UDP over TCP by user %d' %
# (self._user_id, ))
else: else:
common.connect_log('TCP request %s:%d by user %d' % common.connect_log('TCP request %s:%d by user %d' %
(common.to_str(remote_addr), remote_port, self._user_id)) (common.to_str(remote_addr), remote_port, self._user_id))
@ -711,7 +723,6 @@ class TCPRelayHandler(object):
if result: if result:
ip = result[1] ip = result[1]
if ip: if ip:
try: try:
self._stage = STAGE_CONNECTING self._stage = STAGE_CONNECTING
remote_addr = ip remote_addr = ip

11
shadowsocks/udprelay.py

@ -445,9 +445,9 @@ class UDPRelay(object):
af, socktype, proto, canonname, sa = addrs[0] af, socktype, proto, canonname, sa = addrs[0]
key = client_key(r_addr, af) key = client_key(r_addr, af)
client_pair = self._cache.get(key, None) client_pair = self._cache.get(key, None)
if not client_pair: if client_pair is None:
client_pair = self._cache_dns_client.get(key, None) client_pair = self._cache_dns_client.get(key, None)
if not client_pair: if client_pair is None:
if self._forbidden_iplist: if self._forbidden_iplist:
if common.to_str(sa[0]) in self._forbidden_iplist: if common.to_str(sa[0]) in self._forbidden_iplist:
logging.debug('IP %s is in forbidden list, drop' % logging.debug('IP %s is in forbidden list, drop' %
@ -486,9 +486,6 @@ class UDPRelay(object):
user_id = self._listen_port user_id = self._listen_port
else: else:
user_id = struct.unpack('<I', client_uid)[0] user_id = struct.unpack('<I', client_uid)[0]
common.connect_log('UDP data to %s:%d via port %d by UID %d' %
(common.to_str(server_addr), server_port,
self._listen_port, user_id))
else: else:
client, client_uid = client_pair client, client_uid = client_pair
self._cache.clear(self._udp_cache_size) self._cache.clear(self._udp_cache_size)
@ -509,6 +506,10 @@ class UDPRelay(object):
try: try:
#logging.info('UDP handle_server sendto %s:%d %d bytes' % (common.to_str(server_addr), server_port, len(data))) #logging.info('UDP handle_server sendto %s:%d %d bytes' % (common.to_str(server_addr), server_port, len(data)))
client.sendto(data, (server_addr, server_port)) client.sendto(data, (server_addr, server_port))
if client_pair is None: # new request
addr, port = client.getsockname()[:2]
common.connect_log('UDP data to %s:%d from %s:%d by UID %d' %
(common.to_str(server_addr), server_port, addr, port, user_id))
self.add_transfer_u(client_uid, len(data)) self.add_transfer_u(client_uid, len(data))
except IOError as e: except IOError as e:
err = eventloop.errno_from_exception(e) err = eventloop.errno_from_exception(e)

Loading…
Cancel
Save