📄 socks5util.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace gowk.net.Sockets
{
/// <summary>
/// +----+----------+----------+
/// |VER | NMETHODS | METHODS |
/// +----+----------+----------+
/// | 1 | 1 | 1 to 255 |
/// +----+----------+----------+
/// </summary>
//[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public class AuthenticationMethods
{
public VER VER;
public byte NMETHODS;
// [MarshalAs(UnmanagedType.ByValArray,SizeConst=256)]
public METHOD[] METHODS;
public AuthenticationMethods()
{
}
public AuthenticationMethods(byte[] data)
{
this.VER=(VER)data[0];
this.NMETHODS=data[1];
this.METHODS=new METHOD[data.Length-2];
data.CopyTo(this.METHODS,2);
}
public byte[] GetBytes()
{
byte[] dst=new byte[1+1+NMETHODS];
dst[0]=(byte)VER;
dst[1]=NMETHODS;
System.Buffer.BlockCopy(METHODS,0,dst,2,METHODS.Length);
return dst;
}
}
/// <summary>
///
///o X'00' NO AUTHENTICATION REQUIRED
///o X'01' GSSAPI
///o X'02' USERNAME/PASSWORD
///o X'03' to X'7F' IANA ASSIGNED
///o X'80' to X'FE' RESERVED FOR PRIVATE METHODS
///o X'FF' NO ACCEPTABLE METHODS
///
/// </summary>
public enum METHOD:byte
{
NO_AUTHENTICATION_REQUIRED=0x00,
GSSAPI=0x01,
USERNAME_PASSWORD=0x02,
IANA_ASSIGNED=0x03,
//..........
RESERVED_FOR_PRIVATE_METHODS=0x80,
NO_ACCEPTABLE_METHODS=0xff
}
public enum VER:byte
{
Socks4=0x04,
Socks5=0x05
}
/// <summary>
///
///+----+--------+
///|VER | METHOD |
///+----+--------+
///| 1 | 1 |
///+----+--------+
///
/// </summary>
[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public class AuthenticationMethod
{
public VER VER;
public METHOD METHOD;
public AuthenticationMethod()
{
}
public AuthenticationMethod(byte[] data)
{
this.VER=(VER)data[0];
this.METHOD=(METHOD)data[1];
}
public byte[] GetBytes()
{
byte[] dst=new byte[1+1];
dst[0]=(byte)VER;
dst[1]=(byte)METHOD;
return dst;
}
}
/// <summary>
///CONNECT X'01'
///BIND X'02'
///UDP ASSOCIATE X'03'
/// </summary>
public enum RequestType:byte
{
CONNECT=1,
BIND,
UDP
}
/// <summary>
/// Addressing
/// </summary>
public enum AddressType:byte
{
IPV4=0x01,
DomainName=0x03,
IPV6=0x04
}
/// <summary>
///
///+----+-----+-------+------+----------+----------+
///|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
///+----+-----+-------+------+----------+----------+
///| 1 | 1 | X'00' | 1 | Variable | 2 |
///+----+-----+-------+------+----------+----------+
/// </summary>
//[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
public class SocksRequest
{
public VER VER;
public RequestType CMD;
public byte RSV;
public AddressType ATYP;
public byte[] DSTADDR;
public int DSTPORT;
public SocksRequest()
{
}
public SocksRequest(byte[] data)
{
this.VER=(VER)data[0];
this.CMD=(RequestType)data[1];
this.RSV=data[2];
this.ATYP=(AddressType)data[3];
int len=data[4];
this.DSTADDR=new byte[len];
Buffer.BlockCopy(data,5,this.DSTADDR,0,len);
this.DSTPORT=System.Net.IPAddress.NetworkToHostOrder((short)System.BitConverter.ToUInt16(data,5+len));
}
public byte[] GetBytes()
{
byte[] dst=new byte[7+this.DSTADDR.Length];
dst[0]=(byte)VER;
dst[1]=(byte)CMD;
dst[2]=(byte)RSV;
dst[3]=(byte)ATYP;
dst[4]=(byte)this.DSTADDR.Length;
Buffer.BlockCopy(this.DSTADDR,0,dst,5,this.DSTADDR.Length);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.DSTPORT)),0,dst,5+this.DSTADDR.Length,2);
return dst;
}
public IPEndPoint GetIPEndPoint()
{
IPEndPoint ep=null;
if(this.DSTADDR.Length==0)
{
ep=new IPEndPoint(IPAddress.Any,this.DSTPORT);
}
else
{
switch(this.ATYP)
{
case AddressType.IPV4:
ep=new IPEndPoint(new IPAddress(this.DSTADDR),this.DSTPORT);
break;
case AddressType.DomainName:
IPAddress ad=Dns.GetHostByName(System.Text.Encoding.ASCII.GetString(this.DSTADDR)).AddressList[0];
ep=new IPEndPoint(ad,this.DSTPORT);
break;
case AddressType.IPV6:
throw new System.NotSupportedException();
default:
throw new System.NotSupportedException();
}
}
return ep;
}
}
/// <summary>
///
///o REP Reply field:
///o X'00' succeeded
///o X'01' general SOCKS server failure
///o X'02' connection not allowed by ruleset
///o X'03' Network unreachable
///o X'04' Host unreachable
///o X'05' Connection refused
///o X'06' TTL expired
///o X'07' Command not supported
///o X'08' Address type not supported
///o X'09' to X'FF' unassigned
/// </summary>
public enum ReplyType:byte
{
Succeeded,
General_SOCKS_server_failure,
Connection_not_allow_by_ruleset,
NewWork_unreachable,
Connection_refused,
TTL_expired,
Command_not_supported,
Address_type_not_supported,
Unassigned
//....................
}
/// <summary>
///
///+----+-----+-------+------+----------+----------+
///|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
///+----+-----+-------+------+----------+----------+
///| 1 | 1 | X'00' | 1 | Variable | 2 |
///+----+-----+-------+------+----------+----------+
/// </summary>
public class SocksReply
{
public VER VER;
public ReplyType REP;
public byte RSV;
public AddressType ATYP;
public byte[] BNDADDR;
public int BNDPORT;
public SocksReply()
{
}
public SocksReply(byte[] data)
{
this.VER=(VER)data[0];
this.REP=(ReplyType)data[1];
this.RSV=data[2];
this.ATYP=(AddressType)data[3];
int len=data[4];
Buffer.BlockCopy(data,5,this.BNDADDR,0,len);
this.BNDPORT=System.Net.IPAddress.NetworkToHostOrder(System.BitConverter.ToUInt16(data,5+len));
}
public byte[] GetBytes()
{
byte[] dst=new byte[7+this.BNDADDR.Length];
dst[0]=(byte)VER;
dst[1]=(byte)REP;
dst[2]=(byte)RSV;
dst[3]=(byte)ATYP;
dst[4]=(byte)this.BNDADDR.Length;
Buffer.BlockCopy(this.BNDADDR,0,dst,5,this.BNDADDR.Length);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.BNDPORT)),0,dst,5+this.BNDADDR.Length,2);
return dst;
}
public IPEndPoint GetIPEndPoint()
{
IPEndPoint ep=null;
switch(this.ATYP)
{
case AddressType.IPV4:
ep=new IPEndPoint(new IPAddress(this.BNDADDR),this.BNDPORT);
break;
case AddressType.DomainName:
IPAddress ad=Dns.GetHostByName(System.Text.Encoding.ASCII.GetString(this.BNDADDR)).AddressList[0];
ep=new IPEndPoint(ad,this.BNDPORT);
break;
case AddressType.IPV6:
throw new System.NotSupportedException();
default:
throw new System.NotSupportedException();
}
return ep;
}
}
/// <summary>
///
///+----+------+------+----------+----------+----------+
///|RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
///+----+------+------+----------+----------+----------+
///| 2 | 1 | 1 | Variable | 2 | Variable |
///+----+------+------+----------+----------+----------+
/// </summary>
public class UDPDatagramHead
{
public short RSV;
public byte FRag;
public AddressType ATYP;
public byte[] DSTADDR;
public int DSTPORT;
public byte[] Data;
public UDPDatagramHead()
{
}
public UDPDatagramHead(byte[] data)
{
this.RSV=System.BitConverter.ToInt16(data,0);
this.FRag=data[2];
this.ATYP=(AddressType)data[3];
int len=data[4];
Buffer.BlockCopy(data,5,this.DSTADDR,0,len);
this.DSTPORT=System.Net.IPAddress.NetworkToHostOrder(System.BitConverter.ToUInt16(data,5+len));
this.Data=new byte[data.Length-5-len-2];
Buffer.BlockCopy(data,5+len+2,this.Data,0,this.Data.Length);
}
public byte[] GetBytes()
{
byte[] dst=new byte[7+this.Data.Length+this.DSTADDR.Length];
Buffer.BlockCopy(System.BitConverter.GetBytes(this.RSV),0,dst,0,2);
dst[2]=this.FRag;
dst[3]=(byte)ATYP;
dst[4]=(byte)this.DSTADDR.Length;
Buffer.BlockCopy(this.DSTADDR,0,dst,5,this.DSTADDR.Length);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.DSTPORT)),0,dst,5+this.DSTADDR.Length,2);
Buffer.BlockCopy(this.Data,0,dst,5+this.DSTADDR.Length+2,this.Data.Length);
return dst;
}
public IPEndPoint GetIPEndPoint()
{
IPEndPoint ep=null;
switch(this.ATYP)
{
case AddressType.IPV4:
ep=new IPEndPoint(new IPAddress(this.DSTADDR),this.DSTPORT);
break;
case AddressType.DomainName:
IPAddress ad=Dns.GetHostByName(System.Text.Encoding.ASCII.GetString(this.DSTADDR)).AddressList[0];
ep=new IPEndPoint(ad,this.DSTPORT);
break;
case AddressType.IPV6:
throw new System.NotSupportedException();
default:
throw new System.NotSupportedException();
}
return ep;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -