-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.py
179 lines (153 loc) · 5.81 KB
/
server.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
#!/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
import struct
import socket
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 Remote(Protocol):
def __init__(self, session):
self.session = session
def connectionMade(self):
print 'Remote Connection Made'
self.session.remote_conn = self
self.session.send_to_remote()
def dataReceived(self, data):
self.session.send_seqcache.putin(data)
self.session.send_to_tunnel()
def sendData(self, data):
#print data
self.transport.write(data)
class RemoteFactory(ClientFactory):
def __init__(self, session):
self.session = session
def startedConnecting(self, connector):
print 'connecting remote'
def buildProtocol(self, addr):
return Remote(self.session)
def clientConnectionLost(self, connector, reason):
print 'lost remote connection'
print self.session.sessionid
self.session.close_session()
def clientConnectionFailed(self, connector, reason):
print 'Connecte remote failed:'
self.session.close_session()
class Server(Protocol):
def __init__(self, dct_session):
self.dct_session = dct_session
#stage 1
#First pack head recved
self.stage = 0
#data of seq
self.buf = ''
#seq data len
self.data_len = 0
self.data_seq = 0
self.session = None
def connectionMade(self):
print 'Local connection made'
def connectionLost(self, reason):
print 'Local connection lost'
if self.session is None:
return
self.session.close_session()
def dataReceived(self, data):
self.buf += data
#print 'self.stage=' + str(self.stage)
if self.stage == 0:
#print 'buf', self.buf
if len(self.buf) < 4:
return
session_id = struct.unpack('>I', self.buf[0:4])[0]
#print 'get sissid',session_id
self.buf = self.buf[4:]
if session_id not in self.dct_session:
self.dct_session[session_id] = Session(self.dct_session, session_id)
session = self.dct_session[session_id]
if session.add_conn(self) is False:
#out of SESSION_MAX_TUNNEL close this
self.abortConnection()
return
self.session = session
self.stage = 1
if self.stage >= 1:
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:
#23333333333333333333333333333333
self.buf = self.buf[:self.data_len][::-1] + self.buf[self.data_len:]
#recved a full seq data
if self.data_seq == 0 and self.stage == 1:
#Seq 0
addrtype = ord(self.buf[0])
if addrtype == 1:
dest_addr = socket.inet_ntoa(self.buf[1:5])
dest_port = struct.unpack('>H', self.buf[5:7])[0]
elif addrtype == 3:
addrlen = ord(self.buf[1])
dest_addr = self.buf[2:2 + addrlen]
dest_port = struct.unpack('>H', self.buf[2 + addrlen:4 + addrlen])[0]
elif addrtype == 4:
dest_addr = socket.inet_ntop(socket.AF_INET6, data[1:17])
dest_port = struct.unpack('>H', data[17:19])[0]
else:
print 'wtf'
self.abortConnection()
return
#Connect to Remote
print 'Connecting to %s:%s' % (dest_addr, dest_port)
reactor.connectTCP(dest_addr, dest_port, RemoteFactory(self.session))
self.buf = self.buf[self.data_len:]
self.data_len = 0
self.stage = 2
else:
self.session.recv_seqcache.put(self.data_seq, self.buf[:self.data_len])
#print 'red a seq '+ str(self.data_seq) + ' ' + str(len(self.buf[:self.data_len]))
self.buf = self.buf[self.data_len:]
self.data_len = 0
self.session.send_to_remote()
else:
break
def sendData(self, data):
self.transport.write(data)
class ServerFactory(ServerFactory):
def __init__(self):
self.dct_session = {}
def buildProtocol(self, addr):
return Server(self.dct_session)
reactor.listenTCP(config.SERVER_PORT, ServerFactory())
reactor.run()