Advertisement
plytalent

python proxy portforwarding edit liveoverflow

Jan 6th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. import socket
  2. import os
  3. from threading import Thread
  4. import parser_part9 as parser#ขาดอันนี้อะรอให้ถึงบ้านก่อนนะจะ
  5. import importlib
  6. import socket
  7. iphamachi="เปลียนเป็นip hamachi"
  8. localserverip="เปลียนเป็น ipserverนะจะ"
  9. # Part 9
  10. # Developing a TCP Network Proxy - Pwn Adventure 3
  11. # https://www.youtube.com/watch?v=iApNzWZG-10
  12.  
  13. class Proxy2Server(Thread):
  14.  
  15.     def __init__(self, host, port):
  16.         super(Proxy2Server, self).__init__()
  17.         self.game = None # game client socket not known yet
  18.         self.port = port
  19.         self.host = host
  20.         self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  21.         self.server.connect((host, port))
  22.  
  23.     # run in thread
  24.     def run(self):
  25.         while True:
  26.             data = self.server.recv(8)
  27.             if data:
  28.                 #print "[{}] <- {}".format(self.port, data[:100].encode('hex'))
  29.                 try:
  30.                     importlib.reload(parser)                        
  31.                     parser.parse(data, self.port, 'server')
  32.                 except Exception as e:
  33.                     print('server[{}]'.format(self.port)), e
  34.                 # forward to client
  35.                 self.game.sendall(data)
  36.  
  37. class Game2Proxy(Thread):
  38.  
  39.     def __init__(self, host, port):
  40.         super(Game2Proxy, self).__init__()
  41.         self.server = None # real server socket not known yet
  42.         self.port = port
  43.         self.host = host
  44.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  45.         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  46.         sock.bind((host, port))
  47.         sock.listen(1)
  48.         # waiting for a connection
  49.         self.game, addr = sock.accept()
  50. class Proxy(Thread):
  51.  
  52.     def __init__(self, from_host, to_host, from_port, to_port):
  53.         super(Proxy, self).__init__()
  54.         self.from_host = from_host
  55.         self.to_host = to_host
  56.         self.from_port = from_port
  57.         self.to_port = to_port
  58.         self.running = False
  59.  
  60.     def run(self):
  61.         while True:
  62.             print("[proxy({})]) setting up".format(self.from_port))
  63.             self.g2p = Game2Proxy(self.from_host, self.from_port) # waiting for a client
  64.             self.p2s = Proxy2Server(self.to_host, self.to_port)
  65.             print("[proxy({})]) connection established".format(self.to_port))
  66.             self.g2p.server = self.p2s.server
  67.             self.p2s.game = self.g2p.game
  68.             self.running = True
  69.  
  70.             self.g2p.start()
  71.             self.p2s.start()
  72.  
  73. servers=[]
  74. _upnp_server = Proxy(iphamachi, localserverip, 8000,8000)
  75. _upnp_server.start()
  76. servers.append(_upnp_server)
  77.  
  78.  
  79. while True:
  80.     try:
  81.         cmd = input('$ ')
  82.         if cmd[:4] == 'quit':
  83.             os._exit(0)
  84.         if cmd[:7] == "injectc":
  85.             for server in servers:
  86.                 if server.running:
  87.                     server.g2p.client.sendall(cmd[8:])
  88.             pass
  89.         if cmd[:7] == "injects":
  90.             for server in servers:
  91.                 if server.running:
  92.                     server.p2s.server.sendall(cmd[8:])
  93.             pass
  94.     except Exception as e:
  95.         print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement