public static bool AddItem(TrustedLeafItem item)
        {
            if (IsTrusted(item))
            {
                return(false);
            }

            items.Add(item);

            using (SQLiteConnection connection = new SQLiteConnection("Data Source=\"" + DataPath + "\""))
            {
                connection.Open();

                String query = @"insert into trusted (name, guid) values (@name, @guid)";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.Add(new SQLiteParameter("@name", item.Name));
                    command.Parameters.Add(new SQLiteParameter("@guid", item.Guid.ToString()));
                    command.ExecuteNonQuery();
                }
            }

            return(true);
        }
        public static TrustedLeafItem GetTrusted(IPAddress ip, ushort port, byte[] data)
        {
            if (ip.Equals(IPAddress.Loopback) && port == Settings.Port)
            {
                return(new TrustedLeafItem
                {
                    Guid = new Guid(Settings.Get <byte[]>("guid")),
                    Name = Settings.Name
                });
            }

            TrustedLeafItem result = null;

            using (SHA1 sha1 = SHA1.Create())
                foreach (TrustedLeafItem i in items)
                {
                    List <byte> list = new List <byte>();
                    list.AddRange(Encoding.UTF8.GetBytes(i.Name));
                    list.AddRange(i.Guid.ToByteArray());
                    list.Reverse();
                    byte[] buf = sha1.ComputeHash(list.ToArray());

                    if (buf.SequenceEqual(data))
                    {
                        result = i;
                        break;
                    }
                }

            return(result);
        }
Esempio n. 3
0
        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));
        }
        public static void RemoveItem(TrustedLeafItem item)
        {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=\"" + DataPath + "\""))
            {
                connection.Open();

                String query = @"delete from trusted where name=@name and guid=@guid";

                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.Add(new SQLiteParameter("@name", item.Name));
                    command.Parameters.Add(new SQLiteParameter("@guid", item.Guid.ToString()));
                    command.ExecuteNonQuery();
                }
            }

            items.RemoveAll(x => x.Guid.Equals(item.Guid) && x.Name == item.Name);
        }
 public static bool IsTrusted(TrustedLeafItem item)
 {
     return(items.Find(x => x.Guid.Equals(item.Guid) && x.Name == item.Name) != null);
 }