Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Added a logger for easy debug
-> Renamed the rpc file
-> Added a ascii art for the start of the app
-> Some more debugging helpers
-> Added a hooker for PteroConsole LIB
  • Loading branch information
NaysKutzu committed Oct 3, 2023
1 parent f99371a commit 861548c
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 44 deletions.
1 change: 1 addition & 0 deletions Forms/FrmServerController.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 38 additions & 4 deletions Forms/FrmServerController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
namespace PteroController.Forms
using Newtonsoft.Json;
using PteroController.PteroConsoleHook;
using PteroController.Pterodactyl;
using System.Net;

namespace PteroController.Forms
{
public partial class FrmServerController : Form
{
private string? serverIdentifier;

public FrmServerController(string serverIdentifier)
{
InitializeComponent();
this.serverIdentifier = serverIdentifier;

}
private async void initPteroConsole()
{
try
{
var console = new PteroConsole.NET.PteroConsole();
console.RequestToken += pteroConsole =>
{
Console.WriteLine("Revoking token");
var wc = new WebClient();

Check warning on line 25 in Forms/FrmServerController.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 25 in Forms/FrmServerController.cs

View workflow job for this annotation

GitHub Actions / build (Release)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
wc.Headers.Add("Authorization", "Bearer " + Pterodactyl.User.Info.panel_api_key);
var raw = wc.DownloadString($"{Pterodactyl.User.Info.panel_url}/api/client/servers/{serverIdentifier}/websocket");
var data = JsonConvert.DeserializeObject<WebsocketDataResource>(raw).Data;
return data.Token;
};
var wc = new WebClient();
wc.Headers.Add("Authorization", "Bearer " + Pterodactyl.User.Info.panel_api_key);
var raw = wc.DownloadString($"{Pterodactyl.User.Info.panel_url}/api/client/servers/{serverIdentifier}/websocket");
var data = JsonConvert.DeserializeObject<WebsocketDataResource>(raw).Data;
await console.Connect(Pterodactyl.User.Info.panel_url, data.Socket, data.Token);
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Program.Alert("We are sorry but we can't launch the stats", FrmAlert.enmType.Warning);
}
}

private void FrmServerController_Load(object sender, EventArgs e)
{

initPteroConsole();
}

private void lblminimize_Click(object sender, EventArgs e)
Expand All @@ -38,6 +67,11 @@ private void btnservers_Click(object sender, EventArgs e)
x.Show();
this.Hide();
}

private void pblblpanellogo_Click(object sender, EventArgs e)
{

}
}

}
4 changes: 2 additions & 2 deletions Forms/FrmServerController.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<data name="btnservers.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAEIAAABCCAYAAADjVADoAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAL
DQAACw0B7QfALAAAAzdJREFUeF7tm02IjVEcxu/MhBoLWRhlwoKUlAWasJgy2RiRj0lShs3MRpGS2FBE
DAAACwwBP0AiyAAAAzdJREFUeF7tm02IjVEcxu/MhBoLWRhlwoKUlAWasJgy2RiRj0lShs3MRpGS2FBE
1EQWForNYImVDQ0pdmPnY8dGSigWvvJxPf/zf+ad933dxft5nXM6v3o259z/c57/ae69877nvY1AIBAI
BNpHs9nsgrZDV6FH0GQb9QC6BPUzzv8BAdZAzyEbkE1ZzGjtA4sOQF8kgUW8hZYxYv1gsR7og6xsIc+g
mYxaL1hI3pc2c4BR6wOLzIA+muXsZZJx6wOL9OtaVvMHmsfI9YAFzpil7GcbI9cDFniq61jPBUauHpjP
Expand All @@ -141,7 +141,7 @@
<data name="btnlogout.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAL
DQAACw0B7QfALAAAArpJREFUeF7tmktPFFEQhUfjBl1BcOdCwKDga0E0blzjz9BfQDRujMYY16Ib/RPu
DAAACwwBP0AiyAAAArpJREFUeF7tmktPFFEQhUfjBl1BcOdCwKDga0E0blzjz9BfQDRujMYY16Ib/RPu
fPwRE18s3EHia6NC1IWOp24dMeCtMXTf7mrp+pKTMN01p85UmDu3e2YQBEEQBEEQFGQ4HB6A5qGFQhKv
/bTvLgh5DnoKfYNKI55PoLNs1y0Q7Cb0E2oa6XGDbbsBAl1N0drlMtv7giBT0PcUqV2k5wxj+IEQ91Ic
H5YZww+EeKVZXHjBGH4gxIZmyfIRelNT4mGxwRh+MIjFFMsqA49ptcrDMj+Yw+IQyyojHmqVh2V+MIdF
Expand Down
2 changes: 1 addition & 1 deletion Forms/FrmSessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void PopulateListBoxWithSessions()
}
private void FrmSessions_Load(object sender, EventArgs e)
{
RPCHandler discordRPC = new RPCHandler();
DiscordRPCHandler discordRPC = new DiscordRPCHandler();
discordRPC.InitializeRPC();
}

Expand Down
4 changes: 2 additions & 2 deletions Handlers/RPCHandler.cs → Handlers/DiscordRPCHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace PteroController.Handlers
{
public class RPCHandler
public class DiscordRPCHandler
{
public DiscordRpcClient? client;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void InitializeRPC()
}
catch (Exception ex)
{
Console.WriteLine("Failed to load discord rpc: " + ex.Message);
Program.logger.Log(Managers.LogType.Error, "[Handlers.DiscordRPCHandler.cs]: \n" + ex.Message);
}
}
}
Expand Down
77 changes: 50 additions & 27 deletions Handlers/RegistryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,78 @@ public class RegistryHandler

public void CreateAccountData(string sessionName, string url, string password, string apiKey)
{
using (RegistryKey key = Registry.CurrentUser.CreateSubKey($"{RegistryAccountBasePath}\\{sessionName}"))
try
{
key.SetValue("panel_url", url);
key.SetValue("panel_pwd", password);
key.SetValue("api_key", apiKey);
using (RegistryKey key = Registry.CurrentUser.CreateSubKey($"{RegistryAccountBasePath}\\{sessionName}"))
{
key.SetValue("panel_url", url);
key.SetValue("panel_pwd", password);
key.SetValue("api_key", apiKey);
}
} catch (Exception ex)
{
Program.logger.Log(Managers.LogType.Error, "[Handlers.RegistryHandler.cs]: \n" + ex.Message);
}
}
public List<string> ListSessions()
{
List<string> sessionInfo = new List<string>();

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryAccountBasePath))
try
{
if (key != null)
List<string> sessionInfo = new List<string>();

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryAccountBasePath))

Check warning on line 34 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 34 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
{
foreach (string sessionName in key.GetSubKeyNames())
if (key != null)
{
string panelUrl = key.OpenSubKey(sessionName)?.GetValue("panel_url")?.ToString();
if (!string.IsNullOrEmpty(panelUrl))
foreach (string sessionName in key.GetSubKeyNames())
{
string displayText = $"{sessionName} ({panelUrl})";
sessionInfo.Add(displayText);
string panelUrl = key.OpenSubKey(sessionName)?.GetValue("panel_url")?.ToString();

Check warning on line 40 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 40 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
if (!string.IsNullOrEmpty(panelUrl))
{
string displayText = $"{sessionName} ({panelUrl})";
sessionInfo.Add(displayText);
}
}
}
}
}

return sessionInfo;
return sessionInfo;
}
catch (Exception ex)
{
Program.logger.Log(Managers.LogType.Error, "[Handlers.RegistryHandler.cs]: \n" + ex.Message);
return null;

Check warning on line 55 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Possible null reference return.

Check warning on line 55 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
}
}
public SessionInfo GetSessionInfo(string sessionName)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey($"{RegistryAccountBasePath}\\{sessionName}"))
try
{
if (key != null)
using (RegistryKey key = Registry.CurrentUser.OpenSubKey($"{RegistryAccountBasePath}\\{sessionName}"))

Check warning on line 62 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 62 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
{
string panelUrl = key.GetValue("panel_url")?.ToString();
string panelPwd = key.GetValue("panel_pwd")?.ToString();
string apiKey = key.GetValue("api_key")?.ToString();

return new SessionInfo
if (key != null)
{
PanelUrl = panelUrl,
PanelPwd = panelPwd,
ApiKey = apiKey
};
string panelUrl = key.GetValue("panel_url")?.ToString();

Check warning on line 66 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 66 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
string panelPwd = key.GetValue("panel_pwd")?.ToString();

Check warning on line 67 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 67 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.
string apiKey = key.GetValue("api_key")?.ToString();

Check warning on line 68 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Converting null literal or possible null value to non-nullable type.

Check warning on line 68 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Converting null literal or possible null value to non-nullable type.

return new SessionInfo
{
PanelUrl = panelUrl,
PanelPwd = panelPwd,
ApiKey = apiKey
};
}
}
}

return null;
return null;

Check warning on line 79 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Possible null reference return.

Check warning on line 79 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
}
catch (Exception ex)
{
Program.logger.Log(Managers.LogType.Error, "[Handlers.RegistryHandler.cs]: \n" + ex.Message);
return null;

Check warning on line 84 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Possible null reference return.

Check warning on line 84 in Handlers/RegistryHandler.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference return.
}
}
public void DeleteAccountData(string sessionName)
{
Expand Down
94 changes: 94 additions & 0 deletions Managers/LoggerManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace PteroController.Managers
{
public enum LogType
{
Info,
Warning,
Error
}

public class LoggerManager
{
private string logFilePath;

public LoggerManager()
{
string logDirectory = "logs";
string logFileName = "log.txt";
string logDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), logDirectory);
logFilePath = Path.Combine(logDirectoryPath, logFileName);
Directory.CreateDirectory(logDirectoryPath);
RenameLogFile();
}

public void Log(LogType type, string message)
{
string timestamp = DateTime.Now.ToString("HH:mm:ss");
string logText = $"[{timestamp}] [{type.ToString()}] {message}";

ConsoleColor color = ConsoleColor.White;
switch (type)
{
case LogType.Info:
color = ConsoleColor.Blue;
break;
case LogType.Warning:
color = ConsoleColor.Yellow;
break;
case LogType.Error:
color = ConsoleColor.Red;
break;
}
Console.ForegroundColor = color;
Console.WriteLine(logText);
Console.ResetColor();
AppendToFile(logText);
}

private void AppendToFile(string logText)
{
try
{
using StreamWriter writer = File.AppendText(logFilePath);
writer.WriteLine(logText);
}
catch (Exception ex)
{
string timestamp = DateTime.Now.ToString("HH:mm:ss");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{timestamp}] [Error] Error writing to log file: {ex.Message}");
Console.ResetColor();
}
}

private void RenameLogFile()
{
if (File.Exists(logFilePath))
{
string logFileNameWithoutExtension = Path.GetFileNameWithoutExtension(logFilePath);
string logFileExtension = Path.GetExtension(logFilePath);
#pragma warning disable
string logDirectoryPath = Path.GetDirectoryName(logFilePath);
string newLogFileName = GetUniqueLogFileName(logDirectoryPath, logFileNameWithoutExtension, logFileExtension);
#pragma warning restore
string newLogFilePath = Path.Combine(logDirectoryPath, newLogFileName);
File.Move(logFilePath, newLogFilePath);
}
}

private string GetUniqueLogFileName(string directoryPath, string fileNameWithoutExtension, string fileExtension)
{
string uniqueFileName = $"{fileNameWithoutExtension}-{DateTime.Now:yyyy-MM-dd}{fileExtension}";
string uniqueFilePath = Path.Combine(directoryPath, uniqueFileName);

int counter = 1;
while (File.Exists(uniqueFilePath))
{
uniqueFileName = $"{fileNameWithoutExtension}-{counter++}-{DateTime.Now:yyyy-MM-dd}{fileExtension}";
uniqueFilePath = Path.Combine(directoryPath, uniqueFileName);
}

return uniqueFileName;
}
}
}
19 changes: 15 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using PteroController.Forms;
using System.IO;
using PteroController.Managers;

namespace PteroController
{
Expand All @@ -9,23 +9,34 @@ internal static class Program
/// The main entry point for the application.
/// </summary>
public static string AppVersion = "1.0.0";
public static LoggerManager logger = new LoggerManager();
public static string mcascii = @"
_____ _ _____ _ _ _
| __ \| | / ____| | | | | |
| |__) | |_ ___ _ __ ___ | | ___ _ __ | |_ _ __ ___ | | | ___ _ __
| ___/| __/ _ \ '__/ _ \| | / _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
| | | || __/ | | (_) | |___| (_) | | | | |_| | | (_) | | | __/ |
|_| \__\___|_| \___/ \_____\___/|_| |_|\__|_| \___/|_|_|\___|_|
";
[STAThread]
public static void Main()
{
ApplicationConfiguration.Initialize();
if (!OperatingSystem.IsWindows())
{
Console.WriteLine("We are sorry, but we blocked this app from running on any other OS than Windows!");
logger.Log(LogType.Error, "We are sorry, but we blocked this app from running on any other OS than Windows!");
Application.Exit();
}
if (!File.Exists(Application.StartupPath + @"\PteroConsole.exe"))
{
Application.Run(new FrmInstallTools());
} else
}
else
{
Application.Run(new FrmLoading());
}

}
public static void Alert(string msg, FrmAlert.enmType type)
{
Expand Down
13 changes: 13 additions & 0 deletions PteroConsoleHook/WebsocketData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace PteroController.PteroConsoleHook
{
public class WebsocketData
{
[JsonProperty("token")]
public string? Token { get; set; }

[JsonProperty("socket")]
public string? Socket { get; set; }
}
}
11 changes: 11 additions & 0 deletions PteroConsoleHook/WebsocketDataResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Newtonsoft.Json;

namespace PteroController.PteroConsoleHook
{
public class WebsocketDataResource
{
[JsonProperty("data")]
public WebsocketData? Data { get; set; }
}

}
Loading

0 comments on commit 861548c

Please sign in to comment.