Use the SOCKS5 Proxy Server to connect to the network

Source: Internet
Author: User

/// <Summary>
/// Use the SOCKS5 Proxy Server to connect to the network
/// </Summary>
Class sockproxy
{
Bool m_requireauthorize = false;
String m_user = string. empty;
String m_pass = string. empty;

/// <Summary>
/// Default is false
/// </Summary>
Public bool requireauthorize
{
Get {return m_requireauthorize ;}
Set {m_requireauthorize = value ;}
}
Public String Username
{
Get {return m_pass ;}
Set {m_pass = value ;}
}
Public String Password
{
Get {return m_user ;}
Set {m_user = value ;}
}

Public bool connectproxyserver (string strremotehost, int iremoteport, socket sproxyserver)
{
// Construct the first connection header of the SOCKS5 Proxy Server (no user name or password)
Byte [] bysock5send = new byte [10];
Bysock5send [0] = 5;
Bysock5send [1] = 1;
Bysock5send [2] = requireauthorize? (Byte) 2: (byte) 0;

// Send the first connection information of the SOCKS5 Proxy
Sproxyserver. Send (bysock5send, 3, socketflags. None );

Byte [] bysock5receive = new byte [10];
Int ireccount = sproxyserver. Receive (bysock5receive, bysock5receive. length, socketflags. None );

// User verification
If (bysock5receive [1] = 2 &&! Requireauthorize)
{
Throw new exception ("the proxy server requires Identity confirmation. ");
}
Else if (bysock5receive [1] = 2)
{
// Construct the first connection header of the SOCKS5 Proxy Server (no user name or password)
Byte [] Buser = encoding. Default. getbytes (username );
Byte [] bpass = encoding. Default. getbytes (password );

Int DL = 3 + Buser. Length + bpass. length;

Bysock5send = new byte [DL];
Bysock5send [0] = 5;
Bysock5send [1] = (byte) Buser. length;
Array. Copy (Buser, 0, bysock5send, 2, Buser. Length );
Bysock5send [2 + Buser. Length] = (byte) bpass. length;
Array. Copy (bpass, 0, bysock5send, 3 + Buser. length, bpass. Length );

// Send the first connection information of the SOCKS5 Proxy
Sproxyserver. Send (bysock5send, DL, socketflags. None );

Bysock5receive = new byte [100];
Ireccount = sproxyserver. Receive (bysock5receive, bysock5receive. length, socketflags. None );
}

If (ireccount <2)
{
Sproxyserver. Close ();
Throw new exception ("cannot get the correct response from the proxy server. ");
}

If (bysock5receive [0]! = 5 | (bysock5receive [1]! = 0 & bysock5receive [1]! = 2 ))
{
Sproxyserver. Close ();
Throw new exception ("the response error returned by the proxy service. ");
}

If (bysock5receive [1] = 0)
{
Bysock5send [0] = 5;
Bysock5send [1] = 1;
Bysock5send [2] = 0;
Bysock5send [3] = 1;

IPaddress ipadd = DNS. Resolve (strremotehost). Addresslist [0];
String strip = ipadd. tostring ();
String [] strarytemp = strip. Split (New char [] {'.'});
Bysock5send [4] = convert. tobyte (strarytemp [0]);
Bysock5send [5] = convert. tobyte (strarytemp [1]);
Bysock5send [6] = convert. tobyte (strarytemp [2]);
Bysock5send [7] = convert. tobyte (strarytemp [3]);

Bysock5send [8] = (byte) (iremoteport/256 );
Bysock5send [9] = (byte) (iremoteport % 256 );

Sproxyserver. Send (bysock5send, bysock5send. length, socketflags. None );
Ireccount = sproxyserver. Receive (bysock5receive, bysock5receive. length, socketflags. None );

If (bysock5receive [0]! = 5 | bysock5receive [1]! = 0)
{
Sproxyserver. Close ();
Throw new exception ("the second connection to the SOCKS5 Proxy returned data error. ");
}
Return true;
}

Else
{
If (bysock5receive [1] = 2)
Throw new exception ("the proxy server requires Identity confirmation. ");
Else
Return false;
}
Return false;
}

Public socket getsocket (string stripadd, int iport)
{
IPaddress hostadd = IPaddress. parse (stripadd); // DNS. Resolve (stripadd). Addresslist [0];
Ipendpoint ephost = new ipendpoint (hostadd, iport );
Socket S = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
S. Connect (ephost );
Return S;
}
}
// Usage example:

Private void form1_load (Object sender, eventargs E)
{
String proxyhost = "10.32.32.51"; // "10.4.8.1 ";//
Int proxyprot = 1080;

Byte [] B;
Int RL = 0;
String SR;

Sockproxy P = new sockproxy ();

{// Skip this Code if password verification is not required
P. requireauthorize = true;
P. Username = "Jerry ";
P. Password = "456321 ";
}
Socket SRH = P. getsocket (proxyhost, proxyprot );

P. Fig ("10.32.9.16", 9160, SRH );
B = encoding. Default. getbytes ("Get/WAP/default. aspx HTTP/1.0 ");
SRH. Send (B );
B = new byte [2, 4096];
RL = SRH. Receive (B );
Sr = encoding. Default. getstring (B, 0, rl );
MessageBox. Show (SR );
}

 

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.