Browse Source

merge master

dev
breakwa11 10 years ago
parent
commit
469d9f7bfa
  1. 33
      shadowsocks/udprelay.py
  2. 2
      tests/jenkins.sh
  3. 5
      utils/fail2ban/shadowsocks.conf

33
shadowsocks/udprelay.py

@ -76,8 +76,9 @@ from shadowsocks.common import pre_parse_header, parse_header, pack_addr
BUF_SIZE = 65536 BUF_SIZE = 65536
def client_key(a, b, c, d): def client_key(source_addr, server_af):
return '%s:%s:%s:%s' % (a, b, c, d) # notice this is server af, not dest af
return '%s:%s:%d' % (source_addr[0], source_addr[1], server_af)
class UDPRelay(object): class UDPRelay(object):
@ -102,6 +103,7 @@ class UDPRelay(object):
close_callback=self._close_client) close_callback=self._close_client)
self._client_fd_to_server_addr = \ self._client_fd_to_server_addr = \
lru_cache.LRUCache(timeout=config['timeout']) lru_cache.LRUCache(timeout=config['timeout'])
self._dns_cache = lru_cache.LRUCache(timeout=300)
self._eventloop = None self._eventloop = None
self._closed = False self._closed = False
self._last_time = time.time() self._last_time = time.time()
@ -172,24 +174,25 @@ class UDPRelay(object):
if self._is_local: if self._is_local:
server_addr, server_port = self._get_a_server() server_addr, server_port = self._get_a_server()
key = client_key(r_addr[0], r_addr[1], dest_addr, dest_port)
else: else:
server_addr, server_port = dest_addr, dest_port server_addr, server_port = dest_addr, dest_port
addrs = socket.getaddrinfo(dest_addr, dest_port, 0, socket.SOCK_DGRAM, socket.SOL_UDP)
if addrs: addrs = self._dns_cache.get(server_addr, None)
af, socktype, proto, canonname, sa = addrs[0] if addrs is None:
key = client_key(r_addr[0], r_addr[1], af, 0) addrs = socket.getaddrinfo(server_addr, server_port, 0,
socket.SOCK_DGRAM, socket.SOL_UDP)
if not addrs:
# drop
return
else: else:
key = None self._dns_cache[server_addr] = addrs
af, socktype, proto, canonname, sa = addrs[0]
key = client_key(r_addr, af)
logging.debug(key)
client = self._cache.get(key, None) client = self._cache.get(key, None)
if not client: if not client:
# TODO async getaddrinfo # TODO async getaddrinfo
#logging.info('UDP handle_server %s:%d from %s:%d' % (common.to_str(server_addr), server_port, self._listen_addr, self._listen_port))
addrs = socket.getaddrinfo(server_addr, server_port, 0,
socket.SOCK_DGRAM, socket.SOL_UDP)
if addrs:
af, socktype, proto, canonname, sa = addrs[0]
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' %
@ -200,9 +203,7 @@ class UDPRelay(object):
client.setblocking(False) client.setblocking(False)
self._cache[key] = client self._cache[key] = client
self._client_fd_to_server_addr[client.fileno()] = r_addr self._client_fd_to_server_addr[client.fileno()] = r_addr
else:
# drop
return
self._sockets.add(client.fileno()) self._sockets.add(client.fileno())
self._eventloop.add(client, eventloop.POLL_IN) self._eventloop.add(client, eventloop.POLL_IN)

2
tests/jenkins.sh

@ -69,7 +69,7 @@ if [ -f /proc/sys/net/ipv4/tcp_fastopen ] ; then
fi fi
run_test tests/test_large_file.sh run_test tests/test_large_file.sh
run_test tests/test_udp_src.sh
run_test tests/test_command.sh run_test tests/test_command.sh
coverage combine && coverage report --include=shadowsocks/* coverage combine && coverage report --include=shadowsocks/*

5
utils/fail2ban/shadowsocks.conf

@ -0,0 +1,5 @@
[Definition]
_daemon = shadowsocks
failregex = ^\s+ERROR\s+can not parse header when handling connection from <HOST>:\d+$
Loading…
Cancel
Save