Browse Source

prefer addrtype 1 and 4 over 3 in UDP relay

auth
clowwindy 11 years ago
parent
commit
a323f878e1
  1. 15
      shadowsocks/common.py
  2. 5
      shadowsocks/udprelay.py

15
shadowsocks/common.py

@ -79,6 +79,21 @@ ADDRTYPE_IPV6 = 4
ADDRTYPE_HOST = 3
def pack_addr(address):
for family in (socket.AF_INET, socket.AF_INET6):
try:
r = socket.inet_pton(family, address)
if family == socket.AF_INET6:
return '\x04' + r
else:
return '\x01' + r
except (TypeError, ValueError, OSError, IOError):
pass
if len(address) > 255:
address = address[:255] # TODO
return '\x03' + chr(len(address)) + address
def parse_header(data):
addrtype = ord(data[0])
dest_addr = None

5
shadowsocks/udprelay.py

@ -75,7 +75,7 @@ import random
import encrypt
import eventloop
import lru_cache
from common import parse_header
from common import parse_header, pack_addr
BUF_SIZE = 65536
@ -214,8 +214,7 @@ class UDPRelay(object):
if addrlen > 255:
# drop
return
data = '\x03' + chr(addrlen) + r_addr[0] + \
struct.pack('>H', r_addr[1]) + data
data = pack_addr(r_addr[0]) + struct.pack('>H', r_addr[1]) + data
response = encrypt.encrypt_all(self._password, self._method, 1,
data)
if not response:

Loading…
Cancel
Save