📄 replyrequirmentsocks5message.cs
字号:
namespace Imps.Client.CommLayer.SocksSipConnection
{
using System;
using System.IO;
using System.Text;
internal sealed class ReplyRequirmentSocks5Message : Socks5Message
{
private Imps.Client.CommLayer.SocksSipConnection.HostNameType _atyp = Imps.Client.CommLayer.SocksSipConnection.HostNameType.Ip4;
private string _bndHost = "0.0.0.0";
private int _bndPort;
private Socks5RequirmentReplyType _rep;
private ReplyRequirmentSocks5Message(Socks5RequirmentReplyType rep, string bndHost, int bndPort)
{
this.Rep = rep;
this.BndHost = bndHost;
this.BndPort = bndPort;
}
public static ReplyRequirmentSocks5Message Parse(byte[] messageBuffer)
{
ReplyRequirmentSocks5Message message;
try
{
if (messageBuffer == null)
{
throw new ArgumentNullException("messageBuffer");
}
if (messageBuffer[0] != 5)
{
throw new ArgumentException("当前Socks代理未支持Socks5");
}
try
{
string bndHost;
MemoryStream stream = new MemoryStream(messageBuffer);
stream.Seek((long) 0, SeekOrigin.Begin);
stream.ReadByte();
Socks5RequirmentReplyType rep = (Socks5RequirmentReplyType) ((byte) stream.ReadByte());
stream.ReadByte();
Imps.Client.CommLayer.SocksSipConnection.HostNameType type2 = (Imps.Client.CommLayer.SocksSipConnection.HostNameType) ((byte) stream.ReadByte());
if (type2 == Imps.Client.CommLayer.SocksSipConnection.HostNameType.HostName)
{
byte[] buffer = new byte[stream.ReadByte()];
stream.Read(buffer, 0, buffer.Length);
bndHost = Encoding.UTF8.GetString(buffer);
}
else
{
int num2 = (type2 == Imps.Client.CommLayer.SocksSipConnection.HostNameType.Ip4) ? 4 : 0x10;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < num2; i++)
{
if (i > 0)
{
builder.Append('.');
}
builder.Append(stream.ReadByte().ToString());
}
bndHost = builder.ToString();
}
int bndPort = (stream.ReadByte() << 8) | stream.ReadByte();
message = new ReplyRequirmentSocks5Message(rep, bndHost, bndPort);
}
catch (Exception exception)
{
throw new ArgumentException("转换过程中出现错误", exception);
}
}
catch (Exception exception2)
{
throw new SocksConnectionException("代理服务器对连接请求的应答无效", exception2, SocksConnectionErrorCode.ReplyError);
}
return message;
}
public override byte[] ToBinary()
{
MemoryStream stream = new MemoryStream();
stream.WriteByte((byte) base.Ver);
stream.WriteByte((byte) this.Rep);
stream.WriteByte(0);
stream.WriteByte((byte) this.Atyp);
if (this.Atyp == Imps.Client.CommLayer.SocksSipConnection.HostNameType.HostName)
{
stream.WriteByte((byte) this.BndHost.Length);
byte[] bytes = Encoding.UTF8.GetBytes(this.BndHost);
stream.Write(bytes, 0, bytes.Length);
}
else
{
try
{
foreach (string text in this.BndHost.Split(new char[] { '.' }))
{
stream.WriteByte(byte.Parse(text));
}
}
catch (Exception exception)
{
throw new ArgumentException("所指定的主机名无效!", exception);
}
}
stream.WriteByte((byte) (this.BndPort >> 8));
stream.WriteByte((byte) this.BndPort);
return stream.ToArray();
}
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("VER : {0}\n", base.Ver);
builder.AppendFormat("REP : {0}\n", this.Rep);
builder.AppendFormat("RSV : {0}\n", 0);
builder.AppendFormat("BND.HOST : {0}\n", this.BndHost);
builder.AppendFormat("BND.PORT : {0}", this.BndPort);
return builder.ToString();
}
public Imps.Client.CommLayer.SocksSipConnection.HostNameType Atyp
{
get
{
return this._atyp;
}
}
public string BndHost
{
get
{
return this._bndHost;
}
set
{
if (value == null)
{
throw new ArgumentNullException("ServerHost");
}
int length = value.Split(new char[] { '.' }).Length;
switch (length)
{
case 4:
this._atyp = Imps.Client.CommLayer.SocksSipConnection.HostNameType.Ip4;
break;
case 0x10:
this._atyp = Imps.Client.CommLayer.SocksSipConnection.HostNameType.Ip6;
break;
default:
if (length > 2)
{
throw new ArgumentException("指定的服务器无效");
}
this._atyp = Imps.Client.CommLayer.SocksSipConnection.HostNameType.HostName;
break;
}
this._bndHost = value;
}
}
public int BndPort
{
get
{
return this._bndPort;
}
set
{
this._bndPort = value;
}
}
public bool IsSuccessed
{
get
{
return (this.Rep == Socks5RequirmentReplyType.Succeeded);
}
}
public Socks5RequirmentReplyType Rep
{
get
{
return this._rep;
}
set
{
this._rep = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -