Remove hardcodes and implement WebClient
This commit is contained in:
parent
f25ba8adcd
commit
c316ef0092
11 changed files with 2062 additions and 75 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GsDChat.Client.Network;
|
||||
|
|
@ -11,7 +12,7 @@ public class Program
|
|||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
string serverUrl = "http://localhost:5000";
|
||||
string? serverUrl = null;
|
||||
string? username = null;
|
||||
string? password = null;
|
||||
string? serverPassword = null;
|
||||
|
|
@ -32,88 +33,189 @@ public class Program
|
|||
Console.WriteLine(" GsDChat - Private Community TUI");
|
||||
Console.WriteLine($"========================================{Ansi.Reset}");
|
||||
|
||||
var state = new ClientState
|
||||
{
|
||||
ServerUrl = serverUrl
|
||||
};
|
||||
var historyManager = new ServerHistoryManager();
|
||||
string? rememberedUsername = null;
|
||||
|
||||
using var connection = new ChatClientConnection(state, allowInsecure);
|
||||
|
||||
Console.Write($"Connecting to server at {serverUrl}... ");
|
||||
var info = await connection.GetServerInfoAsync();
|
||||
if (info == null)
|
||||
// If no server was specified via CLI, show interactive server selection menu
|
||||
while (string.IsNullOrEmpty(serverUrl))
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Failed to reach server!{Ansi.Reset}");
|
||||
Console.WriteLine("Ensure the server is running and the URL is correct.");
|
||||
return;
|
||||
}
|
||||
var servers = historyManager.LoadServers();
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightGreen}Connected!{Ansi.Reset}");
|
||||
Console.WriteLine($"Server: {info.ServerName} (v{info.Version})");
|
||||
if (!string.IsNullOrEmpty(info.Motd))
|
||||
{
|
||||
Console.WriteLine($"MOTD: {info.Motd}");
|
||||
}
|
||||
|
||||
if (info.RequiresServerPassword && string.IsNullOrEmpty(serverPassword))
|
||||
{
|
||||
Console.Write("Enter Server Password: ");
|
||||
serverPassword = ReadMaskedPassword();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
Console.Write("\nChoose action: [L]ogin or [R]egister: ");
|
||||
var choice = Console.ReadLine()?.Trim().ToUpperInvariant();
|
||||
if (choice == "R" || choice == "REGISTER")
|
||||
Console.WriteLine($"\n{Ansi.Bold}Choose a Server:{Ansi.Reset}");
|
||||
if (servers.Count > 0)
|
||||
{
|
||||
doRegister = true;
|
||||
for (int i = 0; i < servers.Count; i++)
|
||||
{
|
||||
var s = servers[i];
|
||||
string userTag = !string.IsNullOrEmpty(s.LastUsername) ? $" (@{s.LastUsername})" : "";
|
||||
Console.WriteLine($" {Ansi.BrightYellow}[{i + 1}]{Ansi.Reset} {Ansi.BrightWhite}{s.Name}{Ansi.Reset} {Ansi.Gray}({s.Url}){userTag}{Ansi.Reset}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($" {Ansi.Gray}(No saved servers yet){Ansi.Reset}");
|
||||
}
|
||||
|
||||
Console.Write("Username: ");
|
||||
username = Console.ReadLine()?.Trim();
|
||||
Console.WriteLine($" {Ansi.BrightCyan}[N]{Ansi.Reset} Connect to a New Server");
|
||||
if (servers.Count > 0)
|
||||
{
|
||||
Console.WriteLine($" {Ansi.BrightRed}[D]{Ansi.Reset} Delete a server from history");
|
||||
}
|
||||
Console.WriteLine($" {Ansi.Gray}[Q]{Ansi.Reset} Quit");
|
||||
|
||||
Console.Write("Password: ");
|
||||
password = ReadMaskedPassword();
|
||||
Console.Write($"\nSelect option: ");
|
||||
string? input = Console.ReadLine()?.Trim();
|
||||
|
||||
if (string.Equals(input, "Q", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Console.WriteLine("Goodbye.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.Equals(input, "D", StringComparison.OrdinalIgnoreCase) && servers.Count > 0)
|
||||
{
|
||||
Console.Write("Enter number to delete: ");
|
||||
if (int.TryParse(Console.ReadLine(), out int delIdx) && delIdx >= 1 && delIdx <= servers.Count)
|
||||
{
|
||||
historyManager.RemoveServer(servers[delIdx - 1].Id);
|
||||
Console.WriteLine($"{Ansi.Green}Removed server from history.{Ansi.Reset}");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(input, "N", StringComparison.OrdinalIgnoreCase) || servers.Count == 0 && string.IsNullOrEmpty(input))
|
||||
{
|
||||
Console.Write("\nEnter Server URL or IP (e.g. chat.example.com, http://localhost:5000): ");
|
||||
string? customUrl = Console.ReadLine()?.Trim();
|
||||
if (!string.IsNullOrEmpty(customUrl))
|
||||
{
|
||||
serverUrl = ServerHistoryManager.NormalizeUrl(customUrl);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (int.TryParse(input, out int sel) && sel >= 1 && sel <= servers.Count)
|
||||
{
|
||||
var chosen = servers[sel - 1];
|
||||
serverUrl = chosen.Url;
|
||||
rememberedUsername = chosen.LastUsername;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightRed}Invalid choice. Please select again.{Ansi.Reset}");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
||||
// Connection attempt loop
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Username and password are required.{Ansi.Reset}");
|
||||
return;
|
||||
var state = new ClientState
|
||||
{
|
||||
ServerUrl = serverUrl
|
||||
};
|
||||
|
||||
using var connection = new ChatClientConnection(state, allowInsecure);
|
||||
|
||||
Console.Write($"\nConnecting to server at {Ansi.BrightCyan}{serverUrl}{Ansi.Reset}... ");
|
||||
var info = await connection.GetServerInfoAsync();
|
||||
|
||||
if (info == null)
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Failed to reach server!{Ansi.Reset}");
|
||||
Console.Write("Would you like to [R]etry, choose [A]nother server, or [Q]uit? ");
|
||||
var failChoice = Console.ReadLine()?.Trim().ToUpperInvariant();
|
||||
if (failChoice == "A")
|
||||
{
|
||||
serverUrl = null;
|
||||
await Main(args); // Restart server selection
|
||||
return;
|
||||
}
|
||||
if (failChoice == "Q")
|
||||
{
|
||||
return;
|
||||
}
|
||||
continue; // Retry
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightGreen}Connected!{Ansi.Reset}");
|
||||
Console.WriteLine($"Server: {Ansi.Bold}{info.ServerName}{Ansi.Reset} (v{info.Version})");
|
||||
if (!string.IsNullOrEmpty(info.Motd))
|
||||
{
|
||||
Console.WriteLine($"MOTD: {info.Motd}");
|
||||
}
|
||||
|
||||
if (info.RequiresServerPassword && string.IsNullOrEmpty(serverPassword))
|
||||
{
|
||||
Console.Write("Enter Server Password: ");
|
||||
serverPassword = ReadMaskedPassword();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
Console.Write("\nChoose action: [L]ogin or [R]egister: ");
|
||||
var choice = Console.ReadLine()?.Trim().ToUpperInvariant();
|
||||
if (choice == "R" || choice == "REGISTER")
|
||||
{
|
||||
doRegister = true;
|
||||
}
|
||||
|
||||
string userPrompt = !string.IsNullOrEmpty(rememberedUsername)
|
||||
? $"Username [{rememberedUsername}]: "
|
||||
: "Username: ";
|
||||
|
||||
Console.Write(userPrompt);
|
||||
string? enteredUser = Console.ReadLine()?.Trim();
|
||||
username = string.IsNullOrEmpty(enteredUser) ? rememberedUsername : enteredUser;
|
||||
|
||||
Console.Write("Password: ");
|
||||
password = ReadMaskedPassword();
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Username and password are required.{Ansi.Reset}");
|
||||
username = null;
|
||||
password = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine(doRegister ? "Registering account..." : "Logging in...");
|
||||
|
||||
var authResult = doRegister
|
||||
? await connection.RegisterAsync(username, password, serverPassword)
|
||||
: await connection.LoginAsync(username, password, serverPassword);
|
||||
|
||||
if (!authResult.Success || string.IsNullOrEmpty(authResult.Token))
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Authentication failed: {authResult.ErrorMessage}{Ansi.Reset}");
|
||||
username = null;
|
||||
password = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightGreen}Success! Welcome @{authResult.Username} [{authResult.Role}]{Ansi.Reset}");
|
||||
|
||||
// Save server to history
|
||||
historyManager.SaveServer(serverUrl, info.ServerName, authResult.Username);
|
||||
|
||||
state.Token = authResult.Token;
|
||||
state.CurrentUserId = authResult.UserId ?? "";
|
||||
state.CurrentUsername = authResult.Username ?? username;
|
||||
state.CurrentUserRole = authResult.Role ?? "Member";
|
||||
|
||||
await connection.StartWebSocketAsync();
|
||||
|
||||
// Launch TUI
|
||||
using var cts = new CancellationTokenSource();
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
var ui = new ConsoleUi(state, connection);
|
||||
await ui.RunAsync(cts.Token);
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine(doRegister ? "Registering account..." : "Logging in...");
|
||||
|
||||
var authResult = doRegister
|
||||
? await connection.RegisterAsync(username, password, serverPassword)
|
||||
: await connection.LoginAsync(username, password, serverPassword);
|
||||
|
||||
if (!authResult.Success || string.IsNullOrEmpty(authResult.Token))
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Authentication failed: {authResult.ErrorMessage}{Ansi.Reset}");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightGreen}Success! Welcome @{authResult.Username} [{authResult.Role}]{Ansi.Reset}");
|
||||
|
||||
state.Token = authResult.Token;
|
||||
state.CurrentUserId = authResult.UserId ?? "";
|
||||
state.CurrentUsername = authResult.Username ?? username;
|
||||
state.CurrentUserRole = authResult.Role ?? "Member";
|
||||
|
||||
await connection.StartWebSocketAsync();
|
||||
|
||||
// Launch TUI
|
||||
using var cts = new CancellationTokenSource();
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
var ui = new ConsoleUi(state, connection);
|
||||
await ui.RunAsync(cts.Token);
|
||||
}
|
||||
|
||||
private static string ReadMaskedPassword()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue