private int ProcessSHandshake(Protocol _p) { Handshake.SHandshake p = (Handshake.SHandshake)_p; if (DHContext.TryGetValue(p.Sender.SessionId, out var dhRandom)) { Array.Reverse(p.Argument.dh_data); byte[] material = Handshake.Helper.computeDHKey( Config.HandshakeOptions.DhGroup, new BigInteger(p.Argument.dh_data), dhRandom).ToByteArray(); Array.Reverse(material); System.Net.IPAddress ipaddress = ((IPEndPoint)p.Sender.Socket.RemoteEndPoint).Address; if (ipaddress.IsIPv4MappedToIPv6) { ipaddress = ipaddress.MapToIPv4(); } byte[] key = ipaddress.GetAddressBytes(); logger.Debug("{0} remoteip={1}", p.Sender.SessionId, BitConverter.ToString(key)); int half = material.Length / 2; byte[] hmacMd5 = Digest.HmacMd5(key, material, 0, half); p.Sender.SetOutputSecurityCodec(hmacMd5, p.Argument.c2sneedcompress); hmacMd5 = Digest.HmacMd5(key, material, half, material.Length - half); p.Sender.SetInputSecurityCodec(hmacMd5, p.Argument.s2cneedcompress); DHContext.TryRemove(p.Sender.SessionId, out var _); new Handshake.CHandshakeDone().Send(p.Sender); OnHandshakeDone(p.Sender); return(0); } throw new Exception("handshake lost context."); }
protected void AddHandshakeClientFactoryHandle() { var tmp = new Handshake.SHandshake(); HandshakeProtocols.Add(tmp.TypeId); AddFactoryHandle(tmp.TypeId, new Service.ProtocolFactoryHandle() { Factory = () => new Handshake.SHandshake(), Handle = ProcessSHandshake, NoProcedure = true, }); }