Last active
April 19, 2020 07:44
-
-
Save snower/e1d053a8c64e7e565ab69a57fe2dc640 to your computer and use it in GitHub Desktop.
simple_tcp_proxy
This file contains 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 logging | |
from sevent import tcp | |
from sevent import current | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)1.1s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') | |
def on_connection(server, conn): | |
pconn = tcp.Socket() | |
conn.on_close(lambda s: logging.info('%s -> %s local closed', conn.address, pconn.address)) | |
pconn.on_connect(lambda s: logging.info('%s -> %s proxy connected', conn.address, pconn.address)) | |
pconn.on_close(lambda s: logging.info('%s -> %s proxy closed', conn.address, pconn.address)) | |
conn.link(pconn) | |
pconn.connect((sys.argv[2], int(sys.argv[3]))) | |
logging.info("%s -> %s proxy connecting", conn.address, (sys.argv[2], int(sys.argv[3]))) | |
server = tcp.Server() | |
server.on_connection(on_connection) | |
server.listen(("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