68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
|
import sys
|
||
|
import socket
|
||
|
from threading import Thread
|
||
|
import select
|
||
|
|
||
|
class Proxy(Thread):
|
||
|
BUFF_SIZE = 65535
|
||
|
def __init__(self, listen, target):
|
||
|
Thread.__init__(self)
|
||
|
self.listen = listen
|
||
|
self.target = target
|
||
|
|
||
|
self.clients = []
|
||
|
|
||
|
def run(self):
|
||
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||
|
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||
|
client.bind(self.listen) # Listen
|
||
|
#master.listen(5)
|
||
|
|
||
|
target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||
|
target.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||
|
target.connect(self.target) # Server
|
||
|
#target.
|
||
|
|
||
|
#client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
|
#client.connect(self.listen)
|
||
|
|
||
|
client.setblocking(0)
|
||
|
target.setblocking(0)
|
||
|
|
||
|
while True:
|
||
|
(c, _, _) = select.select([client],[],[],0)
|
||
|
|
||
|
if c:
|
||
|
c_data, c_addr = client.recvfrom(self.BUFF_SIZE)
|
||
|
print("[C] data:{} addr:{}".format(c_data, c_addr))
|
||
|
if c_data:
|
||
|
if c_data[:2] == b"\x08\x1e":
|
||
|
print("Editing port check: {}".format(c_data.hex()))
|
||
|
c_data = b"\x08\x1e\x77\xda"
|
||
|
print("[C] -> {}".format(c_data.hex()))
|
||
|
target.sendto(c_data, self.target)
|
||
|
|
||
|
(s, _, _) = select.select([target],[],[],0)
|
||
|
|
||
|
if s:
|
||
|
s_data = target.recv(self.BUFF_SIZE)
|
||
|
print("[S] data:{}".format(s_data))
|
||
|
if s_data:
|
||
|
print("[C] <- {}".format(s_data.hex()))
|
||
|
client.sendto(s_data, c_addr)
|
||
|
client.close()
|
||
|
|
||
|
if len(sys.argv) < 4:
|
||
|
print("Missing arguments.")
|
||
|
print("Usage: $ python proxy.py [proxy_port] [server_ip] [server_port]")
|
||
|
print("Sample: $ python proxy.py 7777 192.168.2.1 7777")
|
||
|
exit()
|
||
|
|
||
|
LISTEN = ("0.0.0.0", int(sys.argv[1]))
|
||
|
TARGET = (sys.argv[2], int(sys.argv[3]))
|
||
|
|
||
|
##while True:
|
||
|
proxy = Proxy(LISTEN, TARGET)
|
||
|
proxy.start()
|
||
|
proxy.join()
|
||
|
print("Error?")
|