public void Start(int MaxThread) { semaphore = new Semaphore(MaxThread, MaxThread); Language.Initialize(GlobalConfiguration.Language); //Register Listener from beginning. RegisterProcessUnit(TrustedInstallerAuth, new LogUnit()); //Add listening prefixes foreach (var item in GlobalConfiguration.GetListenPrefixes(TrustedInstallerAuth)) { Listener.Prefixes.Add(item.Replace("\\", "/")); } //Load process units. { foreach (var item in GlobalConfiguration.ListTSDRoot(TrustedInstallerAuth, 0)) { if (item.Value == "LWMS.Core.dll") { foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 0, item.Value)) { var t = Type.GetType(UnitTypeName.Value); RegisterProcessUnit(TrustedInstallerAuth, (IPipedProcessUnit)Activator.CreateInstance(t)); } } else { try { FileInfo AssemblyFile = new FileInfo(item.Value); //var asm = Assembly.LoadFrom(AssemblyFile.FullName); var asm = DomainManager.LoadFromFile(TrustedInstallerAuth, AssemblyFile.FullName); foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 0, item.Value)) { var t = asm.GetType(UnitTypeName.Value); RegisterProcessUnit(TrustedInstallerAuth, Activator.CreateInstance(t) as IPipedProcessUnit); } } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Pipeline.Error.Register.R", "Cannot load R pipeline units from: {0}", item.Value)); } } } } RegisterProcessUnit(TrustedInstallerAuth, new ErrorResponseUnit()); //Load W process units. { foreach (var item in GlobalConfiguration.ListTSDRoot(TrustedInstallerAuth, 1)) { if (item.Value == "LWMS.Core.dll") { foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 1, item.Value)) { var t = Type.GetType(UnitTypeName.Value); RegisterWProcessUnit(TrustedInstallerAuth, (IPipedProcessUnit)Activator.CreateInstance(t)); } } else { try { FileInfo AssemblyFile = new FileInfo(item.Value); // var asm = Assembly.LoadFrom(AssemblyFile.FullName); var asm = DomainManager.LoadFromFile(TrustedInstallerAuth, AssemblyFile.FullName); foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 1, item.Value)) { var t = asm.GetType(UnitTypeName.Value); RegisterWProcessUnit(TrustedInstallerAuth, Activator.CreateInstance(t) as IPipedProcessUnit); } } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Pipeline.Error.Register.W", "Cannot load W pipeline units from: {0}", item.Value)); } } } } { { foreach (var item in GlobalConfiguration.ListTSDRoot(TrustedInstallerAuth, 2)) { if (item.Value == "LWMS.Management.dll") { var asm = Assembly.GetAssembly(typeof(Output)); foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 2, item.Value)) { var t = asm.GetType(UnitTypeName.Value); RegisterCmdOutProcessUnit(TrustedInstallerAuth, (IPipedProcessUnit)Activator.CreateInstance(t)); } } else if (item.Value == "LWMS.Core.RemoteShell.Server.dll") { var asm = Assembly.GetAssembly(typeof(RSServer)); foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 2, item.Value)) { var t = asm.GetType(UnitTypeName.Value); RegisterCmdOutProcessUnit(TrustedInstallerAuth, (IPipedProcessUnit)Activator.CreateInstance(t)); } } else { try { FileInfo AssemblyFile = new FileInfo(item.Value); //var asm = Assembly.LoadFrom(AssemblyFile.FullName); var asm = DomainManager.LoadFromFile(TrustedInstallerAuth, AssemblyFile.FullName); foreach (var UnitTypeName in GlobalConfiguration.ListTSDChild(TrustedInstallerAuth, 2, item.Value)) { var t = asm.GetType(UnitTypeName.Value); RegisterCmdOutProcessUnit(TrustedInstallerAuth, Activator.CreateInstance(t) as IPipedProcessUnit); } } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Pipeline.Error.Register.CmdOut", "Cannot load CmdOut pipeline units from: {0}", item.Value)); } } } } } //Apply units ApplyProcessUnits(TrustedInstallerAuth); ApplyWProcessUnits(TrustedInstallerAuth); ApplyCmdProcessUnits(TrustedInstallerAuth); { //Catach all exceptions. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } Listener.Start(); Task.Run(() => { while (WillStop == false) { if (isSuspend == false) { semaphore.WaitOne(); //var __ = await Listener.GetContextAsync(); if (Listener != null) { if (Listener.IsListening) { try { var a = Listener.BeginGetContext(new AsyncCallback((IAsyncResult r) => { try { var __ = ((HttpListener)r.AsyncState).EndGetContext(r); ProcessContext_Internal(__); semaphore.Release(1); } catch (Exception) { } }), Listener); a.AsyncWaitHandle.WaitOne(); } catch (Exception) { } } } //if (isSuspend == true) //{ // __.Response.Close(); // continue; //} //_ = Task.Run(() => // { // }); } else { Thread.Sleep(1); } } }); //Load Manage Modules LoadCommandsFromManifest(); LoadRemoteShell(); //Start invoke scheduled tasks. TaskRunner.StartRun(); }
/// <summary> /// Response to the command packs. /// </summary> /// <param name="args"></param> public static void Control(string Auth, params CommandPack[] args) { Trace.WriteLine(Language.Query("LWMS.Commands.ReceieveCommand", "Received Command:", args[0])); if (!OperatorAuthentication.IsAuthed(Auth, "Basic.ExecuteCommand")) { var name = OperatorAuthentication.GetAuthIDFromAuth(Auth); Trace.WriteLine(Language.Query("LWMS.Command.AuthReject", "Operation rejected: auth {0} have no permission.", name == null?Auth:name)); return; } if (args[0].ToUpper() == "SHUTDOWN" || args[0].ToUpper() == "EXIT" || args[0].ToUpper() == "CLOSE") { try { OperatorAuthentication.AuthedAction(Auth, () => { Output.WriteLine(Language.Query("LWMS.Goodbye", "Goodbye."), Auth); if (LWMSTraceListener.WriteToFile) { LWMSTraceListener.FlushImmediately(); } Environment.Exit(0); }, false, false, "ServerControl.Shutdown", "ServerControl.All"); } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Auth.Reject", "Operation rejected: auth {0} have no permission of {1}.", Auth, "ServerControl.Shutdown")); } } else if (args[0].ToUpper() == "VER" || args[0].ToUpper() == "VERSION") { Output.WriteLine("", Auth); Output.WriteLine(Language.Query("LWMS.Commands.Ver.Shell", "Shell: {0}", Assembly.GetEntryAssembly().GetName().Version.ToString()), Auth); Output.WriteLine(Language.Query("LWMS.Commands.Ver.Core", "Core: {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString()), Auth); Output.WriteLine("", Auth); } else if (args[0].ToUpper() == "CLS" || args[0].ToUpper() == "CLEAR") { Output.Clear(Auth); //Console.Clear(); } else if (args[0].ToUpper() == "SUSPEND") { try { OperatorAuthentication.AuthedAction(Auth, () => { if (LWMSCoreServer.Listener != null) { LWMSCoreServer.Listener.Abort(); LWMSCoreServer.Listener.Close(); LWMSCoreServer.Listener = null; LWMSCoreServer.isSuspend = true; Output.WriteLine(Language.Query("Server.Suspended", "Listener is now suspended."), Auth); } }, false, false, "ServerControl.Suspend", "ServerControl.ListenerControl", "ServerControl.All"); } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Auth.Reject", "Operation rejected: auth {0} have no permission of {1}.", Auth, "ServerControl.Suspend")); } } else if (args[0].ToUpper() == "RESUME") { //LWMSCoreServer.Listener.Start(); // I do not know why HttpListener.Start() will not resume. try { OperatorAuthentication.AuthedAction(Auth, () => { if (LWMSCoreServer.Listener == null) { LWMSCoreServer.Listener = new System.Net.HttpListener(); foreach (var item in GlobalConfiguration.GetListenPrefixes(LWMSCoreServer.TrustedInstallerAuth)) { LWMSCoreServer.Listener.Prefixes.Add(item); } LWMSCoreServer.Listener.Start(); LWMSCoreServer.isSuspend = false; Output.WriteLine(Language.Query("Server.Resumed", "Listener is now resumed."), Auth); } }, false, false, "ServerControl.Resume", "ServerControl.ListenerControl", "ServerControl.All"); } catch (Exception) { Trace.WriteLine(Language.Query("LWMS.Auth.Reject", "Operation rejected: auth {0} have no permission of {1}.", Auth, "ServerControl.Resume")); } } else { foreach (var item in ManageCommands) { if (item.Key.ToUpper() == args[0].PackTotal.ToUpper()) { List <CommandPack> ManageCommandArgs = new List <CommandPack>(args); try { ManageCommandArgs.RemoveAt(0); try { (item.Value.TargetObject as IManageCommand).Invoke(Auth, ManageCommandArgs.ToArray()); } catch (Exception e) { Trace.Write($"Error in {item.Value}: {e}"); } } catch (Exception) { } return; } } foreach (var item in ManageCommandAliases) { if (item.Key.ToUpper() == args[0].PackTotal.ToUpper()) { List <CommandPack> ManageCommandArgs = new List <CommandPack>(args); try { ManageCommandArgs.RemoveAt(0); try { (item.Value.TargetObject as IManageCommand).Invoke(Auth, ManageCommandArgs.ToArray()); } catch (Exception e) { Output.SetForegroundColor(ConsoleColor.Red, Auth); Output.Write($"Error in {item.Value}: {e}", Auth); Output.ResetColor(Auth); } } catch (Exception) { } return; } } Output.WriteLine(Language.Query("LWMS.Commands.Error.NotFound", "Command Not Found."), Auth); } }