/// <summary> /// 处理协议请求 /// </summary> private bool DoProtocolRequest() { //取前4字节 byte[] buffer; IPAddress ipAddress = null; byte rep = 0x07; //不支持的命令 if (SocketUtils.Receive(this.Client, 4, out buffer)) { if (buffer.Length == 4) { //判断地址类型 switch (buffer[3]) { case 0x01: //IPV4 if (SocketUtils.Receive(this.Client, 4, out buffer)) { ipAddress = new IPAddress(buffer); } break; case 0x03: //域名 if (SocketUtils.Receive(this.Client, 1, out buffer)) { //取得域名的长度 if (SocketUtils.Receive(this.Client, (uint)(buffer[0]), out buffer)) { //取得域名地址 string address = Encoding.ASCII.GetString(buffer); IPAddress[] addresses = Dns.GetHostAddresses(address); if (addresses.Length != 0) { ipAddress = addresses[0]; } else { rep = 0x04; //主机不可达 } } } break; case 0x04: //IPV6; if (SocketUtils.Receive(this.Client, 16, out buffer)) { ipAddress = new IPAddress(buffer); } break; default: rep = 0x08; //不支持的地址类型 break; } } } if (ipAddress != null && rep == 0x07) { //取得端口号 if (SocketUtils.Receive(this.Client, 2, out buffer)) { Array.Reverse(buffer); //反转端口值 this.RemoteEndPoint = new IPEndPoint(ipAddress, BitConverter.ToUInt16(buffer, 0)); rep = 0x00; this.Server.WriteLineLog(string.Format("于{0}接收到客户端要求对主机{1}进行连接的请求....", DateTime.Now, this.RemoteEndPoint)); } } //输出应答 MemoryStream stream = new MemoryStream(); stream.WriteByte(0x05); stream.WriteByte(rep); stream.WriteByte(0x00); stream.WriteByte(0x01); IPEndPoint localEP = (IPEndPoint)Client.LocalEndPoint; byte[] localIP = localEP.Address.GetAddressBytes(); stream.Write(localIP, 0, localIP.Length); byte[] localPort = BitConverter.GetBytes((ushort)IPAddress.HostToNetworkOrder(localEP.Port)); stream.Write(localPort, 0, localPort.Length); SocketUtils.Send(this.Client, stream.ToArray()); return(this.RemoteEndPoint != null); }
/// <summary> /// 处理身份验证 /// </summary> private bool ValidateIdentity() { byte[] buffer; byte ep = 0xFF; string username = string.Empty, password = string.Empty; //报文格式:0x01 | 用户名长度(1字节)| 用户名(长度根据用户名长度域指定) | 口令长度(1字节) | 口令(长度由口令长度域指定) if (SocketUtils.Receive(this.Client, 2, out buffer)) { if (buffer.Length == 2) { //用户名为空 if (buffer[1] == 0x00) { if (string.IsNullOrEmpty(this.Server.UserName)) { ep = 0x00; //用户名为空 } } else { if (SocketUtils.Receive(this.Client, (uint)buffer[1], out buffer)) { username = Encoding.ASCII.GetString(buffer); if (!string.IsNullOrEmpty(this.Server.UserName)) { ep = (byte)(username.Equals(this.Server.UserName) ? 0x00 : 0xFF); } } } if (ep == 0x00) { ep = 0xFF; //判断密码 if (SocketUtils.Receive(this.Client, 1, out buffer)) { if (buffer[0] == 0x00) { if (!string.IsNullOrEmpty(this.Server.Password)) { ep = 0x00; //密码为空 } } else { if (SocketUtils.Receive(this.Client, (uint)buffer[0], out buffer)) { password = Encoding.ASCII.GetString(buffer); if (!string.IsNullOrEmpty(this.Server.Password)) { ep = (byte)(password.Equals(this.Server.Password) ? 0x00 : 0xFF); } } } } } } } //输出应答 SocketUtils.Send(this.Client, new byte[] { 0x01, ep }); this.Server.WriteLineLog(string.Format("于{0}接收到客户端{1}的身份验证请求[UserName={3},Password={4}],结果:{2}...", DateTime.Now, this.Client.RemoteEndPoint, (ep == 0x00 ? "通过" : "失败"), username, password)); return(ep == 0x00); }