コード例 #1
0
ファイル: AdminClass.cs プロジェクト: mrB4el/BeeChain
        public async Task DirectControlCommand(string aim, string command, string root_private)
        {
            try
            {
                int        port = server.Port;
                IPAddress  address;
                IPEndPoint target;
                int        timeout = 0;

                byte[] command_bytes = Encoding.UTF8.GetBytes(command);
                byte[] signature     = MakeAdminSignature(command_bytes, root_private);

                byte[] operation = AddOperation(command, AdminClass.command_size, signature);

                operation = AddOperation("admin", UDPServer.operation_size, operation);

                if (aim == null)
                {
                    List <string> black_list = new List <string>(0);
                    List <string> list_to    = await server.GetKnownList();

                    list_to = server.RemoveIPAddresses(list_to, black_list);

                    foreach (var ip_address in list_to)
                    {
                        address = IPAddress.Parse(ip_address);
                        target  = new IPEndPoint(address, port);

                        await server.Send(target, operation);

                        timeout++;
                        window.WriteLine("Updating..." + target.ToString());

                        if (timeout % 20 == 0)
                        {
                            await Task.Delay(100);
                        }
                    }
                }
                else
                {
                    address = IPAddress.Parse(aim);
                    target  = new IPEndPoint(address, port);

                    window.WriteLine("Updating..." + target.ToString());
                    await server.Send(target, operation);
                }
            }
            catch (Exception ex)
            {
                window.WriteLine("Exception in AdminClass.DirectUpdate");
                window.WriteLine(ex.ToString());
            }
        }
コード例 #2
0
        public async Task CheckForUpdate()
        {
            IPAddress  address;
            IPEndPoint target;
            int        port = Convert.ToInt32(server.Port);

            byte[] operation_bytes;

            List <string> black_list = new List <string>(0);
            List <string> list_to    = await server.GetKnownList();

            list_to = server.RemoveIPAddresses(list_to, black_list);

            operation_bytes = server.OperationToBytes("update|tell", UDPServer.operation_size);

            if (list_to.Count > 0)
            {
                int timeout = 0;
                foreach (var ip_address in list_to)
                {
                    address = IPAddress.Parse(ip_address);
                    target  = new IPEndPoint(address, port);

                    await server.Send(target, operation_bytes);

                    timeout++;
                    window.WriteLine("Updating..." + timeout);


                    if (timeout % 20 == 0)
                    {
                        await Task.Delay(100);
                    }
                }
            }
        }
コード例 #3
0
        public async Task Scan(int size = 10)
        {
            try
            {
                string operation       = "registration|1";
                byte[] operation_bytes = Encoding.UTF8.GetBytes(operation);

                List <string> black_list = new List <string>(0);
                string        senpai     = "78.139.208.149";

                IPAddress  address = IPAddress.Parse(senpai);
                int        port    = Convert.ToInt32(server.Port);
                IPEndPoint target  = new IPEndPoint(address, port);

                await server.Send(target, operation_bytes);

                List <string> list_to_connect = new List <string>(0);

                foreach (var ip_gateway in server.GetGatewayAddresses())
                {
                    IPAddress temp_ip = IPAddress.Parse(ip_gateway);
                    byte[]    bytes;


                    for (int i = 0; i < size; i++)
                    {
                        bytes = temp_ip.GetAddressBytes();
                        if (++bytes[3] == 0)
                        {
                            if (++bytes[2] == 0)
                            {
                                if (++bytes[1] == 0)
                                {
                                    ++bytes[0];
                                }
                            }
                        }

                        temp_ip = new IPAddress(bytes);
                        list_to_connect.Add(temp_ip.ToString());
                    }
                }
                list_to_connect = server.RemoveIPAddresses(list_to_connect, black_list);

                if (list_to_connect.Count > 0)
                {
                    int timeout = 0;
                    foreach (var ip_address in list_to_connect)
                    {
                        port    = Convert.ToInt32(server.Port);
                        address = IPAddress.Parse(ip_address);
                        target  = new IPEndPoint(address, port);

                        await server.Send(target, operation_bytes);

                        timeout++;

                        if (timeout % 20 == 0)
                        {
                            await Task.Delay(100);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                window.RichTextBox_Console.AppendText(e.ToString());
            }
        }
コード例 #4
0
        public async Task GetLogic(IPEndPoint source, byte[] data)
        {
            try
            {
                TwoBytesArrays tran = new TwoBytesArrays();
                tran = ByteArrayCut(data, Transactions.size_suboperation);

                string   sub_operation = BytesToOperation(tran.part1);
                string[] sub           = sub_operation.Split('|');
                string   name;
                string   path;

                List <string> ip_list     = new List <string>(0);
                Transaction   transaction = new Transaction();

                window.WriteLine("[45]: " + sub[0] + " from " + source.ToString());
                window.WriteLine("[46]: " + sub_operation);

                if (sub[0] == "new")
                {
                    transaction = ParseTMessage(tran.part2);
                    window.WriteLine("[tr] Transaction got: ");
                    int check = await CheckTransaction(transaction);

                    window.WriteLine("[tr] Checking result: " + check);

                    if ((check == 2) || (check == 1))
                    {
                        name = cryptography.HashToString(cryptography.GetSHA256Hash(tran.part2));
                        path = filesystem.FSConfig.db_path + @"\" + name;

                        int status = await filesystem.AddInfoToFileAsync(path, tran.part2, true);

                        if (status != 3)
                        {
                            byte[] message = Encoding.UTF8.GetBytes(name);
                            message = AddOperation("confirm", Transactions.size_suboperation, message);
                            message = AddOperation("transaction", UDPServer.operation_size, message);

                            await server.Send(source, message);

                            if (debug)
                            {
                                window.WriteLine("Transaction (" + name + ") confirmed to " + source.ToString());
                            }
                        }


                        int sub_times = Convert.ToInt32(sub[1]) - 1;

                        if (sub_times >= 1)
                        {
                            List <string> known_hosts = await server.GetKnownList();

                            List <string> black_list = new List <string>(0);


                            black_list.Add(source.Address.ToString());
                            black_list.AddRange(server.myIpAddresses);

                            known_hosts = server.RemoveIPAddresses(known_hosts, black_list);

                            //await SendTransaction(tran.part2, sub_times, known_hosts);
                        }
                    }
                }

                if (sub[0] == "confirm")
                {
                    name = Encoding.UTF8.GetString(tran.part2);
                    if (debug)
                    {
                        window.WriteLine("Transaction (" + name + ") confirmed by " + source.ToString());
                    }
                }

                ip_list.Add(source.Address.ToString());
                await server.AddToKnownList(ip_list);
            }
            catch (Exception ex)
            {
                window.WriteLine(ex.ToString());
            }
        }