Browse Source

revert encrypt.py

new obfs header
dev
breakwa11 9 years ago
parent
commit
61959945c3
  1. 21
      shadowsocks/common.py
  2. 4
      shadowsocks/encrypt.py
  3. 2
      shadowsocks/tcprelay.py

21
shadowsocks/common.py

@ -21,7 +21,7 @@ from __future__ import absolute_import, division, print_function, \
import socket import socket
import struct import struct
import logging import logging
import binascii
def compat_ord(s): def compat_ord(s):
if type(s) == int: if type(s) == int:
@ -140,7 +140,7 @@ def pack_addr(address):
def pre_parse_header(data): def pre_parse_header(data):
datatype = ord(data[0]) datatype = ord(data[0])
if datatype == 0x80 : if datatype == 0x80:
if len(data) <= 2: if len(data) <= 2:
return None return None
rand_data_size = ord(data[1]) rand_data_size = ord(data[1])
@ -151,7 +151,7 @@ def pre_parse_header(data):
data = data[rand_data_size + 2:] data = data[rand_data_size + 2:]
elif datatype == 0x81: elif datatype == 0x81:
data = data[1:] data = data[1:]
elif datatype == 0x82 : elif datatype == 0x82:
if len(data) <= 3: if len(data) <= 3:
return None return None
rand_data_size = struct.unpack('>H', data[1:3])[0] rand_data_size = struct.unpack('>H', data[1:3])[0]
@ -160,6 +160,21 @@ def pre_parse_header(data):
'encryption method') 'encryption method')
return None return None
data = data[rand_data_size + 3:] data = data[rand_data_size + 3:]
elif datatype == 0x88:
if len(data) <= 7 + 7:
return None
data_size = struct.unpack('>H', data[1:3])[0]
ogn_data = data
data = data[:data_size]
crc = binascii.crc32(data) & 0xffffffff
if crc != 0xffffffff:
logging.warn('uncorrect CRC32, maybe wrong password or '
'encryption method')
return None
start_pos = 3 + ord(data[3])
data = data[start_pos:-4]
if data_size < len(ogn_data):
data += ogn_data[data_size:]
return data return data
def parse_header(data): def parse_header(data):

4
shadowsocks/encrypt.py

@ -21,7 +21,6 @@ import os
import sys import sys
import hashlib import hashlib
import logging import logging
import random
from shadowsocks import common from shadowsocks import common
from shadowsocks.crypto import rc4_md5, openssl, sodium, table from shadowsocks.crypto import rc4_md5, openssl, sodium, table
@ -35,10 +34,7 @@ method_supported.update(table.ciphers)
def random_string(length): def random_string(length):
try:
return os.urandom(length) return os.urandom(length)
except (AttributeError, NotImplementedError):
return ''.join(chr(random.randrange(255)) for _ in range(length))
cached_keys = {} cached_keys = {}

2
shadowsocks/tcprelay.py

@ -341,6 +341,8 @@ class TCPRelayHandler(object):
logging.error('unknown command %d', cmd) logging.error('unknown command %d', cmd)
self.destroy() self.destroy()
return return
if False and ord(data[0]) != 0x88: # force new header
raise Exception('can not parse header')
data = pre_parse_header(data) data = pre_parse_header(data)
if data is None: if data is None:
raise Exception('can not parse header') raise Exception('can not parse header')

Loading…
Cancel
Save