コード例 #1
0
ファイル: Server.cs プロジェクト: bhlshrf/SecurityProject
        private async Task <string> handelClientAsync(TcpClient client, RSA rsa, AES aes)
        {
            aes.Enable(_AESEnable);
            string query = await ReadStringAsyc(client, aes);

            string username = query, pwd = "";

            if (query.StartsWith("register user["))
            {
                string error = "", res = "";
                var    c = (await ReadBytesAsyc(client, aes)).ToX509();

                if (CertificationFactory.Verify(c, ref error))
                {
                    username = query.SubStringFromTo(query.IndexOf("[") + 1, query.IndexOf("]"));
                    if (db.FindByName(username) != null)
                    {
                        res = "this username used, try somthing else";
                    }
                    else
                    {
                        pwd = query.SubStringFromTo(query.IndexOf("[", query.IndexOf("[") + 1) + 1, query.IndexOf("]", query.IndexOf("]") + 1));
                        db.AddAccount(username, pwd);
                        res = "ok";
                    }
                }
                else
                {
                    res = "your certificate not valid";
                }

                WriteString(client, aes, res);
                return(username);
            }
            else
            {
                pwd = await ReadStringAsyc(client, aes);
            }
            var me = db.FindByName(username);

            if (me == null || me.Password != pwd)
            {
                WriteString(client, aes, "no");
            }
            else
            {
                log(me.Username + " logged in");
                WriteString(client, aes, "ok");
                for (string command = "", res = ""; true;)
                {
                    try
                    {
                        aes.Enable(_AESEnable);

                        log("wait '" + me.Username + "' to send...");
                        command = await ReadStringAsyc(client, aes);

                        log(me.Username + " sent : " + command);

                        if (command == "bye")
                        {
                            break;
                        }
                        else if (command == "get total")
                        {
                            res = db.TotalBalance().ToString();
                        }
                        else if (command == "get me")
                        {
                            res = me.Balance.ToString();
                        }
                        else if (command == "get your public key")
                        {
                            res = RSA.KeyToString(rsa.PublicKey);
                        }
                        else if (command.StartsWith("get ")) // get user1
                        {
                            var v = db.FindByName(command.Substring("get ".Length));
                            res = (v == null) ? "null" : v.Balance.ToString();
                        }
                        else if (command == "transfer") //transfer\r\n100 to user3
                        {
                            rsa.Enable(_RSAEnable);
                            var amountAndUser = await ReadStringAsyc(client, rsa);

                            WriteString(client, aes, "ok");
                            log(me.Username + " sent : " + amountAndUser);
                            log("Varify signature for transaction");
                            var signature = await ReadBytesAsyc(client, aes);

                            if (rsa.Varify(amountAndUser.StringToBytes(), signature) == false)
                            {
                                log("signature wrong");
                                res = "error: signature wrong";
                            }
                            else
                            {
                                log("signature ok");

                                int    amount = int.Parse(amountAndUser.SubStringFromTo(0, amountAndUser.IndexOf(" to ")));
                                string toUser = amountAndUser.Substring(amountAndUser.IndexOf(" to ") + " to ".Length);
                                if (db.TransferTo(me, toUser, amount, ref res))
                                {
                                    res = me.Balance.ToString();
                                }
                            }
                        }
                        else
                        {
                            res = "unkown command \"" + command + "\", try 'help'";
                        }
                    }
                    catch
                    {
                        log("error while reading - " + command);
                        res = "error, can't understand command!";
                    }

                    WriteString(client, aes, res);
                }
            }
            return(username);
        }