📄 gsocks5tcpsocket.cs
字号:
using System;
using System.Net;
using System.Net.Sockets;
using Org.Mentalis.Security.Ssl;
namespace gowk.net.Sockets
{
/// <summary>
/// GTcpSocket 的摘要说明。
/// </summary>
public class GSocks5TcpSocket:GSocketBase
{
public GSocks5TcpSocket()
{
base.OwnerSock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
base.OwnerSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,1);
}
public override void Connect(string host,int port)
{
IPHostEntry entry=Dns.Resolve(this.Proxy.Host);
if(entry.AddressList.Length<1)throw new ArithmeticException("无法解释代理服务器地址");
IPEndPoint iep=new IPEndPoint(entry.AddressList[0],this.Proxy.Port);
System.Diagnostics.Trace.WriteLine(iep.ToString());
try
{
this.OwnerSock.Connect(iep);
}
catch
{
throw new ProxyException("无法连接到代理服务器");
}
this.LoginProxy();
this.Negotiation(host,port);
}
public override void LoginProxy()
{
//send loginmesthodes
byte[] getLoginMethode=new byte[4];
getLoginMethode[0]=5;
getLoginMethode[1]=2;
getLoginMethode[2]=0;
getLoginMethode[3]=2;
int rcv=this.OwnerSock.Send(getLoginMethode);
//receive the response
byte[] getLoginMethodeResponse=new byte[2];
rcv=this.OwnerSock.Receive(getLoginMethodeResponse);
if(rcv!=2)throw new ProxyException("代理服务器应答错误!");
if(getLoginMethodeResponse[1]==0)
{
return;
}
else if(getLoginMethodeResponse[1]==2)/* rquest password/username*/
{
/*
+----+------+----------+------+----------+
|VER | ULEN | UNAME | PLEN | PASSWD |
+----+------+----------+------+----------+
| 1 | 1 | 1 to 255 | 1 | 1 to 255 |
+----+------+----------+------+----------+
*/
byte[] name=System.Text.Encoding.ASCII.GetBytes(this.Proxy.Username);
byte[] pass=System.Text.Encoding.ASCII.GetBytes(this.Proxy.Password);
byte[] pass_name=new byte[name.Length+pass.Length+3];
byte pass_len=(byte)name.Length;
byte name_len=(byte)name.Length;
pass_name[0]=5;
pass_name[1]=name_len;
Buffer.BlockCopy(name,0,pass_name,2,name_len);
pass_name[name.Length+2]=pass_len;
Buffer.BlockCopy(pass,0,pass_name,name_len+3,pass_len);
this.OwnerSock.Send(pass_name);
/*
+----+--------+
|VER | STATUS |
+----+--------+
| 1 | 1 |
+----+--------+
*/
byte[] status=new byte[2];
rcv=this.OwnerSock.Receive(status);
if(rcv!=2)throw new ProxyException("代理服务器应答错误!");
if(status[1]!=0)
{
this.OwnerSock.Close();
throw new UnauthorizedAccessException();
}
}
}
public override void Negotiation(string host,int port)
{
byte[] bhost=System.Text.Encoding.ASCII.GetBytes(host);
byte[] bport=System.BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)port));
byte host_len=(byte)bhost.Length;
// byte port_len=2;
byte[] request=new byte[7+host_len];
request[0]=5;
request[1]=1;
request[2]=0;
request[3]=3;
request[4]=host_len;
Buffer.BlockCopy(bhost,0,request,5,host_len);
Buffer.BlockCopy(bport,0,request,5+host_len,2);
this.OwnerSock.Send(request);
System.Diagnostics.Trace.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(request,5,host_len));
/* SocksRequest req=new SocksRequest();
req.ATYP=AddressType.DomainName;
req.CMD=RequestType.CONNECT;
req.DSTADDR=System.Text.Encoding.ASCII.GetBytes(host);
req.DSTPORT=port;
req.RSV=0;
req.VER=VER.Socks5;
Byte[] reqBuffer=req.GetBytes();
this.OwnerSock.Send(reqBuffer);*/
byte[] rspBuffer=new byte[512];
int cnt=this.OwnerSock.Receive(rspBuffer);
if(cnt<7)throw new ProxyException("");
System.Diagnostics.Trace.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(rspBuffer,5,cnt));
if(rspBuffer[1]!=0)throw new ProxyException("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -