/// <summary> /// Checks whether pwsh has been started as a login shell /// and if so, proceeds with the login process. /// This method will return early if pwsh was not started as a login shell /// and will throw if it detects a native call has failed. /// In the event of success, we use an exec() call, so this method never returns. /// </summary> /// <param name="args">The startup arguments to pwsh.</param> private static void AttemptExecPwshLogin(string[] args) { // If the login environment variable is set, we have already done the login logic and have been exec'd if (Environment.GetEnvironmentVariable(LOGIN_ENV_VAR_NAME) != null) { Environment.SetEnvironmentVariable(LOGIN_ENV_VAR_NAME, null); return; } bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); // The first byte (ASCII char) of the name of this process, used to detect '-' for login byte procNameFirstByte; // The path to the executable this process was started from string pwshPath; // On Linux, we can simply use the /proc filesystem if (isLinux) { // Read the process name byte using (FileStream fs = File.OpenRead("/proc/self/cmdline")) { procNameFirstByte = (byte)fs.ReadByte(); } // Run login detection logic if (!IsLogin(procNameFirstByte, args)) { return; } // Read the symlink to the startup executable IntPtr linkPathPtr = Marshal.AllocHGlobal(LINUX_PATH_MAX); IntPtr bufSize = ReadLink("/proc/self/exe", linkPathPtr, (UIntPtr)LINUX_PATH_MAX); pwshPath = Marshal.PtrToStringAnsi(linkPathPtr, (int)bufSize); Marshal.FreeHGlobal(linkPathPtr); // exec pwsh ThrowOnFailure("exec", ExecPwshLogin(args, pwshPath, isMacOS: false)); return; } // At this point, we are on macOS // Set up the mib array and the query for process maximum args size Span <int> mib = stackalloc int[3]; int mibLength = 2; mib[0] = MACOS_CTL_KERN; mib[1] = MACOS_KERN_ARGMAX; int size = IntPtr.Size / 2; int argmax = 0; // Get the process args size unsafe { fixed(int *mibptr = mib) { ThrowOnFailure(nameof(argmax), SysCtl(mibptr, mibLength, &argmax, &size, IntPtr.Zero, 0)); } } // Get the PID so we can query this process' args int pid = GetPid(); // The following logic is based on https://gist.github.com/nonowarn/770696 // Now read the process args into the allocated space IntPtr procargs = Marshal.AllocHGlobal(argmax); IntPtr executablePathPtr = IntPtr.Zero; try { mib[0] = MACOS_CTL_KERN; mib[1] = MACOS_KERN_PROCARGS2; mib[2] = pid; mibLength = 3; unsafe { fixed(int *mibptr = mib) { ThrowOnFailure(nameof(procargs), SysCtl(mibptr, mibLength, procargs.ToPointer(), &argmax, IntPtr.Zero, 0)); } // The memory block we're reading is a series of null-terminated strings // that looks something like this: // // | argc | <int> // | exec_path | ... \0 // | argv[0] | ... \0 // | argv[1] | ... \0 // ... // // We care about argv[0], since that's the name the process was started with. // If argv[0][0] == '-', we have been invoked as login. // Doing this, the buffer we populated also recorded `exec_path`, // which is the path to our executable `pwsh`. // We can reuse this value later to prevent needing to call a .NET API // to generate our exec invocation. // We don't care about argc's value, since argv[0] must always exist. // Skip over argc, but remember where exec_path is for later executablePathPtr = IntPtr.Add(procargs, sizeof(int)); // Skip over exec_path byte *argvPtr = (byte *)executablePathPtr; while (*argvPtr != 0) { argvPtr++; } while (*argvPtr == 0) { argvPtr++; } // First char in argv[0] procNameFirstByte = *argvPtr; } if (!IsLogin(procNameFirstByte, args)) { return; } // Get the pwshPath from exec_path pwshPath = Marshal.PtrToStringAnsi(executablePathPtr); // exec pwsh ThrowOnFailure("exec", ExecPwshLogin(args, pwshPath, isMacOS: true)); } finally { Marshal.FreeHGlobal(procargs); } }
/// <summary> /// Specify whether the current OS platform is <paramref name="platformString"/> /// </summary> /// <param name="platformString">The platform string. Must be a member of <see cref="OSPlatform"/>. Case Insensitive</param> /// <returns></returns> internal static bool IsOSPlatform(string platformString) { return(RuntimeInformation.IsOSPlatform(OSPlatform.Create(platformString.ToUpperInvariant()))); }
public bool ConfigureDcmtk(bool install, Profile profile, string currentProfile) { var stopWatch = new Stopwatch(); stopWatch.Start(); profile.version++; //determine platform var platform = Environment.OSVersion.Platform; Console.WriteLine($"OSVersion is {Environment.OSVersion}"); Console.WriteLine($"{RuntimeInformation.OSDescription}"); Console.WriteLine($"Processor:{System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture} OS Arch:{System.Runtime.InteropServices.RuntimeInformation.OSArchitecture}"); Console.WriteLine($"{System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}"); Console.WriteLine($"Current Profile is {currentProfile}"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Console.WriteLine($"Platform is Linux"); if (install) { Console.WriteLine("Linux: dcmtk-3.6.3-linux-x86_64.tar.bz2 will be uncompressed ../LITE/tools/dcmtk/dcmtk-3.6.3-linux-x86_64"); //tar -xjvf tools/dcmtk/dcmtk-3.6.3-linux-x86_64.tar.bz2 var proc = new Process(); ProcessStartInfo procinfo = new ProcessStartInfo { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, Verb = "runas", //Directory.CreateDirectory("tools/dcmtk/dcmtk-3.6.3-linux-x86_64"); Arguments = $"-xjvf tools/dcmtk/dcmtk-3.6.3-linux-x86_64.tar.bz2 -C tools/dcmtk", FileName = "tar" }; proc.StartInfo = procinfo; proc.Start(); proc.OutputDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => { try { if (!string.IsNullOrEmpty(outLine.Data)) { Console.WriteLine($"{outLine.Data}"); } } catch (Exception e) { _logger.Log(LogLevel.Information, $"{e.Message}{e.StackTrace}"); } }; proc.ErrorDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => { try { if (!String.IsNullOrEmpty(outLine.Data)) { Console.WriteLine($"{outLine.Data}"); } } catch (Exception e) { _logger.Log(LogLevel.Information, $"{e.Message}{e.StackTrace}"); } }; proc.EnableRaisingEvents = true; proc.Exited += (object sender, EventArgs e) => { Process p = (Process)sender; if (p.ExitCode != 0) { Console.WriteLine($"{((Process)sender).StartInfo.FileName} Proc ExitCode:{proc.ExitCode}"); } ; }; while (!proc.HasExited) { Console.WriteLine($"{procinfo.FileName} is running..."); Task.Delay(1000, _taskManager.cts.Token).Wait(); } if (proc.ExitCode != 0) { Console.WriteLine($"Not updating dcmtkLibPath due to extraction error."); return(false); } else { Console.WriteLine($"Updating dcmtkLibPath."); profile.dcmtkLibPath = "tools/dcmtk/dcmtk-3.6.3-linux-x86_64"; //profile.Save(currentProfile); _fileProfileWriter.Save(profile, currentProfile); } } else { if (profile.dcmtkLibPath != null) { Console.WriteLine($"Clearing dcmtkLibPath."); Directory.Delete(profile.dcmtkLibPath, true); profile.dcmtkLibPath = null; //profile.Save(currentProfile); _profileWriter.SaveProfile(profile).Wait(); } } } if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Console.WriteLine($"Platform is OSX"); if (install) { Console.WriteLine("Mac: dcmtk-3.6.3-macosx-x86_64.tar.bz2 will be uncompressed to tools/dcmtk/dcmtk-3.6.3-macosx-x86_64"); //tar -xjvf tools/dcmtk/dcmtk-3.6.3-macosx-x86_64.tar.bz2 var proc = new Process(); ProcessStartInfo procinfo = new ProcessStartInfo { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, Verb = "runas", //Directory.CreateDirectory("tools/dcmtk/dcmtk-3.6.3-macosx-x86_64"); Arguments = $"-xjvf tools/dcmtk/dcmtk-3.6.3-macosx-x86_64.tar.bz2 -C tools/dcmtk", FileName = "tar" }; proc.StartInfo = procinfo; proc.OutputDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => { try { if (!String.IsNullOrEmpty(outLine.Data)) { Console.WriteLine($"{outLine.Data}"); } } catch (Exception e) { _logger.Log(LogLevel.Information, $"{e.Message}{e.StackTrace}"); } }; proc.ErrorDataReceived += (object sendingProcess, DataReceivedEventArgs outLine) => { try { if (!string.IsNullOrEmpty(outLine.Data)) { Console.WriteLine($"{outLine.Data}"); } } catch (Exception e) { _logger.Log(LogLevel.Information, $"{e.Message}{e.StackTrace}"); } }; proc.EnableRaisingEvents = true; proc.Exited += (object sender, EventArgs e) => { Process p = (Process)sender; if (p.ExitCode != 0) { Console.WriteLine($"{((Process)sender).StartInfo.FileName} Proc ExitCode:{proc.ExitCode}"); } ; }; proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); while (!proc.HasExited) { Console.WriteLine($"{procinfo.FileName} is running..."); Task.Delay(1000, _taskManager.cts.Token).Wait(); } if (proc.ExitCode != 0) { return(false); } else { profile.dcmtkLibPath = "tools/dcmtk/dcmtk-3.6.3-maxosx-x86_64"; //profile.Save(currentProfile); _fileProfileWriter.Save(profile, currentProfile); } } else { if (profile.dcmtkLibPath != null) { Console.WriteLine($"Clearing dcmtkLibPath."); Directory.Delete(profile.dcmtkLibPath, true); profile.dcmtkLibPath = null; //profile.Save(currentProfile); _fileProfileWriter.Save(profile, currentProfile); } } } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Console.WriteLine($"Platform is Windows"); Console.WriteLine($"Windows: dcmtk-3.6.3-win64-dynamic.zip will be uncompressed to ../LITE/tools/dcmtk/dcmtk-3.6.3-win64-dynamic"); if (install) { ZipFile.ExtractToDirectory("tools/dcmtk/dcmtk-3.6.3-win64-dynamic.zip", "tools/dcmtk"); profile.dcmtkLibPath = "tools" + Path.DirectorySeparatorChar + Constants.Dirs.dcmtk + Path.DirectorySeparatorChar + "dcmtk-3.6.3-win64-dynamic"; //profile.Save(currentProfile); _fileProfileWriter.Save(profile, currentProfile); } else { if (profile.dcmtkLibPath != null) { Console.WriteLine($"Clearing dcmtkLibPath."); Directory.Delete(profile.dcmtkLibPath, true); profile.dcmtkLibPath = null; //profile.Save(currentProfile); _fileProfileWriter.Save(profile, currentProfile); } } } return(true); }
/// <summary> /// Get info last release asset from GitHub /// </summary> /// <param name="appName"></param> /// <returns></returns> public static (DateTimeOffset?PublishedAt, string BrowserDownloadUrl, string ReleaseNotes, Version Version) GetInfoLastReleaseAssetFromGitHub(string appName) { (DateTimeOffset? PublishedAt, string BrowserDownloadUrl, string ReleaseNotes, Version Version)ret = (null, null, null, null); using (var client = new WebClient()) { client.Headers.Add("User-Agent", appName); var url = $"https://api.github.com/repos/Corsinvest/{appName}/releases"; dynamic releases = JsonConvert.DeserializeObject <IList <ExpandoObject> >(client.DownloadString(url)); dynamic lastRelease = null; foreach (var release in releases) { if (!release.prerelease) { lastRelease = release; break; } } if (lastRelease != null) { ret.PublishedAt = lastRelease.published_at; ret.ReleaseNotes = lastRelease.body; ret.Version = new Version(lastRelease.tag_name.Substring(1)); var downloadEndWith = ""; //check platform if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { downloadEndWith += "win-"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { downloadEndWith += "linux-"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { downloadEndWith += "osx-"; } //check architecure switch (RuntimeInformation.OSArchitecture) { case Architecture.Arm: downloadEndWith += "arm"; break; case Architecture.Arm64: downloadEndWith += "arm64"; break; case Architecture.X64: downloadEndWith += "x64"; break; case Architecture.X86: downloadEndWith += "x86"; break; default: break; } downloadEndWith += ".zip"; ret.BrowserDownloadUrl = ((IList <dynamic>)lastRelease.assets) .Where(a => a.name.StartsWith(appName) && a.name.EndsWith(downloadEndWith)) .FirstOrDefault()?.browser_download_url; } } return(ret); }
public Task <bool> ExecuteAsync() { return(Task.Run(() => { Console.WriteLine("Build Electron Application..."); SimpleCommandLineParser parser = new SimpleCommandLineParser(); parser.Parse(_args); var desiredPlatform = parser.Arguments[_paramTarget][0]; string specifiedFromCustom = string.Empty; if (desiredPlatform == "custom" && parser.Arguments[_paramTarget].Length > 1) { specifiedFromCustom = parser.Arguments[_paramTarget][1]; } string configuration = "Release"; if (parser.Arguments.ContainsKey(_paramDotNetConfig)) { configuration = parser.Arguments[_paramDotNetConfig][0]; } var platformInfo = GetTargetPlatformInformation.Do(desiredPlatform, specifiedFromCustom); Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid}..."); string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "obj", "desktop", desiredPlatform); if (Directory.Exists(tempPath) == false) { Directory.CreateDirectory(tempPath); } Console.WriteLine("Executing dotnet publish in this directory: " + tempPath); string tempBinPath = Path.Combine(tempPath, "bin"); Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid} under {configuration}-Configuration..."); var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c {configuration} --output \"{tempBinPath}\"", Directory.GetCurrentDirectory()); if (resultCode != 0) { Console.WriteLine("Error occurred during dotnet publish: " + resultCode); return false; } DeployEmbeddedElectronFiles.Do(tempPath); var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules"); if (Directory.Exists(checkForNodeModulesDirPath) == false) { Console.WriteLine("node_modules missing in: " + checkForNodeModulesDirPath); Console.WriteLine("Start npm install..."); ProcessHelper.CmdExecute("npm install --production", tempPath); Console.WriteLine("Start npm install electron-packager..."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Works proper on Windows... ProcessHelper.CmdExecute("npm install electron-packager --global", tempPath); } else { // ToDo: find another solution or document it proper // GH Issue https://github.com/electron-userland/electron-prebuilt/issues/48 Console.WriteLine("Electron Packager - make sure you invoke 'sudo npm install electron-packager --global' at " + tempPath + " manually. Sry."); } } else { Console.WriteLine("Skip npm install, because node_modules directory exists in: " + checkForNodeModulesDirPath); } Console.WriteLine("Build Electron Desktop Application..."); string buildPath = Path.Combine(Directory.GetCurrentDirectory(), "bin", "desktop"); Console.WriteLine("Executing electron magic in this directory: " + buildPath); // ToDo: Need a solution for --asar support string electronArch = "x64"; if (parser.Arguments.ContainsKey(_paramElectronArch)) { electronArch = parser.Arguments[_paramElectronArch][0]; } string electronParams = ""; if (parser.Arguments.ContainsKey(_paramElectronParams)) { electronParams = parser.Arguments[_paramElectronParams][0]; } Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}..."); ProcessHelper.CmdExecute($"electron-packager . --platform={platformInfo.ElectronPackerPlatform} --arch={electronArch} {electronParams} --out=\"{buildPath}\" --overwrite", tempPath); Console.WriteLine("... done"); return true; })); }
public static bool IsGnu() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public static string ExecutableName(this string withoutExtension) => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? withoutExtension + ".exe" : withoutExtension;
public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file() { var testAsset = _testAssetsManager .CopyTestAsset("CompilationContext", "PreserveCompilationContext") .WithSource(); testAsset.Restore("TestApp"); testAsset.Restore("TestLibrary"); var appProjectDirectory = Path.Combine(testAsset.TestRoot, "TestApp"); foreach (var targetFramework in new[] { "net46", "netcoreapp1.0" }) { var publishCommand = new PublishCommand(Stage0MSBuild, appProjectDirectory); if (targetFramework == "net46" && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { continue; } publishCommand .Execute($"/p:TargetFramework={targetFramework}") .Should() .Pass(); var publishDirectory = publishCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: "win7-x86"); publishDirectory.Should().HaveFiles(new[] { targetFramework == "net46" ? "TestApp.exe" : "TestApp.dll", "TestLibrary.dll", "Newtonsoft.Json.dll" }); var refsDirectory = new DirectoryInfo(Path.Combine(publishDirectory.FullName, "refs")); // Should have compilation time assemblies refsDirectory.Should().HaveFile("System.IO.dll"); // Libraries in which lib==ref should be deduped refsDirectory.Should().NotHaveFile("TestLibrary.dll"); refsDirectory.Should().NotHaveFile("Newtonsoft.Json.dll"); using (var depsJsonFileStream = File.OpenRead(Path.Combine(publishDirectory.FullName, "TestApp.deps.json"))) { var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); string[] expectedDefines; if (targetFramework == "net46") { expectedDefines = new[] { "DEBUG", "TRACE", "NET46" }; } else { expectedDefines = new[] { "DEBUG", "TRACE", "NETCOREAPP1_0" }; } dependencyContext.CompilationOptions.Defines.Should().BeEquivalentTo(expectedDefines); dependencyContext.CompilationOptions.LanguageVersion.Should().Be(""); dependencyContext.CompilationOptions.Platform.Should().Be("x86"); dependencyContext.CompilationOptions.Optimize.Should().Be(false); dependencyContext.CompilationOptions.KeyFile.Should().Be(""); dependencyContext.CompilationOptions.EmitEntryPoint.Should().Be(true); dependencyContext.CompilationOptions.DebugType.Should().Be("portable"); var compileLibraryNames = dependencyContext.CompileLibraries.Select(cl => cl.Name).ToList(); compileLibraryNames.Should().BeEquivalentTo(targetFramework == "net46" ? Net46CompileLibraryNames : NetCoreAppCompileLibraryNames); // Ensure P2P references are specified correctly var testLibrary = dependencyContext .CompileLibraries .FirstOrDefault(l => string.Equals(l.Name, "testlibrary", StringComparison.OrdinalIgnoreCase)); testLibrary.Assemblies.Count.Should().Be(1); testLibrary.Assemblies[0].Should().Be("TestLibrary.dll"); // Ensure framework references are specified correctly if (targetFramework == "net46") { var mscorlibLibrary = dependencyContext .CompileLibraries .FirstOrDefault(l => string.Equals(l.Name, "mscorlib", StringComparison.OrdinalIgnoreCase)); mscorlibLibrary.Assemblies.Count.Should().Be(1); mscorlibLibrary.Assemblies[0].Should().Be(".NETFramework/v4.6/mscorlib.dll"); var systemCoreLibrary = dependencyContext .CompileLibraries .FirstOrDefault(l => string.Equals(l.Name, "system.core", StringComparison.OrdinalIgnoreCase)); systemCoreLibrary.Assemblies.Count.Should().Be(1); systemCoreLibrary.Assemblies[0].Should().Be(".NETFramework/v4.6/System.Core.dll"); var systemCollectionsLibrary = dependencyContext .CompileLibraries .FirstOrDefault(l => string.Equals(l.Name, "system.collections", StringComparison.OrdinalIgnoreCase)); systemCollectionsLibrary.Assemblies.Count.Should().Be(1); systemCollectionsLibrary.Assemblies[0].Should().Be(".NETFramework/v4.6/Facades/System.Collections.dll"); } } } }
public void It_implicitly_defines_compilation_constants_for_the_target_framework(string targetFramework, string[] expectedDefines, bool buildOnlyOnWindows) { bool shouldCompile = true; var testAsset = _testAssetsManager .CopyTestAsset("AppWithLibraryVB", "ImplicitFrameworkConstantsVB", targetFramework) .WithSource() .WithProjectChanges(project => { // Update target framework in project var ns = project.Root.Name.Namespace; var targetFrameworkProperties = project.Root .Elements(ns + "PropertyGroup") .Elements(ns + "TargetFramework") .ToList(); targetFrameworkProperties.Count.Should().Be(1); if (targetFramework.Contains(",Version=")) { // We use the full TFM for frameworks we don't have built-in support for targeting, so we don't want to run the Compile target shouldCompile = false; var frameworkName = new FrameworkName(targetFramework); var targetFrameworkProperty = targetFrameworkProperties.Single(); targetFrameworkProperty.AddBeforeSelf(new XElement(ns + "TargetFrameworkIdentifier", frameworkName.Identifier)); targetFrameworkProperty.AddBeforeSelf(new XElement(ns + "TargetFrameworkVersion", "v" + frameworkName.Version.ToString())); if (!string.IsNullOrEmpty(frameworkName.Profile)) { targetFrameworkProperty.AddBeforeSelf(new XElement(ns + "TargetFrameworkProfile", frameworkName.Profile)); } // For the NuGet restore task to work with package references, it needs the TargetFramework property to be set. // Otherwise we would just remove the property. targetFrameworkProperty.SetValue(targetFramework); } else { shouldCompile = true; targetFrameworkProperties.Single().SetValue(targetFramework); } }) .Restore(Log, relativePath: "TestLibrary"); if (buildOnlyOnWindows && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { shouldCompile = false; } var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary"); var getValuesCommand = new GetValuesCommand(Log, libraryProjectDirectory, targetFramework, "FinalDefineConstants") { ShouldCompile = shouldCompile }; getValuesCommand .Execute() .Should() .Pass(); var definedConstants = ExpandSequence(getValuesCommand.GetValues()).ToList(); definedConstants.Should().BeEquivalentTo(new[] { "CONFIG=\"Debug\"", "DEBUG=-1", "TRACE=-1", "PLATFORM=\"AnyCPU\"" }.Concat(expectedDefines).ToArray()); }
static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => { if (!Directory.Exists(CrashDir)) { Directory.CreateDirectory(CrashDir); } string crashName = Path.Combine(CrashDir, $"Crash-{DateTimeOffset.Now.ToUnixTimeSeconds()}"); using (MemoryStream memoryStream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) using (Stream log = archive.CreateEntry("exception.log").Open()) using (StreamWriter writer = new StreamWriter(log)) writer.Write( $"CWD: {AppDomain.CurrentDomain.BaseDirectory}\n\n" + $"Operating System: {RuntimeInformation.OSDescription}\n\n" + e.ExceptionObject.ToString() ); File.WriteAllBytes(crashName + ".zip", memoryStream.ToArray()); } }; string temppath = Program.GetBaseFolder("Temp"); if (!Directory.Exists(temppath)) { return; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE", true); if (!key.GetSubKeyNames().Contains("Sysinternals")) { key.CreateSubKey("Sysinternals"); } key.Flush(); key = key.OpenSubKey("Sysinternals", true); if (!key.GetSubKeyNames().Contains("Handle")) { key.CreateSubKey("Handle"); } key.Flush(); key = key.OpenSubKey("Handle", true); key.SetValue("EulaAccepted", 1); key.Close(); } Thread.Sleep(2000); string apollopath = Program.GetBaseFolder("Apollo"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Process handle64 = Process.Start(new ProcessStartInfo(Handle64Path, "-p ApolloUpdate.exe -nobanner") { RedirectStandardOutput = true }); handle64.WaitForExit(); IEnumerable <string> strings = handle64.StandardOutput.ReadToEnd().Split('\n'); string pid = strings.FirstOrDefault(i => i.Contains("pid")); string handle = strings.FirstOrDefault(i => i.Contains(apollopath)); if (handle != null) { Process.Start(new ProcessStartInfo(Handle64Path, $"-p {pid.Trim().Split(' ')[2]} -c {handle.Trim().Split(':')[0]} -y -nobanner")).WaitForExit(); } } Thread.Sleep(1000); if (Directory.Exists(apollopath)) { while (true) { try { Directory.Delete(apollopath, true); break; } catch { Thread.Sleep(1000); } } } Directory.Move(temppath, apollopath); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Process.Start(Path.Combine(apollopath, "Apollo.exe")); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Process.Start("open", $"/Applications/Utilities/Terminal.app \"{Path.Combine(apollopath, "Apollo")}\""); } }
static bool ExecuteScriptFile() { return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ShellHelper.ExecuteFile("win.bat", "C:\\", true) : ShellHelper.ExecuteFile("linux-mac.sh", "-lh", true)); }
public static async Task BeginParse(MainWindow mainWindow, bool showVersionUpToDate) { if (Running) { return; } Running = true; mainWindow.UpdateMenuItem.Sensitive = false; int artifactIndex = -1; // Detect current platform if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { _platformExt = "osx_x64.zip"; artifactIndex = 1; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { _platformExt = "win_x64.zip"; artifactIndex = 2; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { _platformExt = "linux_x64.tar.gz"; artifactIndex = 0; } if (artifactIndex == -1) { GtkDialog.CreateErrorDialog("Your platform is not supported!"); return; } Version newVersion; Version currentVersion; try { currentVersion = Version.Parse(Program.Version); } catch { GtkDialog.CreateWarningDialog("Failed to convert the current Ryujinx version.", "Cancelling Update!"); Logger.Error?.Print(LogClass.Application, "Failed to convert the current Ryujinx version!"); return; } // Get latest version number from Appveyor try { using (WebClient jsonClient = new WebClient()) { // Fetch latest build information string fetchedJson = await jsonClient.DownloadStringTaskAsync($"{AppveyorApiUrl}/projects/gdkchan/ryujinx/branch/master"); JObject jsonRoot = JObject.Parse(fetchedJson); JToken buildToken = jsonRoot["build"]; _jobId = (string)buildToken["jobs"][0]["jobId"]; _buildVer = (string)buildToken["version"]; _buildUrl = $"{AppveyorApiUrl}/buildjobs/{_jobId}/artifacts/ryujinx-{_buildVer}-{_platformExt}"; // If build not done, assume no new update are availaible. if ((string)buildToken["jobs"][0]["status"] != "success") { if (showVersionUpToDate) { GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", ""); } return; } } } catch (Exception exception) { Logger.Error?.Print(LogClass.Application, exception.Message); GtkDialog.CreateErrorDialog("An error has occurred when trying to get release information from AppVeyor."); return; } try { newVersion = Version.Parse(_buildVer); } catch { GtkDialog.CreateWarningDialog("Failed to convert the received Ryujinx version from AppVeyor.", "Cancelling Update!"); Logger.Error?.Print(LogClass.Application, "Failed to convert the received Ryujinx version from AppVeyor!"); return; } if (newVersion <= currentVersion) { if (showVersionUpToDate) { GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", ""); } Running = false; mainWindow.UpdateMenuItem.Sensitive = true; return; } // Fetch build size information to learn chunk sizes. using (WebClient buildSizeClient = new WebClient()) { try { buildSizeClient.Headers.Add("Range", "bytes=0-0"); await buildSizeClient.DownloadDataTaskAsync(new Uri(_buildUrl)); string contentRange = buildSizeClient.ResponseHeaders["Content-Range"]; _buildSize = long.Parse(contentRange.Substring(contentRange.IndexOf('/') + 1)); } catch (Exception ex) { Logger.Warning?.Print(LogClass.Application, ex.Message); Logger.Warning?.Print(LogClass.Application, "Couldn't determine build size for update, will use single-threaded updater"); _buildSize = -1; } } // Show a message asking the user if they want to update UpdateDialog updateDialog = new UpdateDialog(mainWindow, newVersion, _buildUrl); updateDialog.Show(); }
private static async void InstallUpdate(UpdateDialog updateDialog, string updateFile) { // Extract Update updateDialog.MainText.Text = "Extracting Update..."; updateDialog.ProgressBar.Value = 0; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { using (Stream inStream = File.OpenRead(updateFile)) using (Stream gzipStream = new GZipInputStream(inStream)) using (TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII)) { updateDialog.ProgressBar.MaxValue = inStream.Length; await Task.Run(() => { TarEntry tarEntry; while ((tarEntry = tarStream.GetNextEntry()) != null) { if (tarEntry.IsDirectory) { continue; } string outPath = Path.Combine(UpdateDir, tarEntry.Name); Directory.CreateDirectory(Path.GetDirectoryName(outPath)); using (FileStream outStream = File.OpenWrite(outPath)) { tarStream.CopyEntryContents(outStream); } File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc)); TarEntry entry = tarEntry; Application.Invoke(delegate { updateDialog.ProgressBar.Value += entry.Size; }); } }); updateDialog.ProgressBar.Value = inStream.Length; } } else { using (Stream inStream = File.OpenRead(updateFile)) using (ZipFile zipFile = new ZipFile(inStream)) { updateDialog.ProgressBar.MaxValue = zipFile.Count; await Task.Run(() => { foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsDirectory) { continue; } string outPath = Path.Combine(UpdateDir, zipEntry.Name); Directory.CreateDirectory(Path.GetDirectoryName(outPath)); using (Stream zipStream = zipFile.GetInputStream(zipEntry)) using (FileStream outStream = File.OpenWrite(outPath)) { zipStream.CopyTo(outStream); } File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc)); Application.Invoke(delegate { updateDialog.ProgressBar.Value++; }); } }); } } // Delete downloaded zip File.Delete(updateFile); List <string> allFiles = EnumerateFilesToDelete().ToList(); updateDialog.MainText.Text = "Renaming Old Files..."; updateDialog.ProgressBar.Value = 0; updateDialog.ProgressBar.MaxValue = allFiles.Count; // Replace old files await Task.Run(() => { foreach (string file in allFiles) { try { File.Move(file, file + ".ryuold"); Application.Invoke(delegate { updateDialog.ProgressBar.Value++; }); } catch { Logger.Warning?.Print(LogClass.Application, "Updater was unable to rename file: " + file); } } Application.Invoke(delegate { updateDialog.MainText.Text = "Adding New Files..."; updateDialog.ProgressBar.Value = 0; updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length; }); MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog); }); Directory.Delete(UpdateDir, true); SetUnixPermissions(); updateDialog.MainText.Text = "Update Complete!"; updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?"; updateDialog.Modal = true; updateDialog.ProgressBar.Hide(); updateDialog.YesButton.Show(); updateDialog.NoButton.Show(); }
public static void Main(string[] args) { var consoleTitle = $"ACEmulator - v{ServerBuildInfo.FullVersion}"; Console.Title = consoleTitle; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); // Typically, you wouldn't force the current culture on an entire application unless you know sure your application is used in a specific region (which ACE is not) // We do this because almost all of the client/user input/output code does not take culture into account, and assumes en-US formatting. // Without this, many commands that require special characters like , and . will break Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); // Init our text encoding options. This will allow us to use more than standard ANSI text, which the client also supports. System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // Look for the log4net.config first in the current environment directory, then in the ExecutingAssembly location var exeLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var containerConfigDirectory = "/ace/Config"; var log4netConfig = Path.Combine(exeLocation, "log4net.config"); var log4netConfigExample = Path.Combine(exeLocation, "log4net.config.example"); var log4netConfigContainer = Path.Combine(containerConfigDirectory, "log4net.config"); if (IsRunningInContainer && File.Exists(log4netConfigContainer)) File.Copy(log4netConfigContainer, log4netConfig, true); var log4netFileInfo = new FileInfo("log4net.config"); if (!log4netFileInfo.Exists) log4netFileInfo = new FileInfo(log4netConfig); if (!log4netFileInfo.Exists) { var exampleFile = new FileInfo(log4netConfigExample); if (!exampleFile.Exists) { Console.WriteLine("log4net Configuration file is missing. Please copy the file log4net.config.example to log4net.config and edit it to match your needs before running ACE."); throw new Exception("missing log4net configuration file"); } else { if (!IsRunningInContainer) { Console.WriteLine("log4net Configuration file is missing, cloning from example file."); File.Copy(log4netConfigExample, log4netConfig); } else { if (!File.Exists(log4netConfigContainer)) { Console.WriteLine("log4net Configuration file is missing, ACEmulator is running in a container, cloning from docker file."); var log4netConfigDocker = Path.Combine(exeLocation, "log4net.config.docker"); File.Copy(log4netConfigDocker, log4netConfig); File.Copy(log4netConfigDocker, log4netConfigContainer); } else { File.Copy(log4netConfigContainer, log4netConfig); } } } } var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.ConfigureAndWatch(logRepository, log4netFileInfo); if (Environment.ProcessorCount < 2) log.Warn("Only one vCPU was detected. ACE may run with limited performance. You should increase your vCPU count for anything more than a single player server."); // Do system specific initializations here try { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // On many windows systems, the default resolution for Thread.Sleep is 15.6ms. This allows us to command a tighter resolution MM_BeginPeriod(1); } } catch (Exception ex) { log.Error(ex.ToString()); } log.Info("Starting ACEmulator..."); if (IsRunningInContainer) log.Info("ACEmulator is running in a container..."); var configFile = Path.Combine(exeLocation, "Config.js"); var configConfigContainer = Path.Combine(containerConfigDirectory, "Config.js"); if (IsRunningInContainer && File.Exists(configConfigContainer)) File.Copy(configConfigContainer, configFile, true); if (!File.Exists(configFile)) { if (!IsRunningInContainer) DoOutOfBoxSetup(configFile); else { if (!File.Exists(configConfigContainer)) { DoOutOfBoxSetup(configFile); File.Copy(configFile, configConfigContainer); } else File.Copy(configConfigContainer, configFile); } } log.Info("Initializing ConfigManager..."); ConfigManager.Initialize(); if (ConfigManager.Config.Server.WorldName != "ACEmulator") { consoleTitle = $"{ConfigManager.Config.Server.WorldName} | {consoleTitle}"; Console.Title = consoleTitle; } if (ConfigManager.Config.Offline.PurgeDeletedCharacters) { log.Info($"Purging deleted characters, and their possessions, older than {ConfigManager.Config.Offline.PurgeDeletedCharactersDays} days ({DateTime.Now.AddDays(-ConfigManager.Config.Offline.PurgeDeletedCharactersDays)})..."); ShardDatabaseOfflineTools.PurgeCharactersInParallel(ConfigManager.Config.Offline.PurgeDeletedCharactersDays, out var charactersPurged, out var playerBiotasPurged, out var possessionsPurged); log.Info($"Purged {charactersPurged:N0} characters, {playerBiotasPurged:N0} player biotas and {possessionsPurged:N0} possessions."); } if (ConfigManager.Config.Offline.PurgeOrphanedBiotas) { log.Info($"Purging orphaned biotas..."); ShardDatabaseOfflineTools.PurgeOrphanedBiotasInParallel(out var numberOfBiotasPurged); log.Info($"Purged {numberOfBiotasPurged:N0} biotas."); } if (ConfigManager.Config.Offline.AutoUpdateWorldDatabase) { CheckForWorldDatabaseUpdate(); if (ConfigManager.Config.Offline.AutoApplyWorldCustomizations) AutoApplyWorldCustomizations(); } else log.Info($"AutoUpdateWorldDatabase is disabled..."); if (ConfigManager.Config.Offline.AutoApplyDatabaseUpdates) AutoApplyDatabaseUpdates(); else log.Info($"AutoApplyDatabaseUpdates is disabled..."); // This should only be enabled manually. To enable it, simply uncomment this line //ACE.Database.OfflineTools.Shard.BiotaGuidConsolidator.ConsolidateBiotaGuids(0xC0000000, out int numberOfBiotasConsolidated, out int numberOfErrors); ShardDatabaseOfflineTools.CheckForBiotaPropertiesPaletteOrderColumnInShard(); log.Info("Initializing ServerManager..."); ServerManager.Initialize(); log.Info("Initializing DatManager..."); DatManager.Initialize(ConfigManager.Config.Server.DatFilesDirectory, true); log.Info("Initializing DatabaseManager..."); DatabaseManager.Initialize(); if (DatabaseManager.InitializationFailure) { log.Fatal("DatabaseManager initialization failed. ACEmulator will now abort startup."); ServerManager.StartupAbort(); Environment.Exit(0); } log.Info("Starting DatabaseManager..."); DatabaseManager.Start(); log.Info("Starting PropertyManager..."); PropertyManager.Initialize(); log.Info("Initializing GuidManager..."); GuidManager.Initialize(); if (ConfigManager.Config.Server.ServerPerformanceMonitorAutoStart) { log.Info("Server Performance Monitor auto starting..."); ServerPerformanceMonitor.Start(); } if (ConfigManager.Config.Server.WorldDatabasePrecaching) { log.Info("Precaching Weenies..."); DatabaseManager.World.CacheAllWeeniesInParallel(); log.Info("Precaching Cookbooks..."); DatabaseManager.World.CacheAllCookbooksInParallel(); log.Info("Precaching Events..."); DatabaseManager.World.GetAllEvents(); log.Info("Precaching House Portals..."); DatabaseManager.World.CacheAllHousePortals(); log.Info("Precaching Points Of Interest..."); DatabaseManager.World.CacheAllPointsOfInterest(); log.Info("Precaching Spells..."); DatabaseManager.World.CacheAllSpells(); log.Info("Precaching Treasures - Death..."); DatabaseManager.World.CacheAllTreasuresDeath(); log.Info("Precaching Treasures - Material Base..."); DatabaseManager.World.CacheAllTreasureMaterialBase(); log.Info("Precaching Treasures - Material Groups..."); DatabaseManager.World.CacheAllTreasureMaterialGroups(); log.Info("Precaching Treasures - Material Colors..."); DatabaseManager.World.CacheAllTreasureMaterialColor(); log.Info("Precaching Treasures - Wielded..."); DatabaseManager.World.CacheAllTreasureWielded(); } else log.Info("Precaching World Database Disabled..."); log.Info("Initializing PlayerManager..."); PlayerManager.Initialize(); log.Info("Initializing HouseManager..."); HouseManager.Initialize(); log.Info("Initializing InboundMessageManager..."); InboundMessageManager.Initialize(); log.Info("Initializing SocketManager..."); SocketManager.Initialize(); log.Info("Initializing WorldManager..."); WorldManager.Initialize(); log.Info("Initializing EventManager..."); EventManager.Initialize(); // Free up memory before the server goes online. This can free up 6 GB+ on larger servers. log.Info("Forcing .net garbage collection..."); for (int i = 0 ; i < 10 ; i++) GC.Collect(); // This should be last log.Info("Initializing CommandManager..."); CommandManager.Initialize(); if (!PropertyManager.GetBool("world_closed", false).Item) { WorldManager.Open(null); } }
public static bool IsWin() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public CryptoProvider(CertificateList certificateList) { _certificateList = certificateList; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { _keyShareProvider = new KeyExchange.OpenSsl11.KeyshareProvider(); _hashProvider = new Hash.OpenSsl11.HashProvider(); _bulkCipherProvider = new BulkCipher.OpenSsl11.BulkCipherProvider(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { //_keyShareProvider = new KeyExchange.Windows.KeyshareProvider(); _keyShareProvider = new KeyExchange.OpenSsl11.KeyshareProvider(); //_hashProvider = new Hash.Windows.HashProvider(); _hashProvider = new Hash.OpenSsl11.HashProvider(); _bulkCipherProvider = new BulkCipher.OpenSsl11.BulkCipherProvider(); } else { throw new NotImplementedException(); } _priorityOrderedEchdeExchanges = new NamedGroup[] { NamedGroup.secp256r1, NamedGroup.secp384r1, NamedGroup.secp521r1 }; _prioritySignatureSchemes = new SignatureScheme[] { SignatureScheme.ecdsa_secp256r1_sha256, SignatureScheme.ecdsa_secp384r1_sha384, SignatureScheme.ecdsa_secp521r1_sha512, SignatureScheme.rsa_pkcs1_sha256, SignatureScheme.rsa_pkcs1_sha384, SignatureScheme.rsa_pkcs1_sha512, //SignatureScheme.rsa_pss_sha256, //SignatureScheme.rsa_pss_sha384, //SignatureScheme.rsa_pss_sha512 }; _priorityOrderedKeyExchanges = new NamedGroup[] { NamedGroup.x25519, NamedGroup.x448, NamedGroup.secp256r1, NamedGroup.secp521r1, NamedGroup.secp384r1, NamedGroup.ffdhe8192, NamedGroup.ffdhe6144, NamedGroup.ffdhe4096, NamedGroup.ffdhe3072, NamedGroup.ffdhe2048 }; _priorityOrderedCipherSuitesTls13 = new CipherSuite[] { new CipherSuite() { BulkCipherType = BulkCipherType.CHACHA20_POLY1305, CipherCode = 0x1303, HashType = HashType.SHA256 }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_128_GCM, CipherCode = 0x1301, HashType = HashType.SHA256 }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_256_GCM, CipherCode = 0x1302, HashType = HashType.SHA384 }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_128_CCM, CipherCode = 0x1304, HashType = HashType.SHA256 }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_128_CCM_8, CipherCode = 0x1305, HashType = HashType.SHA256 } }; _priorityOrderedCipherSuitesTls12 = new CipherSuite[] { //new CipherSuite() {BulkCipherType = BulkCipherType.AES_128_GCM, HashType = HashType.SHA256, CipherName = "TLS_RSA_WITH_AES_128_GCM_SHA256", CipherCode = 0x009C }, //new CipherSuite() {BulkCipherType = BulkCipherType.AES_256_GCM, HashType = HashType.SHA384, CipherCode = 0x009D, CipherName = "TLS_RSA_WITH_AES_256_GCM_SHA384" }, //new CipherSuite() {BulkCipherType = BulkCipherType.AES_128_GCM, HashType = HashType.SHA256, CipherCode = 0x009E, CipherName = "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" }, //new CipherSuite() { BulkCipherType = BulkCipherType.AES_256_GCM, HashType = HashType.SHA384, CipherCode = 0x009F, CipherName = "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_128_GCM, HashType = HashType.SHA256, CipherCode = 0xC02B, CipherName = "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", ExchangeType = KeyExchangeType.Ecdhe, RequiredCertificateType = CertificateType.ecdsa }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_256_GCM, HashType = HashType.SHA384, CipherCode = 0xC02C, CipherName = "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", ExchangeType = KeyExchangeType.Ecdhe, RequiredCertificateType = CertificateType.ecdsa }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_128_GCM, HashType = HashType.SHA256, CipherCode = 0xC02F, CipherName = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", ExchangeType = KeyExchangeType.Ecdhe, RequiredCertificateType = CertificateType.rsa }, new CipherSuite() { BulkCipherType = BulkCipherType.AES_256_GCM, HashType = HashType.SHA384, CipherCode = 0xC030, CipherName = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", ExchangeType = KeyExchangeType.Ecdhe, RequiredCertificateType = CertificateType.rsa }, // new CipherSuite() {BulkCipherType = BulkCipherType.CHACHA20_POLY1305, HashType = HashType.SHA256, CipherCode = 0xCCA8, CipherName ="TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" , ExchangeType = KeyExchangeType.Ecdhe}, new CipherSuite() { BulkCipherType = BulkCipherType.CHACHA20_POLY1305, HashType = HashType.SHA256, CipherCode = 0xCCA9, CipherName = "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", ExchangeType = KeyExchangeType.Ecdhe, RequiredCertificateType = CertificateType.ecdsa }, //new CipherSuite() {BulkCipherType = BulkCipherType.CHACHA20_POLY1305, HashType = HashType.SHA256, CipherCode = 0xCCAA, CipherName = "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" }, }; }
public static bool IsMac() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public int Collect(IConsole console, int processId, string output, bool diag, bool crashreport, DumpTypeOption type, string name) { Console.WriteLine(name); if (name != null) { if (processId != 0) { Console.WriteLine("Can only specify either --name or --process-id option."); return(0); } processId = CommandUtils.FindProcessIdWithName(name); if (processId < 0) { return(0); } } if (processId == 0) { console.Error.WriteLine("ProcessId is required."); return(1); } try { if (output == null) { // Build timestamp based file path string timestamp = $"{DateTime.Now:yyyyMMdd_HHmmss}"; output = Path.Combine(Directory.GetCurrentDirectory(), RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"dump_{timestamp}.dmp" : $"core_{timestamp}"); } // Make sure the dump path is NOT relative. This path could be sent to the runtime // process on Linux which may have a different current directory. output = Path.GetFullPath(output); // Display the type of dump and dump path string dumpTypeMessage = null; switch (type) { case DumpTypeOption.Full: dumpTypeMessage = "full"; break; case DumpTypeOption.Heap: dumpTypeMessage = "dump with heap"; break; case DumpTypeOption.Mini: dumpTypeMessage = "dump"; break; } console.Out.WriteLine($"Writing {dumpTypeMessage} to {output}"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (crashreport) { Console.WriteLine("Crash reports not supported on Windows."); return(0); } Windows.CollectDump(processId, output, type); } else { var client = new DiagnosticsClient(processId); DumpType dumpType = DumpType.Normal; switch (type) { case DumpTypeOption.Full: dumpType = DumpType.Full; break; case DumpTypeOption.Heap: dumpType = DumpType.WithHeap; break; case DumpTypeOption.Mini: dumpType = DumpType.Normal; break; case DumpTypeOption.Triage: dumpType = DumpType.Triage; break; } WriteDumpFlags flags = WriteDumpFlags.None; if (diag) { flags |= WriteDumpFlags.LoggingEnabled; } if (crashreport) { flags |= WriteDumpFlags.CrashReportEnabled; } // Send the command to the runtime to initiate the core dump client.WriteDump(dumpType, output, flags); } } catch (Exception ex) when (ex is FileNotFoundException || ex is ArgumentException || ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is PlatformNotSupportedException || ex is UnsupportedCommandException || ex is InvalidDataException || ex is InvalidOperationException || ex is NotSupportedException || ex is DiagnosticsClientException) { console.Error.WriteLine($"{ex.Message}"); return(1); } console.Out.WriteLine($"Complete"); return(0); }
private static IDnsResolver CreateDnsResolverWrapper(IServiceProvider provider) { return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? new DnsResolverWrapper(Resolver.DefaultDnsServers.ToList()) : new DnsResolverWrapper(provider.GetRequiredService <IDnsNameServerProvider>().GetNameServers().Select(_ => new IPEndPoint(_, 53)).ToList())); }
/* goodB2G1() - use badsource and goodsink by changing second IO.staticFive==5 to IO.staticFive!=5 */ private void GoodB2G1(HttpRequest req, HttpResponse resp) { string data; if (IO.staticFive == 5) { data = ""; /* initialize data in case there are no cookies */ /* Read data from cookies */ { HttpCookieCollection cookieSources = req.Cookies; if (cookieSources != null) { /* POTENTIAL FLAW: Read data from the first cookie value */ data = cookieSources[0].Value; } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; } if (IO.staticFive != 5) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.WriteLine("Benign, fixed string"); } else { string xmlFile = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { /* running on Windows */ xmlFile = "..\\..\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "../../CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ string[] tokens = data.Split("||".ToCharArray()); if (tokens.Length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ string username = System.Security.SecurityElement.Escape(tokens[0]); string password = System.Security.SecurityElement.Escape(tokens[1]); /* build xpath */ XPathDocument inputXml = new XPathDocument(xmlFile); XPathNavigator xPath = inputXml.CreateNavigator(); string query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; string secret = (string)xPath.Evaluate(query); } } }
/// <summary> /// Helper for the ExpectEvent function. /// </summary> /// <param name="watcher">The FileSystemWatcher to test</param> /// <param name="expectedEvents">All of the events that are expected to be raised by this action</param> /// <param name="action">The Action that will trigger events.</param> /// <param name="assertExpected">True if results should be asserted. Used if there is no retry.</param> /// <param name="expectedPath"> Adds path verification to all expected events.</param> /// <returns>True if the events raised correctly; else, false.</returns> public static bool ExecuteAndVerifyEvents(FileSystemWatcher watcher, WatcherChangeTypes expectedEvents, Action action, bool assertExpected, string[] expectedPaths, int timeout) { bool result = true, verifyChanged = true, verifyCreated = true, verifyDeleted = true, verifyRenamed = true; AutoResetEvent changed = null, created = null, deleted = null, renamed = null; string[] expectedFullPaths = expectedPaths == null ? null : expectedPaths.Select(e => Path.GetFullPath(e)).ToArray(); // On OSX we get a number of extra events tacked onto valid events. As such, we can not ever confidently // say that an event won't occur, only that one will occur. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { if (verifyChanged = ((expectedEvents & WatcherChangeTypes.Changed) > 0)) { changed = WatchChanged(watcher, expectedPaths); } if (verifyCreated = ((expectedEvents & WatcherChangeTypes.Created) > 0)) { created = WatchCreated(watcher, expectedPaths); } if (verifyDeleted = ((expectedEvents & WatcherChangeTypes.Deleted) > 0)) { deleted = WatchDeleted(watcher, expectedPaths); } if (verifyRenamed = ((expectedEvents & WatcherChangeTypes.Renamed) > 0)) { renamed = WatchRenamed(watcher, expectedPaths); } } else { changed = WatchChanged(watcher, (expectedEvents & WatcherChangeTypes.Changed) > 0 ? expectedPaths : null); created = WatchCreated(watcher, (expectedEvents & WatcherChangeTypes.Created) > 0 ? expectedPaths : null); deleted = WatchDeleted(watcher, (expectedEvents & WatcherChangeTypes.Deleted) > 0 ? expectedPaths : null); renamed = WatchRenamed(watcher, (expectedEvents & WatcherChangeTypes.Renamed) > 0 ? expectedPaths : null); } watcher.EnableRaisingEvents = true; action(); // Verify Changed if (verifyChanged) { bool Changed_expected = ((expectedEvents & WatcherChangeTypes.Changed) > 0); bool Changed_actual = changed.WaitOne(timeout); result = Changed_expected == Changed_actual; if (assertExpected) { Assert.True(Changed_expected == Changed_actual, "Changed event did not occur as expected"); } } // Verify Created if (verifyCreated) { bool Created_expected = ((expectedEvents & WatcherChangeTypes.Created) > 0); bool Created_actual = created.WaitOne(verifyChanged ? SubsequentExpectedWait : timeout); result = result && Created_expected == Created_actual; if (assertExpected) { Assert.True(Created_expected == Created_actual, "Created event did not occur as expected"); } } // Verify Deleted if (verifyDeleted) { bool Deleted_expected = ((expectedEvents & WatcherChangeTypes.Deleted) > 0); bool Deleted_actual = deleted.WaitOne(verifyChanged || verifyCreated ? SubsequentExpectedWait : timeout); result = result && Deleted_expected == Deleted_actual; if (assertExpected) { Assert.True(Deleted_expected == Deleted_actual, "Deleted event did not occur as expected"); } } // Verify Renamed if (verifyRenamed) { bool Renamed_expected = ((expectedEvents & WatcherChangeTypes.Renamed) > 0); bool Renamed_actual = renamed.WaitOne(verifyChanged || verifyCreated || verifyDeleted? SubsequentExpectedWait : timeout); result = result && Renamed_expected == Renamed_actual; if (assertExpected) { Assert.True(Renamed_expected == Renamed_actual, "Renamed event did not occur as expected"); } } watcher.EnableRaisingEvents = false; return(result); }
static LoggingExtensions() { _usingFallbackKeyWithExpirationAsDefaultKey = LoggerMessage.Define <Guid, DateTimeOffset>( eventId: new EventId(1, "UsingFallbackKeyWithExpirationAsDefaultKey"), logLevel: LogLevel.Warning, formatString: "Policy resolution states that a new key should be added to the key ring, but automatic generation of keys is disabled. Using fallback key {KeyId:B} with expiration {ExpirationDate:u} as default key."); _usingKeyAsDefaultKey = LoggerMessage.Define <Guid>( eventId: new EventId(2, "UsingKeyAsDefaultKey"), logLevel: LogLevel.Debug, formatString: "Using key {KeyId:B} as the default key."); _openingCNGAlgorithmFromProviderWithHMAC = LoggerMessage.Define <string, string?>( eventId: new EventId(3, "OpeningCNGAlgorithmFromProviderWithHMAC"), logLevel: LogLevel.Debug, formatString: "Opening CNG algorithm '{HashAlgorithm}' from provider '{HashAlgorithmProvider}' with HMAC."); _openingCNGAlgorithmFromProviderWithChainingModeCBC = LoggerMessage.Define <string, string?>( eventId: new EventId(4, "OpeningCNGAlgorithmFromProviderWithChainingModeCBC"), logLevel: LogLevel.Debug, formatString: "Opening CNG algorithm '{EncryptionAlgorithm}' from provider '{EncryptionAlgorithmProvider}' with chaining mode CBC."); _performingUnprotectOperationToKeyWithPurposes = LoggerMessage.Define <Guid, string>( eventId: new EventId(5, "PerformingUnprotectOperationToKeyWithPurposes"), logLevel: LogLevel.Trace, formatString: "Performing unprotect operation to key {KeyId:B} with purposes {Purposes}."); _keyWasNotFoundInTheKeyRingUnprotectOperationCannotProceed = LoggerMessage.Define <Guid>( eventId: new EventId(6, "KeyWasNotFoundInTheKeyRingUnprotectOperationCannotProceed"), logLevel: LogLevel.Trace, formatString: "Key {KeyId:B} was not found in the key ring. Unprotect operation cannot proceed."); _keyWasRevokedCallerRequestedUnprotectOperationProceedRegardless = LoggerMessage.Define <Guid>( eventId: new EventId(7, "KeyWasRevokedCallerRequestedUnprotectOperationProceedRegardless"), logLevel: LogLevel.Debug, formatString: "Key {KeyId:B} was revoked. Caller requested unprotect operation proceed regardless."); _keyWasRevokedUnprotectOperationCannotProceed = LoggerMessage.Define <Guid>( eventId: new EventId(8, "KeyWasRevokedUnprotectOperationCannotProceed"), logLevel: LogLevel.Debug, formatString: "Key {KeyId:B} was revoked. Unprotect operation cannot proceed."); _openingCNGAlgorithmFromProviderWithChainingModeGCM = LoggerMessage.Define <string, string?>( eventId: new EventId(9, "OpeningCNGAlgorithmFromProviderWithChainingModeGCM"), logLevel: LogLevel.Debug, formatString: "Opening CNG algorithm '{EncryptionAlgorithm}' from provider '{EncryptionAlgorithmProvider}' with chaining mode GCM."); _usingManagedKeyedHashAlgorithm = LoggerMessage.Define <string>( eventId: new EventId(10, "UsingManagedKeyedHashAlgorithm"), logLevel: LogLevel.Debug, formatString: "Using managed keyed hash algorithm '{FullName}'."); _usingManagedSymmetricAlgorithm = LoggerMessage.Define <string>( eventId: new EventId(11, "UsingManagedSymmetricAlgorithm"), logLevel: LogLevel.Debug, formatString: "Using managed symmetric algorithm '{FullName}'."); _keyIsIneligibleToBeTheDefaultKeyBecauseItsMethodFailed = LoggerMessage.Define <Guid, string>( eventId: new EventId(12, "KeyIsIneligibleToBeTheDefaultKeyBecauseItsMethodFailed"), logLevel: LogLevel.Warning, formatString: "Key {KeyId:B} is ineligible to be the default key because its {MethodName} method failed."); _consideringKeyWithExpirationDateAsDefaultKey = LoggerMessage.Define <Guid, DateTimeOffset>( eventId: new EventId(13, "ConsideringKeyWithExpirationDateAsDefaultKey"), logLevel: LogLevel.Debug, formatString: "Considering key {KeyId:B} with expiration date {ExpirationDate:u} as default key."); _keyIsNoLongerUnderConsiderationAsDefault = LoggerMessage.Define <Guid>( eventId: new EventId(14, "KeyIsNoLongerUnderConsiderationAsDefault"), logLevel: LogLevel.Debug, formatString: "Key {KeyId:B} is no longer under consideration as default key because it is expired, revoked, or cannot be deciphered."); _unknownElementWithNameFoundInKeyringSkipping = LoggerMessage.Define <XName>( eventId: new EventId(15, "UnknownElementWithNameFoundInKeyringSkipping"), logLevel: LogLevel.Warning, formatString: "Unknown element with name '{Name}' found in keyring, skipping."); _markedKeyAsRevokedInTheKeyring = LoggerMessage.Define <Guid>( eventId: new EventId(16, "MarkedKeyAsRevokedInTheKeyring"), logLevel: LogLevel.Debug, formatString: "Marked key {KeyId:B} as revoked in the keyring."); _triedToProcessRevocationOfKeyButNoSuchKeyWasFound = LoggerMessage.Define <Guid>( eventId: new EventId(17, "TriedToProcessRevocationOfKeyButNoSuchKeyWasFound"), logLevel: LogLevel.Warning, formatString: "Tried to process revocation of key {KeyId:B}, but no such key was found in keyring. Skipping."); _foundKey = LoggerMessage.Define <Guid>( eventId: new EventId(18, "FoundKey"), logLevel: LogLevel.Debug, formatString: "Found key {KeyId:B}."); _foundRevocationOfAllKeysCreatedPriorTo = LoggerMessage.Define <DateTimeOffset>( eventId: new EventId(19, "FoundRevocationOfAllKeysCreatedPriorTo"), logLevel: LogLevel.Debug, formatString: "Found revocation of all keys created prior to {RevocationDate:u}."); _foundRevocationOfKey = LoggerMessage.Define <Guid>( eventId: new EventId(20, "FoundRevocationOfKey"), logLevel: LogLevel.Debug, formatString: "Found revocation of key {KeyId:B}."); _exceptionWhileProcessingRevocationElement = LoggerMessage.Define <XElement>( eventId: new EventId(21, "ExceptionWhileProcessingRevocationElement"), logLevel: LogLevel.Error, formatString: "An exception occurred while processing the revocation element '{RevocationElement}'. Cannot continue keyring processing."); _revokingAllKeysAsOfForReason = LoggerMessage.Define <DateTimeOffset, string?>( eventId: new EventId(22, "RevokingAllKeysAsOfForReason"), logLevel: LogLevel.Information, formatString: "Revoking all keys as of {RevocationDate:u} for reason '{Reason}'."); _keyCacheExpirationTokenTriggeredByOperation = LoggerMessage.Define <string>( eventId: new EventId(23, "KeyCacheExpirationTokenTriggeredByOperation"), logLevel: LogLevel.Debug, formatString: "Key cache expiration token triggered by '{OperationName}' operation."); _anExceptionOccurredWhileProcessingTheKeyElement = LoggerMessage.Define <XElement>( eventId: new EventId(24, "ExceptionOccurredWhileProcessingTheKeyElement"), logLevel: LogLevel.Error, formatString: "An exception occurred while processing the key element '{Element}'."); _anExceptionOccurredWhileProcessingTheKeyElementDebug = LoggerMessage.Define <XElement>( eventId: new EventId(25, "ExceptionOccurredWhileProcessingTheKeyElementDebug"), logLevel: LogLevel.Trace, formatString: "An exception occurred while processing the key element '{Element}'."); _encryptingToWindowsDPAPIForCurrentUserAccount = LoggerMessage.Define <string>( eventId: new EventId(26, "EncryptingToWindowsDPAPIForCurrentUserAccount"), logLevel: LogLevel.Debug, formatString: "Encrypting to Windows DPAPI for current user account ({Name})."); _encryptingToWindowsDPAPINGUsingProtectionDescriptorRule = LoggerMessage.Define <string>( eventId: new EventId(27, "EncryptingToWindowsDPAPINGUsingProtectionDescriptorRule"), logLevel: LogLevel.Debug, formatString: "Encrypting to Windows DPAPI-NG using protection descriptor rule '{DescriptorRule}'."); _anErrorOccurredWhileEncryptingToX509CertificateWithThumbprint = LoggerMessage.Define <string>( eventId: new EventId(28, "ErrorOccurredWhileEncryptingToX509CertificateWithThumbprint"), logLevel: LogLevel.Error, formatString: "An error occurred while encrypting to X.509 certificate with thumbprint '{Thumbprint}'."); _encryptingToX509CertificateWithThumbprint = LoggerMessage.Define <string>( eventId: new EventId(29, "EncryptingToX509CertificateWithThumbprint"), logLevel: LogLevel.Debug, formatString: "Encrypting to X.509 certificate with thumbprint '{Thumbprint}'."); _exceptionOccurredWhileTryingToResolveCertificateWithThumbprint = LoggerMessage.Define <string>( eventId: new EventId(30, "ExceptionOccurredWhileTryingToResolveCertificateWithThumbprint"), logLevel: LogLevel.Error, formatString: "An exception occurred while trying to resolve certificate with thumbprint '{Thumbprint}'."); _performingProtectOperationToKeyWithPurposes = LoggerMessage.Define <Guid, string>( eventId: new EventId(31, "PerformingProtectOperationToKeyWithPurposes"), logLevel: LogLevel.Trace, formatString: "Performing protect operation to key {KeyId:B} with purposes {Purposes}."); _descriptorDeserializerTypeForKeyIs = LoggerMessage.Define <Guid, string>( eventId: new EventId(32, "DescriptorDeserializerTypeForKeyIs"), logLevel: LogLevel.Debug, formatString: "Descriptor deserializer type for key {KeyId:B} is '{AssemblyQualifiedName}'."); _keyEscrowSinkFoundWritingKeyToEscrow = LoggerMessage.Define <Guid>( eventId: new EventId(33, "KeyEscrowSinkFoundWritingKeyToEscrow"), logLevel: LogLevel.Debug, formatString: "Key escrow sink found. Writing key {KeyId:B} to escrow."); _noKeyEscrowSinkFoundNotWritingKeyToEscrow = LoggerMessage.Define <Guid>( eventId: new EventId(34, "NoKeyEscrowSinkFoundNotWritingKeyToEscrow"), logLevel: LogLevel.Debug, formatString: "No key escrow sink found. Not writing key {KeyId:B} to escrow."); _noXMLEncryptorConfiguredKeyMayBePersistedToStorageInUnencryptedForm = LoggerMessage.Define <Guid>( eventId: new EventId(35, "NoXMLEncryptorConfiguredKeyMayBePersistedToStorageInUnencryptedForm"), logLevel: LogLevel.Warning, formatString: "No XML encryptor configured. Key {KeyId:B} may be persisted to storage in unencrypted form."); _revokingKeyForReason = LoggerMessage.Define <Guid, DateTimeOffset, string?>( eventId: new EventId(36, "RevokingKeyForReason"), logLevel: LogLevel.Information, formatString: "Revoking key {KeyId:B} at {RevocationDate:u} for reason '{Reason}'."); _readingDataFromFile = LoggerMessage.Define <string>( eventId: new EventId(37, "ReadingDataFromFile"), logLevel: LogLevel.Debug, formatString: "Reading data from file '{FullPath}'."); _nameIsNotSafeFileName = LoggerMessage.Define <string, string>( eventId: new EventId(38, "NameIsNotSafeFileName"), logLevel: LogLevel.Debug, formatString: "The name '{FriendlyName}' is not a safe file name, using '{NewFriendlyName}' instead."); _writingDataToFile = LoggerMessage.Define <string>( eventId: new EventId(39, "WritingDataToFile"), logLevel: LogLevel.Information, formatString: "Writing data to file '{FileName}'."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { _readingDataFromRegistryKeyValue = LoggerMessage.Define <RegistryKey, string>( eventId: new EventId(40, "ReadingDataFromRegistryKeyValue"), logLevel: LogLevel.Debug, formatString: "Reading data from registry key '{RegistryKeyName}', value '{Value}'."); } _nameIsNotSafeRegistryValueName = LoggerMessage.Define <string, string>( eventId: new EventId(41, "NameIsNotSafeRegistryValueName"), logLevel: LogLevel.Debug, formatString: "The name '{FriendlyName}' is not a safe registry value name, using '{NewFriendlyName}' instead."); _decryptingSecretElementUsingWindowsDPAPING = LoggerMessage.Define <string?>( eventId: new EventId(42, "DecryptingSecretElementUsingWindowsDPAPING"), logLevel: LogLevel.Debug, formatString: "Decrypting secret element using Windows DPAPI-NG with protection descriptor rule '{DescriptorRule}'."); _exceptionOccurredTryingToDecryptElement = LoggerMessage.Define( eventId: new EventId(43, "ExceptionOccurredTryingToDecryptElement"), logLevel: LogLevel.Error, formatString: "An exception occurred while trying to decrypt the element."); _encryptingUsingNullEncryptor = LoggerMessage.Define( eventId: new EventId(44, "EncryptingUsingNullEncryptor"), logLevel: LogLevel.Warning, formatString: "Encrypting using a null encryptor; secret information isn't being protected."); _usingEphemeralDataProtectionProvider = LoggerMessage.Define( eventId: new EventId(45, "UsingEphemeralDataProtectionProvider"), logLevel: LogLevel.Information, formatString: "Using ephemeral data protection provider. Payloads will be undecipherable upon application shutdown."); _existingCachedKeyRingIsExpiredRefreshing = LoggerMessage.Define( eventId: new EventId(46, "ExistingCachedKeyRingIsExpiredRefreshing"), logLevel: LogLevel.Debug, formatString: "Existing cached key ring is expired. Refreshing."); _errorOccurredWhileRefreshingKeyRing = LoggerMessage.Define( eventId: new EventId(47, "ErrorOccurredWhileRefreshingKeyRing"), logLevel: LogLevel.Error, formatString: "An error occurred while refreshing the key ring. Will try again in 2 minutes."); _errorOccurredWhileReadingKeyRing = LoggerMessage.Define( eventId: new EventId(48, "ErrorOccurredWhileReadingKeyRing"), logLevel: LogLevel.Error, formatString: "An error occurred while reading the key ring."); _keyRingDoesNotContainValidDefaultKey = LoggerMessage.Define( eventId: new EventId(49, "KeyRingDoesNotContainValidDefaultKey"), logLevel: LogLevel.Error, formatString: "The key ring does not contain a valid default key, and the key manager is configured with auto-generation of keys disabled."); _usingInmemoryRepository = LoggerMessage.Define( eventId: new EventId(50, "UsingInMemoryRepository"), logLevel: LogLevel.Warning, formatString: "Using an in-memory repository. Keys will not be persisted to storage."); _decryptingSecretElementUsingWindowsDPAPI = LoggerMessage.Define( eventId: new EventId(51, "DecryptingSecretElementUsingWindowsDPAPI"), logLevel: LogLevel.Debug, formatString: "Decrypting secret element using Windows DPAPI."); _defaultKeyExpirationImminentAndRepository = LoggerMessage.Define( eventId: new EventId(52, "DefaultKeyExpirationImminentAndRepository"), logLevel: LogLevel.Debug, formatString: "Default key expiration imminent and repository contains no viable successor. Caller should generate a successor."); _repositoryContainsNoViableDefaultKey = LoggerMessage.Define( eventId: new EventId(53, "RepositoryContainsNoViableDefaultKey"), logLevel: LogLevel.Debug, formatString: "Repository contains no viable default key. Caller should generate a key with immediate activation."); _errorOccurredWhileEncryptingToWindowsDPAPI = LoggerMessage.Define( eventId: new EventId(54, "ErrorOccurredWhileEncryptingToWindowsDPAPI"), logLevel: LogLevel.Error, formatString: "An error occurred while encrypting to Windows DPAPI."); _encryptingToWindowsDPAPIForLocalMachineAccount = LoggerMessage.Define( eventId: new EventId(55, "EncryptingToWindowsDPAPIForLocalMachineAccount"), logLevel: LogLevel.Debug, formatString: "Encrypting to Windows DPAPI for local machine account."); _errorOccurredWhileEncryptingToWindowsDPAPING = LoggerMessage.Define( eventId: new EventId(56, "ErrorOccurredWhileEncryptingToWindowsDPAPING"), logLevel: LogLevel.Error, formatString: "An error occurred while encrypting to Windows DPAPI-NG."); _policyResolutionStatesThatANewKeyShouldBeAddedToTheKeyRing = LoggerMessage.Define( eventId: new EventId(57, "PolicyResolutionStatesThatANewKeyShouldBeAddedToTheKeyRing"), logLevel: LogLevel.Debug, formatString: "Policy resolution states that a new key should be added to the key ring."); _creatingKey = LoggerMessage.Define <Guid, DateTimeOffset, DateTimeOffset, DateTimeOffset>( eventId: new EventId(58, "CreatingKey"), logLevel: LogLevel.Information, formatString: "Creating key {KeyId:B} with creation date {CreationDate:u}, activation date {ActivationDate:u}, and expiration date {ExpirationDate:u}."); _usingEphemeralKeyRepository = LoggerMessage.Define( eventId: new EventId(59, "UsingEphemeralKeyRepository"), logLevel: LogLevel.Warning, formatString: "Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits."); _usingEphemeralFileSystemLocationInContainer = LoggerMessage.Define <string>( eventId: new EventId(60, "UsingEphemeralFileSystemLocationInContainer"), logLevel: LogLevel.Warning, formatString: Resources.FileSystem_EphemeralKeysLocationInContainer); _usingRegistryAsKeyRepositoryWithDPAPI = LoggerMessage.Define <string>( eventId: new EventId(61, "UsingRegistryAsKeyRepositoryWithDPAPI"), logLevel: LogLevel.Information, formatString: "User profile not available. Using '{Name}' as key repository and Windows DPAPI to encrypt keys at rest."); _usingProfileAsKeyRepository = LoggerMessage.Define <string>( eventId: new EventId(62, "UsingProfileAsKeyRepository"), logLevel: LogLevel.Information, formatString: "User profile is available. Using '{FullName}' as key repository; keys will not be encrypted at rest."); _usingProfileAsKeyRepositoryWithDPAPI = LoggerMessage.Define <string>( eventId: new EventId(63, "UsingProfileAsKeyRepositoryWithDPAPI"), logLevel: LogLevel.Information, formatString: "User profile is available. Using '{FullName}' as key repository and Windows DPAPI to encrypt keys at rest."); _usingAzureAsKeyRepository = LoggerMessage.Define <string>( eventId: new EventId(64, "UsingAzureAsKeyRepository"), logLevel: LogLevel.Information, formatString: "Azure Web Sites environment detected. Using '{FullName}' as key repository; keys will not be encrypted at rest."); _keyRingWasLoadedOnStartup = LoggerMessage.Define <Guid>( eventId: new EventId(65, "KeyRingWasLoadedOnStartup"), logLevel: LogLevel.Debug, formatString: "Key ring with default key {KeyId:B} was loaded during application startup."); _keyRingFailedToLoadOnStartup = LoggerMessage.Define( eventId: new EventId(66, "KeyRingFailedToLoadOnStartup"), logLevel: LogLevel.Information, formatString: "Key ring failed to load during application startup."); }
public static BuildTargetResult CompileCoreHost(BuildTargetContext c) { var hostVersion = c.BuildContext.Get <HostVersion>("HostVersion"); var configuration = c.BuildContext.Get <string>("Configuration"); string rid = c.BuildContext.Get <string>("TargetRID"); string platform = c.BuildContext.Get <string>("Platform"); // Generate build files var cmakeOut = Path.Combine(Dirs.CorehostLatest, "cmake"); Rmdir(cmakeOut); Mkdirp(cmakeOut); // Run the build string corehostSrcDir = Path.Combine(c.BuildContext.BuildDirectory, "src", "corehost"); string commitHash = c.BuildContext.Get <string>("CommitHash"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Create .rc files on Windows. var resourceDir = GenerateVersionResource(c); if (configuration.Equals("Release")) { // Cmake calls it "RelWithDebInfo" in the generated MSBuild configuration = "RelWithDebInfo"; } // Why does Windows directly call cmake but Linux/Mac calls "build.sh" in the corehost dir? // See the comment in "src/corehost/build.sh" for details. It doesn't work for some reason. string cmakeBaseRid, visualStudio, archMacro, arch; string ridMacro = $"-DCLI_CMAKE_RUNTIME_ID:STRING={rid}"; string cmakeHostVer = $"-DCLI_CMAKE_HOST_VER:STRING={hostVersion.LatestHostVersion.ToString()}"; string cmakeHostPolicyVer = $"-DCLI_CMAKE_HOST_POLICY_VER:STRING={hostVersion.LatestHostPolicyVersion.ToString()}"; string cmakeHostFxrVer = $"-DCLI_CMAKE_HOST_FXR_VER:STRING={hostVersion.LatestHostFxrVersion.ToString()}"; string cmakeCommitHash = $"-DCLI_CMAKE_COMMIT_HASH:STRING={commitHash}"; string cmakeResourceDir = $"-DCLI_CMAKE_RESOURCE_DIR:STRING={resourceDir}"; switch (platform.ToLower()) { case "x86": cmakeBaseRid = "-DCLI_CMAKE_PKG_RID:STRING=win7-x86"; visualStudio = "Visual Studio 14 2015"; archMacro = "-DCLI_CMAKE_PLATFORM_ARCH_I386=1"; arch = "x86"; break; case "arm64": cmakeBaseRid = "-DCLI_CMAKE_PKG_RID:STRING=win10-arm64"; visualStudio = "Visual Studio 14 2015 Win64"; archMacro = "-DCLI_CMAKE_PLATFORM_ARCH_ARM64=1"; arch = "arm64"; if (Environment.GetEnvironmentVariable("__ToolsetDir") == null) { throw new Exception("Toolset Dir must be set when the Platform is ARM64"); } break; case "x64": cmakeBaseRid = "-DCLI_CMAKE_PKG_RID:STRING=win7-x64"; visualStudio = "Visual Studio 14 2015 Win64"; archMacro = "-DCLI_CMAKE_PLATFORM_ARCH_AMD64=1"; arch = "x64"; break; default: throw new PlatformNotSupportedException("Target Architecture: " + platform + " is not currently supported."); } ExecIn(cmakeOut, "cmake", corehostSrcDir, archMacro, ridMacro, cmakeHostVer, cmakeHostFxrVer, cmakeHostPolicyVer, cmakeBaseRid, cmakeCommitHash, cmakeResourceDir, "-G", visualStudio); var pf32 = RuntimeInformation.OSArchitecture == Architecture.X64 ? Environment.GetEnvironmentVariable("ProgramFiles(x86)") : Environment.GetEnvironmentVariable("ProgramFiles"); string msbuildPath = Path.Combine(pf32, "MSBuild", "14.0", "Bin", "MSBuild.exe"); string cmakeOutPath = Path.Combine(cmakeOut, "ALL_BUILD.vcxproj"); string configParameter = $"/p:Configuration={configuration}"; if (arch == "arm64") { Exec(msbuildPath, cmakeOutPath, configParameter, "/p:useEnv=true"); } else { Exec(msbuildPath, cmakeOutPath, configParameter); } // Copy the output out File.Copy(Path.Combine(cmakeOut, "cli", "exe", configuration, "dotnet.exe"), Path.Combine(Dirs.CorehostLatest, "dotnet.exe"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "exe", configuration, "dotnet.pdb"), Path.Combine(Dirs.CorehostLatest, "dotnet.pdb"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "dll", configuration, "hostpolicy.dll"), Path.Combine(Dirs.CorehostLatest, "hostpolicy.dll"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "dll", configuration, "hostpolicy.pdb"), Path.Combine(Dirs.CorehostLatest, "hostpolicy.pdb"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "fxr", configuration, "hostfxr.dll"), Path.Combine(Dirs.CorehostLatest, "hostfxr.dll"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "fxr", configuration, "hostfxr.pdb"), Path.Combine(Dirs.CorehostLatest, "hostfxr.pdb"), overwrite: true); } else { ExecIn(cmakeOut, Path.Combine(c.BuildContext.BuildDirectory, "src", "corehost", "build.sh"), "--arch", "x64", "--hostver", hostVersion.LatestHostVersion.ToString(), "--fxrver", hostVersion.LatestHostFxrVersion.ToString(), "--policyver", hostVersion.LatestHostPolicyVersion.ToString(), "--rid", rid, "--commithash", commitHash); // Copy the output out File.Copy(Path.Combine(cmakeOut, "cli", "exe", "dotnet"), Path.Combine(Dirs.CorehostLatest, "dotnet"), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "dll", HostArtifactNames.HostPolicyBaseName), Path.Combine(Dirs.CorehostLatest, HostArtifactNames.HostPolicyBaseName), overwrite: true); File.Copy(Path.Combine(cmakeOut, "cli", "fxr", HostArtifactNames.DotnetHostFxrBaseName), Path.Combine(Dirs.CorehostLatest, HostArtifactNames.DotnetHostFxrBaseName), overwrite: true); } return(c.Success()); }
public StatsService(DiscordSocketClient client, CommandHandler cmdHandler, IBotCredentials creds, EvilMortyBot evilMorty, IDataCache cache) { _log = LogManager.GetCurrentClassLogger(); _client = client; _creds = creds; _redis = cache.Redis; _started = DateTime.UtcNow; _client.MessageReceived += _ => Task.FromResult(Interlocked.Increment(ref _messageCounter)); cmdHandler.CommandExecuted += (_, e) => Task.FromResult(Interlocked.Increment(ref _commandsRan)); _client.ChannelCreated += (c) => { var _ = Task.Run(() => { if (c is ITextChannel) { Interlocked.Increment(ref _textChannels); } else if (c is IVoiceChannel) { Interlocked.Increment(ref _voiceChannels); } }); return(Task.CompletedTask); }; _client.ChannelDestroyed += (c) => { var _ = Task.Run(() => { if (c is ITextChannel) { Interlocked.Decrement(ref _textChannels); } else if (c is IVoiceChannel) { Interlocked.Decrement(ref _voiceChannels); } }); return(Task.CompletedTask); }; _client.GuildAvailable += (g) => { var _ = Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; Interlocked.Add(ref _textChannels, tc); Interlocked.Add(ref _voiceChannels, vc); }); return(Task.CompletedTask); }; _client.JoinedGuild += (g) => { var _ = Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; Interlocked.Add(ref _textChannels, tc); Interlocked.Add(ref _voiceChannels, vc); }); return(Task.CompletedTask); }; _client.GuildUnavailable += (g) => { var _ = Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; Interlocked.Add(ref _textChannels, -tc); Interlocked.Add(ref _voiceChannels, -vc); }); return(Task.CompletedTask); }; _client.LeftGuild += (g) => { var _ = Task.Run(() => { var tc = g.Channels.Count(cx => cx is ITextChannel); var vc = g.Channels.Count - tc; Interlocked.Add(ref _textChannels, -tc); Interlocked.Add(ref _voiceChannels, -vc); }); return(Task.CompletedTask); }; if (_client.ShardId == 0) { _carbonitexTimer = new Timer(async(state) => { if (string.IsNullOrWhiteSpace(_creds.CarbonKey)) { return; } try { using (var http = new HttpClient()) { using (var content = new FormUrlEncodedContent( new Dictionary <string, string> { { "servercount", evilMorty.GuildCount.ToString() }, { "key", _creds.CarbonKey } })) { content.Headers.Clear(); content.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); await http.PostAsync("https://www.carbonitex.net/discord/data/botdata.php", content).ConfigureAwait(false); } } } catch { // ignored } }, null, TimeSpan.FromHours(1), TimeSpan.FromHours(1)); } _botlistTimer = new Timer(async(state) => { if (string.IsNullOrWhiteSpace(_creds.BotListToken)) { return; } try { using (var http = new HttpClient()) { using (var content = new FormUrlEncodedContent( new Dictionary <string, string> { { "shard_count", _creds.TotalShards.ToString() }, { "shard_id", client.ShardId.ToString() }, { "server_count", client.Guilds.Count().ToString() } })) { content.Headers.Clear(); content.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); http.DefaultRequestHeaders.Add("Authorization", _creds.BotListToken); await http.PostAsync($"https://discordbots.org/api/bots/{client.CurrentUser.Id}/stats", content).ConfigureAwait(false); } } } catch (Exception ex) { _log.Error(ex); // ignored } }, null, TimeSpan.FromMinutes(5), TimeSpan.FromHours(1)); var platform = "other"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { platform = "linux"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { platform = "osx"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { platform = "windows"; } _dataTimer = new Timer(async(state) => { try { using (var http = new HttpClient()) { using (var content = new FormUrlEncodedContent( new Dictionary <string, string> { { "id", string.Concat(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(_creds.ClientId.ToString())).Select(x => x.ToString("X2"))) }, { "guildCount", evilMorty.GuildCount.ToString() }, { "version", BotVersion }, { "platform", platform } })) { content.Headers.Clear(); content.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); await http.PostAsync("https://selfstats.evilMortybot.me/", content).ConfigureAwait(false); } } } catch { // ignored } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromHours(1)); }
private void ProcessInputFileList( ITaskItem[] inputFiles, List <ITaskItem> imageCompilationList, List <ITaskItem> symbolsCompilationList, List <ITaskItem> r2rFilesPublishList, List <ITaskItem> r2rReferenceList, bool hasValidDiaSymReaderLib) { if (inputFiles == null) { return; } // TODO: ExcludeList for composite mode var exclusionSet = ExcludeList == null || Crossgen2Composite ? null : new HashSet <string>(ExcludeList, StringComparer.OrdinalIgnoreCase); foreach (var file in inputFiles) { var eligibility = GetInputFileEligibility(file, exclusionSet); if (eligibility == Eligibility.None) { continue; } r2rReferenceList.Add(file); if (!Crossgen2Composite && (eligibility == Eligibility.ReferenceOnly)) { continue; } var outputR2RImageRelativePath = file.GetMetadata(MetadataKeys.RelativePath); var outputR2RImage = Path.Combine(OutputPath, outputR2RImageRelativePath); string outputPDBImage = null; string outputPDBImageRelativePath = null; string crossgen1CreatePDBCommand = null; if (EmitSymbols) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && hasValidDiaSymReaderLib) { outputPDBImage = Path.ChangeExtension(outputR2RImage, "ni.pdb"); outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, "ni.pdb"); crossgen1CreatePDBCommand = $"/CreatePDB \"{Path.GetDirectoryName(outputPDBImage)}\""; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { using (FileStream fs = new FileStream(file.ItemSpec, FileMode.Open, FileAccess.Read)) { PEReader pereader = new PEReader(fs); MetadataReader mdReader = pereader.GetMetadataReader(); Guid mvid = mdReader.GetGuid(mdReader.GetModuleDefinition().Mvid); outputPDBImage = Path.ChangeExtension(outputR2RImage, "ni.{" + mvid + "}.map"); outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, "ni.{" + mvid + "}.map"); crossgen1CreatePDBCommand = $"/CreatePerfMap \"{Path.GetDirectoryName(outputPDBImage)}\""; } } } if (!Crossgen2Composite) { // This TaskItem is the IL->R2R entry, for an input assembly that needs to be compiled into a R2R image. This will be used as // an input to the ReadyToRunCompiler task TaskItem r2rCompilationEntry = new TaskItem(file); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputR2RImage, outputR2RImage); if (outputPDBImage != null && ReadyToRunUseCrossgen2 && !_crossgen2IsVersion5) { r2rCompilationEntry.SetMetadata(MetadataKeys.EmitSymbols, "true"); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, Path.GetDirectoryName(outputPDBImage)); } r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec); imageCompilationList.Add(r2rCompilationEntry); } else if (file.ItemSpec == MainAssembly.ItemSpec) { // Create a TaskItem for <MainAssembly>.r2r.dll var compositeR2RImageRelativePath = file.GetMetadata(MetadataKeys.RelativePath); compositeR2RImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, "r2r" + Path.GetExtension(compositeR2RImageRelativePath)); var compositeR2RImage = Path.Combine(OutputPath, compositeR2RImageRelativePath); string compositePDBImage = null; string compositePDBRelativePath = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && hasValidDiaSymReaderLib) { compositePDBImage = Path.ChangeExtension(compositeR2RImage, ".ni.pdb"); compositePDBRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, ".ni.pdb"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { compositePDBImage = Path.ChangeExtension(compositeR2RImage, ".ni.{composite}.map"); compositePDBRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, ".ni.{composite}.map"); } TaskItem r2rCompilationEntry = new TaskItem(file); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputR2RImage, compositeR2RImage); if (compositePDBImage != null && ReadyToRunUseCrossgen2 && !_crossgen2IsVersion5) { r2rCompilationEntry.SetMetadata(MetadataKeys.EmitSymbols, "true"); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, Path.GetDirectoryName(compositePDBImage)); // Publish composite PDB file TaskItem r2rSymbolsFileToPublish = new TaskItem(file); r2rSymbolsFileToPublish.ItemSpec = compositePDBImage; r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositePDBRelativePath); r2rSymbolsFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); if (!IncludeSymbolsInSingleFile) { r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.ExcludeFromSingleFile, "true"); } r2rFilesPublishList.Add(r2rSymbolsFileToPublish); } r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec); imageCompilationList.Add(r2rCompilationEntry); // Publish it TaskItem compositeR2RFileToPublish = new TaskItem(file); compositeR2RFileToPublish.ItemSpec = compositeR2RImage; compositeR2RFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); compositeR2RFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositeR2RImageRelativePath); r2rFilesPublishList.Add(compositeR2RFileToPublish); } // This TaskItem corresponds to the output R2R image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path // for the newly created R2R image TaskItem r2rFileToPublish = new TaskItem(file); r2rFileToPublish.ItemSpec = outputR2RImage; r2rFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); r2rFilesPublishList.Add(r2rFileToPublish); // Note: ReadyToRun PDB/Map files are not needed for debugging. They are only used for profiling, therefore the default behavior is to not generate them // unless an explicit PublishReadyToRunEmitSymbols flag is enabled by the app developer. There is also another way to profile that the runtime supports, which does // not rely on the native PDBs/Map files, so creating them is really an opt-in option, typically used by advanced users. // For debugging, only the IL PDBs are required. if (!Crossgen2Composite && outputPDBImage != null) { if (!ReadyToRunUseCrossgen2 || _crossgen2IsVersion5) { // This TaskItem is the R2R->R2RPDB entry, for a R2R image that was just created, and for which we need to create native PDBs. This will be used as // an input to the ReadyToRunCompiler task TaskItem pdbCompilationEntry = new TaskItem(file); pdbCompilationEntry.ItemSpec = outputR2RImage; pdbCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, outputPDBImage); pdbCompilationEntry.SetMetadata(MetadataKeys.CreatePDBCommand, crossgen1CreatePDBCommand); symbolsCompilationList.Add(pdbCompilationEntry); } // This TaskItem corresponds to the output PDB image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path // for the newly created PDB image. TaskItem r2rSymbolsFileToPublish = new TaskItem(file); r2rSymbolsFileToPublish.ItemSpec = outputPDBImage; r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.RelativePath, outputPDBImageRelativePath); r2rSymbolsFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); if (!IncludeSymbolsInSingleFile) { r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.ExcludeFromSingleFile, "true"); } r2rFilesPublishList.Add(r2rSymbolsFileToPublish); } } }
public override bool CanRunOnPlatform() { return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); }
private string GetRuntimeDataRootPathString() { return(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? GetWindowsRuntimeDataRoot() : GetNonWindowsRuntimeDataRoot()); }
static int Main(string[] args) { var maxLevel = LogLevel.Information; var serviceCollection = new ServiceCollection() .AddLogging(builder => { builder .AddFilter(level => level >= maxLevel) .AddConsole(options => { options.IncludeScopes = true; }); }); var serviceProvider = serviceCollection.BuildServiceProvider(); var logger = serviceProvider.GetRequiredService <ILogger <Program> >(); if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Console.Error.WriteLine("Azure Sign Tool is only supported on Windows."); return(E_PLATFORMNOTSUPPORTED); } var application = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "azuresigntool", FullName = "Azure Sign Tool", }; var signCommand = application.Command("sign", throwOnUnexpectedArg: false, configuration: cfg => { cfg.Description = "Signs a file."; var fileDigestAlgorithm = cfg.Option("-fd | --file-digest", "The digest algorithm to hash the file with.", CommandOptionType.SingleValue); var azureKeyVaultUrl = cfg.Option("-kvu | --azure-key-vault-url", "The URL to an Azure Key Vault.", CommandOptionType.SingleValue); var azureKeyVaultClientId = cfg.Option("-kvi | --azure-key-vault-client-id", "The Client ID to authenticate to the Azure Key Vault.", CommandOptionType.SingleValue); var azureKeyVaultClientSecret = cfg.Option("-kvs | --azure-key-vault-client-secret", "The Client Secret to authenticate to the Azure Key Vault.", CommandOptionType.SingleValue); var azureKeyVaultCertificateName = cfg.Option("-kvc | --azure-key-vault-certificate", "The name of the certificate in Azure Key Vault.", CommandOptionType.SingleValue); var azureKeyVaultAccessToken = cfg.Option("-kva | --azure-key-vault-accesstoken", "The Access Token to authenticate to the Azure Key Vault.", CommandOptionType.SingleValue); var description = cfg.Option("-d | --description", "Provide a description of the signed content.", CommandOptionType.SingleValue); var descriptionUrl = cfg.Option("-du | --description-url", "Provide a URL with more information about the signed content.", CommandOptionType.SingleValue); var rfc3161TimeStamp = cfg.Option("-tr | --timestamp-rfc3161", "Specifies the RFC 3161 timestamp server's URL. If this option (or -t) is not specified, the signed file will not be timestamped.", CommandOptionType.SingleValue); var rfc3161Digest = cfg.Option("-td | --timestamp-digest", "Used with the -tr switch to request a digest algorithm used by the RFC 3161 timestamp server.", CommandOptionType.SingleValue); var acTimeStamp = cfg.Option("-t | --timestamp-authenticode", "Specify the timestamp server's URL. If this option is not present, the signed file will not be timestamped.", CommandOptionType.SingleValue); var additionalCertificates = cfg.Option("-ac | --additional-certificates", "Specify one or more certificates to include in the public certificate chain.", CommandOptionType.MultipleValue); var verbose = cfg.Option("-v | --verbose", "Include additional output.", CommandOptionType.NoValue); var quiet = cfg.Option("-q | --quiet", "Do not print any output to the console.", CommandOptionType.NoValue); var pageHashing = cfg.Option("-ph | --page-hashing", "Generate page hashes for executable files if supported.", CommandOptionType.NoValue); var noPageHashing = cfg.Option("-nph | --no-page-hashing", "Suppress page hashes for executable files if supported.", CommandOptionType.NoValue); var continueOnError = cfg.Option("-coe | --continue-on-error", "Continue signing multiple files if an error occurs.", CommandOptionType.NoValue); var inputFileList = cfg.Option("-ifl | --input-file-list", "A path to a file that contains a list of files, one per line, to sign.", CommandOptionType.SingleValue); var maxDegreeOfParallelism = cfg.Option("-mdop | --max-degree-of-parallelism", "The maximum number of concurrent signing operations.", CommandOptionType.SingleValue); var file = cfg.Argument("file", "The path to the file.", multipleValues: true); cfg.HelpOption("-? | -h | --help"); cfg.OnExecute(async() => { X509Certificate2Collection certificates; switch (GetAdditionalCertificates(additionalCertificates.Values, logger)) { case ErrorOr <X509Certificate2Collection> .Ok d: certificates = d.Value; break; case ErrorOr <X509Certificate2Collection> .Err err: logger.LogError(err.Error, err.Error.Message); return(E_INVALIDARG); default: logger.LogInformation("Failed to include additional certificates."); return(E_INVALIDARG); } if (!CheckMutuallyExclusive(logger, quiet, verbose)) { return(E_INVALIDARG); } if (quiet.HasValue()) { maxLevel = LogLevel.Critical; } else if (verbose.HasValue()) { maxLevel = LogLevel.Trace; } if (!CheckMutuallyExclusive(logger, acTimeStamp, rfc3161TimeStamp) | !CheckRequired(logger, azureKeyVaultUrl, azureKeyVaultCertificateName) | !CheckMutuallyExclusive(logger, pageHashing, noPageHashing)) { return(E_INVALIDARG); } if (!azureKeyVaultAccessToken.HasValue() && !CheckRequired(logger, azureKeyVaultClientId, azureKeyVaultClientSecret)) { return(E_INVALIDARG); } int?signingConcurrency = null; if (maxDegreeOfParallelism.HasValue()) { if (int.TryParse(maxDegreeOfParallelism.Value(), out var maxSigningConcurrency) && (maxSigningConcurrency > 0 || maxSigningConcurrency == -1)) { signingConcurrency = maxSigningConcurrency; } else { logger.LogInformation("Value specified for --max-degree-of-parallelism is not a valid value."); return(E_INVALIDARG); } } var listOfFilesToSign = new HashSet <string>(); listOfFilesToSign.UnionWith(file.Values); if (inputFileList.HasValue()) { if (!File.Exists(inputFileList.Value())) { logger.LogInformation($"Input file list {inputFileList.Value()} does not exist."); return(E_INVALIDARG); } listOfFilesToSign.UnionWith(File.ReadAllLines(inputFileList.Value()).Where(s => !string.IsNullOrWhiteSpace(s))); } if (listOfFilesToSign.Count == 0) { logger.LogError("File or list of files is required."); return(E_INVALIDARG); } foreach (var filePath in listOfFilesToSign) { try { if (!File.Exists(filePath)) { logger.LogInformation($"File {filePath} does not exist or does not have permission."); return(E_FILE_NOT_FOUND); } } catch { logger.LogInformation($"File {filePath} does not exist or does not have permission."); return(E_FILE_NOT_FOUND); } } var configuration = new AzureKeyVaultSignConfigurationSet { AzureKeyVaultUrl = azureKeyVaultUrl.Value(), AzureKeyVaultCertificateName = azureKeyVaultCertificateName.Value(), AzureClientId = azureKeyVaultClientId.Value(), AzureAccessToken = azureKeyVaultAccessToken.Value(), AzureClientSecret = azureKeyVaultClientSecret.Value(), }; var digestAlgorithm = GetValueFromOption(fileDigestAlgorithm, AlgorithmFromInput, HashAlgorithmName.SHA256); TimeStampConfiguration timeStampConfiguration; if (rfc3161TimeStamp.HasValue()) { timeStampConfiguration = new TimeStampConfiguration( rfc3161TimeStamp.Value(), GetValueFromOption(rfc3161Digest, AlgorithmFromInput, HashAlgorithmName.SHA256), TimeStampType.RFC3161 ); } else if (acTimeStamp.HasValue()) { timeStampConfiguration = new TimeStampConfiguration( acTimeStamp.Value(),
[ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))] // This test is being fixed as part of issue #1441. public void MatrixFactorizationSimpleTrainAndPredict() { var mlContext = new MLContext(seed: 1, conc: 1); // Specific column names of the considered data set string labelColumnName = "Label"; string userColumnName = "User"; string itemColumnName = "Item"; string scoreColumnName = "Score"; // Create reader for both of training and test data sets var reader = new TextLoader(mlContext, GetLoaderArgs(labelColumnName, userColumnName, itemColumnName)); // Read training data as an IDataView object var data = reader.Read(new MultiFileSource(GetDataPath(TestDatasets.trivialMatrixFactorization.trainFilename))); // Create a pipeline with a single operator. var pipeline = mlContext.Recommendation().Trainers.MatrixFactorization(userColumnName, itemColumnName, labelColumnName, advancedSettings: s => { s.NumIterations = 3; s.NumThreads = 1; // To eliminate randomness, # of threads must be 1. s.K = 7; }); // Train a matrix factorization model. var model = pipeline.Fit(data); // Read the test data set as an IDataView var testData = reader.Read(new MultiFileSource(GetDataPath(TestDatasets.trivialMatrixFactorization.testFilename))); // Apply the trained model to the test set var prediction = model.Transform(testData); // Get output schema and check its column names var outputSchema = model.GetOutputSchema(data.Schema); var expectedOutputNames = new string[] { labelColumnName, userColumnName, itemColumnName, scoreColumnName }; foreach (var col in outputSchema) { Assert.True(col.Name == expectedOutputNames[col.Index]); } // Retrieve label column's index from the test IDataView testData.Schema.TryGetColumnIndex(labelColumnName, out int labelColumnId); // Retrieve score column's index from the IDataView produced by the trained model prediction.Schema.TryGetColumnIndex(scoreColumnName, out int scoreColumnId); // Compute prediction errors var metrices = mlContext.Recommendation().Evaluate(prediction, label: labelColumnName, score: scoreColumnName); // Determine if the selected metric is reasonable for different platforms double tolerance = Math.Pow(10, -7); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // Linux case var expectedUnixL2Error = 0.616821448679879; // Linux baseline Assert.InRange(metrices.L2, expectedUnixL2Error - tolerance, expectedUnixL2Error + tolerance); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { // The Mac case is just broken. Should be fixed later. Re-enable when done. // Mac case //var expectedMacL2Error = 0.61192207960271; // Mac baseline //Assert.InRange(metrices.L2, expectedMacL2Error - 5e-3, expectedMacL2Error + 5e-3); // 1e-7 is too small for Mac so we try 1e-5 } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Windows case var expectedWindowsL2Error = 0.61528733643754685; // Windows baseline Assert.InRange(metrices.L2, expectedWindowsL2Error - tolerance, expectedWindowsL2Error + tolerance); } var modelWithValidation = pipeline.Train(data, testData); }
public Task <int> Analyze(FileInfo dump_path, string[] command) { _consoleProvider.WriteLine($"Loading core dump: {dump_path} ..."); // Attempt to load the persisted command history string dotnetHome; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { dotnetHome = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), ".dotnet"); } else { dotnetHome = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".dotnet"); } string historyFileName = Path.Combine(dotnetHome, "dotnet-dump.history"); try { string[] history = File.ReadAllLines(historyFileName); _consoleProvider.AddCommandHistory(history); } catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is SecurityException) { } // Load any extra extensions LoadExtensions(); try { using DataTarget dataTarget = DataTarget.LoadDump(dump_path.FullName); OSPlatform targetPlatform = dataTarget.DataReader.TargetPlatform; if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || dataTarget.DataReader.EnumerateModules().Any((module) => Path.GetExtension(module.FileName) == ".dylib")) { targetPlatform = OSPlatform.OSX; } _target = new TargetFromDataReader(dataTarget.DataReader, targetPlatform, this, _targetIdFactory++, dump_path.FullName); _contextService.SetCurrentTarget(_target); _target.ServiceProvider.AddServiceFactory <SOSHost>(() => new SOSHost(_contextService.Services)); // Automatically enable symbol server support, default cache and search for symbols in the dump directory _symbolService.AddSymbolServer(msdl: true, symweb: false, symbolServerPath: null, authToken: null, timeoutInMinutes: 0); _symbolService.AddCachePath(_symbolService.DefaultSymbolCache); _symbolService.AddDirectoryPath(Path.GetDirectoryName(dump_path.FullName)); // Run the commands from the dotnet-dump command line if (command != null) { foreach (string cmd in command) { _commandProcessor.Execute(cmd, _contextService.Services); if (_consoleProvider.Shutdown) { break; } } } if (!_consoleProvider.Shutdown && (!Console.IsOutputRedirected || Console.IsInputRedirected)) { // Start interactive command line processing _consoleProvider.WriteLine("Ready to process analysis commands. Type 'help' to list available commands or 'help [command]' to get detailed help on a command."); _consoleProvider.WriteLine("Type 'quit' or 'exit' to exit the session."); _consoleProvider.Start((string commandLine, CancellationToken cancellation) => { _commandProcessor.Execute(commandLine, _contextService.Services); }); } } catch (Exception ex) when (ex is ClrDiagnosticsException || ex is FileNotFoundException || ex is DirectoryNotFoundException || ex is UnauthorizedAccessException || ex is PlatformNotSupportedException || ex is InvalidDataException || ex is InvalidOperationException || ex is NotSupportedException) { _consoleProvider.WriteLine(OutputType.Error, $"{ex.Message}"); return(Task.FromResult(1)); } finally { if (_target != null) { DestroyTarget(_target); } // Persist the current command history try { File.WriteAllLines(historyFileName, _consoleProvider.GetCommandHistory()); } catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException || ex is NotSupportedException || ex is SecurityException) { } // Send shutdown event on exit OnShutdownEvent.Fire(); } return(Task.FromResult(0)); }