Code consistency fix, Replace Random to RandomNumberGenerator, Mutex dispose

This commit is contained in:
Namhyeon Go 2025-03-23 15:12:15 +09:00
parent d3135bef8e
commit c1052e0147
3 changed files with 16 additions and 9 deletions

View File

@ -28,6 +28,7 @@ namespace WelsonJS.Launcher
Application.Run(new MainForm());
mutex.ReleaseMutex();
mutex.Dispose();
}
public static void RunCommandPrompt(string workingDirectory, string entryFileName, string scriptName, bool isConsoleApplication = false, bool isInteractiveServiceAapplication = false)

View File

@ -105,7 +105,7 @@ namespace WelsonJS.Launcher.Tools
const string devtoolsPrefix = "devtools/";
if (path.StartsWith(devtoolsPrefix, StringComparison.OrdinalIgnoreCase))
{
await ServeDevTools(context, path.Substring(devtoolsPrefix.Length - 1));
await ServeDevTools(context, path.Substring(devtoolsPrefix.Length));
return;
}
@ -129,7 +129,7 @@ namespace WelsonJS.Launcher.Tools
const string tfaPrefix = "tfa/";
if (path.StartsWith(tfaPrefix, StringComparison.OrdinalIgnoreCase))
{
ServeTfaRequest(context, path.Substring(tfaPrefix.Length - 1));
ServeTfaRequest(context, path.Substring(tfaPrefix.Length));
return;
}
@ -178,7 +178,7 @@ namespace WelsonJS.Launcher.Tools
{
using (HttpClient client = new HttpClient())
{
string url = "http://localhost:9222" + endpoint;
string url = "http://localhost:9222/" + endpoint;
string data = await client.GetStringAsync(url);
ServeResource(context, data, "application/json");
@ -263,7 +263,7 @@ namespace WelsonJS.Launcher.Tools
{
Tfa _tfa = new Tfa();
if (endpoint.Equals("/pubkey"))
if (endpoint.Equals("pubkey"))
{
ServeResource(context, _tfa.GetPubKey(), "text/plain", 200);
return;

View File

@ -32,13 +32,19 @@ namespace WelsonJS.Launcher.Tools
public string GetPubKey()
{
var rand = new Random();
var key = new char[16];
for (int i = 0; i < 16; i++)
using (var rng = RandomNumberGenerator.Create())
{
key[i] = Base32Chars[rand.Next(Base32Chars.Length)];
var key = new char[16];
var randomBytes = new byte[16];
rng.GetBytes(randomBytes);
for (int i = 0; i < 16; i++)
{
key[i] = Base32Chars[randomBytes[i] % Base32Chars.Length];
}
return string.Join(" ", Enumerable.Range(0, 4).Select(i => new string(key, i * 4, 4)));
}
return string.Join(" ", Enumerable.Range(0, 4).Select(i => new string(key, i * 4, 4)));
}
private static byte[] DecodeBase32(string key)