Skip to content

Needing to register httplistener stuff #2

@GoogolFisch

Description

@GoogolFisch

Under Windows there is a problem if you want to connect this to a LAN.
You can listen to traffic on the LAN with:

var server = new LightHttpServer();
var prefix = $"http://*:{port}/"; // connect to any ip
server.Listener.Prefixes.Add(prefix);

But when doing this, I needed to register the server with this command:
netsh http add urlacl url=http://*:{port} user=currentUser

Under Linux you don't need to register the server.

Dotnet-version: 8.0.115

Automatic register:

public static Task<int> RegisterHttp(string prefix)
{
	var source = new TaskCompletionSource<int>();
	//var source = new TaskCompletionSource<int>();
	string cmdArgs = $"http add urlacl url={prefix} user={System.Security.Principal.WindowsIdentity.GetCurrent().Name}";
	var process = new Process
	{
		StartInfo = new ProcessStartInfo
		{
			FileName = "netsh",
			Arguments = cmdArgs,
			//Verb = "runas", // if you aren't admin?
			RedirectStandardOutput = true,
			RedirectStandardError = true,
			UseShellExecute = false,
			CreateNoWindow = true
		},
		EnableRaisingEvents = true
	};
	process.Exited += (sender, args) =>
	{
		Console.WriteLine(process.StandardError.ReadToEnd());
		Console.WriteLine(process.StandardError.ReadToEnd());
		if (process.ExitCode == 0)
			source.SetResult(0);
		else
			source.SetException(new Exception($"Command `{cmd}` failed with exit code `{process.ExitCode}`"));

		process.Dispose();
	};

	try
	{
		process.Start();
	}
	catch (Exception e)
	{
          Console.WriteLine($"{cmd} has failed");
		//logger.LogError(e, "Command {} failed", cmd);
	}
	return source.Task;
}

how I call this function

bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
	ShellHelper.RegisterHttp(webServerUrl);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions