1 Port mapping
Give an example of the role of port mapping.
Have a, B, c three computers, a, b interoperability, B, c interoperability, but a, C does not pass, this time in C opened a Web service, how to let a access to C Web services?
The simplest and most effective way is to open a port mapping service on B, and then let a access a port of B, B will all traffic on this port is forwarded to the C Web service port, and all the traffic returned by the C Web service is forwarded to a. For A, B is the springboard for indirect access to the C Web service.
2 Implementation Process
The principle of port mapping is not complicated, this article takes TCP as an example to introduce the implementation process, simply draw a time series diagram (below), here is no longer use the text to repeat.
It is important to note that because port mapping is simply a traffic forwarding, the application layer data is not processed, so the multi-channel protocol is not supported (such as the FTP protocol).
3 Code Examples
Following the above process, Python is implemented as follows (recommended to look forward from behind):
#-*-Coding:utf-8-*-# TCP mapping created by Hutaow (hutaow.com) at 2014-08-31import socketimport threading# port mapping configuration information cfg _remote_ip = ' 192.168.0.10 ' cfg_remote_port = 22cfg_local_ip = ' 0.0.0.0 ' Cfg_local_port = 10022# receive data cache size Pkt_buff_size = 20 48# Debug Logs encapsulate Def send_log (content): Print content return# unidirectional streaming data transfer def tcp_mapping_worker (Conn_receiver, Conn_sender): While True:try:data = CONN_RECEIVER.RECV (pkt_buff_size) except Exception:send_log (' Event:connection closed. ') Break if not data:send_log (' Info:no more data is received. ') Break Try:conn_sender.sendall (data) except Exception:send_log (' error:failed sending data. ') Break # Send_log (' info:mapping data >%s '% repr (data)) Send_log (' info:mapping >%s,%s >%d bytes. '% (Conn_receiver.getpeername (), Conn_sender.getpeername (), Len (data))) Conn_receiver.close () Conn_sender.close () return# Port mapping Request processing Def tcp_mapping_request (Local_conn, REMOTE_IP, Remote_porT): Remote_conn = Socket.socket (socket.af_inet, socket. Sock_stream) Try:remote_conn.connect ((Remote_ip, remote_port)) except Exception:local_conn.close () Send_log (' Error:unable to connect to the remote server. ') return Threading. Thread (Target=tcp_mapping_worker, args= (Local_conn, Remote_conn)). Start () threading. Thread (Target=tcp_mapping_worker, args= (Remote_conn, Local_conn)). Start () return# port mapping function def tcp_mapping (REMOTE_IP, Remote_port, Local_ip, local_port): Local_server = Socket.socket (socket.af_inet, socket. Sock_stream) Local_server.bind ((Local_ip, Local_port)) Local_server.listen (5) Send_log (' event:starting mapping Service on ' + local_ip + ': ' + str (local_port) + ' ... ') while True:try: (Local_conn, local_addr) = Local_serve R.accept () except Keyboardinterrupt, Exception:local_server.close () send_log (' Event:stop mapping service. ' ) Break Threading. Thread (Target=tcp_mapping_request, args= (Local_conn, Remote_ip, Remote_port)).Start () send_log (' event:receive mapping request from%s:%d. '% local_addr ' return# main function if __name__ = = ' __main__ ': TCP _mapping (Cfg_remote_ip, Cfg_remote_port, Cfg_local_ip, Cfg_local_port)
4 run
The effect is as follows, 192.168.0.20 successfully accesses the 192.168.0.10 SSH service (22 port) by connecting the 10022 port of the mapping server: