Ejemplo n.º 1
0
        public async Task <byte[]> TcpDataGet(IPEndPoint source, int result_size)
        {
            zero_all();
            Cryptography cryptography = new Cryptography();

            byte[] result = new byte[result_size];

            try
            {
                new_data = new byte[0];
                window.WriteLine("TCP client: " + source.Address.ToString() + ":" + source.Port + " started (wait for: " + result_size + " bytes)");
                result = await GetData(source, result_size);

                if (result.Length < result_size)
                {
                    window.WriteLine("starting TCP again");

                    result = await TcpDataGet(source, result_size);
                }
                window.WriteLine("MD5 (got): " + cryptography.HashToString(cryptography.GetSHA256Hash(result)) + " size: " + result.Length);
            }
            catch (Exception ex)
            {
                window.WriteLine("Exception in filetransfering (get)");
                window.WriteLine(ex.ToString());
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void TcpDataSend(byte[] data)
        {
            try
            {
                zero_all();
                IPEndPoint local = new IPEndPoint(0, 6879);

                Cryptography cryptography = new Cryptography();

                window.WriteLine("TCP server: " + local.Address.ToString() + ":" + local.Port + " started");
                window.WriteLine("MD5 (send): " + cryptography.HashToString(cryptography.GetSHA256Hash(data)) + " size: " + data.Length);

                int res = ReceiveData(local, data);

                if (res != 0)
                {
                    TcpDataSend(data);
                }
            }
            catch (Exception ex)
            {
                window.WriteLine("Exception in filetransfering (send)");
                window.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 3
0
        // версия размер(версия+подпись) подпись(версия+информация)

        private async Task CheckVersion(byte[] data, IPEndPoint source)
        {
            string version;

            byte[] signature = new byte[0];
            byte[] last_data;
            string file_size = string.Empty;

            TwoBytesArrays temp = new TwoBytesArrays();

            temp      = ByteArrayCut(data, version_size);
            version   = BytesToOperation(temp.part1);
            last_data = temp.part2;

            temp      = ByteArrayCut(last_data, size_size);
            file_size = BytesToOperation(temp.part1);
            signature = temp.part2;

            window.WriteLine("Siganture: " + cryptography.HashToString(signature));

            if (String.CompareOrdinal(version, info.version) > 0)
            {
                last_data = Encoding.UTF8.GetBytes(source.Address.ToString());
                last_data = AddOperation("update|give", UDPServer.operation_size, last_data);
                await server.Send(source, last_data);
                await UpdateSelf(file_size, signature, source, version);
            }
            else
            {
                window.WriteLine("no updates");
            }
        }
Ejemplo n.º 4
0
        public async Task <int> BlockCreate(bool admin = false)
        {
            int result = 0;

            try
            {
                string previous  = cryptography.GetHashString("system");    // system hash bbc5e661e106c6dcd8dc6dd186454c2fcba3c710fb4d8e71a60c93eaf077f073
                string previous2 = cryptography.GetHashString("system");    // system hash bbc5e661e106c6dcd8dc6dd186454c2fcba3c710fb4d8e71a60c93eaf077f073
                string path;
                string name;

                previous = await ActualBlockGet();

                if (admin)
                {
                    previous = cryptography.GetHashString("system");
                }

                byte[]         transactions      = new byte[0];
                List <string>  transactions_list = new List <string>();
                byte[]         transaction       = new byte[0];
                string         transaction_hash  = string.Empty;
                string         hash             = string.Empty;
                string         transaction_info = string.Empty;
                TwoBytesArrays temp             = new TwoBytesArrays();
                temp.part1 = new byte[0];
                int    counter = 0;
                byte[] block;

                path = filesystem.FSConfig.db_path;
                transactions_list = filesystem.GetFilesListFromDirectory(path);

                foreach (string element in transactions_list)
                {
                    path        = filesystem.FSConfig.db_path + @"\" + element;
                    transaction = await filesystem.GetFromFileAsync(path);

                    transaction_hash = cryptography.HashToString((cryptography.GetSHA256Hash(transaction)));
                    hash            += transaction_hash;
                    hash             = cryptography.GetHashString(hash);


                    transaction_info += transaction_hash + "#" + counter.ToString() + "|";

                    temp.part2 = transaction;
                    temp.part1 = ByteArrayJoin(temp);

                    transaction = temp.part1;

                    counter++;
                    if (!File.Exists(filesystem.FSConfig.db_temp_path + @"\" + element))
                    {
                        File.Move(filesystem.FSConfig.db_path + @"\" + element, filesystem.FSConfig.db_temp_path + @"\" + element);
                    }
                    else
                    {
                        File.Delete(filesystem.FSConfig.db_path + @"\" + element);
                    }
                }
                transactions = temp.part2;

                int count = transactions_list.Count;

                byte[] header;
                header = await Task.Run(() => BlockGenerate(previous, count, Encoding.UTF8.GetBytes(hash), Encoding.UTF8.GetBytes(transaction_info)));

                name = cryptography.HashToString(cryptography.GetSHA256Hash(header));

                window.WriteLine("Header size: " + header.Length);

                previous2 = await ActualBlockGet();

                if (admin)
                {
                    previous2 = cryptography.GetHashString("system");
                }


                if ((previous == previous2) && (previous.Length != 0))
                {
                    path = filesystem.FSConfig.db_blocks_path + @"\" + name;

                    block = AddOperation(header.Length.ToString(), Blocks.header_info_size, header);

                    temp.part1 = block;
                    temp.part2 = transaction;

                    block = ByteArrayJoin(temp);

                    window.WriteLine("[block]: transactions part (" + transaction.Length + ")");

                    await filesystem.AddInfoToFileAsync(path, block, true);

                    window.WriteLine("[block]: block created(" + path + ")");

                    if (!admin)
                    {
                        await SayNewBlock(name);
                    }

                    await ActualBlockSet(name);

                    result = 1;
                }
                else
                {
                    result = 2;
                }
            }
            catch (Exception ex)
            {
                window.WriteLine(ex.ToString());
            }

            return(result);
        }
Ejemplo n.º 5
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());
            }
        }