Use C # To implement the SOCKS5 proxy service (Open Source Code)

Source: Internet
Author: User

In the morning, breeze wrote a SOCKS5 Proxy ProtocolArticleC # implements socket5 proxy protocol communication, and in the comments, howaaa said, "By the way, we also talk about how to use C # To implement a SOCKS5 proxy server. There is very little information about this, I believe it is very valuable. "It took me an afternoon to seek out the SOCKS5 proxy server.Program.The program is simple.(You only need to understand the SOCKS5 protocol ),CodeVery messy. If you are interested, you can see if you are not interested ....

In breeze's article, I have missing an explanation of password authentication. Here I will make up for it.

========================================================== ========================================================== ================

When the client is connected to the SOCKS5 Proxy Server and handshakes, the client sends the following handshake packets:

Version Number (1 byte) | Number of authentication methods (1 byte) | authentication method sequence (1-bytes in length)

For example, the message (for example, the V5 version ):

0x05 0x01 0x00 indicates that the client only supports one (0x1) authentication method 0x00 (no verification requirement)

0x05 0x01 0x02 indicates that the client only supports one (0x01) authentication method 0x02 (User Name/password verification)

0x050x020x00 0x02 indicates that the client supports two types (0x02) Authentication method "0x00 and 0x02"

The SOCKS5 Protocol defines the following authentication methods:

0x00 no verification required

0x01 universal security service application interface (gssapi)

0x02 user name/password (username/password)

0x03 to X '7f' IANA allocation (IANA assigned)

0x80 to X 'fe 'private method reserved for private methods)

0xff unacceptable method (no acceptable methods)

 

Here, we want to explain in detail the process of "0x02 user name/password" verification.

When the client sends a packet with the 0x02 authentication method (for example, "0x05 0x01 0x02") to the server, the server learns that the client supports user name/password authentication (0x02). Therefore, if the server needs verification, it sends a "0x05 0x02" response, in this way, the client will enter the "user name/password" verification process.

The message format of the "0x02 user name/password" authentication protocol is:

0x01 | User Name Length (1 byte) | User Name (Length Specified based on the user name length domain) | password length (1 byte) | password (length specified by the password length domain)

Therefore, the length of the message depends on the length of the user name and password, for example, the following message:

0x01 0x02 0x41 0x42 0x02 0x43 0x43

Indicates the verification message with the user name "AB" and password "cc" sent.

After receiving the user name/password verification message, the server processes the message and returns the response message in the following format:

0x01 | verification result flag

If the verification succeeds, the value of the "Verification Result flag" is "0x00". Otherwise, all other values indicate Verification Failed! The next step is not allowed.

 

After the handshake and authentication process are complete, the "connection request" process will take place. In this step, the server should check the connection status of the remote server address sent from the client, however, in my program, I stole my laziness and simply went to the "connection" session process.

Source codeDownload:

Http://files.cnblogs.com/kingthy/Socks5Server.rar

This program has been tested with QQ and thunder, and can basically run normally...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.