Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Python3 + Tornado6 #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ FROM ubuntu:focal

LABEL org.opencontainers.image.authors="[email protected]"

RUN apt-get update && apt-get install -y python2 python2-dev iptables dnsmasq uml-utilities net-tools build-essential curl && apt-get clean

RUN curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py && python2 get-pip.py && rm get-pip.py
RUN apt-get update && apt-get install -y python2 python2-dev iptables dnsmasq python3-pip uml-utilities net-tools build-essential curl && apt-get clean

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this install python3 python3-dev instead, now?


COPY docker-image-config/docker-startup.sh switchedrelay.py limiter.py requirements.txt /opt/websockproxy/
COPY docker-image-config/dnsmasq/interface docker-image-config/dnsmasq/dhcp /etc/dnsmasq.d/

WORKDIR /opt/websockproxy/

RUN pip2 install -r /opt/websockproxy/requirements.txt
RUN python3 -m pip install -r /opt/websockproxy/requirements.txt

EXPOSE 80

Expand Down
2 changes: 1 addition & 1 deletion docker-image-config/docker-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ echo 1 > /proc/sys/net/ipv4/ip_forward
#########################################

/etc/init.d/dnsmasq start
python2 switchedrelay.py
python3 switchedrelay.py
6 changes: 2 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
argparse==1.2.1
python-pytun==2.2.1
tornado==3.1.1
wsgiref==0.1.2
python-pytun==2.4.1
tornado==6.2
57 changes: 22 additions & 35 deletions switchedrelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,40 @@

from pytun import TunTapDevice, IFF_TAP, IFF_NO_PI


from limiter import RateLimitingState

import tornado.concurrent
import tornado.gen
import tornado.ioloop
import tornado.web
import tornado.options

from tornado.concurrent import return_future

from tornado import websocket


FORMAT = '%(asctime)-15s %(message)s'
RATE = 40980.0 #unit: bytes
BROADCAST = '%s%s%s%s%s%s' % (chr(0xff),chr(0xff),chr(0xff),chr(0xff),chr(0xff),chr(0xff))
PING_INTERVAL = 30

logger = logging.getLogger('relay')


macmap = {}

@return_future
def delay_future(t, callback):
timestamp = time.time()
if timestamp < t:
return
else:
callback(t)
yield tornado.gen.sleep(t)
callback(time.time())

class TunThread(threading.Thread):
def __init__(self, *args, **kwargs):
super(TunThread, self).__init__(*args, **kwargs)
self.running = True
self.tun = TunTapDevice(name="tap0", flags= (IFF_TAP | IFF_NO_PI))
self.tun.addr = '10.5.0.1'
self.tun.netmask = '255.255.0.0'
self.tun.mtu = 1500
self.tun.up()
tap_name = tornado.options.options.tap_name or "tap0"
self.tun = TunTapDevice(name=tap_name, flags= (IFF_TAP | IFF_NO_PI))
if tornado.options.options.tap_name is None:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this change allows the non-root user to run the server.

You have to set up the TAP device as root, and assign it to your user account, but you don't need to run the server as root. :-)

So if you pass --tap-name=tap0 (or w/e, tap1 etc.) it'll Just Work™

logger.info("Setting TUN/TAP settings because not set…")
self.tun.addr = '10.5.0.1'
self.tun.netmask = '255.255.0.0'
self.tun.mtu = 1500
self.tun.up()

def write(self, message):
self.tun.write(message)
Expand Down Expand Up @@ -96,22 +91,13 @@ def __init__(self, *args, **kwargs):
logger.info('%s: connected.' % self.remote_ip)
self.thread = None
self.mac = ''
self.allowance = RATE #unit: messages
self.allowance = tornado.options.options.rate #unit: messages
self.last_check = time.time() #floating-point, e.g. usec accuracy. Unit: seconds
self.upstream = RateLimitingState(RATE, name='upstream', clientip=self.remote_ip)
self.downstream = RateLimitingState(RATE, name='downstream', clientip=self.remote_ip)

ping_future = delay_future(time.time()+PING_INTERVAL, self.do_ping)
loop.add_future(ping_future, lambda: None)

def do_ping(self, timestamp):
self.ping(str(timestamp))

ping_future = delay_future(time.time()+PING_INTERVAL, self.do_ping)
loop.add_future(ping_future, lambda: None)
self.upstream = RateLimitingState(self.allowance, name='upstream', clientip=self.remote_ip)
self.downstream = RateLimitingState(self.allowance, name='downstream', clientip=self.remote_ip)

def on_pong(self, data):
pass
logger.debug("Pong")

def rate_limited_downstream(self, message):
if self.downstream.do_throttle(message):
Expand Down Expand Up @@ -177,13 +163,14 @@ def on_close(self):
application = tornado.web.Application([(r'/', MainHandler)])

if __name__ == '__main__':

tunthread = TunThread()
tunthread.start()

args = sys.argv
tornado.options.define("tap_name", default=None, help="TUN/TAP device name. If not set, will try to create it.")
tornado.options.define("ws_port", default=80, help="Port to launch WebSocket server on")
tornado.options.define("rate", default=40980, help="Rate limiting")
tornado.options.parse_command_line(args)
application.listen(80)
tunthread = TunThread()
tunthread.start()
application.listen(tornado.options.options.ws_port)
loop = tornado.ioloop.IOLoop.instance()
try:
loop.start()
Expand Down