-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_forward.py
More file actions
132 lines (120 loc) · 3.65 KB
/
port_forward.py
File metadata and controls
132 lines (120 loc) · 3.65 KB
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
'''
Provides fake p2p, port forwarding.
Ignored the thread-danger of sockets.
Expect unexpected behaviors.
'''
__all__ = ['Forwarder', 'fakeP2P', 'portForward', 'bothForward']
import socket
from threading import Thread
from interactive import listen
from time import sleep
import os
import signal
CHUNK = 4096
class Forwarder(Thread):
SPF = 0.1
id = 0
def __init__(self, fro, to, description = ''):
Thread.__init__(self)
self.to = to
self.fro = fro
if description == '':
self.description = str(__class__.id)
__class__.id += 1
else:
self.description = description
def run(self):
while True:
try:
data = self.fro.recv(CHUNK)
except:
data = b''
if data == b'':
self.to.close()
self.fro.close()
break
else:
try:
self.to.sendall(data)
except:
break
print('Forwarder', str(self), 'has stopped. ')
def __str__(self):
return '<Forwarder %s>' % str(self.description)
def bothForward(socket_1, socket_2, addr):
forwarder_1 = Forwarder(socket_1, socket_2, addr)
forwarder_2 = Forwarder(socket_2, socket_1)
forwarder_1.start()
forwarder_2.start()
return (forwarder_1, forwarder_2)
def fakeP2P(port = 2333):
s = socket.socket()
s.bind(('', port))
s.listen(2)
print('Listening... ')
p_1, addr = s.accept()
print(addr)
p_2, addr = s.accept()
print(addr)
forwarders = bothForward(p_1, p_2)
print('Go! ')
print('Enter to end...')
[x.join() for x in forwarders]
input('Ended. Enter... ')
def portForward(inside_port, inbound_port, afraid = False):
'''
Set `afraid` to True to manually accept connections.
'''
outServerSocket = socket.socket()
outServerSocket.bind(('', inbound_port))
outServerSocket.listen(afraid and 1 or 10)
allForwarders = []
allSockets = []
try:
while True:
print('listening at port %d...' % inbound_port)
outSocket, addr = outServerSocket.accept()
print('Inbound connection from', addr)
if afraid == True:
print('Accept? y/n')
if listen(['y', 'n']) != b'y':
outSocket.close()
print('Refused. ')
continue
inSocket = socket.socket()
allSockets.append(outSocket)
allSockets.append(inSocket)
print('Connecting inside port', inside_port)
inSocket.connect(('localhost', inside_port))
allForwarders += bothForward(outSocket, inSocket, addr)
print('-= ESTABLISHED =-')
print()
except KeyboardInterrupt:
print('Ctrl+C received. ')
finally:
print('Closing all sockets...')
[x.close() for x in allSockets]
outServerSocket.close()
print('Joining...')
panic = Panic()
panic.start()
for forwarder in allForwarders:
forwarder.join()
print('All have joined. ')
panic.all_is_fine = True
class Panic(Thread):
def __init__(self):
super(__class__, self).__init__()
self.all_is_fine = False
def run(self):
print('Panic starts. ')
sleep(0.3)
if self.all_is_fine:
print('Panic sooths. ')
else:
print('Panic!!! SIG KILL')
os.kill(os.getpid(), signal.SIGKILL)
if __name__ == '__main__':
print(__all__)
from console import console
console(globals())