Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. """
  2. <reconnect the pipe did not close the session, like so fixed>
  3.  
  4. usage 'pinhole port host [newport]'
  5.  
  6. Pinhole forwards the port to the host specified.
  7. The optional newport parameter may be used to
  8. redirect to a different port.
  9.  
  10. eg. pinhole 80 webserver
  11. Forward all incoming WWW sessions to webserver.
  12.  
  13. pinhole 23 localhost 2323
  14. Forward all telnet sessions to port 2323 on localhost.
  15. """
  16.  
  17. import sys
  18. from socket import *
  19. from threading import Thread
  20. import time
  21.  
  22. LOGGING = 1
  23.  
  24. def log( s ):
  25. if LOGGING:
  26. print '%s:%s' % ( time.ctime(), s )
  27. sys.stdout.flush()
  28.  
  29. class PipeThread( Thread ):
  30. pipes = []
  31. def __init__( self, source, sink, suid):
  32. Thread.__init__( self )
  33. self.source = source
  34. self.sink = sink
  35. self.suid = suid
  36.  
  37. log( 'Creating new pipe thread %s ( %s -> %s )' % \
  38. ( self, source.getpeername(), sink.getpeername() ))
  39. PipeThread.pipes.append( self )
  40. log( '%s pipes active' % len( PipeThread.pipes ))
  41.  
  42. def run( self ):
  43. while 1:
  44. try:
  45. data = self.source.recv( 1024 )
  46. if not data: break
  47. self.sink.send( data )
  48. except:
  49. break
  50.  
  51. #remove child pipes
  52. p_mas = []
  53.  
  54. p_mas = list(filter(lambda x:x.suid==self.suid,PipeThread.pipes))
  55. log( '%s child terminating!' % p_mas )
  56.  
  57.  
  58. for i in p_mas:
  59. PipeThread.pipes.remove( i )
  60.  
  61. log( '%s worked pipes active' % len( PipeThread.pipes ))
  62.  
  63.  
  64. class Pinhole( Thread ):
  65. uid = 0
  66. def __init__( self, port, newhost, newport ):
  67. Thread.__init__( self )
  68. log( 'Redirecting: localhost:%s -> %s:%s' % ( port, newhost, newport ))
  69. self.newhost = newhost
  70. self.newport = newport
  71. self.sock = socket( AF_INET, SOCK_STREAM )
  72. self.sock.bind(( '', port ))
  73. self.sock.listen(5)
  74.  
  75. def run( self ):
  76. while 1:
  77. Pinhole.uid += 1
  78. newsock, address = self.sock.accept()
  79. log( 'Creating new session for %s %s ' % address )
  80. fwd = socket( AF_INET, SOCK_STREAM )
  81. fwd.connect(( self.newhost, self.newport ))
  82. PipeThread( newsock, fwd, Pinhole.uid).start()
  83. PipeThread( fwd, newsock, Pinhole.uid).start()
  84. log( 'End Create %s %s ' % address )
  85.  
  86.  
  87. if __name__ == '__main__':
  88.  
  89. print 'Starting Pinhole'
  90.  
  91. import sys
  92. sys.stdout = open( 'pinhole.log', 'w' )
  93.  
  94. if len( sys.argv ) > 1:
  95. port = newport = int( sys.argv[1] )
  96. newhost = sys.argv[2]
  97. if len( sys.argv ) == 4: newport = int( sys.argv[3] )
  98. Pinhole( port, newhost, newport ).start()
  99. else:
  100. Pinhole( 50001, 'localhost', 3389 ).start()
  101. # Pinhole( 50002, 'localhost', 50002 ).start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement