forked from tuoxie007/simple-encrypted-socks5-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolkit.py
98 lines (83 loc) · 2.36 KB
/
toolkit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
__author__ = 'xuke'
import traceback, threading, select, config
from random import choice
from string import maketrans
def ordlong(string):
decimal = 0
for i in range(len(string)):
decimal += ord(string[i])*(256**(len(string)-i-1))
return decimal
def chrlong(integer, length=2):
string = ''
for i in range(length):
string += chr(integer/(256**(length-i-1))%256)
return string
def generate_key(length=32):
return ''.join((choice('abcdefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ0123456789') for i in range(length)))
connection_id = 0
def generate_connection_id():
global connection_id
cid = connection_id
connection_id += 1
return cid
def db():
traceback.print_exc()
def start_thread(target, daemon=True, args=None):
if not args:
th = threading.Thread(target=target)
else:
th = threading.Thread(target=target, args=args)
th.setDaemon(daemon)
th.start()
intab = ''
outtab = ''
for c in range(256):
intab += chr(c)
outtab += config.encode(c)
trantab = maketrans(intab, outtab)
def xor(src):
return src.translate(trantab)
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append(hv)
return reduce(lambda x,y:x+y, lst) if lst else ''
def pipe(reader_sock, writer_file):
reader_sock.setblocking(0)
while True:
try:
select.select([reader_sock], [], [])
try:
data = reader_sock.recv(1024)
if data:
try:
writer_file.write(xor(data))
except KeyboardInterrupt:
raise
except: # writer socket closed
# shutdown_connection(reader_sock)
break
else: # read end, socket closed
break
except KeyboardInterrupt:
raise
except: # reader socket closed
break
except KeyboardInterrupt:
raise
except:
break
writer_file.loseConnection()
def shutdown_connection(connection):
if type(connection) is list:
for c in connection:
shutdown_connection(c)
else:
try:
connection.close()
except:
print "shutdown failed"
pass