internal void KcpDataBackHandle(RemoteHyperSocket remote, byte[] data) { if (remote.isValid) { if (hyperSocket.config.UseSSL && (hyperSocket.config.SSLMode == 0 || hyperSocket.config.SSLMode == 2)) { data = remote.ssl.AESDecrypt(data); } if (data.Compare(HyperSocket.HeartPingBytes)) { remote.SendPong(); } else { listener.OnUdpReceive(data, remote); } } else { long verifyCode = remote.SessionId * (hyperSocket.UdpPort / 10); var waitVerifyCode = Encoding.UTF8.GetString(data); if (verifyCode.ToString() == waitVerifyCode) { remote.isValid = true; var str = hyperSocket.config.UseSSL ? ("1" + hyperSocket.config.SSLMode + hyperSocket.ssl.GetRSAPublicKey()) : "0"; remote.SendKcp(Encoding.UTF8.GetBytes(str)); } else { remote.CloseSocket(); } } }
/// <summary> /// 生成认证通道 /// </summary> /// <returns></returns> internal byte[] GenerateVerifyConnection(out ushort sessionId) { try { sessionId = (ushort)GetUnusedSocketIndex(); if (sessionId > ushort.MinValue) { // 先加入验证 RemoteHyperSocket remote = new RemoteHyperSocket(sessionId, this, config); SetSocketAtIndex(sessionId, remote); byte[] data = new byte[8]; data[0] = (byte)((UdpPort >> 8) & 0xFF); data[1] = (byte)((UdpPort) & 0xFF); data[2] = (byte)((sessionId >> 8) & 0xFF); data[3] = (byte)((sessionId) & 0xFF); // 认证部分 data[4] = (byte)(data[0] + data[1]); data[5] = (byte)(data[2] + data[3]); data[6] = (byte)(data[0] + data[3]); data[7] = (byte)(data[1] + data[2]); return(data); } } catch (Exception ex) { svrListener.SocketError(ex); } sessionId = 0; return(null); }
/// <summary> /// 构造函数 /// </summary> /// <param name="IsServerMode"></param> /// <param name="ip"></param> /// <param name="tcpPort"></param> /// <param name="udpPort"></param> /// <param name="connectMaxNum"></param> /// <param name="config"></param> private HyperSocket(bool IsServerMode, string ip, uint tcpPort, uint udpPort, uint connectMaxNum, HyperSocketConfig config) { this.IsServerMode = IsServerMode; this.ip = ip; TcpPort = tcpPort; UdpPort = udpPort; if (connectMaxNum < ushort.MaxValue - 1) { connectMaxNum += 1; } this.connectMaxNum = connectMaxNum; this.config = config; if (this.IsServerMode) { remoteSockets = new RemoteHyperSocket[connectMaxNum]; ssl = new SSL(SSL.SSLMode.RSA); } timeFlow = BaseTimeFlow.CreateTimeFlow(this); }
/// <summary> /// 设置远程套接字 /// </summary> /// <returns></returns> internal void SetSocketAtIndex(int index, RemoteHyperSocket socket) { lock (remoteSockets) remoteSockets[index] = socket; }