This is mainly about the use of. NET implementation based on the SOCKET5 under the Proxy protocol for client communication, SOCKET4 implementation is similar, the attention of things, here is not to talk about using C # to implement a proxy server, because the implementation of a proxy server needs to implement a lot of protocols, the first big, and now there are many off-the-shelf proxy server with , good performance, direct use is not good? But in C # to achieve the client through a proxy server for SOCKET5 communication, this simple point, generally Socket5 good, Socket4 now also gradually obsolete, basically proxy server support SOCKET5.
First of all, we need to understand that the Socket5 agent protocol is based on the TCP protocol, which extends from SOCKET4 to provide a set of firewall protocols for other protocols such as HTTP and FTP.
(here to do a little explanation, in fact, it should be socks V5 agreement, but the current online use of Socket5 so called, the present article also temporarily said)
And then the following simple say the content of the agreement, specifically divided into two parts:
Part is the client based on the TCP protocol
When a client based on a TCP protocol wants to establish a connection with a target that can only be reached through the firewall (which is determined by the implementation), it must first establish a TCP connection to the socks port on the SOCKS server. Typically this TCP port is 1080. When the connection is established, the client enters the "handshake (negotiation)" process of the protocol: the choice of authentication mode, authentication according to the chosen way, and then send forward request. The SOCKS server checks this requirement, depending on the result, or establishes the appropriate connection, or rejects it. Is it a bit like FTP? Almost.
The process of shaking hands:
After the client connects to the server, it then sends a request to negotiate the version and authentication methods:
VER |
Nmethods |
METHODS |
1 |
1 |
1 to 255 |
In this version of the SOCKS protocol, the Ver field is set to X ' 05 '. The Nmethods field contains the number of method marks (in bytes) that appear in the methods field. Simplification is like sending a server 05 01 00
The server selects one of these given methods and sends a message back to the client with the selected method:
If the selected message is X ' FF ', which means that none of the methods listed in the client list are selected, the client must close the connection.
Here we are concerned about X ' 00 ' Without authentication and X ' 02 ' username/password, simplifying is the first two bytes returned by the server
will be 05 00 or 05 02,02 to verify.
After the validation process is complete, the client sends detailed request information. If the negotiated method contains encapsulation for integrity checking and/or security purposes, these requests must be encapsulated in the manner defined by the method.
After the handshake is completed, to make a request connection, this is the connection to the remote server, we know that now since the connection is the proxy server, we actually send the data object is not it, that is, we have to tell the proxy server to connect to the real So-and-so, now is to do this step.
The format of the SOCKS request is as follows:
VER |
Cmd |
Rsv |
Atyp |
Dst. ADDR |
Dst. Prot |
1 |
1 |
X ' 00 ' |
1 |
Variable |
2 |