internal override async Task StartAsync(Site site) { var name = site.Applications[0].ApplicationPoolName; var pool = ApplicationPools.FirstOrDefault(item => item.Name == name); var fileName = Path.Combine( Environment.GetFolderPath( pool != null && pool.Enable32BitAppOnWin64 ? Environment.SpecialFolder.ProgramFilesX86 : Environment.SpecialFolder.ProgramFiles), "IIS Express", "iisexpress.exe"); if (!File.Exists(fileName)) { fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "IIS Express", "iisexpress.exe"); } var process = new Process { StartInfo = new ProcessStartInfo { FileName = fileName, Arguments = site.CommandLine, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true } }; try { process.Start(); process.WaitForExit(5000); if (process.HasExited) { throw new InvalidOperationException("process terminated"); } site.State = ObjectState.Started; } catch (Exception ex) { throw new COMException( string.Format("cannot start site: {0}, {1}", ex.Message, process.StandardOutput.ReadToEnd())); } finally { site.State = process.HasExited ? ObjectState.Stopped : ObjectState.Started; } }
public async Task LoadAsync() { SortedDictionary <string, List <string> > variables = null; if (Credentials == null) { var lines = File.ReadAllLines("jws.conf"); variables = new SortedDictionary <string, List <string> >(); foreach (var line in lines) { var index = line.IndexOf('#'); var content = index == -1 ? line : line.Substring(0, index); var parts = content.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { continue; } var key = parts[0].Trim().ToLowerInvariant(); var value = parts[1].Trim(); if (variables.ContainsKey(key)) { variables[key].Add(value); } else { variables.Add(key, new List <string> { value }); } } } else { using (var client = GetClient()) { HttpResponseMessage response = await client.GetAsync("api/server/"); if (response.IsSuccessStatusCode) { variables = (SortedDictionary <string, List <string> >) await response.Content.ReadAsAsync(typeof(SortedDictionary <string, List <string> >)); } } } var newPool = new ApplicationPool(null, ApplicationPools); newPool.Name = "DefaultAppPool"; newPool.ManagedRuntimeVersion = variables.Load(new List <string> { "v2.0" }, "runtime")[0]; newPool.ProcessModel.MaxProcesses = long.Parse(variables.Load(new List <string> { "1" }, "httpd.processes")[0]); newPool.ProcessModel.UserName = variables.Load(new List <string> { string.Empty }, "httpd.user")[0]; newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser; ApplicationPools.Add(newPool); SiteFolder = variables.Load(new List <string> { "siteconf" }, "siteconfigdir")[0]; LogFolder = variables.Load(new List <string> { "log" }, "sitelogdir")[0]; _certificateCache = variables.Load(new List <string> { string.Empty }, "certificatefile")[0]; _keyCache = variables.Load(new List <string> { string.Empty }, "certificatekeyfile")[0]; IEnumerable <string> sites = null; if (Credentials == null) { sites = Directory.GetFiles(SiteFolder).Where(name => !name.Contains("_")); } else { using (var client = GetClient()) { HttpResponseMessage response = await client.GetAsync("api/site/"); if (response.IsSuccessStatusCode) { sites = (List <string>) await response.Content.ReadAsAsync(typeof(List <string>)); } } } long count = 0; Debug.Assert(sites != null, "sites != null"); foreach (var file in sites) { var site = Credentials == null ? new Site(Sites) { Name = Path.GetFileName(file), Id = count } : new Site(Sites) { Name = file, Id = count }; Sites.Add(site); count = await LoadAsync(site, count, file, SiteFolder); } Extra = variables; }
private void StartInner(Site site, bool restart) { var name = site.Applications[0].ApplicationPoolName; var pool = ApplicationPools.FirstOrDefault(item => item.Name == name); var fileName = Path.Combine( Environment.GetFolderPath( pool != null && pool.Enable32BitAppOnWin64 ? Environment.SpecialFolder.ProgramFilesX86 : Environment.SpecialFolder.ProgramFiles), "IIS Express", "iisexpress.exe"); if (!File.Exists(fileName)) { fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "IIS Express", "iisexpress.exe"); } var temp = Path.GetTempFileName(); using (var process = new Process()) { var start = process.StartInfo; start.Verb = site.Bindings.ElevationRequired && !PublicNativeMethods.IsProcessElevated ? "runas" : null; start.FileName = "cmd"; var extra = restart ? "/r" : string.Empty; start.Arguments = $"/c \"\"{Path.Combine(Environment.CurrentDirectory, "certificateinstaller.exe")}\" /launcher:\"{fileName}\" /config:\"{site.FileContext.FileName}\" /siteId:{site.Id} /resultFile:\"{temp}\"\" {extra}"; start.CreateNoWindow = true; start.WindowStyle = ProcessWindowStyle.Hidden; InjectEnvironmentVariables(site, start); try { process.Start(); process.WaitForExit(); if (process.ExitCode == 0) { site.State = ObjectState.Started; } else if (process.ExitCode == 1) { throw new InvalidOperationException("The process has terminated"); } } catch (Win32Exception ex) { // elevation is cancelled. if (ex.NativeErrorCode != NativeMethods.ErrorCancelled) { throw new COMException( $"cannot start site: {ex.Message}, {File.ReadAllText(temp)}"); } throw new COMException( $"site start cancelled: {ex.Message}, {File.ReadAllText(temp)}"); } catch (Exception ex) { throw new COMException( $"cannot start site: {ex.Message}, {File.ReadAllText(temp)}"); } finally { site.State = process.ExitCode == 0 ? ObjectState.Started : ObjectState.Stopped; } } }