-
Notifications
You must be signed in to change notification settings - Fork 21
/
local.py
215 lines (188 loc) · 6.73 KB
/
local.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# author: mengskysama
# license: GPLv3 (Read COPYING file.)
#
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory, ServerFactory
from sys import stdout
import struct
import socket
import random
import config
import platform
_platform = platform.system().lower()
if _platform == 'windows':
from twisted.internet import iocpreactor
try:
#http://sourceforge.net/projects/pywin32/
iocpreactor.install()
except:
pass
elif _platform == 'darwin':
from twisted.internet import selectreactor
try:
selectreactor.install()
except:
pass
else:
from twisted.internet import epollreactor
try:
epollreactor.install()
except:
pass
from session import Session
class Server(Protocol):
def __init__(self, session):
self.session = session
self.buf = ''
self.data_len = 0
def connectionMade(self):
#session id
self.sendData(struct.pack('>I', self.session.sessionid))
self.session.add_conn(self)
#Seq0
self.session.send_to_tunnel()
def dataReceived(self, data):
self.buf += data
while True:
if self.data_len == 0:
if len(self.buf) < 4:
return
self.data_seq = struct.unpack('>H', self.buf[0:2])[0]
self.data_len = struct.unpack('>H', self.buf[2:4])[0]
self.buf = self.buf[4:]
#print len(self.buf)
#print 'seq %s len %s' % (self.data_seq, self.data_len)
#print 'buflen %s' % len(self.buf)
if len(self.buf) >= self.data_len:
#recved a full seq data
#2333333333333333333333333333333333333
self.session.recv_seqcache.put(self.data_seq, self.buf[:self.data_len][::-1])
self.buf = self.buf[self.data_len:]
self.data_len = 0
self.session.send_to_socks()
else:
break
def sendData(self, data):
self.transport.write(data)
class ServerFactory(ClientFactory):
def __init__(self, session):
self.session = session
#def startedConnecting(self, connector):
# print 'Started to connect.'
def buildProtocol(self, addr):
return Server(self.session)
def clientConnectionLost(self, connector, reason):
print 'Lost server connection.'
self.session.close_session()
def clientConnectionFailed(self, connector, reason):
print 'Connection server failed.'
self.session.close_session()
class S5Server(Protocol):
def __init__(self, dct_session):
self.dct_session = dct_session
#stage 1
#Socks head recv
self.stage = 0
self.buf = ''
#seq data len
self.session = None
def connectionMade(self):
print 'Socks5 connection made'
def connectionLost(self, reason):
print 'Socks5 connection lost'
if self.session is None:
return
self.session.close_session()
def dataReceived(self, data):
self.buf += data
if self.stage == 3:
self.session.send_seqcache.putin(self.buf)
self.session.send_to_tunnel()
self.buf = ''
return
if self.stage == 0:
if len(self.buf) < 3:
return
#+----+----------+----------+
#|VER | NMETHODS | METHODS |
#+----+----------+----------+
#| 1 | 1 | 1 to 255 |
#+----+----------+----------+
method = ord(data[2])
if method != 0:
print 'S5Server Method Err'
self.transport.abortConnection()
return
self.sendData('\x05\x00')
self.buf = ''
self.stage = 1
elif self.stage == 1:
if len(self.buf) < 3:
return
#+----+-----+-------+------+----------+----------+
#|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
#+----+-----+-------+------+----------+----------+
#| 1 | 1 | X'00' | 1 | Variable | 2 |
#+----+-----+-------+------+-----------+---------+
self.buf = self.buf[3:]
self.stage = 2
if self.stage == 2:
addrtype = ord(self.buf[0])
if addrtype == 1:
if len(self.buf) >= 7:
dest_addr = socket.inet_ntoa(self.buf[1:5])
dest_port = struct.unpack('>H', self.buf[5:7])[0]
header_length = 7
else:
return
elif addrtype == 3:
if len(self.buf) > 2:
addrlen = ord(self.buf[1])
if len(self.buf) >= 2 + addrlen:
dest_addr = self.buf[2:2 + addrlen]
dest_port = struct.unpack('>H', self.buf[2 + addrlen:4 + addrlen])[0]
header_length = 4 + addrlen
else:
return
else:
return
elif addrtype == 4:
if len(self.buf) >= 19:
dest_addr = socket.inet_ntop(socket.AF_INET6, self.buf[1:17])
dest_port = struct.unpack('>H', self.buf[17:19])[0]
header_length = 19
else:
return
else:
print 'S5Server Type Unkown'
self.transport.abortConnection()
return
if dest_addr is None:
print 'S5Server Recv Unkown Data'
self.transport.abortConnection()
return
self.sendData('\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00')
#sock5 head done put to seq0
#random a session id
session_id = random.randint(1, 999999999)
self.session = Session(self.dct_session, session_id)
self.session.socks_conn = self
self.dct_session[session_id] = self.session
#connect to server
for i in range(0, config.SESSION_MAX_TUNNEL):
reactor.connectTCP(config.SERVER_IP, config.SERVER_PORT, ServerFactory(self.session))
self.session.send_seqcache.dct_seq[0] = self.buf[:header_length]
self.buf = self.buf[header_length:]
self.stage = 3
def sendData(self, data):
self.transport.write(data)
class S5ServerFactory(ServerFactory):
def __init__(self):
self.dct_session = {}
def buildProtocol(self, addr):
return S5Server(self.dct_session)
reactor.listenTCP(config.LOCAL_PORT, S5ServerFactory())
reactor.run()