📄 socksbase.cs
字号:
namespace Imps.Client.CommLayer.SocksSipConnection
{
using Imps.Client.CommLayer;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
internal abstract class SocksBase : IDisposable
{
private Socket _Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private bool _IsConnected;
private SocksSipConnectionOption _option;
public event EventHandler<SocksConnectedEventArgs> Connected;
public event EventHandler DisConnected;
public event EventHandler<SocksErrorArgs> Error;
public event EventHandler<SocksErrorArgs> Failed;
public SocksBase(SocksSipConnectionOption option)
{
this._option = option;
}
public void Close()
{
if (this.Client.Connected)
{
try
{
this.Client.Shutdown(SocketShutdown.Both);
this.Client.BeginDisconnect(false, new AsyncCallback(this.EndDisConnected), this.Client);
}
catch (Exception)
{
this.IsConnected = false;
this.RaiseDisConnectedEvent();
}
}
}
public void Connect(string host, int port)
{
try
{
this.Client.set_SendTimeout(this.Option.Timeout * 0x3e8);
this.Client.set_ReceiveTimeout(this.Option.Timeout * 0x3e8);
this.Client.BeginConnect(this.Option.ProxyHost, this.Option.ProxyPort, new AsyncCallback(this.SocketConnected), new object[] { host, port });
}
catch (Exception exception)
{
this.IsConnected = false;
this.RaiseFailedEvent(exception);
}
}
protected abstract void ConsultToProxy(string host, int port);
public void Dispose()
{
if (this.Client != null)
{
((IDisposable) this.Client).Dispose();
}
}
private void EndDisConnected(IAsyncResult ar)
{
try
{
this.Client.EndDisconnect(ar);
}
catch (Exception exception)
{
this.RaiseErrorEvent(exception);
}
finally
{
this.IsConnected = false;
((IDisposable) this.Client).Dispose();
this.RaiseDisConnectedEvent();
}
}
protected void RaiseConnectedEvent()
{
WaitCallback callBack = null;
this.IsConnected = true;
EventHandler<SocksConnectedEventArgs> bkConnected = this.Connected;
if (bkConnected != null)
{
if (callBack == null)
{
callBack = delegate {
bkConnected.Invoke(this, new SocksConnectedEventArgs(this.LocalEndPoint, this.RemoteEndPoint));
};
}
ThreadPool.QueueUserWorkItem(callBack);
}
}
protected void RaiseDisConnectedEvent()
{
this.IsConnected = false;
EventHandler disConnected = this.DisConnected;
if (disConnected != null)
{
disConnected(this, EventArgs.Empty);
}
}
protected void RaiseErrorEvent(Exception ex)
{
EventHandler<SocksErrorArgs> error = this.Error;
if (error != null)
{
error.Invoke(this, new SocksErrorArgs(ex));
}
}
protected void RaiseFailedEvent(Exception ex)
{
WaitCallback callBack = null;
this.IsConnected = false;
EventHandler<SocksErrorArgs> bkFailed = this.Failed;
if (bkFailed != null)
{
if (callBack == null)
{
callBack = delegate {
bkFailed.Invoke(this, new SocksErrorArgs(ex));
};
}
ThreadPool.QueueUserWorkItem(callBack);
}
}
public int ReceiveMessage(byte[] buffer)
{
if (!this.IsConnected)
{
throw new SipConnectionException("尚未和代理服务器成功连接");
}
this.Client.set_ReceiveTimeout(0);
return this.Client.Receive(buffer);
}
public void SendMessage(byte[] buffer)
{
if (!this.IsConnected)
{
throw new SipConnectionException("尚未和代理服务器成功连接");
}
this.Client.Send(buffer);
}
protected byte[] SendSocksMessage(SocksMessage message)
{
byte[] buffer2;
if (!this.IsConnected)
{
throw new SocksConnectionException("尚未和代理服务器成功连接", SocksConnectionErrorCode.ServerFailure);
}
this.Client.Send(message.ToBinary());
using (MemoryStream stream = new MemoryStream())
{
byte[] buffer = new byte[100];
try
{
int count = this.Client.Receive(buffer);
if (count == 0)
{
throw new SocksConnectionException("代理服务器连接错误", SocksConnectionErrorCode.ProxyConnectionError);
}
stream.Write(buffer, 0, count);
while (this.Client.Available > 0)
{
count = this.Client.Receive(buffer);
stream.Write(buffer, 0, count);
}
buffer2 = stream.ToArray();
}
catch (SocketException exception)
{
if (exception.get_SocketErrorCode() == 0x274c)
{
throw new SocksConnectionException("和代理服务器的协商过程超时", SocksConnectionErrorCode.Timeout);
}
throw;
}
}
return buffer2;
}
private void SocketConnected(IAsyncResult ar)
{
try
{
object[] objArray = ar.AsyncState as object[];
string host = objArray[0] as string;
int port = (int) objArray[1];
this.Client.EndConnect(ar);
this.IsConnected = true;
ThreadPool.QueueUserWorkItem(delegate {
this.ConsultToProxy(host, port);
});
}
catch (Exception exception)
{
this.IsConnected = false;
this.RaiseFailedEvent(exception);
}
}
public override string ToString()
{
return "SOCKS 代理";
}
protected Socket Client
{
get
{
return this._Client;
}
}
public bool IsConnected
{
get
{
return this._IsConnected;
}
private set
{
this._IsConnected = value;
}
}
protected IPEndPoint LocalEndPoint
{
get
{
if (this.Client != null)
{
return (this.Client.LocalEndPoint as IPEndPoint);
}
return null;
}
}
public SocksSipConnectionOption Option
{
get
{
return this._option;
}
}
protected IPEndPoint RemoteEndPoint
{
get
{
if (this.Client != null)
{
return (this.Client.RemoteEndPoint as IPEndPoint);
}
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -