private static void LeafLogin(Leaf leaf, TCPPacketReader packet, ulong time, LinkMode mode) { if (leaf.LoginPhase != LinkLogin.AwaitingLogin) { leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol)); leaf.Disconnect(); return; } byte[] credentials = packet.ReadBytes(20); leaf.Protocol = packet; leaf.Port = packet; Leaf existing = LeafPool.Leaves.Find(x => x.ExternalIP.Equals(leaf.ExternalIP) && x.Port == leaf.Port && x.Ident != leaf.Ident); if (existing != null) { existing.Disconnect(); } TrustedLeafItem item = TrustedLeavesManager.GetTrusted(leaf.ExternalIP, leaf.Port, credentials); if (item == null) { leaf.SendPacket(HubOutbound.LinkError(LinkError.Untrusted)); leaf.Disconnect(); return; } if (mode != LinkMode.Hub) { leaf.SendPacket(HubOutbound.LinkError(LinkError.Unavailable)); leaf.Disconnect(); return; } leaf.Name = item.Name; leaf.Guid = item.Guid; if (leaf.Protocol < Settings.LINK_PROTO) { leaf.SendPacket(HubOutbound.LinkError(LinkError.ExpiredProtocol)); leaf.Disconnect(); return; } leaf.LoginPhase = LinkLogin.AwaitingUserlist; leaf.Key = Crypto.CreateKey; leaf.IV = Crypto.CreateIV; leaf.SendPacket(HubOutbound.HubAck(leaf)); }