Created
November 29, 2018 07:50
-
-
Save snower/d746c5815f4b8e0eb9a7e775673d9491 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# 15/5/21 | |
# create by: snower | |
import sys | |
import socket | |
import struct | |
from sevent import udp | |
from sevent import current | |
class Request(object): | |
_caches = {} | |
def __init__(self, conn, address): | |
self.conn = conn | |
self.address = address | |
self.pconn = udp.Socket() | |
self.pconn.on("data", self.on_pdata) | |
self.pconn.on("close", self.on_pclose) | |
def on_pdata(self, conn, address, buffer): | |
while buffer: | |
data = buffer.next() | |
self.conn.write(self.address, data[10:]) | |
def on_close(self, conn): | |
self.pconn.end() | |
print(self.conn.address, "closed") | |
def on_pclose(self, conn): | |
self.conn.end() | |
print(self.conn.address, "close") | |
def write(self, buffer): | |
while buffer: | |
data = buffer.next() | |
data = "".join(['\x00\x00\x00', struct.pack(">B", 1), socket.inet_aton("8.8.8.8"), struct.pack(">H", 53), data]) | |
self.pconn.write((sys.argv[2], int(sys.argv[3])), data) | |
@staticmethod | |
def on_data(server, address, buffer): | |
if address not in Request._caches: | |
Request._caches[address] = Request(server, address) | |
print(address, "connect") | |
Request._caches[address].write(buffer) | |
server = udp.Server() | |
server.on("data", Request.on_data) | |
server.bind(("0.0.0.0", int(sys.argv[1]))) | |
loop = current() | |
loop.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment