Ejemplo n.º 1
0
 public static void LoadStart(XmlNode serverNode) {
     List<XmlNode> startUpNodes = GetNodes(serverNode, XmlStrings.StartRoute);
     StartUpCommands =
         startUpNodes.Where(n => n.Attributes[XmlStrings.StartEnabledAttribute].Value.ToLower().Equals("true"))
             .Select(n => n.InnerText).ToArray();
     NLConsole.WriteLine(UIStrings.ConfigStartUpLoadComplete, ConsoleColor.DarkYellow);
 }
Ejemplo n.º 2
0
        public static ServerManager AddServer(Int16 port, ServerManager server)
        {
            Servers.Add(port, server);
            String message = String.Format(UIStrings.ServerAdded, server.Server.GetName(), port);

            NLConsole.WriteLine(message, ConsoleColor.White);
            return(server);
        }
Ejemplo n.º 3
0
        protected override void DefaultAction(CommandPattern command, TcpClient client)
        {
            IPEndPoint endPoint      = client.Client.RemoteEndPoint as IPEndPoint;
            IPAddress  ipaddress     = endPoint.Address;
            String     consoleNotice = String.Format("[{0}] Transmitted an unrecognised command: {1}.", ipaddress, command.Original);

            NLConsole.WriteLine(consoleNotice, ConsoleColor.Red);
            Byte[] response = Encoding.ASCII.GetBytes("UNRECOGNISED ACTION");
            client.GetStream().Write(response, 0, response.Length);
        }
Ejemplo n.º 4
0
 public void Connect()
 {
     if (!IsAlive)
     {
         _listenerThread      = new Thread(new ThreadStart(Listen));
         _listenerThread.Name = Thread.CurrentThread.Name + "." + _server.GetType().Name;
         _listenerThread.Start();
         String message = String.Format(UIStrings.ServerConnected, _server.GetName(), _port);
         NLConsole.WriteLine(message, ConsoleColor.Cyan);
     }
 }
Ejemplo n.º 5
0
        private Boolean CheckHashAction(String[] parameters, TcpClient client)
        {
            IPEndPoint endPoint      = client.Client.RemoteEndPoint as IPEndPoint;
            IPAddress  ipaddress     = endPoint.Address;
            String     consoleNotice = String.Format("[{0}] Executed hash check action.", ipaddress);

            NLConsole.WriteLine(consoleNotice, ConsoleColor.Yellow);
            Byte[] response = Encoding.ASCII.GetBytes("EXECUTED HASH CHECK ACTION");
            client.GetStream().Write(response, 0, response.Length);
            return(true);
        }
Ejemplo n.º 6
0
        private Boolean AddNewServer(String[] parameters)
        {
            Int16 port;

            if (parameters.Length != 2)
            {
                NLConsole.WriteLine(UIStrings.InvNoOfArgs);
                return(false);
            }
            else if (!Int16.TryParse(parameters[0], out port))
            {
                NLConsole.WriteLine(UIStrings.InvPort);
                return(false);
            }
            else if (ServersDirector.Exists(port))
            {
                String message = String.Format(UIStrings.ServerExistsOnPort, port);
                NLConsole.WriteLine(message, ConsoleColor.White);
                return(false);
            }
            else if (!ServersDirector.IsPortAvailable(port))
            {
                String message = String.Format(UIStrings.PortUnavailable, port);
                NLConsole.WriteLine(message, ConsoleColor.White);
                return(false);
            }

            String controllerName = parameters[1];

            if (!controllerName.StartsWith("NL."))
            {
                controllerName = "NL.Server.Controllers." + controllerName;
            }

            Assembly          assembly = Assembly.GetExecutingAssembly();
            IRemoteController controller;

            try {
                controller = (IRemoteController)assembly.CreateInstance(controllerName, true);
            } catch (InvalidCastException e) {
                NLConsole.WriteLine(UIStrings.ControllerDoesNotExist, ConsoleColor.White);
                return(false);
            }

            if (controller == null)
            {
                NLConsole.WriteLine(UIStrings.ControllerDoesNotExist, ConsoleColor.White);
                return(false);
            }

            ServersDirector.AddServer(port, controller);
            return(true);
        }
Ejemplo n.º 7
0
 public static void LoadBeeps(XmlNode serverNode) {
     XmlNode beepOnConnectNode = GetNodes(serverNode, XmlStrings.BeepOnConnectRoute).FirstOrDefault();
     XmlNode beepOnDisconnectNode = GetNodes(serverNode, XmlStrings.BeepOnDisconnectRoute).FirstOrDefault();
     Boolean success = true;
     if (beepOnConnectNode != null) {
         success = !Boolean.TryParse(beepOnConnectNode.InnerText, out BeepOnConnection) ? false : success;
     }
     if (beepOnDisconnectNode != null) {
         success = !Boolean.TryParse(beepOnDisconnectNode.InnerText, out BeepOnDisconnection) ? false : success;
     }
     if (success) NLConsole.WriteLine(UIStrings.ConfigBeepLoadComplete, ConsoleColor.DarkYellow);
     else NLConsole.WriteLine(UIStrings.ConfigBeepLoadFailed, ConsoleColor.DarkRed);
 }
Ejemplo n.º 8
0
        public void Disconnect()
        {
            if (IsAlive)
            {
                foreach (ServerHandle handle in _handles)
                {
                    handle.Die();
                }
                _listener.Stop();
            }
            String message = String.Format(UIStrings.ServerDisconnected, _server.GetName(), _port);

            NLConsole.WriteLine(message, ConsoleColor.Cyan);
        }
Ejemplo n.º 9
0
 public AdminController()
     : base()
 {
     ActionDictionary.Add("DCIP", DisconnectIP);
     ActionDictionary.Add("DCSERVER", DisconnectServer);
     ActionDictionary.Add("CSERVER", ConnectServer);
     ActionDictionary.Add("ACSERVER", AddConnectNewServer);
     ActionDictionary.Add("ADDSERVER", AddNewServer);
     ActionDictionary.Add("DELSERVER", RemoveServer);
     ActionDictionary.Add("LISTSERVERS", ListServers);
     ActionDictionary.Add("DCL", DisableCommand);
     ActionDictionary.Add("CLS", ClearConsole);
     ActionDictionary.Add("EXIT", Exit);
     ActionDictionary.Add("HELP", Help);
     NLConsole.WriteLine(UIStrings.HelpPrompt, ConsoleColor.Red);
 }
Ejemplo n.º 10
0
 private Boolean Help(String[] parameters)
 {
     String[] help = UIStrings.CommandHelp.Split(';');
     for (int i = 0; i < help.Length; i++)
     {
         if (i % 2 == 0)
         {
             NLConsole.WriteLine(help[i], ConsoleColor.DarkRed);
         }
         else
         {
             NLConsole.WriteLine(help[i]);
         }
     }
     return(true);
 }
Ejemplo n.º 11
0
        public static void HashRepository()
        {
            DateTime start = DateTime.UtcNow;

            try {
                currentIndex = FileHashIndex.Create(ServerConfiguration.Repository);
            } catch (IOException e) {
                NLConsole.WriteLine(e.Message.Trim(), ConsoleColor.Red);
                return;
            }

            DateTime end       = DateTime.UtcNow;
            TimeSpan timeTaken = end - start;

            NLConsole.WriteLine(currentIndex.ToString(), ConsoleColor.DarkGreen);
            NLConsole.WriteLine(String.Format(UIStrings.TimeTakenToHash, timeTaken.TotalSeconds));
        }
Ejemplo n.º 12
0
        private Boolean DisconnectIP(String[] parameters)
        {
            if (parameters.Length != 1)
            {
                NLConsole.WriteLine(UIStrings.InvNoOfArgs, ConsoleColor.White);
                return(false);
            }
            IPAddress ipaddress;

            if (!IPAddress.TryParse(parameters[0], out ipaddress))
            {
                NLConsole.WriteLine(UIStrings.InvIPAddress, ConsoleColor.White);
                return(false);
            }
            Int32  disconnected = ServersDirector.DisconnectIP(ipaddress);
            String response     = String.Format(UIStrings.IPDisconnected, disconnected);

            NLConsole.WriteLine(response, ConsoleColor.White);
            return(true);
        }
Ejemplo n.º 13
0
        static int Main(string[] args)
        {
            Thread.CurrentThread.Name = "NL.Server";

            NLConsole.Clear();
            NLConsole.Title(UIStrings.NLConsoleTitle);
            NLConsole.StartCommandLine();

            FileService.HashRepository();

            NLConsole.AddController(new AdminController());
            NLConsole.AddController(new QueryController());

            foreach (String command in ServerConfiguration.StartUpCommands)
            {
                NLConsole.InvokeConsoleCommand(command);
            }

            Close();
            return(0);
        }
Ejemplo n.º 14
0
 private void Listen()
 {
     _listener = new TcpListener(IPAddress.Any, _port);
     _listener.Start();
     while (true)
     {
         TcpClient client;
         try { client = _listener.AcceptTcpClient(); } catch (Exception e) {
             if (!(e is SocketException) || (e as SocketException).ErrorCode != 10004)
             {
                 String message = String.Format(UIStrings.ServerAbnormallyTerminated, _port, e.Message + "(" + e.Source + ")");
                 NLConsole.WriteLine(message, ConsoleColor.Red);
             }
             break;
         }
         ServerHandle handle = new ServerHandle(_server, client);
         handle.OnDeath += (ServerHandle e) => this._handles.Remove(e);
         handle.Start();
         _handles.Add(handle);
     }
 }
