private void InternalStart(object args) { if (!string.IsNullOrEmpty(OptionLcid)) { Extensions.SetCurrentThreadCulture(OptionLcid); } if (_services.Count == 0) { Log("Service Host '" + OptionName + "' has no hosted services configured. Stopping."); return; } if (Configuration.StartServicesAsync) { Log("Service Host '" + OptionName + "' starting asynchronously."); List <IAsyncResult> _results = new List <IAsyncResult>(_services.Count); List <WaitHandle> _waits = new List <WaitHandle>(_services.Count); foreach (IService service in _services) { if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running) { Log("Service '" + service.Name + "' starting"); ServiceCallDelegate d = new ServiceCallDelegate(ServiceStart); IAsyncResult result = d.BeginInvoke(service, null, service); _results.Add(result); _waits.Add(result.AsyncWaitHandle); } } if (_waits.Count > 0) { WaitHandle.WaitAll(_waits.ToArray(), Timeout.Infinite, Configuration.WaitExitContext); } int successCount = 0; foreach (IAsyncResult result in _results) { IService service = (IService)result.AsyncState; if (service.StartException == null) { successCount++; } HandleServiceStarted(service); } if (successCount == 0) { Log("Service Host '" + OptionName + "' has no hosted services successfully started. Stopping."); return; } } else { Log("Service Host '" + OptionName + "' starting"); foreach (IService service in _services) { try { if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running) { Log("Service '" + service.Name + "' starting"); service.Start(); Log("Service '" + service.Name + "' started"); } } catch (Exception e) { service.StartException = e; } HandleServiceStarted(service); } } Log("Service Host '" + OptionName + "' started"); EventLog.WriteEntry("Service Host '" + OptionName + "' was started successfully.", EventLogEntryType.Information); _closed.Reset(); if (!OptionService) { do { ConsoleKeyInfo i; try { i = Console.ReadKey(true); } catch (InvalidOperationException) { // ok, someone has killed the console, bail out break; } if (i.Key == ConsoleKey.Enter || i.Key == ConsoleKey.Escape || i.KeyChar == 'q' || i.KeyChar == 'Q') { break; } if ((i.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control) { if (i.Key == ConsoleKey.Pause || i.KeyChar == 'c' || i.KeyChar == 'C') { break; } } if (i.KeyChar == 'c' || i.KeyChar == 'C') { Console.Clear(); } }while (true); } }
private void ListenerCallback(IAsyncResult result) { //Host.Log(this, "ListenerCallback result: " + result); if (!_listener.IsListening) { return; } if (!string.IsNullOrEmpty(Program.OptionLcid)) { Extensions.SetCurrentThreadCulture(Program.OptionLcid); } _listener.BeginGetContext(ListenerCallback, null); HttpListenerContext context = _listener.EndGetContext(result); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; if (_listener.AuthenticationSchemes != AuthenticationSchemes.Anonymous) { if (!OnAuthenticate(context)) { Host.Log(this, "Request Access is denied"); Write(response, "Access is denied", (int)HttpStatusCode.Unauthorized, "Access is denied"); response.OutputStream.Close(); return; } } //Host.Log(this, "AssemblyDate: " + Extensions.AssemblyDate); //Host.Log(this, "Request LocalEndPoint: " + request.LocalEndPoint); //Host.Log(this, "Request ServiceName: " + request.ServiceName); Host.Log(this, "Request Method: " + request.HttpMethod); //Host.Log(this, "Request RemoteEndPoint: " + request.RemoteEndPoint); Host.Log(this, "Request Url: " + request.Url); //Host.Log(this, "Request RawUrl: " + request.RawUrl); foreach (string name in request.Headers) { Host.Log(this, "Header " + name + ": " + request.Headers[name]); } if (request.Url == null) { return; } Host.Log(this, "Request RelativeUrl: " + ServiceSection.Current.WebServer.Prefixes.GetRelativePath(request.RawUrl)); try { context.Response.AddHeader("Server", "TraceSpyServer/" + Assembly.GetExecutingAssembly().GetInformationalVersion() + "/"); ProcessRequest(context); } catch (HttpListenerException he) { Host.Log(this, "HttpListenerException (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he); if (IsDeadClient(he)) { // don't log this Host.Log(this, "Client is dead."); return; } Write(response, he); } catch (Exception e) { Host.Log(this, "Exception: " + e); Write(response, e); } try { response.OutputStream.Close(); } catch (HttpListenerException he) { // client may have disappeared Host.Log(this, "HttpListenerException (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he.Message); } catch (Exception e) { Host.Log(this, "write Exception:" + e.Message); } }
static void SafeMain(string[] args) { AddERExcludedApplication(Process.GetCurrentProcess().MainModule.ModuleName); Console.WriteLine("TraceSpy Service - " + (Environment.Is64BitProcess ? "64" : "32") + "-bit - Build Number " + Assembly.GetExecutingAssembly().GetInformationalVersion()); Console.WriteLine("Copyright (C) SoftFluent S.A.S 2012-" + DateTime.Now.Year + ". All rights reserved."); var token = Extensions.GetTokenElevationType(); if (token != TokenElevationType.Full) { Console.WriteLine(""); Console.WriteLine("Warning: token elevation type (UAC level) is " + token + ". You may experience access denied errors from now on. You may fix these errors if you restart with Administrator rights or without UAC."); Console.WriteLine(""); } OptionHelp = CommandLineUtilities.GetArgument(args, "?", false); if (!OptionHelp) { OptionHelp = CommandLineUtilities.GetArgument(args, "h", false); if (!OptionHelp) { OptionHelp = CommandLineUtilities.GetArgument(args, "help", false); } } OptionService = CommandLineUtilities.GetArgument(args, "s", false); if (!OptionService) { if (OptionHelp) { Console.WriteLine("Format is " + Assembly.GetExecutingAssembly().GetName().Name + ".exe [options]"); Console.WriteLine("[options] can be a combination of the following:"); Console.WriteLine(" /? Displays this help"); Console.WriteLine(" /i Installs the <name> service"); Console.WriteLine(" /k Kills this process on any exception"); Console.WriteLine(" /u Uninstalls the <name> service"); Console.WriteLine(" /t Displays traces on the console"); Console.WriteLine(" /l:<name> Locale used"); Console.WriteLine(" default is " + CultureInfo.CurrentCulture.LCID); Console.WriteLine(" /name:<name> (Un)Installation uses <name> for the service name"); Console.WriteLine(" default is \"" + DefaultName + "\""); Console.WriteLine(" /displayName:<dname> (Un)Installation uses <dname> for the display name"); Console.WriteLine(" default is \"" + DefaultDisplayName + "\""); Console.WriteLine(" /description:<desc.> Installation ses <desc.> for the service description"); Console.WriteLine(" default is \"" + DefaultDisplayName + "\""); Console.WriteLine(" /startType:<type> Installation uses <type> for the service start mode"); Console.WriteLine(" default is \"" + ServiceStartMode.Manual + "\""); Console.WriteLine(" Values are " + ServiceStartMode.Automatic + ", " + ServiceStartMode.Disabled + " or " + ServiceStartMode.Manual); Console.WriteLine(" /user:<name> Name of the account under which the service should run"); Console.WriteLine(" default is Local System"); Console.WriteLine(" /password:<text> Password to the account name"); Console.WriteLine(" /config:<path> Path to the configuration file"); Console.WriteLine(" /dependson:<list> A comma separated list of service to depend on"); Console.WriteLine(""); Console.WriteLine("Examples:"); Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " /i /name:MyService /displayName:\"My Service\" /startType:Automatic"); Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " /u /name:MyOtherService"); return; } } OptionTrace = CommandLineUtilities.GetArgument(args, "t", false); OptionKillOnException = CommandLineUtilities.GetArgument(args, "k", false); OptionInstall = CommandLineUtilities.GetArgument(args, "i", false); OptionStartType = (ServiceStartMode)CommandLineUtilities.GetArgument(args, "starttype", ServiceStartMode.Manual); OptionLcid = CommandLineUtilities.GetArgument <string>(args, "l", null); OptionUninstall = CommandLineUtilities.GetArgument(args, "u", false); OptionAccount = (ServiceAccount)CommandLineUtilities.GetArgument(args, "user", ServiceAccount.User); OptionPassword = CommandLineUtilities.GetArgument <string>(args, "password", null); OptionUser = CommandLineUtilities.GetArgument <string>(args, "user", null); string dependsOn = CommandLineUtilities.GetArgument <string>(args, "dependson", null); if (!string.IsNullOrEmpty(dependsOn)) { OptionDependsOn = dependsOn.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } else { OptionDependsOn = null; } OptionConfigPath = CommandLineUtilities.GetArgument <string>(args, "config", null); if (!string.IsNullOrEmpty(OptionConfigPath) && !Path.IsPathRooted(OptionConfigPath)) { OptionConfigPath = Path.GetFullPath(OptionConfigPath); } _configuration = ServiceSection.Get(OptionConfigPath); OptionDisplayName = CommandLineUtilities.GetArgument(args, "displayname", DefaultDisplayName); OptionDescription = CommandLineUtilities.GetArgument(args, "description", DefaultDescription); if (OptionInstall) { ServiceInstaller si = new ServiceInstaller(); ServiceProcessInstaller spi = new ServiceProcessInstaller(); si.ServicesDependedOn = OptionDependsOn; Console.WriteLine("OptionAccount=" + OptionAccount); Console.WriteLine("OptionUser="******"Password cannot be empty if Account is set to User."); return; } spi.Username = OptionUser; spi.Password = OptionPassword; } } else { spi.Account = OptionAccount; } si.Parent = spi; si.DisplayName = OptionDisplayName; si.Description = OptionDescription; si.ServiceName = OptionName; si.StartType = OptionStartType; si.Context = new InstallContext(Assembly.GetExecutingAssembly().GetName().Name + ".install.log", null); string asmpath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, AppDomain.CurrentDomain.FriendlyName); // TODO: add instance specific parameters here (ports, etc...) string binaryPath = "\"" + asmpath + "\"" // exe path + " /s" // we run as a service + " /name:" + OptionName; // our name if (!string.IsNullOrEmpty(OptionConfigPath)) { binaryPath += " /c:\"" + OptionConfigPath + "\""; } si.Context.Parameters["assemblypath"] = binaryPath; IDictionary stateSaver = new Hashtable(); si.Install(stateSaver); // see remarks in the function FixServicePath(si.ServiceName, binaryPath); return; } if (OptionUninstall) { ServiceInstaller si = new ServiceInstaller(); ServiceProcessInstaller spi = new ServiceProcessInstaller(); si.Parent = spi; si.ServiceName = OptionName; si.Context = new InstallContext(Assembly.GetExecutingAssembly().GetName().Name + ".uninstall.log", null); si.Uninstall(null); return; } if (!OptionService) { if (OptionTrace) { Trace.Listeners.Add(new ConsoleListener()); } if (!string.IsNullOrEmpty(OptionLcid)) { Extensions.SetCurrentThreadCulture(OptionLcid); } Console.WriteLine("Console Mode"); Console.WriteLine("Service Host name: " + OptionName); WindowsIdentity identity = WindowsIdentity.GetCurrent(); Console.WriteLine("Service Host identity: " + (identity != null ? identity.Name : "null")); Console.WriteLine("Service Host bitness: " + (IntPtr.Size == 4 ? "32-bit" : "64-bit")); Console.WriteLine("Service Host display name: '" + OptionDisplayName + "'"); Console.WriteLine("Service Host event log source: " + _service.EventLog.Source); Console.WriteLine("Service Host trace enabled: " + OptionTrace); Console.WriteLine("Service Host administrator mode: " + IsAdministrator()); string configPath = OptionConfigPath; if (configPath == null) { configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; } Console.WriteLine("Service Host config file path: " + configPath); Console.WriteLine("Service Host current locale: " + Thread.CurrentThread.CurrentCulture.LCID + " (" + Thread.CurrentThread.CurrentCulture.Name + ")"); Console.Title = OptionDisplayName; ConsoleControl cc = new ConsoleControl(); cc.Event += OnConsoleControlEvent; _service.InternalStart(args); if (!_stopping) { _service.InternalStop(); } else { int maxWaitTime = Configuration.ConsoleCloseMaxWaitTime; if (maxWaitTime <= 0) { maxWaitTime = Timeout.Infinite; } _closed.WaitOne(maxWaitTime, Configuration.WaitExitContext); } return; } ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { _service }; Run(ServicesToRun); }