Esempio n. 1
0
        protected override void Execute()
        {
            using (TcpConnection Connection = new TcpConnection())
            {
                if (Connection.Listen(_LocalAddress, _LocalPort))
                {
                    RaiseBoundEvent();

                    while (!_Stop)
                    {
                        // Accept an incoming connection
                        if (Connection.CanAccept(1000)) // 1 second
                        {
                            try
                            {
                                TcpConnection NewConnection = Connection.AcceptTCP();
                                if (NewConnection != null)
                                {
                                    TcpConnection TypedConnection = null;
                                    switch (_ConnectionType)
                                    {
                                    case ConnectionType.RLogin:
                                        TypedConnection = new RLoginConnection();
                                        break;

                                    case ConnectionType.Telnet:
                                        TypedConnection = new TelnetConnection();
                                        break;

                                    case ConnectionType.WebSocket:
                                        TypedConnection = new WebSocketConnection();
                                        break;
                                    }
                                    if (TypedConnection != null)
                                    {
                                        TypedConnection.Open(NewConnection.GetSocket());

                                        if (IsIgnoredIP(TypedConnection.GetRemoteIP()))
                                        {
                                            // Do nothing for ignored IPs
                                            TypedConnection.Close();
                                        }
                                        else
                                        {
                                            RaiseMessageEvent("Incoming " + _ConnectionType.ToString() + " connection from " + TypedConnection.GetRemoteIP() + ":" + TypedConnection.GetRemotePort());

                                            TerminalType TT = GetTerminalType(TypedConnection);
                                            if (IsBannedIP(TypedConnection.GetRemoteIP()))
                                            {
                                                DisplayAnsi("IP_BANNED", TypedConnection, TT);
                                                RaiseWarningMessageEvent("IP " + TypedConnection.GetRemoteIP() + " matches banned IP filter");
                                                TypedConnection.Close();
                                            }
                                            else if (_Paused)
                                            {
                                                DisplayAnsi("SERVER_PAUSED", TypedConnection, TT);
                                                TypedConnection.Close();
                                            }
                                            else
                                            {
                                                if (!TypedConnection.Connected)
                                                {
                                                    RaiseMessageEvent("No carrier detected (maybe it was a 'ping'?)");
                                                    TypedConnection.Close();
                                                }
                                                else
                                                {
                                                    ClientThread NewClientThread = new ClientThread();
                                                    int          NewNode         = RaiseConnectEvent(ref NewClientThread);
                                                    if (NewNode == 0)
                                                    {
                                                        NewClientThread.Dispose();
                                                        DisplayAnsi("SERVER_BUSY", TypedConnection, TT);
                                                        TypedConnection.Close();
                                                    }
                                                    else
                                                    {
                                                        NewClientThread.Start(NewNode, TypedConnection, _ConnectionType, TT);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                RaiseExceptionEvent("Error in ServerThread::Execute()", ex);
                            }
                        }
                    }
                }
                else
                {
                    RaiseErrorMessageEvent("Server Thread unable to listen on " + _LocalAddress + ":" + _LocalPort);
                    RaiseBindFailedEvent();
                }
            }
        }