Ejemplo n.º 15
0
        private Boolean RemoveServer(String[] parameters)
        {
            Int16 port;

            if (parameters.Length != 1)
            {
                NLConsole.WriteLine(UIStrings.InvNoOfArgs);
                return(false);
            }
            else if (!Int16.TryParse(parameters[0], out port))
            {
                NLConsole.WriteLine(UIStrings.InvPort);
                return(false);
            }
            else if (!ServersDirector.Exists(port))
            {
                String message = String.Format(UIStrings.ServerDoesNotExistOnPort, port);
                NLConsole.WriteLine(message, ConsoleColor.White);
                return(false);
            }
            ServersDirector.RemoveServer(port);
            return(true);
        }
Ejemplo n.º 16
0
        private Boolean DisconnectServer(String[] parameters)
        {
            Int16 port = 0;

            if (parameters.Length != 1)
            {
                NLConsole.WriteLine(UIStrings.InvNoOfArgs, ConsoleColor.White);
                return(false);
            }

            if (parameters[0].Equals("*"))
            {
                ServersDirector.DisconnectAll();
                return(false);
            }

            if (!Int16.TryParse(parameters[0], out port))
            {
                NLConsole.WriteLine(UIStrings.InvPort, ConsoleColor.White);
                return(false);
            }
            else if (!ServersDirector.Exists(port))
            {
                NLConsole.WriteLine(UIStrings.ServerDoesNotExistOnPort, ConsoleColor.White);
                return(false);
            }
            else if (!ServersDirector.IsConnected(port))
            {
                NLConsole.WriteLine(UIStrings.ServerNotConnected, ConsoleColor.White);
                return(false);
            }
            else
            {
                ServersDirector.Disconnect(port);
                return(true);
            }
        }
Ejemplo n.º 17
0
        private void Handle()
        {
            if (EndPoint == null)
            {
                _client.Close();
                return;
            }

            if (ServerConfiguration.BeepOnConnection)
            {
                Console.Beep(1000, 80);
                Console.Beep(1500, 80);
                Console.Beep(2000, 200);
            }

            IPAddress     ipaddress    = EndPoint.Address;
            NetworkStream clientStream = _client.GetStream();

            NLConsole.WriteLine(String.Format(UIStrings.ClientConnected, _controller.GetName(), ipaddress), _controller.ClientConnectedColor);

            int bytesRead = 0;

            Byte[]  messageBytes = new Byte[64];
            Boolean complete     = false;

            while (!ended)
            {
                try { bytesRead = clientStream.Read(messageBytes, 0, messageBytes.Length); }
                catch {
                    if (!ended)
                    {
                        NLConsole.WriteLine(String.Format(UIStrings.ClientTerminated, _controller.GetName(), ipaddress), _controller.ClientTerminatedColor);
                    }
                    break;
                }
                if (bytesRead == 0)
                {
                    complete = true;
                    break;
                }

                String         message = Encoding.ASCII.GetString(messageBytes, 0, bytesRead);
                CommandPattern command = CommandPattern.Create(message);
                _controller.InvokeAction(command, _client);
            }

            _client.Close();
            OnDeath.Invoke(this);

            if (complete)
            {
                NLConsole.WriteLine(String.Format(UIStrings.ClientDisconnected, _controller.GetName(), ipaddress), _controller.ClientDisconnectedColor);
            }

            if (ServerConfiguration.BeepOnDisconnection)
            {
                Console.Beep(2000, 80);
                Console.Beep(1500, 80);
                Console.Beep(1000, 200);
            }
        }
Ejemplo n.º 18
0
 private Boolean CheckHashAction(String[] parameters)
 {
     NLConsole.WriteLine("Executed Hash Check Action", ConsoleColor.White);
     return(true);
 }
Ejemplo n.º 19
0
 private Boolean ListServers(String[] parameters)
 {
     NLConsole.WriteLine(Servers.ServersDirector.ToString(), ConsoleColor.White);
     return(true);
 }
Ejemplo n.º 20
0
 private Boolean ClearConsole(String[] parameters)
 {
     NLConsole.Clear();
     return(true);
 }
Ejemplo n.º 21
0
 private Boolean DisableCommand(String[] parameters)
 {
     NLConsole.StopCommandLine();
     return(true);
 }