⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsecuresocket.cs

📁 语音视频功能 里面实现了基本的QQ与语音对话
💻 CS
字号:
using System;
using System.Net;
using System.Net.Sockets;
using Org.Mentalis.Security.Ssl;

namespace gowk.net.Sockets
{
	/// <summary>
	/// GSecuritySocket 的摘要说明。
	/// </summary>
	public sealed class GSecureSocket:ISocket
	{
		private Proxy m_Proxy;
		SecureSocket OwnerSock;
		SecurityOptions m_option;
		public GSecureSocket(SecurityOptions option)
		{
			this.m_option=option;
			this.OwnerSock=new SecureSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			this.OwnerSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,1);
		}
		public int Send(byte[] buffer)
		{
			return this.OwnerSock.Send(buffer);
		}
		public int SendTo(byte[] buffer, EndPoint remoteEP)
		{
			return this.OwnerSock.SendTo(buffer,remoteEP);
		}
		
		public int Receive(byte[] buffer)
		{
			return this.OwnerSock.Receive(buffer);
		}
		public int ReceiveFrom(byte[] buffer, ref EndPoint remoteEP)
		{
			return this.OwnerSock.ReceiveFrom(buffer,ref remoteEP);
		}

		public void Close()
		{
			this.OwnerSock.Close();			
		}
		public void Connect(string host,int port)
		{
			if(this.Proxy==null)
			{
				IPHostEntry entry=Dns.Resolve(host);
				if(entry.AddressList.Length<1)throw new SocketException();
				IPEndPoint iep=new IPEndPoint(entry.AddressList[0],port);		
				this.OwnerSock.Connect(iep);
			}
			else
			{
				IPEndPoint iep=null;
				if(this.Proxy.Host.Trim()=="localhost")
				{
					iep=new IPEndPoint(IPAddress.Any,this.Proxy.Port);
				}
				else
				{
					IPHostEntry entry=Dns.Resolve(this.Proxy.Host);
					if(entry.AddressList.Length<1)throw new ProxyException("无法解释代理服务器地址");
					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 object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
		{
			return this.OwnerSock.GetSocketOption(optionLevel,optionName);
		}
		public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
		{
			this.OwnerSock.SetSocketOption(optionLevel,optionName,optionValue);
		}
		public void Bind(EndPoint ep)
		{
			this.OwnerSock.Bind(ep);
		}
		public Proxy Proxy
		{
			get{return m_Proxy;}
			set{this.m_Proxy=value;}
		}
		public 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 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("");
		}
		public void AsyncInvoke(AsyncCallback callback)
		{
			callback.BeginInvoke(null,null,null);
		}
		public System.IO.Stream GetStream()
		{
			return new SecureNetworkStream(this.OwnerSock);
		}
		public void StartSSL()
		{
			this.OwnerSock.ChangeSecurityProtocol(this.m_option);
		}
		public bool Connected
		{
			get{return (this.OwnerSock!=null)&&this.OwnerSock.Connected;}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -