Commit 4b3a29ff27b30c0ee556de57088ff9e683a9b1fd
1 parent
1610fce59a
Exists in
master
quick fixes to pinhole
Showing 1 changed file with 9 additions and 7 deletions Inline Diff
scripts/pinhole.py
View file @
4b3a29f
#!/usr/bin/env python | 1 | 1 | #!/usr/bin/env python | |
""" | 2 | 2 | """ | |
usage 'pinhole port host [newport]' | 3 | 3 | usage 'pinhole port host [newport]' | |
4 | 4 | |||
Pinhole forwards the port to the host specified. | 5 | 5 | Pinhole forwards the port to the host specified. | |
The optional newport parameter may be used to | 6 | 6 | The optional newport parameter may be used to | |
redirect to a different port. | 7 | 7 | redirect to a different port. | |
8 | 8 | |||
eg. pinhole 80 webserver | 9 | 9 | eg. pinhole 80 webserver | |
Forward all incoming WWW sessions to webserver. | 10 | 10 | Forward all incoming WWW sessions to webserver. | |
11 | 11 | |||
pinhole 23 localhost 2323 | 12 | 12 | pinhole 23 localhost 2323 | |
Forward all telnet sessions to port 2323 on localhost. | 13 | 13 | Forward all telnet sessions to port 2323 on localhost. | |
14 | 14 | |||
""" | 15 | 15 | """ | |
16 | 16 | |||
import sys | 17 | 17 | import sys | |
from socket import * | 18 | 18 | from socket import * | |
from threading import Thread | 19 | 19 | from threading import Thread | |
import time | 20 | 20 | import time | |
import signal | 21 | 21 | import signal | |
import os | 22 | 22 | import os | |
23 | 23 | |||
running = True | 24 | 24 | #enable logging to file | |
25 | #sys.stdout = open( 'pinhole.log', 'w' ) | |||
25 | 26 | |||
27 | ||||
def log( s ): | 26 | 28 | def log( s ): | |
print '%s:%s' % ( time.ctime(), s ) | 27 | 29 | print '%s:%s' % ( time.ctime(), s ) | |
sys.stdout.flush() | 28 | 30 | sys.stdout.flush() | |
29 | 31 | |||
32 | ||||
class PipeThread( Thread ): | 30 | 33 | class PipeThread( Thread ): | |
pipes = [] | 31 | 34 | pipes = [] | |
def __init__( self, source, sink ): | 32 | 35 | def __init__( self, source, sink ): | |
Thread.__init__( self ) | 33 | 36 | Thread.__init__( self ) | |
self.source = source | 34 | 37 | self.source = source | |
self.sink = sink | 35 | 38 | self.sink = sink | |
36 | 39 | |||
log( 'Creating new pipe thread %s ( %s -> %s )' % \ | 37 | 40 | log( 'Creating new pipe thread %s ( %s -> %s )' % \ | |
( self, source.getpeername(), sink.getpeername() )) | 38 | 41 | ( self, source.getpeername(), sink.getpeername() )) | |
PipeThread.pipes.append( self ) | 39 | 42 | PipeThread.pipes.append( self ) | |
log( '%s pipes active' % len( PipeThread.pipes )) | 40 | 43 | log( '%s pipes active' % len( PipeThread.pipes )) | |
41 | 44 | |||
def run( self ): | 42 | 45 | def run( self ): | |
while running: | 43 | 46 | while True: | |
try: | 44 | 47 | try: | |
data = self.source.recv( 1024 ) | 45 | 48 | data = self.source.recv( 1024 ) | |
if not data: break | 46 | 49 | if not data: break | |
self.sink.send( data ) | 47 | 50 | self.sink.send( data ) | |
except: | 48 | 51 | except: | |
break | 49 | 52 | break | |
50 | 53 | |||
log( '%s terminating' % self ) | 51 | 54 | log( '%s terminating' % self ) | |
PipeThread.pipes.remove( self ) | 52 | 55 | PipeThread.pipes.remove( self ) | |
log( '%s pipes active' % len( PipeThread.pipes )) | 53 | 56 | log( '%s pipes active' % len( PipeThread.pipes )) | |
54 | 57 | |||
58 | ||||
class Pinhole( Thread ): | 55 | 59 | class Pinhole( Thread ): | |
def __init__( self, port, newhost, newport ): | 56 | 60 | def __init__( self, port, newhost, newport ): | |
Thread.__init__( self ) | 57 | 61 | Thread.__init__( self ) | |
log( 'Redirecting: localhost:%s -> %s:%s' % ( port, newhost, newport )) | 58 | 62 | log( 'Redirecting: localhost:%s -> %s:%s' % ( port, newhost, newport )) | |
self.newhost = newhost | 59 | 63 | self.newhost = newhost | |
self.newport = newport | 60 | 64 | self.newport = newport | |
self.sock = socket( AF_INET, SOCK_STREAM ) | 61 | 65 | self.sock = socket( AF_INET, SOCK_STREAM ) | |
self.sock.bind(( '', port )) | 62 | 66 | self.sock.bind(( '', port )) | |
self.sock.listen(5) | 63 | 67 | self.sock.listen(5) | |
64 | 68 | |||
def run( self ): | 65 | 69 | def run( self ): | |
while running: | 66 | 70 | while True: | |
newsock, address = self.sock.accept() | 67 | 71 | newsock, address = self.sock.accept() | |
log( 'Creating new session for %s %s ' % address ) | 68 | 72 | log( 'Creating new session for %s %s ' % address ) | |
fwd = socket( AF_INET, SOCK_STREAM ) | 69 | 73 | fwd = socket( AF_INET, SOCK_STREAM ) | |
fwd.connect(( self.newhost, self.newport )) | 70 | 74 | fwd.connect(( self.newhost, self.newport )) | |
PipeThread( newsock, fwd ).start() | 71 | 75 | PipeThread( newsock, fwd ).start() | |
PipeThread( fwd, newsock ).start() | 72 | 76 | PipeThread( fwd, newsock ).start() | |
73 | 77 | |||
if __name__ == '__main__': | 74 | |||
75 | 78 | |||
79 | if __name__ == '__main__': | |||
print 'Starting Pinhole' | 76 | 80 | print 'Starting Pinhole' | |
77 | ||||
import sys | 78 | |||
#sys.stdout = open( 'pinhole.log', 'w' ) | 79 |