zoukankan      html  css  js  c++  java
  • 网络端口的转发和重定向

    需要将某个网络端口转发到另外一个主机(forwarding),但可能会是不同的端口(redirecting)

    import sys, socket, time, threading
    LOGGING = True
    loglock = threading.Lock()
    def log(s, *a):
        if LOGGING:
            loglock.acquire()
            try:
                print '%s:%s' %(time.ctime(), (s%a))
                sys.stdout.flush()
            finally:
                loglock.release()
    
    class PipeThread(threading.Thread):
        pipes = [ ]
        pipeslock = threading.Lock()
        def __init__(self, source, sink):
            super(PipeThread,self).__init__()
            self.source = source
            self.sink = sink
            log('Creating new pipe thread %s ( %s -> %s)',self, source.getpeername(), sink.getpeername())
            self.pipeslock.acquire()
            try:
                self.pipes.append(self)
            finally:
                self.pipeslock.release()
            self.pipeslock.acquire()
            try:
                pipes_now = len(self.pipes)
            finally:
                self.pipeslock.release()
            log('%s pipes now active', pipes_now)
    
        def run(self):
            while True:
                try:
                    data = self.source.recv(1024)
                    if not data: 
                        break
                    self.sink.send(data)
                except:
                    break
            log('%s terminating', self)
            self.pipeslock.acquire()
            try:
                self.pipes.remove(self)
            finally:
                self.pipeslock.release()
            self.pipeslock.acquire()
            try:
                pipes_left = len(self.pipes)
            finally:
                self.pipeslock.release()
            log('%s pipes still active', pipes_left)
    
    
    class Pinhole(threading.Thread):
        def __init__(self, port, newhost, newport):
            super(Pinhole,self).__init__()
            log('Redirecting:localhost:%s -> %s:%s', port,newhost,newport)
            self.newhost = newhost
            self.newport = newport
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.bind(('', port))
            self.sock.listen(5)
        def run(self):
            while True:
                newsock, address = self.sock.accept()
                log('Creating new session for %s:%s', *address)
                fwd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                fwd.connect((self.newhost, self.newport))
                PipeThread(newsock,fwd).start()
                PipeThread(fwd, newsock).start()
    
    if __name__ == '__main__':
        print 'Starting Pinhole port forwarder/redirector'
        import sys
        try:
            port = int(sys.argv[1])
            newhost = sys.argv[2]
            try:
                newport = int(sys.argv[3])
            except IndexError:
                newport = port
        except (ValueError, IndexError):
            print 'Usage: %s port newhost [newport]' % sys.argv[0]
            sys.exit(1)
    
        #begin to work
        sys.stdout = open('pinhole.log','w')
        Pinhole(port, newhost, newport).start()

    #python redirectport.py 8000 localhost 80

    Sat Aug 25 08:38:55 2012:Creating new session for 127.0.0.1:49618
    Sat Aug 25 08:38:55 2012:Creating new pipe thread <PipeThread(Thread-16, initial)> ( ('127.0.0.1', 49618) -> ('127.0.0.1', 80))
    Sat Aug 25 08:38:55 2012:3 pipes now active
    Sat Aug 25 08:38:55 2012:Creating new pipe thread <PipeThread(Thread-17, initial)> ( ('127.0.0.1', 80) -> ('127.0.0.1', 49618))
    Sat Aug 25 08:38:55 2012:4 pipes now active
    Sat Aug 25 08:38:55 2012:Creating new session for 127.0.0.1:49619
    Sat Aug 25 08:38:55 2012:Creating new pipe thread <PipeThread(Thread-18, initial)> ( ('127.0.0.1', 49619) -> ('127.0.0.1', 80))
    Sat Aug 25 08:38:55 2012:5 pipes now active
    Sat Aug 25 08:38:55 2012:Creating new pipe thread <PipeThread(Thread-19, initial)> ( ('127.0.0.1', 80) -> ('127.0.0.1', 49619))
    Sat Aug 25 08:38:55 2012:6 pipes now active
    Sat Aug 25 08:39:08 2012:<PipeThread(Thread-17, started 140323525043968)> terminating
    Sat Aug 25 08:39:08 2012:5 pipes still active
    Sat Aug 25 08:39:09 2012:<PipeThread(Thread-19, started 140323550222080)> terminating
    Sat Aug 25 08:39:09 2012:4 pipes still active
    Sat Aug 25 08:39:10 2012:<PipeThread(Thread-18, started 140323541829376)> terminating
    Sat Aug 25 08:39:10 2012:3 pipes still active
    Sat Aug 25 08:39:10 2012:<PipeThread(Thread-16, started 140323592185600)> terminating
    Sat Aug 25 08:39:10 2012:2 pipes still active

  • 相关阅读:
    虚函数和纯虚函数
    MS CRM 2011中PartyList类型字段的实例化
    MS CRM 2011的自定义与开发(12)——表单脚本扩展开发(4)
    MS CRM 2011的自定义与开发(12)——表单脚本扩展开发(2)
    MS CRM 2011的自定义和开发(10)——CRM web服务介绍(第二部分)——IOrganizationService(二)
    MS CRM 2011 SDK 5.08已经发布
    MS CRM 2011 Q2的一些更新
    最近很忙
    Microsoft Dynamics CRM 2011最近的一些更新
    补一篇,Update Rollup 12 终于发布了
  • 原文地址:https://www.cnblogs.com/hzhida/p/2655626.html
Copyright © 2011-2022 走看看