Skip to content

Instantly share code, notes, and snippets.

@Tarliton
Created April 26, 2018 03:33
Show Gist options
  • Save Tarliton/577cfc5cdb6296d274e73ef7e9096822 to your computer and use it in GitHub Desktop.
Save Tarliton/577cfc5cdb6296d274e73ef7e9096822 to your computer and use it in GitHub Desktop.
import socket, sys, select
class Proxy:
def __init__(self, raddr, rport, laddr='0.0.0.0', lport=5000):
self.local_addr = laddr
self.local_port = lport
self.lsock = []
self.remote_addr = raddr
self.remote_port = rport
self.msg_queue = {}
def tcp_server(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.setblocking(0)
sock.bind((self.local_addr, int(self.local_port)))
sock.listen(3)
self.lsock.append(sock)
print('[*] Listening on {0} {1}' .format(self.local_addr,self.local_port))
while True:
readable, writable, exceptional = select.select(self.lsock, [], [])
for s in readable:
if s == sock:
rserver =self.remote_conn()
if rserver:
client, addr = sock.accept()
print('Accepted connection {0} {1}'.format(addr[0], addr[1]))
self.store_sock(client, addr, rserver)
break
else:
print('the connection with the remote server can\'t be established')
print('Connection with {} is closed'.format(addr[0]))
client.close()
data = self.received_from(s, 3)
self.msg_queue[s].send(data)
if len(data) == 0:
self.close_sock(s)
break
else:
print('Received {} bytes from client '.format(len(data)))
self.hexdump(data)
except KeyboardInterrupt:
print ('Ending server')
except Exception as e:
print('Failed to listen on {}:{} {}'.format(self.local_addr, self.local_port, e))
sys.exit(0)
finally:
sys.exit(0)
def remote_conn(self):
try:
remote_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_sock.connect((self.remote_addr, int(self.remote_port)))
return remote_sock
except Exception as e:
print(e)
return False
def store_sock(self, client, addr, rserver):
self.lsock.append(client)
self.lsock.append(rserver)
self.msg_queue[client] = rserver
self.msg_queue[rserver] = client
def received_from(self, sock, timeout):
data = ""
sock.settimeout(timeout)
try:
while True:
data = sock.recv(4096)
if not data:
break
data =+ data
except:
pass
return data
def close_sock(self, sock):
print ('End of connection with {}'.format(sock.getpeername()))
self.lsock.remove(self.msg_queue[sock])
self.lsock.remove(self.msg_queue[self.msg_queue[sock]])
serv = self.msg_queue[sock]
self.msg_queue[serv].close()
self.msg_queue[sock].close()
del self.msg_queue[sock]
del self.msg_queue[serv]
def hexdump(self, data, length=16):
print(data)
if __name__ == '__main__':
remote_addr = sys.argv[1]
remote_port = int(sys.argv[2])
p = Proxy(remote_addr, remote_port)
p.tcp_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment