diff options
author | Matt Hart <matthew.hart@linaro.org> | 2014-04-18 21:08:44 +0100 |
---|---|---|
committer | Matt Hart <matthew.hart@linaro.org> | 2014-04-18 21:08:44 +0100 |
commit | 43d160078d792ceb5c3b370b6056f4f49a83e4c9 (patch) | |
tree | 4d137a60bc377dcdca2b03773f1c8fae6f369041 | |
parent | c1d9dd8c94dc9e423760f147b66e1c57e8183501 (diff) |
More examples for port forwarding
-rw-r--r-- | lavaproxy/simpleproxyexample.py | 20 | ||||
-rw-r--r-- | lavaproxy/yetanotherexample.py | 66 |
2 files changed, 86 insertions, 0 deletions
diff --git a/lavaproxy/simpleproxyexample.py b/lavaproxy/simpleproxyexample.py new file mode 100644 index 0000000..d975dfc --- /dev/null +++ b/lavaproxy/simpleproxyexample.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +from socket import * +bufsize = 1024 # Modify to suit your needs +targetHost = "somehost.yourdomain.com" +listenPort = 1123 + +def forward(data, port): + print "Forwarding: '%s' from port %s" % (data, port) + sock = socket(AF_INET, SOCK_DGRAM) + sock.bind(("localhost", port)) # Bind to the port data came in on + sock.sendto(data, (targetHost, listenPort)) + +def listen(host, port): + listenSocket = socket(AF_INET, SOCK_DGRAM) + listenSocket.bind((host, port)) + while True: + data, addr = listenSocket.recvfrom(bufsize) + forward(data, addr[1]) # data and port + +listen("localhost", listenPort) diff --git a/lavaproxy/yetanotherexample.py b/lavaproxy/yetanotherexample.py new file mode 100644 index 0000000..7c290f9 --- /dev/null +++ b/lavaproxy/yetanotherexample.py @@ -0,0 +1,66 @@ +# Author: Mario Scondo (www.Linux-Support.com) +# Date: 2010-01-08 +# Script template by Stephen Chappell +# +# This script forwards a number of configured local ports +# to local or remote socket servers. +# +# Configuration: +# Add to the config file port-forward.config lines with +# contents as follows: +# <local incoming port> <dest hostname> <dest port> +# +# Start the application at command line with 'python port-forward.py' +# and stop the application by keying in <ctrl-c>. +# +# Error messages are stored in file 'error.log'. +# + +import socket +import sys +import thread +import time + +def main(setup, error): + # 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) + # wait for <ctrl-c> + while True: + time.sleep(60) + +def parse(setup): + settings = list() + for line in file(setup): + 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) + dock_socket.bind(('', settings[0])) + dock_socket.listen(5) + while True: + client_socket = dock_socket.accept()[0] + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_socket.connect((settings[1], settings[2])) + thread.start_new_thread(forward, (client_socket, server_socket)) + thread.start_new_thread(forward, (server_socket, client_socket)) + finally: + thread.start_new_thread(server, settings) + +def forward(source, destination): + string = ' ' + while string: + string = source.recv(1024) + if string: + destination.sendall(string) + else: + source.shutdown(socket.SHUT_RD) + destination.shutdown(socket.SHUT_WR) + +if __name__ == '__main__': + main('port-forward.config', 'error.log') |