📄 sockssipconnection.cs
字号:
namespace Imps.Client.CommLayer.SocksSipConnection
{
using Imps.Client.Base;
using Imps.Client.CommLayer.Common;
using Imps.Client.Resource;
using Imps.Client.Utils;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
internal sealed class SocksSipConnection : ThreadSipConnectionBase
{
private IPEndPoint _LocalEndPoint;
private IPEndPoint _RemoteEndPoint;
private SocksBase Client;
public event EventHandler<FailedEventArgs> ConnectFailed;
public SocksSipConnection(object context, SocksSipConnectionOption option) : base(context)
{
base.Option = option;
}
private void Client_DisConnected(object sender, EventArgs e)
{
base.State = ConnectionState.Disconnected;
this.Client.Dispose();
}
public override void Close()
{
if (base.State == ConnectionState.Connected)
{
base.Close();
base.State = ConnectionState.Disconnecting;
this.Client.DisConnected += new EventHandler(this.Client_DisConnected);
this.Client.Close();
}
}
public override void Connect(string host, int port)
{
SocksErrorCallBack callBack = null;
SocksErrorCallBack back2 = null;
SocksErrorCallBack back3 = null;
this.WriteConnectionLog();
base.State = ConnectionState.Connecting;
SocksVer? ver = this.Option.Ver;
if ((((SocksVer) ver.GetValueOrDefault()) == SocksVer.Socks5) && ver.get_HasValue())
{
if (callBack == null)
{
callBack = delegate (Exception ex) {
this.DoConnectionFailed(ex);
};
}
this.TryConnectSocks5(host, port, callBack);
}
else
{
SocksVer? nullable2 = this.Option.Ver;
if ((((SocksVer) nullable2.GetValueOrDefault()) == SocksVer.Socks4) && nullable2.get_HasValue())
{
if (back2 == null)
{
back2 = delegate (Exception ex) {
this.DoConnectionFailed(ex);
};
}
this.TryConnectSocks4(host, port, back2);
}
else
{
if (back3 == null)
{
back3 = delegate (Exception ex) {
SocksErrorCallBack callBack = null;
if ((ex is SocketException) && ((ex as SocketException).get_SocketErrorCode() == 0x2742))
{
this.DoConnectionFailed(ex);
}
else
{
if (callBack == null)
{
callBack = delegate (Exception ex1) {
this.DoConnectionFailed(ex1);
};
}
this.TryConnectSocks4(host, port, callBack);
}
};
}
this.TryConnectSocks5(host, port, back3);
}
}
}
private void DoConnectionFailed(Exception ex)
{
base.State = ConnectionState.Disconnected;
this.RaiseConnectFailed(new FailedEventArgs(ex, ex.Message));
}
private void RaiseConnectFailed(FailedEventArgs argument)
{
EventHandler<FailedEventArgs> connectFailed = this.ConnectFailed;
if (connectFailed != null)
{
connectFailed.Invoke(this, argument);
}
}
protected override int ReceiveMessage(byte[] buffer)
{
return this.Client.ReceiveMessage(buffer);
}
protected override void SendMessage(byte[] buffer)
{
this.Client.SendMessage(buffer);
}
public override string ToString()
{
if (this.Client == null)
{
return (StringTable.ClientCommLayerString.SocksProxy + " - 未连接");
}
return (this.Client.ToString() + " - " + Tools.GetConnectionStateString(base.State));
}
private void TryConnectSocks4(string host, int port, SocksErrorCallBack callBack)
{
<>c__DisplayClass12 class2;
this.Client = new Socks4(this.Option);
this.Client.Connected += new EventHandler<SocksConnectedEventArgs>(this, (IntPtr) this.<TryConnectSocks4>b__f);
this.Client.Failed += new EventHandler<SocksErrorArgs>(class2, (IntPtr) this.<TryConnectSocks4>b__10);
this.Client.Error += new EventHandler<SocksErrorArgs>(this, (IntPtr) this.<TryConnectSocks4>b__11);
this.Client.Connect(host, port);
}
private void TryConnectSocks5(string host, int port, SocksErrorCallBack callBack)
{
<>c__DisplayClassd classd;
this.Client = new Socks5(this.Option);
this.Client.Connected += new EventHandler<SocksConnectedEventArgs>(this, (IntPtr) this.<TryConnectSocks5>b__a);
this.Client.Failed += new EventHandler<SocksErrorArgs>(classd, (IntPtr) this.<TryConnectSocks5>b__b);
this.Client.Error += new EventHandler<SocksErrorArgs>(this, (IntPtr) this.<TryConnectSocks5>b__c);
this.Client.Connect(host, port);
}
private void WriteConnectionLog()
{
string detail = string.Concat(new object[] { "ProxyHost:", this.Option.ProxyHost, " ProxyPort:", this.Option.ProxyPort });
string ext = string.Concat(new object[] { "<ProxyHost>", this.Option.ProxyHost, "</ProxyHost>\r\n", "<ProxyPort>", this.Option.ProxyPort, "</ProxyPort>" });
ClientLogger.WriteConnection("HttpSipConnection", 0, detail, ext);
}
public override IPEndPoint LocalEndPoint
{
get
{
if (base.State != ConnectionState.Connected)
{
return null;
}
return this._LocalEndPoint;
}
}
public SocksSipConnectionOption Option
{
get
{
return (base.Option as SocksSipConnectionOption);
}
}
public override IPEndPoint RemoteEndPoint
{
get
{
if (base.State != ConnectionState.Connected)
{
return null;
}
return this._RemoteEndPoint;
}
}
private delegate void SocksErrorCallBack(Exception ex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -