Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
breakwa11 committed Jul 14, 2015
1 parent 69d3692 commit 469d9f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
53 changes: 27 additions & 26 deletions shadowsocks/udprelay.py
Expand Up @@ -76,8 +76,9 @@
BUF_SIZE = 65536


def client_key(a, b, c, d):
return '%s:%s:%s:%s' % (a, b, c, d)
def client_key(source_addr, server_af):
# notice this is server af, not dest af
return '%s:%s:%d' % (source_addr[0], source_addr[1], server_af)


class UDPRelay(object):
Expand All @@ -102,6 +103,7 @@ def __init__(self, config, dns_resolver, is_local):
close_callback=self._close_client)
self._client_fd_to_server_addr = \
lru_cache.LRUCache(timeout=config['timeout'])
self._dns_cache = lru_cache.LRUCache(timeout=300)
self._eventloop = None
self._closed = False
self._last_time = time.time()
Expand Down Expand Up @@ -172,37 +174,36 @@ def _handle_server(self):

if self._is_local:
server_addr, server_port = self._get_a_server()
key = client_key(r_addr[0], r_addr[1], dest_addr, dest_port)
else:
server_addr, server_port = dest_addr, dest_port
addrs = socket.getaddrinfo(dest_addr, dest_port, 0, socket.SOCK_DGRAM, socket.SOL_UDP)
if addrs:
af, socktype, proto, canonname, sa = addrs[0]
key = client_key(r_addr[0], r_addr[1], af, 0)
else:
key = None

client = self._cache.get(key, None)
if not client:
# 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 = self._dns_cache.get(server_addr, None)
if addrs is None:
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 common.to_str(sa[0]) in self._forbidden_iplist:
logging.debug('IP %s is in forbidden list, drop' %
common.to_str(sa[0]))
# drop
return
client = socket.socket(af, socktype, proto)
client.setblocking(False)
self._cache[key] = client
self._client_fd_to_server_addr[client.fileno()] = r_addr
else:
if not addrs:
# drop
return
else:
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)
if not client:
# TODO async getaddrinfo
if self._forbidden_iplist:
if common.to_str(sa[0]) in self._forbidden_iplist:
logging.debug('IP %s is in forbidden list, drop' %
common.to_str(sa[0]))
# drop
return
client = socket.socket(af, socktype, proto)
client.setblocking(False)
self._cache[key] = client
self._client_fd_to_server_addr[client.fileno()] = r_addr

self._sockets.add(client.fileno())
self._eventloop.add(client, eventloop.POLL_IN)

Expand Down
2 changes: 1 addition & 1 deletion tests/jenkins.sh
Expand Up @@ -69,7 +69,7 @@ if [ -f /proc/sys/net/ipv4/tcp_fastopen ] ; then
fi

run_test tests/test_large_file.sh

run_test tests/test_udp_src.sh
run_test tests/test_command.sh

coverage combine && coverage report --include=shadowsocks/*
Expand Down
5 changes: 5 additions & 0 deletions 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+$

0 comments on commit 469d9f7

Please sign in to comment.