Example #1
0
        private void Login(User user, string Id, byte[] Password)
        {
            Authority  userAuthority = Base.Security.GetUserAuthority(Id, Password);
            LoginReply reply         = new LoginReply(user.ClientId, userAuthority, BaseConfiguration.ServerVersion);

            if (userAuthority > Authority.None)
            {
                user.Login(Id);
                user.Send <LoginReply>(reply);
                ClientInfoMessage clientInfo = Base.ClientServer.GetClientInfo();
                lock (this.log)
                {
                    clientInfo.AddLog(this.log);
                }
                user.Send <ClientInfoMessage>(clientInfo);
                foreach (Emergency.EmergencyInformation information in Base.Emergency.Emergencies)
                {
                    EmergencyCallMessage message = new EmergencyCallMessage();
                    message.AddEmergencyCallInfo(information.Department, information.ID, information.Name, information.PhoneNumber, information.Mail, information.Rank);
                    user.Send <EmergencyCallMessage>(message);
                }
            }
            else
            {
                user.Send <LoginReply>(reply);
            }
        }
Example #2
0
        public ClientInfoMessage GetClientInfo()
        {
            ClientInfoMessage clientInfoMessage = new ClientInfoMessage(Base.WorkGroupString, Base.ServerGroupString);

            foreach (ClientServer.Client client in this.RCClientInfo.Values)
            {
                if (client.RCClient.Initialized)
                {
                    clientInfoMessage.AddClient(client.ID, client.RCClient);
                }
            }
            return(clientInfoMessage);
        }
        public async Task ConnectToServer(string ipAddress, int port)
        {
            //Client.ClientName = serverName;
            await Client.Connect(ipAddress, port);

            await WatchInboundQueue(Client);
            await WatchForInboundNetworkTraffic(Client);
            await WatchForOutboundMessages(Client);

            var clientInfoMessage = new ClientInfoMessage(Client.ClientName)
            {
                MessageId = GetNextMessageId()
            };

            Client.OutboundMessages.Enqueue(clientInfoMessage);

            while (!ConnectedToServer)
            {
                await Task.Delay(1);
            }
        }
Example #4
0
        async Task OnClientInfo(ClientInfoMessage message, ClientConnection connection)
        {
            var clientInfo = message.ClientInfo;

            if (!message.IsMessageValid(clientInfo.ConnectionPublicKey))
            {
                await connection.Close(DisconnectReasons.ProtocolError);

                return;
            }

            connection.ClientInfo = clientInfo;
            if (message.RequestCode != 0)
            {
                if (connection.Token == null)
                {
                    connection.Token = Rand.NextSeed(16);
                }

                await connection.Send(new ClientInfoResponseMessage(message.RequestCode, connection.Token) { SignKey = _configuration.LocaleNodePrivateKey });
            }
        }
 private void ProcessMessage(object rawMessage)
 {
     if (rawMessage is LoginReply)
     {
         LoginReply loginReply = rawMessage as LoginReply;
         if (loginReply.Authority == Authority.None)
         {
             if (this.LoginFail != null)
             {
                 this.LoginFail(this, new EventArgs <Exception>(new Exception("Fail to login")));
             }
             this.Stop();
             return;
         }
         if (this.LoginSuccess != null)
         {
             this.LoginSuccess(this, EventArgs.Empty);
             return;
         }
     }
     else
     {
         if (rawMessage is ClientInfoMessage)
         {
             ClientInfoMessage clientInfoMessage = rawMessage as ClientInfoMessage;
             this.workGroups = WorkGroupStructureNode.GetWorkGroup(clientInfoMessage.WorkGroup);
             using (IEnumerator <KeyValuePair <int, RCClient> > enumerator = clientInfoMessage.Clients.GetEnumerator())
             {
                 while (enumerator.MoveNext())
                 {
                     KeyValuePair <int, RCClient> keyValuePair = enumerator.Current;
                     RCClient rcclient = new RCClient(keyValuePair.Key, keyValuePair.Value.ClientIP);
                     rcclient.AssignFrom(keyValuePair.Value);
                     this.AddClient(rcclient);
                     this.CheckAndAddHeroesAdmin(rcclient);
                 }
                 return;
             }
         }
         if (rawMessage is ClientAddedMessage)
         {
             ClientAddedMessage clientAddedMessage = rawMessage as ClientAddedMessage;
             RCClient           rcclient2          = new RCClient(clientAddedMessage.ID, clientAddedMessage.Client.ClientIP);
             rcclient2.AssignFrom(clientAddedMessage.Client);
             this.AddClient(rcclient2);
             this.CheckAndAddHeroesAdmin(rcclient2);
             return;
         }
         if (rawMessage is ClientRemovedMessage)
         {
             ClientRemovedMessage clientRemovedMessage = rawMessage as ClientRemovedMessage;
             this.RemoveClient(clientRemovedMessage.ID);
             this.CheckAndRemoveHeroesAdmin(clientRemovedMessage.ID);
             return;
         }
         if (rawMessage is ControlReplyMessage)
         {
             ControlReplyMessage controlReplyMessage = rawMessage as ControlReplyMessage;
             if (this.clientList.ContainsKey(controlReplyMessage.ID))
             {
                 this.ProcessClientMessage(new Packet(controlReplyMessage.Packet), this.clientList[controlReplyMessage.ID]);
             }
         }
     }
 }
Example #6
0
        async Task <ClientConnection> Connect(ClientInfo clientInfo)
        {
            try
            {
                await _semaphore.WaitAsync();

                lock (_lock)
                {
                    if (_connection != null)
                    {
                        return(_connection);
                    }
                }

                Log.Trace($"Connecting to {ChainEndPoint}.", this);
                var nodeInfo = (await DownloadNodeInfo()).Data;

                if (nodeInfo != null)
                {
                    foreach (var key in _invalidNetworkKeys)
                    {
                        if (nodeInfo.NetworkKey == key)
                        {
                            Log.Warn($"Could not connect to {ChainEndPoint}, NetworkKey is invalid.");
                            return(null);
                        }
                    }

                    if (_allowedNetworkKeys.Count > 0)
                    {
                        var found = false;
                        foreach (var key in _allowedNetworkKeys)
                        {
                            if (key == nodeInfo.NetworkKey)
                            {
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            Log.Warn($"Could not connect to {ChainEndPoint}, NetworkKey not allowed.");
                            return(null);
                        }
                    }

                    Log.Trace($"Recived NodeInfo, NodeId {nodeInfo.NodeId.HexString}, NetworkKey: {nodeInfo.NetworkKey.HexString}.", this);
                    var connection = await OpenClientConnection();

                    if (connection.Connected)
                    {
                        Log.Trace($"Connection established to {ChainEndPoint} with id {connection.ConnectionId}.", this);
                        connection.NodeInfo = nodeInfo;
                        connection.ConnectionClosedEvent = ConnectionClosed;
                        TaskRunner.Run(() => connection.Receive(this));

                        var m = new ClientInfoMessage(clientInfo)
                        {
                            SignKey = _clientKey
                        };
                        AddAwaitableResponse(m);
                        await connection.Send(m);

                        if (await WaitResponse(m) is ClientInfoResponseMessage response)
                        {
                            lock (_lock)
                            {
                                _connection       = connection;
                                _connection.Token = response.Token;
                                _lastNodeInfo     = connection.NodeInfo;
                                return(_connection);
                            }
                        }

                        await connection.Close(DisconnectReasons.TimeOut);
                    }
                    else
                    {
                        Log.Warn($"Connecting to endpoint {ChainEndPoint} failed: {connection.State}.");
                    }
                }
                else
                {
                    Log.Warn($"Downloading NodeInfo for {ChainEndPoint} failed.", this);
                }
                return(null);
            }
            finally
            {
                _semaphore.Release();
            }
        }
Example #7
0
 private void ProcessMessage(object rawMessage)
 {
     if (rawMessage is LoginReply)
     {
         this.clientList.Clear();
         LoginReply loginReply = rawMessage as LoginReply;
         if (BaseConfiguration.ServerVersion != loginReply.ServerVersion)
         {
             if (this.ConnectionResulted != null)
             {
                 this.ConnectionResulted(this, new EventArgs <RCUserHandler.ConnectionResult>(RCUserHandler.ConnectionResult.VersionMismatch));
             }
             this.Stop();
             return;
         }
         this.Authority = loginReply.Authority;
         if (this.ConnectionResulted != null)
         {
             this.ConnectionResulted(this, new EventArgs <RCUserHandler.ConnectionResult>(RCUserHandler.ConnectionResult.Success));
             return;
         }
     }
     else
     {
         if (rawMessage is EmergencyCallMessage)
         {
             EmergencyCallMessage emergencyCallMessage = rawMessage as EmergencyCallMessage;
             List <string>        value = new List <string>(emergencyCallMessage.Emergencies);
             this.EmergencyCallInfo(this, new EventArgs <List <string> >(value));
             return;
         }
         if (rawMessage is ClientInfoMessage)
         {
             ClientInfoMessage clientInfoMessage = rawMessage as ClientInfoMessage;
             if (this.WorkGroupStructureChange != null)
             {
                 this.WorkGroupStructureChange(this, new RCUserHandler.WorkGroupStructureEventArgs(WorkGroupStructureNode.GetWorkGroup(clientInfoMessage.WorkGroup)));
             }
             if (this.ServerGroupStructureChange != null)
             {
                 this.ServerGroupStructureChange(this, new RCUserHandler.WorkGroupStructureEventArgs(ServerGroupStructureNode.GetServerGroup(clientInfoMessage.ServerGroup)));
             }
             foreach (KeyValuePair <int, RCClient> keyValuePair in clientInfoMessage.Clients)
             {
                 RCClient rcclient = new RCClient(keyValuePair.Key, keyValuePair.Value.ClientIP);
                 rcclient.AssignFrom(keyValuePair.Value);
                 this.clientList.Add(keyValuePair.Key, rcclient);
                 if (this.ClientAdd != null)
                 {
                     this.ClientAdd(this, new EventArgs <RCClient>(rcclient));
                 }
             }
             foreach (NotifyMessage value2 in clientInfoMessage.Logs)
             {
                 this.Notify(this, new EventArgs <NotifyMessage>(value2));
             }
             this.firstUpdated = true;
             return;
         }
         if (rawMessage is GetUserListReply)
         {
             GetUserListReply getUserListReply = rawMessage as GetUserListReply;
             List <Member>    list             = new List <Member>();
             foreach (KeyValuePair <string, Authority> keyValuePair2 in getUserListReply.Users)
             {
                 list.Add(new Member(keyValuePair2.Key, keyValuePair2.Value));
             }
             this.ReceivedUserListReply(this, new EventArgs <IEnumerable <Member> >(list));
             return;
         }
         if (rawMessage is WorkGroupChangeMessage)
         {
             if (this.WorkGroupStructureChange != null)
             {
                 WorkGroupChangeMessage workGroupChangeMessage = rawMessage as WorkGroupChangeMessage;
                 this.WorkGroupStructureChange(this, new RCUserHandler.WorkGroupStructureEventArgs(WorkGroupStructureNode.GetWorkGroup(workGroupChangeMessage.WorkGroup)));
                 return;
             }
         }
         else if (rawMessage is ServerGroupChangeMessage)
         {
             if (this.ServerGroupStructureChange != null)
             {
                 ServerGroupChangeMessage serverGroupChangeMessage = rawMessage as ServerGroupChangeMessage;
                 this.ServerGroupStructureChange(this, new RCUserHandler.WorkGroupStructureEventArgs(ServerGroupStructureNode.GetServerGroup(serverGroupChangeMessage.ServerGroup)));
                 return;
             }
         }
         else if (rawMessage is ClientAddedMessage)
         {
             ClientAddedMessage clientAddedMessage = rawMessage as ClientAddedMessage;
             RCClient           rcclient2          = new RCClient(clientAddedMessage.ID, clientAddedMessage.Client.ClientIP);
             rcclient2.AssignFrom(clientAddedMessage.Client);
             if (this.clientList.ContainsKey(clientAddedMessage.ID))
             {
                 Utility.ShowErrorMessage(LocalizeText.Get(217) + clientAddedMessage.ID);
                 return;
             }
             this.clientList.Add(rcclient2.ID, rcclient2);
             this.ClientAdd(this, new EventArgs <RCClient>(rcclient2));
             return;
         }
         else if (rawMessage is ClientRemovedMessage)
         {
             ClientRemovedMessage clientRemovedMessage = rawMessage as ClientRemovedMessage;
             if (!this.clientList.ContainsKey(clientRemovedMessage.ID))
             {
                 return;
             }
             RCClient value3 = this.clientList[clientRemovedMessage.ID];
             this.clientList.Remove(clientRemovedMessage.ID);
             if (this.ClientRemove != null)
             {
                 this.ClientRemove(this, new EventArgs <RCClient>(value3));
                 return;
             }
         }
         else if (rawMessage is ControlEnterReply)
         {
             if (this.ControlReply != null)
             {
                 ControlEnterReply value4 = rawMessage as ControlEnterReply;
                 this.ControlReply(this, new EventArgs <ControlEnterReply>(value4));
                 return;
             }
         }
         else if (rawMessage is ControlReplyMessage)
         {
             ControlReplyMessage controlReplyMessage = rawMessage as ControlReplyMessage;
             if (this.firstUpdated)
             {
                 if (this.clientList.Count == 0 || (this.clientList.Count > 0 && !this.clientList.ContainsKey(controlReplyMessage.ID)))
                 {
                     Type type = this.MF.GetType(new Packet(controlReplyMessage.Packet));
                     Utility.ShowErrorMessage(string.Concat(new object[]
                     {
                         LocalizeText.Get(220),
                         controlReplyMessage.ID,
                         ":",
                         type.ToString()
                     }));
                     return;
                 }
                 this.ProcessRCClientMessage(this.clientList[controlReplyMessage.ID], controlReplyMessage.Packet);
                 return;
             }
         }
         else if (rawMessage is NotifyMessage)
         {
             if (this.Notify != null)
             {
                 NotifyMessage value5 = rawMessage as NotifyMessage;
                 this.Notify(this, new EventArgs <NotifyMessage>(value5));
                 return;
             }
         }
         else if (rawMessage is ChildProcessLogListReplyMessage)
         {
             if (this.ChildProcessListed != null)
             {
                 ChildProcessLogListReplyMessage childProcessLogListReplyMessage = rawMessage as ChildProcessLogListReplyMessage;
                 if (this.clientList.ContainsKey(childProcessLogListReplyMessage.ClientID))
                 {
                     RCClient sender = this.clientList[childProcessLogListReplyMessage.ClientID];
                     this.ChildProcessListed(sender, new EventArgs <ChildProcessLogListReplyMessage>(childProcessLogListReplyMessage));
                     return;
                 }
             }
         }
         else if (rawMessage is ChildProcessLogReplyMessage)
         {
             if (this.ChildProcessLogOpened != null)
             {
                 ChildProcessLogReplyMessage value6 = rawMessage as ChildProcessLogReplyMessage;
                 this.ChildProcessLogOpened(this, new EventArgs <ChildProcessLogReplyMessage>(value6));
                 return;
             }
         }
         else if (rawMessage is ChildProcessLogMessage)
         {
             if (this.ChildProcessLogged != null)
             {
                 ChildProcessLogMessage value7 = rawMessage as ChildProcessLogMessage;
                 this.ChildProcessLogged(this, new EventArgs <ChildProcessLogMessage>(value7));
                 return;
             }
         }
         else if (rawMessage is ExeInfoReplyMessage && this.ChildProcessListed != null)
         {
             ExeInfoReplyMessage exeInfoReplyMessage = rawMessage as ExeInfoReplyMessage;
             if (this.clientList.ContainsKey(exeInfoReplyMessage.ClientID))
             {
                 RCClient sender2 = this.clientList[exeInfoReplyMessage.ClientID];
                 this.ExeInfo(sender2, new EventArgs <ExeInfoReplyMessage>(exeInfoReplyMessage));
             }
         }
     }
 }