Example #1
0
        public static void AddMinerStudioSession(IWsSessionAdapter session)
        {
            if (!WsRoot.TryGetUser(session.AuthorizationBase64, out WsUserName wsUserName, out UserData userData, out string errMsg))
            {
                session.CloseAsync(WsCloseCode.Normal, errMsg);
                return;
            }
            IMinerStudioSession minerSession = MinerStudioSession.Create(userData, wsUserName, session.SessionId, WsRoot.WsServer.MinerStudioWsSessionsAdapter);

            WsRoot.MinerStudioSessionSet.Add(minerSession);
            session.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ServerTime)
            {
                Data = Timestamp.GetTimestamp()
            }.SignToBytes(userData.Password));
        }
Example #2
0
        public static bool TryGetUser(this WebSocketBehavior behavior, out WsUserName wsUserName, out UserData userData)
        {
            string base64String = behavior.Context.User.Identity.Name;

            return(WsRoot.TryGetUser(base64String, out wsUserName, out userData));
        }
Example #3
0
        public static void AddMinerClientSession(IWsSessionAdapter session)
        {
            if (!WsRoot.TryGetUser(session.AuthorizationBase64, out WsUserName wsUserName, out UserData userData, out string errMsg))
            {
                session.CloseAsync(WsCloseCode.Normal, errMsg);
                return;
            }
            IMinerClientSession minerSession = MinerClientSession.Create(userData, wsUserName, session.SessionId, WsRoot.WsServer.MinerClientWsSessionsAdapter);

            WsRoot.MinerClientSessionSet.Add(minerSession);
            // 通知WebApiServer节点该矿机Ws连线了
            WsRoot.MinerClientMqSender.SendMinerClientWsOpened(minerSession.LoginName, minerSession.ClientId);
            bool isMinerSignChanged;

            if (!WsRoot.MinerSignSet.TryGetByClientId(wsUserName.ClientId, out MinerSign minerSign))
            {
                // 此时该矿机是第一次在服务端出现
                minerSign = new MinerSign {
                    Id            = LiteDB.ObjectId.NewObjectId().ToString(),
                    ClientId      = wsUserName.ClientId,
                    LoginName     = userData.LoginName,
                    OuterUserId   = wsUserName.UserId,
                    AESPassword   = string.Empty,
                    AESPasswordOn = Timestamp.UnixBaseTime
                };
                isMinerSignChanged = true;
            }
            else
            {
                isMinerSignChanged = minerSign.OuterUserId != wsUserName.UserId || minerSign.LoginName != userData.LoginName;
                if (isMinerSignChanged)
                {
                    minerSign.OuterUserId = wsUserName.UserId;
                    minerSign.LoginName   = userData.LoginName;
                }
            }
            // 通常执行不到,因为用户注册的时候已经生成了RSA公私钥对了
            if (string.IsNullOrEmpty(userData.PublicKey) || string.IsNullOrEmpty(userData.PrivateKey))
            {
                var key = Cryptography.RSAUtil.GetRASKey();
                userData.PublicKey  = key.PublicKey;
                userData.PrivateKey = key.PrivateKey;
                WsRoot.UserMqSender.SendUpdateUserRSAKey(userData.LoginName, key);
            }
            DateTime now = DateTime.Now;

            if (string.IsNullOrEmpty(minerSign.AESPassword) || minerSign.AESPasswordOn.AddDays(1) < now)
            {
                isMinerSignChanged      = true;
                minerSign.AESPassword   = Cryptography.AESUtil.GetRandomPassword();
                minerSign.AESPasswordOn = now;
            }
            session.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.UpdateAESPassword)
            {
                Data = new AESPassword {
                    PublicKey = userData.PublicKey,
                    Password  = Cryptography.RSAUtil.EncryptString(minerSign.AESPassword, userData.PrivateKey)
                }
            }.SignToJson(minerSign.AESPassword));
            if (isMinerSignChanged)
            {
                WsRoot.MinerClientMqSender.SendChangeMinerSign(minerSign);
            }
        }