Skip to content

Commit

Permalink
merge manyuser
Browse files Browse the repository at this point in the history
  • Loading branch information
breakwa11 committed Oct 27, 2015
1 parent eebd5c5 commit 7df3152
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 62 deletions.
44 changes: 35 additions & 9 deletions shadowsocks/asyncdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
import re
import logging

if __name__ == '__main__':
import sys
import inspect
file_path = os.path.dirname(os.path.realpath(inspect.getfile(inspect.currentframe())))
os.chdir(file_path)
sys.path.insert(0, os.path.join(file_path, '../'))

from shadowsocks import common, lru_cache, eventloop, shell


Expand Down Expand Up @@ -71,6 +78,17 @@
QTYPE_NS = 2
QCLASS_IN = 1

def detect_ipv6_supprot():
if 'has_ipv6' in dir(socket):
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
try:
s.connect(('ipv6.google.com', 0))
return True
except:
pass
return False

IPV6_CONNECTION_SUPPORT = detect_ipv6_supprot()

def build_address(address):
address = address.strip(b'.')
Expand Down Expand Up @@ -338,17 +356,17 @@ def _handle_data(self, data):
answer[2] == QCLASS_IN:
ip = answer[0]
break
if not ip and self._hostname_status.get(hostname, STATUS_IPV6) \
== STATUS_IPV4:
self._hostname_status[hostname] = STATUS_IPV6
self._send_req(hostname, QTYPE_AAAA)
if not ip and self._hostname_status.get(hostname, STATUS_IPV4) \
== STATUS_IPV6:
self._hostname_status[hostname] = STATUS_IPV4
self._send_req(hostname, QTYPE_A)
else:
if ip:
self._cache[hostname] = ip
self._call_callback(hostname, ip)
elif self._hostname_status.get(hostname, None) == STATUS_IPV6:
elif self._hostname_status.get(hostname, None) == STATUS_IPV4:
for question in response.questions:
if question[1] == QTYPE_AAAA:
if question[1] == QTYPE_A:
self._call_callback(hostname, None)
break

Expand Down Expand Up @@ -414,14 +432,21 @@ def resolve(self, hostname, callback):
return
arr = self._hostname_to_cb.get(hostname, None)
if not arr:
self._hostname_status[hostname] = STATUS_IPV4
self._send_req(hostname, QTYPE_A)
if IPV6_CONNECTION_SUPPORT:
self._hostname_status[hostname] = STATUS_IPV6
self._send_req(hostname, QTYPE_AAAA)
else:
self._hostname_status[hostname] = STATUS_IPV4
self._send_req(hostname, QTYPE_A)
self._hostname_to_cb[hostname] = [callback]
self._cb_to_hostname[callback] = hostname
else:
arr.append(callback)
# TODO send again only if waited too long
self._send_req(hostname, QTYPE_A)
if IPV6_CONNECTION_SUPPORT:
self._send_req(hostname, QTYPE_AAAA)
else:
self._send_req(hostname, QTYPE_A)

def close(self):
if self._sock:
Expand Down Expand Up @@ -479,3 +504,4 @@ def callback(result, error):

if __name__ == '__main__':
test()

10 changes: 10 additions & 0 deletions shadowsocks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ def to_str(s):
return s.decode('utf-8')
return s

def int32(x):
if x > 0xFFFFFFFF or x < 0:
x &= 0xFFFFFFFF
if x > 0x7FFFFFFF:
x = int(0x100000000 - x)
if x < 0x80000000:
return -x
else:
return -2147483648
return x

def inet_ntop(family, ipstr):
if family == socket.AF_INET:
Expand Down
22 changes: 14 additions & 8 deletions shadowsocks/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, key, method):
self.iv = None
self.iv_sent = False
self.cipher_iv = b''
self.iv_buf = b''
self.decipher = None
method = method.lower()
self._method_info = self.get_method_info(method)
Expand Down Expand Up @@ -122,16 +123,21 @@ def encrypt(self, buf):
def decrypt(self, buf):
if len(buf) == 0:
return buf
if self.decipher is None:
decipher_iv_len = self._method_info[1]
decipher_iv = buf[:decipher_iv_len]
if self.decipher is not None: #optimize
return self.decipher.update(buf)

decipher_iv_len = self._method_info[1]
if len(self.iv_buf) <= decipher_iv_len:
self.iv_buf += buf
if len(self.iv_buf) > decipher_iv_len:
decipher_iv = self.iv_buf[:decipher_iv_len]
self.decipher = self.get_cipher(self.key, self.method, 0,
iv=decipher_iv)
buf = buf[decipher_iv_len:]
if len(buf) == 0:
return buf
return self.decipher.update(buf)

buf = self.iv_buf[decipher_iv_len:]
del self.iv_buf
return self.decipher.update(buf)
else:
return b''

def encrypt_all(password, method, op, data):
result = []
Expand Down
7 changes: 6 additions & 1 deletion shadowsocks/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import logging
import signal

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))
if __name__ == '__main__':
import inspect
file_path = os.path.dirname(os.path.realpath(inspect.getfile(inspect.currentframe())))
os.chdir(file_path)
sys.path.insert(0, os.path.join(file_path, '../'))

from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns


Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def send_data(data_dict):
send_data(r)
r.clear()
i = 0
if len(r) > 0:
if len(r) > 0 :
send_data(r)
self._statistics.clear()

Expand Down
Loading

0 comments on commit 7df3152

Please sign in to comment.