private static URIStartResult HandleURIStart(string[] args) { Console.WriteLine(GetExecutablePath()); const string appName = "AmongUsCapture"; mutex = new Mutex(true, appName, out bool createdNew); bool wasURIStart = args.Length > 0 && args[0].StartsWith(UriScheme + "://"); URIStartResult result = URIStartResult.CONTINUE; if (!createdNew) // send it to already existing instance if applicable, then close { if (wasURIStart) { var pipeClient = new NamedPipeClientStream(".", "AmongUsCapturePipe", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); pipeClient.Connect(); var ss = new StreamString(pipeClient); ss.WriteString(args[0]); pipeClient.Close(); } return(URIStartResult.CLOSE); } else if (wasURIStart) // URI start on new instance, continue as normal but also handle current argument { result = URIStartResult.PARSE; } RegisterProtocol(); return(result); }
public void RunLoop(string initialURI) { if (initialURI != null) { OnToken?.Invoke(this, StartToken.FromString(initialURI)); } while (true) { PipeSecurity ps = new PipeSecurity(); ps.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); NamedPipeServerStream pipeServer = NamedPipeServerStreamConstructors.New("AmongUsCapturePipe", PipeDirection.InOut, 1, pipeSecurity: ps); pipeServer.WaitForConnection(); Console.WriteLine("Client connected"); try { //Read the request from the client. Once the client has // written to the pipe its security token will be available. StreamString ss = new StreamString(pipeServer); string rawToken = ss.ReadString(); StartToken startToken = StartToken.FromString(rawToken); Console.WriteLine($@"Decoded message as {JsonConvert.SerializeObject(startToken, Formatting.Indented)}"); OnToken?.Invoke(this, startToken); } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Console.WriteLine(@"ERROR: {0}", e.Message); } pipeServer.Close(); } }