Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
XKaguya authored Dec 1, 2024
1 parent 3136f37 commit 72b37b2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 42 deletions.
2 changes: 2 additions & 0 deletions StoPasswordBook/StoPasswordBook/Extern/AutoUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.IO;
using System.Windows;
using log4net;
using StoPasswordBook;
using StoPasswordBook.Generic;
Expand Down Expand Up @@ -44,6 +45,7 @@ public static void CheckAndUpdate()
if (!File.Exists(commonUpdaterPath))
{
Log.Info("There's no CommonUpdater in the folder. Failed to update.");
MessageBox.Show("There's no CommonUpdater in the folder. Failed to update.\nIf you want get more supports, Please use the AutoUpdate feature");
return;
}

Expand Down
51 changes: 37 additions & 14 deletions StoPasswordBook/StoPasswordBook/Generic/Api.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Media;
Expand Down Expand Up @@ -187,14 +189,14 @@ private static void AppendElement(XmlDocument doc, XmlNode root, string name, st
root.AppendChild(element);
}

private static async Task Locate()
private static async Task Locate()
{
try
{
if (BrowserManager.Browser == null)
{
Log.Error("Browser is null.");
return;
throw new UnreachableException();
}

var page = await BrowserManager.Browser.NewPageAsync();
Expand All @@ -204,7 +206,7 @@ private static async Task Locate()

await pageHandler.NavigateTo(GlobalVariables.DebugUrl);
Log.Debug($"Attempting accessing {GlobalVariables.DebugUrl}");
MainWindow.UpdateText($"Attempting accessing {GlobalVariables.DebugUrl}");
MainWindow.UpdateText("Attempting accessing STO Launcher");

var linkElement = await pageHandler.QuerySelector("a[title='Cryptic Launcher | Star Trek']");
if (linkElement != null)
Expand All @@ -214,7 +216,7 @@ private static async Task Locate()
GlobalVariables.DebugUrl = hrefJson.ToString();
await page.GoToAsync(GlobalVariables.DebugUrl);
Log.Debug(GlobalVariables.DebugUrl);
MainWindow.UpdateText($"Attempting accessing {GlobalVariables.DebugUrl}");
MainWindow.UpdateText("Attempting accessing STO Launcher");
}

string pattern = @"ws=127\.0\.0\.1:\d+/devtools/page/([A-F0-9]{32})";
Expand All @@ -226,15 +228,13 @@ private static async Task Locate()
}
else
{
MainWindow.UpdateText("Launcher timed out.");
KillExistingInstances("Star Trek Online.exe");
throw new UnreachableException();
}

if (pageId == "null")
{
Log.Error("PageId is null.");
MainWindow.UpdateText("Please try again.");
return;
throw new UnreachableException();
}

string webSocketUrl = $"ws://localhost:{GlobalVariables.DebugPort}/devtools/page/{pageId}";
Expand All @@ -244,7 +244,7 @@ private static async Task Locate()

WebSocketManager.InitWebSocket(GlobalVariables.WebSocketUrl);

MainWindow.UpdateText("Done.", Brushes.Green);
MainWindow.UpdateText("Done! Please choose a Account for login.", Brushes.Green);
Log.Info("Api initialized.");

await page.CloseAsync();
Expand All @@ -254,8 +254,20 @@ private static async Task Locate()
catch (Exception ex)
{
Log.Error(ex.Message + ex.StackTrace);
throw new UnreachableException();
}
}

private static int GetAvailablePort()
{
using TcpListener listener = new TcpListener(IPAddress.Loopback, 0);

listener.Start();
IPEndPoint endpoint = (IPEndPoint)listener.LocalEndpoint;
listener.Stop();

return endpoint.Port;
}

public static async Task InitApi()
{
Expand All @@ -282,9 +294,8 @@ public static async Task InitApi()
SaveSettings();
Log.Debug(GlobalVariables.LauncherPath);

Random random = new Random();
int randomNum = random.Next(500, 65535);
GlobalVariables.DebugPort = randomNum;
int availablePort = GetAvailablePort();
GlobalVariables.DebugPort = availablePort;
Log.Debug(GlobalVariables.DebugPort);

if (!File.Exists(GlobalVariables.LauncherPath))
Expand All @@ -301,9 +312,21 @@ public static async Task InitApi()
Process.Start(processStartInfo);
Log.Debug($"Trying to start launcher with {processStartInfo.Arguments}");

await Task.Delay(TimeSpan.FromSeconds(GlobalVariables.WaitInterval));
await BrowserManager.InitBrowser();
await Locate();
await RetryLocate();
}

private static async Task RetryLocate()
{
try
{
await Locate();
}
catch (UnreachableException ex)
{
Log.Error($"Locate failed. Retrying...");
await RetryLocate();
}
}
}
}
3 changes: 0 additions & 3 deletions StoPasswordBook/StoPasswordBook/Generic/GlobalVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ public class GlobalVariables

[IgnoreSetting]
public static string WebSocketUrl { get; set; } = "null";

[Description("How many seconds the program will wait. \nDefault value: 5")]
public static int WaitInterval { get; set; } = 10;
}
}
21 changes: 0 additions & 21 deletions StoPasswordBook/StoPasswordBook/Generic/PageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,5 @@ public async Task NavigateTo(string url)
return null;
}
}

public async Task FillInputField(string selector, string value)
{
try
{
var element = await QuerySelector(selector);
if (element != null)
{
await element.EvaluateFunctionAsync("el => el.value = arguments[0]", value);
Console.WriteLine($"{selector} filled with {value}.");
}
else
{
Console.WriteLine($"Element {selector} not found.");
}
}
catch (Exception e)
{
Console.WriteLine($"Error filling input field {selector}: {e.Message}");
}
}
}
}
19 changes: 17 additions & 2 deletions StoPasswordBook/StoPasswordBook/Generic/WebSocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ private static void OnMessage(object? sender, MessageEventArgs ev)
{
rst = rst.Replace(LastAccount[0], "ACCOUNT_HIDDEN_DUE_TO_PRIVACY");
Log.Info($"Response From Username Column: {rst}");

MainWindow.UpdateText("Sent account and passwords to Launcher.", Brushes.CornflowerBlue);
return;
}

if (rst.Contains(LastAccount[1]))
{
rst = rst.Replace(LastAccount[1], "PASSWORD_HIDDEN_DUE_TO_PRIVACY");
Log.Info($"Response From Password Column: {rst}");

MainWindow.UpdateText("Sent account and passwords to Launcher.", Brushes.CornflowerBlue);
return;
}
}
Expand All @@ -65,10 +69,22 @@ private static void OnMessage(object? sender, MessageEventArgs ev)
}
}

public static bool SetUsernameAndPassword(WebSocket webSocket, string userStr, string pwdStr)
public static bool SetUsernameAndPassword(WebSocket? webSocket, string? userStr, string? pwdStr)
{
try
{
if (webSocket == null)
{
MainWindow.UpdateText("Websocket is null. Please try again.");
return false;
}

if (string.IsNullOrEmpty(userStr) || string.IsNullOrEmpty(pwdStr))
{
MainWindow.UpdateText("Account or Password is null or empty. Please check Shadow.xml");
return false;
}

Random random = new Random();
int random0 = random.Next(1, 10000);
int random1 = random.Next(1, 10000);
Expand Down Expand Up @@ -96,7 +112,6 @@ public static bool SetUsernameAndPassword(WebSocket webSocket, string userStr, s
}
};
webSocket.Send(JsonConvert.SerializeObject(setPassword));
MainWindow.UpdateText("Sent account and passwords to Launcher.", Brushes.CornflowerBlue);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions StoPasswordBook/StoPasswordBook/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace StoPasswordBook;
/// </summary>
public partial class MainWindow : FluentWindow
{
public static readonly string Version = "1.0.1";
public static readonly string Version = "1.0.2";

private static readonly ILog Log = LogManager.GetLogger(typeof(MainWindow));

Expand Down Expand Up @@ -98,7 +98,7 @@ private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
var selectedAccount = AccountComboBox.SelectedItem as KeyValuePair<string, string>?;

if (selectedAccount.HasValue)
if (selectedAccount != null && selectedAccount.HasValue)
{
var accountName = selectedAccount.Value.Key;
var selectedPassword = selectedAccount.Value.Value;
Expand Down

0 comments on commit 72b37b2

Please sign in to comment.