Skip to content

Commit 030b4a5

Browse files
committed
Bug fixes
Fixes #1
1 parent 1f8a54e commit 030b4a5

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

secretsocks/secretsocks.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ def __init__(self):
161161
def new_request(self, sock, addr, client):
162162
# Client sends version and methods
163163
sock.setblocking(True)
164-
ver, = struct.unpack('!B', sock.recv(1))
164+
data = sock.recv(1)
165+
if not data:
166+
return None
167+
ver, = struct.unpack('!B', data)
165168
if DEBUG:
166169
print('Version:', ver)
167170
if ver == 4:
@@ -224,6 +227,8 @@ def _socks5_init(self, sock, client):
224227
elif atyp == 3:
225228
size, = struct.unpack('!B', sock.recv(1))
226229
dstaddr = sock.recv(size)
230+
if type(dstaddr) == bytes:
231+
dstaddr = dstaddr.decode('utf8')
227232
dstport, = struct.unpack('!H', sock.recv(2))
228233
#TODO: ipv6 addr support
229234
#elif atyp = 4:

secretsocks/server.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _dataparse(self):
5555
continue
5656
id, = struct.unpack('<H', data[:2])
5757
# ID 0 is to close a con
58+
# TODO: extend this to be a command channel for opening and closing cons
5859
if id == 0:
5960
id, = struct.unpack('<H', data[2:4])
6061
if DEBUG:
@@ -64,11 +65,11 @@ def _dataparse(self):
6465
data = data[4:]
6566
# If we dont have that conn ID open
6667
elif not self._id_check(id):
67-
if DEBUG:
68-
print('Server requested to open', id)
6968
cmd, = struct.unpack('<B', data[2:3])
7069
# Connect Request
7170
if cmd == 1:
71+
if DEBUG:
72+
print('Server requested to open', id)
7273
port, = struct.unpack('<H', data[3:5])
7374
addr = ""
7475
i = 5
@@ -81,9 +82,12 @@ def _dataparse(self):
8182
# Open Socket
8283
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8384
try:
85+
print(addr, port)
8486
s.connect((addr, port))
8587
s.settimeout(10)
86-
except:
88+
except Exception as e:
89+
if DEBUG:
90+
print(e)
8791
s.close()
8892
s = None
8993
self._close_id(id)
@@ -93,6 +97,14 @@ def _dataparse(self):
9397
t.daemon = True
9498
t.start()
9599
data = data[i+1:]
100+
# Invalid commands are most likely a size for a close connection
101+
else:
102+
if DEBUG:
103+
print('Garbage data received', id)
104+
if len(data) >= cmd+4:
105+
data = data[cmd+4:]
106+
else:
107+
needmore = True
96108
# Else we send the data
97109
else:
98110
tosend = False

0 commit comments

Comments
 (0)