optimize decrypt
first recv size < iv_len will cause decrypt error
This commit is contained in:
parent
79260ff4ab
commit
041a5c1046
3 changed files with 17 additions and 11 deletions
|
@ -8,6 +8,7 @@ import sys
|
||||||
from server_pool import ServerPool
|
from server_pool import ServerPool
|
||||||
import Config
|
import Config
|
||||||
import traceback
|
import traceback
|
||||||
|
from shadowsocks import common
|
||||||
|
|
||||||
class DbTransfer(object):
|
class DbTransfer(object):
|
||||||
|
|
||||||
|
@ -115,7 +116,7 @@ class DbTransfer(object):
|
||||||
allow = False
|
allow = False
|
||||||
|
|
||||||
port = row['port']
|
port = row['port']
|
||||||
passwd = row['passwd']
|
passwd = common.to_bytes(row['passwd'])
|
||||||
|
|
||||||
if port not in cur_servers:
|
if port not in cur_servers:
|
||||||
cur_servers[port] = passwd
|
cur_servers[port] = passwd
|
||||||
|
|
|
@ -187,9 +187,9 @@ class ServerPool(object):
|
||||||
|
|
||||||
if 'server_ipv6' in self.config:
|
if 'server_ipv6' in self.config:
|
||||||
if port not in self.tcp_ipv6_servers_pool:
|
if port not in self.tcp_ipv6_servers_pool:
|
||||||
logging.info("stopped server at %s:%d already stop" % (self.config['server_ipv6'], port))
|
logging.info("stopped server at [%s]:%d already stop" % (self.config['server_ipv6'], port))
|
||||||
else:
|
else:
|
||||||
logging.info("stopped server at %s:%d" % (self.config['server_ipv6'], port))
|
logging.info("stopped server at [%s]:%d" % (self.config['server_ipv6'], port))
|
||||||
try:
|
try:
|
||||||
self.tcp_ipv6_servers_pool[port].close(False)
|
self.tcp_ipv6_servers_pool[port].close(False)
|
||||||
del self.tcp_ipv6_servers_pool[port]
|
del self.tcp_ipv6_servers_pool[port]
|
||||||
|
|
|
@ -77,6 +77,7 @@ class Encryptor(object):
|
||||||
self.iv = None
|
self.iv = None
|
||||||
self.iv_sent = False
|
self.iv_sent = False
|
||||||
self.cipher_iv = b''
|
self.cipher_iv = b''
|
||||||
|
self.iv_buf = b''
|
||||||
self.decipher = None
|
self.decipher = None
|
||||||
method = method.lower()
|
method = method.lower()
|
||||||
self._method_info = self.get_method_info(method)
|
self._method_info = self.get_method_info(method)
|
||||||
|
@ -122,16 +123,20 @@ class Encryptor(object):
|
||||||
def decrypt(self, buf):
|
def decrypt(self, buf):
|
||||||
if len(buf) == 0:
|
if len(buf) == 0:
|
||||||
return buf
|
return buf
|
||||||
if self.decipher is None:
|
if self.decipher is not None: #optimize
|
||||||
decipher_iv_len = self._method_info[1]
|
|
||||||
decipher_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)
|
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 = self.iv_buf[decipher_iv_len:]
|
||||||
|
return self.decipher.update(buf)
|
||||||
|
else:
|
||||||
|
return b''
|
||||||
|
|
||||||
def encrypt_all(password, method, op, data):
|
def encrypt_all(password, method, op, data):
|
||||||
result = []
|
result = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue