Ejemplo n.º 1
0
        private void NetworkAcceptDataAsync(IAsyncResult result)
        {
            try {
                var container = (PacketDataContainer)result.AsyncState;

                var len = _socket.EndReceiveFrom(result, ref container.Remote);
                if (!_remotePackets.ContainsKey(container.Remote))
                {
                    if (len != _handshakeIn.Length || !container.ValidateBlock(_handshakeIn))
                    {
                        return;
                    }

                    Logger.Info($"Received handshake packet from [/{container.Remote}], len: {len}");
                    try {
                        Logger.Info($"Response auth success to [/{container.Remote}]");
                        var instanceRemoteState = new RemotePacketState();
                        _remotePackets.Add(container.Remote, instanceRemoteState);
                        _handshakeOut[_handshakeOut.Length - 1] = instanceRemoteState.MagicByte;
                        _socket.SendTo(_handshakeOut, container.Remote);
                    } catch {
                        ;
                    }
                }

                var remoteState = _remotePackets[container.Remote];

                // If user trying to send auth handshake again, response that all ok
                if (container.ValidateBlock(_handshakeIn))
                {
                    _handshakeOut[_handshakeOut.Length - 1] = remoteState.MagicByte;
                    _socket.SendTo(_handshakeOut, container.Remote);
                    return;
                }

                var magicByteRemote = container.Buffer[0];
                if (magicByteRemote != remoteState.MagicByte)
                {
                    Logger.Warn($"Invalid remote magic byte '{magicByteRemote}' instead of '{remoteState.MagicByte}' from [/{container.Remote as IPEndPoint}]");
                    return;
                }

                NetworkRemoteControl.AcceptPacket(remoteState, container);
            } catch (ObjectDisposedException) {
                ;
            } catch (Exception e) {
                Logger.Error("Accept data packet error", e);
            } finally {
                NetworkStartAcceptingData();
            }
        }
Ejemplo n.º 2
0
        public void SendPacket(NetPacket packet, PacketDataContainer dataContainer, RemotePacketState remoteState)
        {
            if (packet == null)
            {
                Logger.Warn("Trying to send empty packet!");
                return;
            }

            var packetId = NetworkRemoteControl.FindPacketIdByType(packet.GetType());

            if (packetId == null)
            {
                Logger.Debug($"Trying to send unknown packet {packet.GetType().FullName}");
                return;
            }

            using (var ms = new MemoryStream()) {
                using (var bw = new BinaryWriter(ms)) {
                    bw.Write((uint)packetId);
                    packet.Write(new StreamWriter(ms), remoteState);
                    _socket.SendTo(ms.GetBuffer(), dataContainer.Remote);
                }
            }
        }
Ejemplo n.º 3
0
        internal static void Main(string[] args)
        {
            _isCanShutdown = false;
            Native.SetConsoleCtrlHandler(ConsoleCtrlCheck, true);
            NetworkRemoteControl.Initialize();

            Console.Title             = "RHMS Client Data Collecting Server";
            Thread.CurrentThread.Name = "main";

            AppLogger = new Logger();
            AppLogger.Initialize();
            Logger.Info("Starting server");
            Logger.Info($"Working directory '{Directory.GetCurrentDirectory()}'");

            BridgeDriver.RegisterLoggerCallback(DriverLoggerCallback);

            KernelDriverInitState = KernelDriverBridge.InitializeEnvironment();
            if (KernelDriverInitState == KernelDriverInitState.RhmsDrvNoError)
            {
                var cpusInfo = Drivers.Presentation.Cpuid.Get();
                var firstCpu = cpusInfo?[0];
                if (firstCpu != null)
                {
                    Console.WriteLine(firstCpu.ToString());
                }
            }

            Logger.Auto(KernelDriverInitState == KernelDriverInitState.RhmsDrvNoError ? DriverLogLevel.Info : DriverLogLevel.Error,
                        $"Load driver state: {KernelDriverInitState}");

            if (KernelDriverInitState != KernelDriverInitState.RhmsDrvNoError)
            {
                if (KernelDriverInitState == KernelDriverInitState.RhmsDriverManagerIncorrectDrvSignature)
                {
                    Logger.Warn("Kernel driver signature checking is enabled in your system, RHMS server is not able to load custom kernel driver.");
                    Logger.Warn("You need to disable signature checking on target subsystem.");
                }
                else
                {
                    Logger.Warn("Kernel driver are failed to initializate, maybe driver signing checks are enabled on target system.");
                }

                Logger.Warn("Some features as collecting physical devices data will not available.");
            }

            CollectingServer = new RhmsCollectingServer();
            Debug.Assert(CollectingServer.GetModuleLoader() != null);

            if (!CollectingServer.LoadSettings())
            {
                Logger.Warn("Unable to load server settings!");
                AppLogger.Shutdown();
                return;
            }

            Logger.Info("Initialize collecting server");
            try {
                if (!CollectingServer.Initialize())
                {
                    Logger.Error("Could not initialize RHMS Collecting Server! Press any key to exit.");
                    Console.ReadLine();
                    return;
                }
            } catch (Exception e) {
                Logger.Error("Could not initialize RHMS Collecting Server, internal error has occured! Press any key to exit.", e);
                Console.ReadLine();
                return;
            }

            Logger.Info("Loading modules");
            CollectingServer.GetModuleLoader().LoadFromFolder(Directory.GetCurrentDirectory() + @"\modules");
            Logger.Info("Loading modules has finished.");

            CollectingServer.StartNetworkUpdates();
            CollectingServer.StartHardwareUpdates();
            _isCanShutdown = true;

            // There's no commands yet, just listen for any line and close
            Console.ReadLine();

            Shutdown();
        }