예제 #1
0
        private void GetSecret(string socketstr)
        {
            var lineByte = Crypt.Base64Decode(socketstr);

            if (lineByte.Length != 8)
            {
                return;
            }
            var serverkey = lineByte; //Crypt.DHExchange(lineByte);

//                Debug.Log("dhkey: " + Crypt.HexEncode(serverkey));

            _challenge.secret = Crypt.DHSecret(_challenge.clientkey, serverkey);;
//                Debug.Log("secret: " + Crypt.HexEncode(_challenge.secret));

            var hmackey = Crypt.HMAC64(_challenge.challenge, _challenge.secret);

//                Debug.Log("hmac: " + Crypt.HexEncode(hmackey));

            Request(hmackey);

            _state = LoginAuthState.SendLogin;

            DoLoginAction();
        }
예제 #2
0
        private void LoginResult(string socketstr)
        {
            AuthPackageResp resp = new AuthPackageResp();

            var code         = int.Parse(socketstr.Substring(0, 3));
            var subidByte    = Crypt.Base64Decode(socketstr.Substring(4));
            var resultString = Encoding.UTF8.GetString(subidByte);

            if (code == 200)
            {
                var split = Regex.Split(resultString, "['$']");
                resp.gate   = split[0];
                resp.port   = Int32.Parse(split[1]);
                resp.uid    = split[2];
                resp.secret = split[3];
                resp.subid  = split[4];
            }
//            Debug.Log("login result code:" + code);
//            Debug.Log("login result gate:" + resp.gate);
//            Debug.Log("login result port:" + resp.port);
//            Debug.Log("login result uid:" + resp.uid);
//            Debug.Log("login result subid:" + resp.subid);
//            Debug.Log("login result secret:" + resp.secret);
            _state = LoginAuthState.LoginFinished;
            OnLoginCallBack?.Invoke(code, resp);
        }
예제 #3
0
        private void GetChallenge(string socketline)
        {
            var challengeByte = Crypt.Base64Decode(socketline);

            if (challengeByte.Length != 8)
            {
                return;
            }
            _challenge.challenge = challengeByte;
            var dhkey = Crypt.RandomKey();

            _challenge.clientkey = Crypt.DHExchange(dhkey);
            Request(_challenge.clientkey);

            _state = LoginAuthState.GetSecret;
        }
예제 #4
0
파일: Login.cs 프로젝트: lineCode/BallGame
    public async void OnClickLogin()
    {
        if (UserData.LoginSeverID == 0)
        {
            var result = await Network.AsyncConnect("127.0.0.1", 42346, Moon.SocketProtocolType.Text);

            if (result.ConnectionId == 0)
            {
                MessageBox.Show(result.Data.GetString());
                return;
            }
            UserData.LoginSeverID = result.ConnectionId;
        }

        string handshake = "";

        {
            var line = await Network.ReadLine(UserData.LoginSeverID);

            Debug.LogFormat("1. challenge {0}", line.Data.GetString());
            var challenge = BitConverter.ToUInt64(Crypt.Base64Decode(line.Data.GetString()), 0);
            var clientkey = Crypt.Random();
            Network.Send(UserData.LoginSeverID, Crypt.Base64Encode(Crypt.DHExchange(clientkey)) + "\n");

            line = await Network.ReadLine(UserData.LoginSeverID);

            var secret = Crypt.DHSecret(BitConverter.ToUInt64(Crypt.Base64Decode(line.Data.GetString()), 0), clientkey);
            Debug.Log(string.Format("2. sceret is {0}", Crypt.ToHex(BitConverter.GetBytes(secret))));
            Network.Send(UserData.LoginSeverID, Crypt.HMAC64_BASE64(challenge, secret) + "\n");

            string server = "game_1";
            string user   = userName.text;
            string pass   = "******";

            string token  = string.Format("{0}@{1}:{2}", Crypt.Base64Encode(user), Crypt.Base64Encode(server), Crypt.Base64Encode(pass));
            var    etoken = Crypt.DesEncodeBase64(BitConverter.GetBytes(secret), Encoding.Default.GetBytes(token));
            Network.Send(UserData.LoginSeverID, etoken + "\n");
            line = await Network.ReadLine(UserData.LoginSeverID);

            var result = line.Data.GetString();
            var code   = result.Substring(0, 3);
            Debug.LogFormat("3. code {0}", code);
            if (code != "200")
            {
                return;
            }
            Network.Close(UserData.LoginSeverID);
            UserData.LoginSeverID = 0;

            var subid = Crypt.Base64Decode(result.Substring(4));
            Debug.Log("login ok, subid= " + Encoding.Default.GetString(subid));

            handshake = string.Format("{0}@{1}#{2}:{3}", Crypt.Base64Encode(user), Crypt.Base64Encode(server), Crypt.Base64Encode(subid), 1);
            string hmac = Crypt.HMAC64_BASE64(BitConverter.ToUInt64(Crypt.HashKey(handshake), 0), secret);
            handshake = handshake + ":" + hmac;

            UserData.uid = int.Parse(Encoding.Default.GetString(subid));
        }


        if (UserData.GameSeverID == 0)
        {
            if (Ip.text.Length == 0)
            {
                Ip.text = "127.0.0.1";
            }

            if (Port.text.Length == 0)
            {
                Port.text = 12345.ToString();
            }

            var result = await Network.AsyncConnect(Ip.text, int.Parse(Port.text), Moon.SocketProtocolType.Socket);

            if (result.ConnectionId == 0)
            {
                MessageBox.Show(result.Data.GetString());
                return;
            }
            UserData.GameSeverID = result.ConnectionId;
        }

        var v = await Network.Call <S2CLogin>(UserData.GameSeverID, new C2SLogin { token = handshake });

        if (v.res == "200 OK")
        {
            UserData.username = userName.text;

            SceneManager.LoadScene("Game");
        }
        else
        {
            MessageBox.Show(v.res);
            Debug.Log(v.res);
        }
    }