forked from twossh/vpspack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
241 lines (204 loc) · 6.67 KB
/
proxy.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
# encoding: utf-8
import socket, threading, thread, select, signal, sys, time
from os import system
system("clear")
#conexao
IP = '0.0.0.0'
try:
PORT = int(sys.argv[1])
except:
PORT = 80
PASS = ''
BUFLEN = 4096 * 4
TIMEOUT = 60
MSG = 'VPSPACK'
DEFAULT_HOST = '0.0.0.0:22'
RESPONSE = 'HTTP/1.1 200 Connection established\r\n\r\n'
class Server(threading.Thread):
def __init__(self, host, port):
threading.Thread.__init__(self)
self.running = False
self.host = host
self.port = port
self.threads = []
self.threadsLock = threading.Lock()
self.logLock = threading.Lock()
def run(self):
self.soc = socket.socket(socket.AF_INET)
self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.soc.settimeout(2)
self.soc.bind((self.host, self.port))
self.soc.listen(0)
self.running = True
try:
while self.running:
try:
c, addr = self.soc.accept()
c.setblocking(1)
except socket.timeout:
continue
conn = ConnectionHandler(c, self, addr)
conn.start();
self.addConn(conn)
finally:
self.running = False
self.soc.close()
def printLog(self, log):
self.logLock.acquire()
print log
self.logLock.release()
def addConn(self, conn):
try:
self.threadsLock.acquire()
if self.running:
self.threads.append(conn)
finally:
self.threadsLock.release()
def removeConn(self, conn):
try:
self.threadsLock.acquire()
self.threads.remove(conn)
finally:
self.threadsLock.release()
def close(self):
try:
self.running = False
self.threadsLock.acquire()
threads = list(self.threads)
for c in threads:
c.close()
finally:
self.threadsLock.release()
class ConnectionHandler(threading.Thread):
def __init__(self, socClient, server, addr):
threading.Thread.__init__(self)
self.clientClosed = False
self.targetClosed = True
self.client = socClient
self.client_buffer = ''
self.server = server
self.log = 'Conexao: ' + str(addr)
def close(self):
try:
if not self.clientClosed:
self.client.shutdown(socket.SHUT_RDWR)
self.client.close()
except:
pass
finally:
self.clientClosed = True
try:
if not self.targetClosed:
self.target.shutdown(socket.SHUT_RDWR)
self.target.close()
except:
pass
finally:
self.targetClosed = True
def run(self):
try:
self.client_buffer = self.client.recv(BUFLEN)
hostPort = self.findHeader(self.client_buffer, 'X-Real-Host')
if hostPort == '':
hostPort = DEFAULT_HOST
split = self.findHeader(self.client_buffer, 'X-Split')
if split != '':
self.client.recv(BUFLEN)
if hostPort != '':
passwd = self.findHeader(self.client_buffer, 'X-Pass')
if len(PASS) != 0 and passwd == PASS:
self.method_CONNECT(hostPort)
elif len(PASS) != 0 and passwd != PASS:
self.client.send('HTTP/1.1 400 WrongPass!\r\n\r\n')
if hostPort.startswith(IP):
self.method_CONNECT(hostPort)
else:
self.client.send('HTTP/1.1 403 Forbidden!\r\n\r\n')
else:
print '- No X-Real-Host!'
self.client.send('HTTP/1.1 400 NoXRealHost!\r\n\r\n')
except Exception as e:
self.log += ' - error: ' + e.strerror
self.server.printLog(self.log)
pass
finally:
self.close()
self.server.removeConn(self)
def findHeader(self, head, header):
aux = head.find(header + ': ')
if aux == -1:
return ''
aux = head.find(':', aux)
head = head[aux+2:]
aux = head.find('\r\n')
if aux == -1:
return ''
return head[:aux];
def connect_target(self, host):
i = host.find(':')
if i != -1:
port = int(host[i+1:])
host = host[:i]
else:
if self.method=='CONNECT':
port = 443
else:
port = 22
(soc_family, soc_type, proto, _, address) = socket.getaddrinfo(host, port)[0]
self.target = socket.socket(soc_family, soc_type, proto)
self.targetClosed = False
self.target.connect(address)
def method_CONNECT(self, path):
self.log += ' - CONNECT ' + path
self.connect_target(path)
self.client.sendall(RESPONSE)
self.client_buffer = ''
self.server.printLog(self.log)
self.doCONNECT()
def doCONNECT(self):
socs = [self.client, self.target]
count = 0
error = False
while True:
count += 1
(recv, _, err) = select.select(socs, [], socs, 3)
if err:
error = True
if recv:
for in_ in recv:
try:
data = in_.recv(BUFLEN)
if data:
if in_ is self.target:
self.client.send(data)
else:
while data:
byte = self.target.send(data)
data = data[byte:]
count = 0
else:
break
except:
error = True
break
if count == TIMEOUT:
error = True
if error:
break
def main(host=IP, port=PORT):
print "\033[0;34m━"*8,"\033[1;32m PROXY SOCKS","\033[0;34m━"*8,"\n"
print "\033[1;33mIP:\033[1;32m " + IP
print "\033[1;33mPORTA:\033[1;32m " + str(PORT) + "\n"
print "\033[0;34m━"*10,"\033[1;32m ATIVADO","\033[0;34m━\033[1;37m"*11,"\n"
server = Server(IP, PORT)
server.start()
while True:
try:
time.sleep(2)
except KeyboardInterrupt:
print '\nParando...'
server.close()
break
if __name__ == '__main__':
main()