public static void Start(Connection connection)
        {
            var obj    = new ConnectionKeepAliveThread(connection);
            var thread = new Thread(new ThreadStart(obj.DoWork));

            thread.Start();
        }
Esempio n. 2
0
        public async Task Listen()
        {
            var ps = new PipeSecurity();

            ps.AddAccessRule(new PipeAccessRule(
                                 new SecurityIdentifier(_allowedSid),
                                 PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance,
                                 AccessControlType.Allow));

            var pipeName = GetPipeName(_allowedSid, _allowedPid);

            Logger.Instance.Log($"Using named pipe {pipeName}.", LogLevel.Debug);

            Logger.Instance.Log($"Access allowed only for ProcessID {_allowedPid} and childs", LogLevel.Debug);

            while (!cancellationTokenSource.IsCancellationRequested)
            {
                using (NamedPipeServerStream dataPipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, MAX_SERVER_INSTANCES,
                                                                                  PipeTransmissionMode.Message, PipeOptions.Asynchronous, GlobalSettings.BufferSize, GlobalSettings.BufferSize, ps))
                {
                    using (NamedPipeServerStream controlPipe = new NamedPipeServerStream(pipeName + "_control", PipeDirection.InOut, MAX_SERVER_INSTANCES,
                                                                                         PipeTransmissionMode.Message, PipeOptions.Asynchronous, GlobalSettings.BufferSize, GlobalSettings.BufferSize, ps))
                    {
                        Logger.Instance.Log("NamedPipeServer listening.", LogLevel.Debug);
                        Task.WaitAll(
                            new Task[]
                        {
                            dataPipe.WaitForConnectionAsync(cancellationTokenSource.Token),
                            controlPipe.WaitForConnectionAsync(cancellationTokenSource.Token),
                        },
                            cancellationTokenSource.Token
                            );

                        if (dataPipe.IsConnected && controlPipe.IsConnected && !cancellationTokenSource.IsCancellationRequested)
                        {
                            var connection = new Connection()
                            {
                                ControlStream = controlPipe, DataStream = dataPipe
                            };

                            ConnectionKeepAliveThread.Start(connection);

                            Logger.Instance.Log("Incoming Connection.", LogLevel.Info);

                            var clientPid = dataPipe.GetClientProcessId();

                            if (!IsAuthorized(clientPid, _allowedPid))
                            {
                                Logger.Instance.Log($"Unauthorized access from PID {clientPid}", LogLevel.Warning);

                                await controlPipe.WriteAsync($"{Constants.TOKEN_ERROR}Unauthorized.{Constants.TOKEN_ERROR}").ConfigureAwait(false);

                                await controlPipe.FlushAsync().ConfigureAwait(false);

                                controlPipe.WaitForPipeDrain();

                                dataPipe.Disconnect();
                                controlPipe.Disconnect();

                                // kill the server.
                                return;
                            }

                            ConnectionAccepted?.Invoke(this, connection);

                            while (connection.IsAlive)
                            {
                                await Task.Delay(10).ConfigureAwait(false);
                            }

                            ConnectionClosed?.Invoke(this, connection);
                        }

                        Logger.Instance.Log("Listener Closed.", LogLevel.Debug);
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task Listen()
        {
            var ps = new PipeSecurity();

            ps.AddAccessRule(new PipeAccessRule(
                                 new SecurityIdentifier(_allowedSid),
                                 PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance,
                                 AccessControlType.Allow));

            var networkSid = new SecurityIdentifier("S-1-5-2");

            // deny remote connections.
            ps.AddAccessRule(new PipeAccessRule(
                                 networkSid,
                                 PipeAccessRights.FullControl,
                                 System.Security.AccessControl.AccessControlType.Deny));

            var pipeName = NamedPipeNameFactory.GetPipeName(_allowedSid, _allowedPid);

            Logger.Instance.Log($"Listening on named pipe {pipeName}.", LogLevel.Debug);

            Logger.Instance.Log($"Access allowed only for ProcessID {_allowedPid} and children", LogLevel.Debug);

            _ = Task.Factory.StartNew(CancelIfAllowedProcessEnds, _cancellationTokenSource.Token,
                                      TaskCreationOptions.LongRunning, TaskScheduler.Current);

            do
            {
                using (NamedPipeServerStream dataPipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, MAX_SERVER_INSTANCES,
                                                                                  PipeTransmissionMode.Message, PipeOptions.Asynchronous, Settings.BufferSize, Settings.BufferSize, ps))
                {
                    using (NamedPipeServerStream controlPipe = new NamedPipeServerStream(pipeName + "_control", PipeDirection.InOut, MAX_SERVER_INSTANCES,
                                                                                         PipeTransmissionMode.Message, PipeOptions.Asynchronous, Settings.BufferSize, Settings.BufferSize, ps))
                    {
                        Logger.Instance.Log("NamedPipeServer listening.", LogLevel.Debug);
                        Task.WaitAll(
                            new Task[]
                        {
                            dataPipe.WaitForConnectionAsync(_cancellationTokenSource.Token),
                            controlPipe.WaitForConnectionAsync(_cancellationTokenSource.Token),
                        },
                            _cancellationTokenSource.Token
                            );

                        if (dataPipe.IsConnected && controlPipe.IsConnected && !_cancellationTokenSource.IsCancellationRequested)
                        {
                            var connection = new Connection()
                            {
                                ControlStream = controlPipe, DataStream = dataPipe
                            };

                            ConnectionKeepAliveThread.Start(connection);

                            Logger.Instance.Log("Incoming Connection.", LogLevel.Info);

                            var clientPid = dataPipe.GetClientProcessId();

                            if (!IsAuthorized(clientPid, _allowedPid))
                            {
                                Logger.Instance.Log($"Unauthorized access from PID {clientPid}", LogLevel.Warning);

                                await controlPipe.WriteAsync($"{Constants.TOKEN_ERROR}Unauthorized.{Constants.TOKEN_ERROR}").ConfigureAwait(false);

                                await controlPipe.FlushAsync().ConfigureAwait(false);

                                controlPipe.WaitForPipeDrain();

                                dataPipe.Disconnect();
                                controlPipe.Disconnect();

                                // kill the server.
                                return;
                            }

                            ConnectionAccepted?.Invoke(this, connection);

                            while (connection.IsAlive)
                            {
                                await Task.Delay(10).ConfigureAwait(false);
                            }

                            ConnectionClosed?.Invoke(this, connection);
                            Logger.Instance.Log("Connection Closed.", LogLevel.Info);
                        }
                    }
                }
            } while (!_singleUse && !_cancellationTokenSource.IsCancellationRequested);
            Logger.Instance.Log("Listener Closed.", LogLevel.Debug);
            _exeLock?.Close();
        }