-
Notifications
You must be signed in to change notification settings - Fork 46
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
ctrlcctrlv
wants to merge
3
commits into
benjamincburns:main
Choose a base branch
from
ctrlcctrlv:python3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27
−44
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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 | ||
|
||
|
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
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) | ||
|
@@ -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): | ||
|
@@ -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() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?