Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt shadowsocks3.0 #89

Open
wants to merge 1 commit into
base: manyuser
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions shadowsocks/asyncdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from __future__ import absolute_import, division, print_function, \
with_statement

import sys
import os
import socket
import struct
Expand All @@ -30,7 +29,7 @@

CACHE_SWEEP_INTERVAL = 30

VALID_HOSTNAME = re.compile(br"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
VALID_HOSTNAME = re.compile(br"(?!-)[A-Z\d\-_]{1,63}(?<!-)$", re.IGNORECASE)

common.patch_socket()

Expand Down Expand Up @@ -277,15 +276,18 @@ def _parse_resolv(self):
content = f.readlines()
for line in content:
line = line.strip()
if line:
if line.startswith(b'nameserver'):
parts = line.split()
if len(parts) >= 2:
server = parts[1]
if common.is_ip(server) == socket.AF_INET:
if type(server) != str:
server = server.decode('utf8')
self._servers.append(server)
if not (line and line.startswith(b'nameserver')):
continue

parts = line.split()
if len(parts) < 2:
continue

server = parts[1]
if common.is_ip(server) == socket.AF_INET:
if type(server) != str:
server = server.decode('utf8')
self._servers.append(server)
except IOError:
pass
if not self._servers:
Expand All @@ -300,13 +302,17 @@ def _parse_hosts(self):
for line in f.readlines():
line = line.strip()
parts = line.split()
if len(parts) >= 2:
ip = parts[0]
if common.is_ip(ip):
for i in range(1, len(parts)):
hostname = parts[i]
if hostname:
self._hosts[hostname] = ip
if len(parts) < 2:
continue

ip = parts[0]
if not common.is_ip(ip):
continue

for i in range(1, len(parts)):
hostname = parts[i]
if hostname:
self._hosts[hostname] = ip
except IOError:
self._hosts['localhost'] = '127.0.0.1'

Expand Down
8 changes: 8 additions & 0 deletions shadowsocks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def patch_socket():

def pack_addr(address):
address_str = to_str(address)
address = to_bytes(address)
for family in (socket.AF_INET, socket.AF_INET6):
try:
r = socket.inet_pton(family, address_str)
Expand All @@ -160,6 +161,13 @@ def pack_addr(address):
return b'\x03' + chr(len(address)) + address


# add ss header
def add_header(address, port, data=b''):
_data = b''
_data = pack_addr(address) + struct.pack('>H', port) + data
return _data


def parse_header(data):
addrtype = ord(data[0])
dest_addr = None
Expand Down
Loading