场景:C能主动访问S,但S无法主动访问到C。

目的:在S上访问到C上的Web服务,端口8080。

思路:C主动与S建立socket连接,之后S通过此socket来访问C上的服务。

首先在S上监听2个端口s1和s2,C主动连接(S,s1),socket建立成功之后,在S上访问(S,s2), S将(S,s2)收到的数据转发给(S,s1),即通过之前C和S建立的连接,C将接收到的数据发送给C上的Web服务,然后将接收到的数据通过C和S之间的连接发送过去,S(S,s1)将接收到的数据发送给(S,s2),最终我们得到了数据。


tc.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#coding=utf-8
 
import socket
import sys
 
if __name__=='__main__':
    if len(sys.argv) < 2:
        print 'tc.py rport:rip:lip:lport'
        print 'forward rip:rport socket data to lip:lport'
        sys.exit()
    rip = None
    rport = 80
    lip = '127.0.0.1'
    lport = None
    arg = sys.argv[1].split(':')
    if len(arg) != 4:
        print 'para num error'
        sys.exit()
 
    if len(arg[0]) > 0:
        rport = int(arg[0])
     
    rip = arg[1]
    if len(arg[2])>0:
        lip = arg[2]
     
    lport = int(arg[3])
     
    def proxysocket(data,sc):
        global lip,lport
        = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        HOST = lip
        PORT = lport
        s.connect((HOST, PORT))
        s.settimeout(30)
        s.sendall(data)
         
        while 1:
            msg = s.recv(4096)
            #print msg
            if not msg:
                break
            sc.send(msg)
        s.close()
     
    sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    HOST = rip
    PORT = rport
    sc.connect((HOST, PORT))
     
    while 1:
        try:
            data = sc.recv(1024)
            #print data
            if len(data) == 0:
                #print 'len(data) == 0'
                sc.shutdown(socket.SHUT_RDWR)
                sc.close()
                sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
                sc.connect((HOST, PORT))
                continue
            try:
                proxysocket(data, sc)
            except Exception,e:
                print '[error in proxysocket]', e
        except Exception,e:
            print '[error in main]', e
            try:
                sc.shutdown(socket.SHUT_RDWR)
                sc.close()
                sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
                sc.connect((HOST, PORT))
            except Exception, e:
                print '[error in exception]',e




ts.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#coding=utf-8
import socket
import threading
import time
import sys
 
if __name__=='__main__':
    if len(sys.argv) < 2:
        print 'ts.py rport:rip:lip:lport'
        print 'ts.py 8080:::80'
        print 'forward rip:rport socket data to lip:lport'
        sys.exit()
    rip = '127.0.0.1'
    rport = None
    lip = '0.0.0.0'
    lport = 80
     
    arg = sys.argv[1].split(':')
    if len(arg) != 4:
        print 'para num error'
        sys.exit()
 
    if len(arg[0]) > 0:
        rport = int(arg[0])
     
    rip = arg[1]
    if len(arg[2])>0:
        lip = arg[2]
    if len(arg[3])>0:
        lport = int(arg[3])
 
    serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serv.bind((lip, lport))
    serv.listen(1)
 
    so_self = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #self
    so_self.bind((rip, rport))
    so_self.listen(1)
 
    def showdata(data, length = 50):
        if len(data) > length:
            print data[:length]
        else:
            print data
 
    def fun1():
        global conn
        global self_conn
        conn, addr  = serv.accept()
        while 'self_conn' not in globals():
            time.sleep(1)
         
        while 1:
            try:
                indata = conn.recv(1024)
                #print indata
                showdata(indata)
                if len(indata) != 0:
                    try:
                        self_conn.send(indata)
                    except Exception, e:
                        print '[error in self_conn, fun1]',e
                        self_conn, self_addr  = so_self.accept()
                elif len(indata) == 0:
                    print 'len(indata) == 0, fun1'
                    conn.shutdown(socket.SHUT_RDWR)
                    conn.close()
                    conn, addr  = so_self.accept()
                    time.sleep(1)
            except Exception,e:
                print '[error conn, fun1]', e
                conn.shutdown(socket.SHUT_RDWR)
                conn.close()
                conn, addr  = serv.accept()
     
    def fun2():
        global self_conn
        global conn
        count = 0
        self_conn, self_addr  = so_self.accept()
        while 'conn' not in globals():
            time.sleep(1)
        while 1:
            try:
                indata = self_conn.recv(1024)
                #print indata
                showdata(indata)
                if len(indata) > 0:
                    conn.send(indata)
                elif len(indata) == 0:
                    print 'len == 0 , fun2'
                    self_conn.shutdown(socket.SHUT_RDWR)
                    self_conn, self_addr  = so_self.accept()
                    time.sleep(1)
            except Exception, e:
                print '[error in self_conn, fun2]',e
                self_conn.shutdown(socket.SHUT_RDWR)
                self_conn, self_addr  = so_self.accept()
 
    thread2 = threading.Thread(target=fun2)
    thread2.start()
 
    thread1 = threading.Thread(target=fun1)
    thread1.start()