Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- <reconnect the pipe did not close the session, like so fixed>
- 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
- LOGGING = 1
- def log( s ):
- if LOGGING:
- print '%s:%s' % ( time.ctime(), s )
- sys.stdout.flush()
- class PipeThread( Thread ):
- pipes = []
- def __init__( self, source, sink, suid):
- Thread.__init__( self )
- self.source = source
- self.sink = sink
- self.suid = suid
- 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 1:
- try:
- data = self.source.recv( 1024 )
- if not data: break
- self.sink.send( data )
- except:
- break
- #remove child pipes
- p_mas = []
- p_mas = list(filter(lambda x:x.suid==self.suid,PipeThread.pipes))
- log( '%s child terminating!' % p_mas )
- for i in p_mas:
- PipeThread.pipes.remove( i )
- log( '%s worked pipes active' % len( PipeThread.pipes ))
- class Pinhole( Thread ):
- uid = 0
- 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 1:
- Pinhole.uid += 1
- 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, Pinhole.uid).start()
- PipeThread( fwd, newsock, Pinhole.uid).start()
- log( 'End Create %s %s ' % address )
- 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( 50001, 'localhost', 3389 ).start()
- # Pinhole( 50002, 'localhost', 50002 ).start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement