From f74923bc7f72516f7e371da6605e2d6c419f1332 Mon Sep 17 00:00:00 2001 From: Ian Foster Date: Sat, 18 Jan 2014 20:34:11 -0800 Subject: [PATCH] added pinhole scrip --- scripts/pinhole.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 scripts/pinhole.py diff --git a/scripts/pinhole.py b/scripts/pinhole.py new file mode 100755 index 0000000..a11ae7a --- /dev/null +++ b/scripts/pinhole.py @@ -0,0 +1,94 @@ +#! /usr/bin/env python +""" +usage 'pinhole port host [newport]' + +Pinhole forwards the port to the host specified. +The optional newport parameter may be used to +redirect to a different port. + +eg. pinhole 80 webserver + Forward all incoming WWW sessions to webserver. + + pinhole 23 localhost 2323 + Forward all telnet sessions to port 2323 on localhost. + +""" + +import sys +from socket import * +from threading import Thread +import time +import signal +import os + +running = True + +def log( s ): + print '%s:%s' % ( time.ctime(), s ) + sys.stdout.flush() + +class PipeThread( Thread ): + pipes = [] + def __init__( self, source, sink ): + Thread.__init__( self ) + self.source = source + self.sink = sink + + log( 'Creating new pipe thread %s ( %s -> %s )' % \ + ( self, source.getpeername(), sink.getpeername() )) + PipeThread.pipes.append( self ) + log( '%s pipes active' % len( PipeThread.pipes )) + + def run( self ): + while running: + try: + data = self.source.recv( 1024 ) + if not data: break + self.sink.send( data ) + except: + break + + log( '%s terminating' % self ) + PipeThread.pipes.remove( self ) + log( '%s pipes active' % len( PipeThread.pipes )) + +class Pinhole( Thread ): + def __init__( self, port, newhost, newport ): + Thread.__init__( self ) + log( 'Redirecting: localhost:%s -> %s:%s' % ( port, newhost, newport )) + self.newhost = newhost + self.newport = newport + self.sock = socket( AF_INET, SOCK_STREAM ) + self.sock.bind(( '', port )) + self.sock.listen(5) + + def run( self ): + while running: + newsock, address = self.sock.accept() + log( 'Creating new session for %s %s ' % address ) + fwd = socket( AF_INET, SOCK_STREAM ) + fwd.connect(( self.newhost, self.newport )) + PipeThread( newsock, fwd ).start() + PipeThread( fwd, newsock ).start() + +if __name__ == '__main__': + + print 'Starting Pinhole' + + import sys + #sys.stdout = open( 'pinhole.log', 'w' ) + + if len( sys.argv ) > 1: + port = newport = int( sys.argv[1] ) + newhost = sys.argv[2] + if len( sys.argv ) >= 4: newport = int( sys.argv[3] ) + Pinhole( port, newhost, newport ).start() + else: + Pinhole( 8080, 'google.com', 80 ).start() + Pinhole( 8081, 'google.com', 443 ).start() + + try: + while 1: + time.sleep(2) + except (KeyboardInterrupt, SystemExit): + os.kill(os.getpid(), signal.SIGTERM) -- 1.9.1