Skip to content

Commit

Permalink
fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
vinodpandey committed Feb 17, 2020
1 parent 31a6ade commit d9e8c1e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
error.log
.idea
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -5,13 +5,33 @@ This script forwards a number of configured local ports to local or remote socke
```
git clone git@github.com:vinodpandey/python-port-forward.git
cd python-port-forward
There are 2 ways to use this script
1. Command-line
2. Using config file
```

## Usage: Command-line
```
sudo python port-forward.py <local incoming port>:<dest hostname>:<dest port>
e.g.
sudo python port-forward.py 80:localhost:8000
```

## Usage: Using config file
```
# update port-forward.config file
# default port forward is localhost:80 > localhost:8080 (80 localhost 8080)
sudo python port-forward.py
# with default port-forward.config, access: http://localhost/ -> this should now show content from http://localhost:8080/
# you can run a test python http server using below command to check the default configuration
# python -m SimpleHTTPServer 8080
```

## Configuration:
Expand Down
23 changes: 18 additions & 5 deletions port-forward.py
Expand Up @@ -21,12 +21,18 @@
import thread
import time

def main(setup, error):
def main(setup, error, args):
# open file for error messages
sys.stderr = file(error, 'a')
# read settings for port forwarding
for settings in parse(setup):
thread.start_new_thread(server, settings)

# if args
if (len(args) > 0):
for settings in parse_args(args):
thread.start_new_thread(server, settings)
else:
# read settings for port forwarding
for settings in parse(setup):
thread.start_new_thread(server, settings)
# wait for <ctrl-c>
while True:
time.sleep(60)
Expand All @@ -42,6 +48,13 @@ def parse(setup):
settings.append((int(parts[0]), parts[1], int(parts[2])))
return settings

def parse_args(args):
settings = list()
for line in args:
parts = line.split(":")
settings.append((int(parts[0]), parts[1], int(parts[2])))
return settings

def server(*settings):
try:
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand All @@ -67,4 +80,4 @@ def forward(source, destination):
destination.shutdown(socket.SHUT_WR)

if __name__ == '__main__':
main('port-forward.config', 'error.log')
main('port-forward.config', 'error.log', sys.argv[1:])

0 comments on commit d9e8c1e

Please sign in to comment.