-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportube.py
189 lines (174 loc) · 8.13 KB
/
portube.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
import select
import socket
import argparse
class Portube(object):
DATA_BLOCK = 4096
def __init__(self, laddr, lport, raddr, rport):
self.l_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.r_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.l_connected = None
self.r_connected = None
self.laddr = laddr
self.lport = lport
self.raddr = raddr
self.rport = rport
# -tran
# 本地开始监听port1端口,当port1端口上接收到来自客户端的主动连接之后,将主动连接ip:port2,并且负责port1端口和ip:port2之间的数据转发。
def port2host(self):
try:
self.l_socket.bind((self.laddr, self.lport))
self.l_socket.listen(1)
except Exception as reason:
print("Create Listen Port Failed!")
exit(0)
self.rlist = [self.l_socket]
self.wlist = []
self.elist = []
tube = []
while True:
rs, ws, es = select.select(self.rlist, self.wlist, self.elist)
for sockfd in rs:
if sockfd == self.l_socket:
self.l_connected, addr = sockfd.accept()
self.rlist.append(self.l_connected)
print("[√] Local connected:laddr={},raddr={}".format(self.l_connected.getsockname(),
self.l_connected.getpeername()))
try:
self.r_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.r_socket.connect((self.raddr, self.rport))
except Exception as reason:
print("Connect Source Port Failed!")
exit(0)
self.rlist.append(self.r_socket)
print("[√] Remote connected:laddr={},raddr={}".format(self.r_socket.getsockname(),
self.r_socket.getpeername()))
tube.append((self.l_connected, self.r_socket))
continue
else:
for tb in tube:
if sockfd in tb:
r_i = tb.index(sockfd)
w_i = 0 if r_i == 1 else 1
rr = self.copy(sockfd, tb[w_i])
if rr < 0:
self.rlist.remove(tb[0])
self.rlist.remove(tb[1])
tube.remove(tb)
print("[x] connect close:laddr={},raddr={}".
format(tb[0].getsockname(), tb[0].getpeername()))
tb[0].close()
print("[x] connect close:laddr={},raddr={}".
format(tb[1].getsockname(), tb[1].getpeername()))
tb[1].close()
# -slave
# 本地开始主动连接ip1:port1主机和ip2:port2主机,当连接成功之后,负责这两个主机之间的数据转发。
def host2host(self):
try:
self.l_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.l_socket.connect((self.laddr, self.lport))
self.r_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.r_socket.connect((self.raddr, self.rport))
except Exception as reason:
print("Connect Source Port Failed!")
exit(0)
self.rlist = [self.l_socket, self.r_socket]
self.wlist = []
self.elist = [self.l_socket, self.r_socket]
loop_w = True
while loop_w:
rs, ws, es = select.select(self.rlist, self.wlist, self.elist)
for sockfd in rs:
for i, v in enumerate(self.rlist):
ret = 0
rev = None
if sockfd == v and i % 2 == 0:
rev = self.rlist[i + 1]
ret = self.copy(sockfd, rev)
elif sockfd == v and i % 2 == 1:
rev = self.rlist[i - 1]
ret = self.copy(sockfd, rev)
if ret < 0:
self.rlist.remove(sockfd)
self.rlist.remove(rev)
print("[x] connect close:laddr={},raddr={}".
format(sockfd.getsockname(), sockfd.getpeername()))
sockfd.close()
print("[x] connect close:laddr={},raddr={}".
format(rev.getsockname(), rev.getpeername()))
rev.close()
loop_w = False
break
self.host2host()
# -listen
# 同时监听port1端口和port2端口,当两个客户端主动连接上这两个监听端口之后,负责这两个端口间的数据转发。
def port2port(self):
try:
self.l_socket.bind((self.laddr, self.lport))
self.l_socket.listen(1)
self.r_socket.bind((self.raddr, self.rport))
self.r_socket.listen(1)
except Exception as reason:
print("Create Listen Port Failed!")
exit(0)
self.rlist = [self.l_socket, self.r_socket]
self.wlist = []
self.elist = []
while True:
rs, ws, es = select.select(self.rlist, self.wlist, self.elist)
for sockfd in rs:
if sockfd == self.l_socket:
self.l_connected, addr = sockfd.accept()
self.rlist.append(self.l_connected)
continue
elif sockfd == self.r_socket:
self.r_connected, addr = sockfd.accept()
self.rlist.append(self.r_connected)
continue
else:
tube = self.rlist[2:]
if len(tube) < 2 or len(tube) % 2 == 1:
continue
for i, v in enumerate(tube):
ret = 0
rev = None
if sockfd == v and i % 2 == 0:
rev = tube[i + 1]
ret = self.copy(sockfd, rev)
elif sockfd == v and i % 2 == 1:
rev = tube[i - 1]
ret = self.copy(sockfd, rev)
if ret < 0:
self.rlist.remove(sockfd)
self.rlist.remove(rev)
print("[x] connect close:laddr={},raddr={}".
format(sockfd.getsockname(), sockfd.getpeername()))
sockfd.close()
print("[x] connect close:laddr={},raddr={}".
format(rev.getsockname(), rev.getpeername()))
rev.close()
def copy(self, reader, writer):
data = reader.recv(self.DATA_BLOCK)
if len(data) <= 0:
return -1
w = writer.send(data)
if w <= 0:
return -2
print("[->] tran [{}] : from {} to {} ".format(len(data), reader.getsockname(), writer.getsockname()))
return 0
def main():
parser = argparse.ArgumentParser()
parser.add_argument('type', help="[listen] [slave] [tran] [l] [s] [t]")
parser.add_argument('-l', '--laddr', dest='laddr', default='0.0.0.0', help="IP eg: 0.0.0.0")
parser.add_argument("-p", "--lport", dest="lport", help="Port eg: 3306")
parser.add_argument('-R', '--raddr', dest='raddr', default='0.0.0.0', help="IP eg: 0.0.0.0")
parser.add_argument("-P", "--rport", dest="rport", help="Port eg: 3306")
options = parser.parse_args()
pt = Portube(options.laddr, int(options.lport), options.raddr, int(options.rport))
if options.type in ("listen", "l"):
pt.port2port()
elif options.type in ("slave", "s"):
pt.host2host()
elif options.type in ("tran", "t"):
pt.port2host()
if __name__ == "__main__":
main()