Skip to content

Commit

Permalink
Try both hostnames, one after another, and take whichever succeeds.
Browse files Browse the repository at this point in the history
Instead of using always "127.0.0.1" or "localhost", try both (preferring "127.0.0.1").
  • Loading branch information
Jackabomb committed Jun 24, 2024
1 parent cbad236 commit 1f43911
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions KeeAnywhere/OAuth2/OidcSystemBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@ private static bool CreateListener(out string redirectUri, out HttpListener list
ports = Enumerable.Range(49215, 16321);
}

string[] hosts = { "127.0.0.1", "localhost" };
foreach (var port in ports)
{
redirectUri = CreateRedirectUri(port);
listener = new HttpListener();
listener.Prefixes.Add(redirectUri);
try
foreach (var host in hosts)
{
listener.Start();
redirectUri = CreateRedirectUri(host, port);
listener = new HttpListener();
listener.Prefixes.Add(redirectUri);
try
{
listener.Start();

return true;
}
catch
{
// nothing to do here -- the listener disposes itself when Start throws
return true;
}
catch
{
// nothing to do here -- the listener disposes itself when Start throws
}
}
}

Expand All @@ -58,9 +62,9 @@ private static bool CreateListener(out string redirectUri, out HttpListener list
return false;
}

private static string CreateRedirectUri(int port)
private static string CreateRedirectUri(string host, int port)
{
return "http://localhost:" + port + "/";
return "http://" + host + ":" + port + "/";
}

public string RedirectUri
Expand Down

0 comments on commit 1f43911

Please # to comment.