From 85aa26aaa172d9c411f3311910e528ac61148869 Mon Sep 17 00:00:00 2001 From: NaysKutzu <87282334+NaysKutzu@users.noreply.github.com> Date: Mon, 11 Sep 2023 19:29:33 +0200 Subject: [PATCH] PUSH --- Console/Program.cs | 82 +++++++++++++++++++ Console/PteroConsole.csproj | 18 +++++ Console/PteroConsole.sln | 25 ++++++ Console/WebsocketData.cs | 11 +++ Console/WebsocketDataResource.cs | 10 +++ Form1.Designer.cs | 57 +++++++++++++ Form1.cs | 87 ++++++++++++++++++++ Form1.resx | 120 +++++++++++++++++++++++++++ FrmServerController.cs | 23 +++--- FrmServerSelector.cs | 135 +++++++++++++++---------------- PteroController.csproj | 7 +- PteroController.csproj.user | 3 + PteroController.sln | 2 +- 13 files changed, 495 insertions(+), 85 deletions(-) create mode 100644 Console/Program.cs create mode 100644 Console/PteroConsole.csproj create mode 100644 Console/PteroConsole.sln create mode 100644 Console/WebsocketData.cs create mode 100644 Console/WebsocketDataResource.cs create mode 100644 Form1.Designer.cs create mode 100644 Form1.cs create mode 100644 Form1.resx diff --git a/Console/Program.cs b/Console/Program.cs new file mode 100644 index 0000000..18c13e5 --- /dev/null +++ b/Console/Program.cs @@ -0,0 +1,82 @@ +using Newtonsoft.Json; +using PteroControllerConsole; +using System.Net; + +class Program +{ + static async Task Main(string[] args) + { + string? panelUrl = null; + string? clientKey = null; + string? serverUuid = null; + + // Parse command-line arguments + for (int i = 0; i < args.Length; i++) + { + if (args[i] == "--panel-url" && i + 1 < args.Length) + { + panelUrl = args[i + 1]; + } + else if (args[i] == "--panel-apikey" && i + 1 < args.Length) + { + clientKey = args[i + 1]; + } + else if (args[i] == "--serveruuid" && i + 1 < args.Length) + { + serverUuid = args[i + 1]; + } + } + + if (string.IsNullOrWhiteSpace(panelUrl) || string.IsNullOrWhiteSpace(clientKey) || string.IsNullOrWhiteSpace(serverUuid)) + { + Console.WriteLine("Usage: PteroConsole.exe --panel-url https://panel.yourdomain.test --panel-apikey test --serveruuid 4"); + return; + } + + Console.WriteLine($"Using {panelUrl}/api/client/servers/{serverUuid}/websocket"); + + + var console = new PteroConsole.NET.PteroConsole(); + + + console.RequestToken += pteroConsole => + { + Console.WriteLine("Revoking token"); + var wc = new WebClient(); + wc.Headers.Add("Authorization", "Bearer " + clientKey); + var raw = wc.DownloadString($"{panelUrl}/api/client/servers/{serverUuid}/websocket"); + var data = JsonConvert.DeserializeObject(raw).Data; + return data.Token; + }; + + console.OnMessage += (sender, s) => + { + Console.WriteLine(s); + }; + + var wc = new WebClient(); + wc.Headers.Add("Authorization", "Bearer " + clientKey); + var raw = wc.DownloadString($"{panelUrl}/api/client/servers/{serverUuid}/websocket"); + var data = JsonConvert.DeserializeObject(raw).Data; + + await console.Connect(panelUrl, data.Socket, data.Token); + + while (true) + { + var command = Console.ReadLine(); + + if (command == "exit") + break; + + // Clear the console screen or overwrite the input line. + Console.SetCursorPosition(0, Console.CursorTop - 1); + Console.Write(new string(' ', Console.WindowWidth)); + Console.SetCursorPosition(0, Console.CursorTop - 1); + Console.WriteLine("\n"); + await console.EnterCommand(command); + } + + + await console.Disconnect(); + } +} diff --git a/Console/PteroConsole.csproj b/Console/PteroConsole.csproj new file mode 100644 index 0000000..d21a723 --- /dev/null +++ b/Console/PteroConsole.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + + diff --git a/Console/PteroConsole.sln b/Console/PteroConsole.sln new file mode 100644 index 0000000..752f82f --- /dev/null +++ b/Console/PteroConsole.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33815.320 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PteroConsole", "PteroConsole.csproj", "{0E71D285-A39A-4BEA-8534-48CCD0CF02AF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0E71D285-A39A-4BEA-8534-48CCD0CF02AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E71D285-A39A-4BEA-8534-48CCD0CF02AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E71D285-A39A-4BEA-8534-48CCD0CF02AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E71D285-A39A-4BEA-8534-48CCD0CF02AF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E71419DD-E75C-4675-9758-6B7F2CA44CD0} + EndGlobalSection +EndGlobal diff --git a/Console/WebsocketData.cs b/Console/WebsocketData.cs new file mode 100644 index 0000000..0815e7f --- /dev/null +++ b/Console/WebsocketData.cs @@ -0,0 +1,11 @@ +using Newtonsoft.Json; +namespace PteroControllerConsole; + +public class WebsocketData +{ + [JsonProperty("token")] + public string? Token { get; set; } + + [JsonProperty("socket")] + public string? Socket { get; set; } +} diff --git a/Console/WebsocketDataResource.cs b/Console/WebsocketDataResource.cs new file mode 100644 index 0000000..1262bee --- /dev/null +++ b/Console/WebsocketDataResource.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + + +namespace PteroControllerConsole; + +public class WebsocketDataResource +{ + [JsonProperty("data")] + public WebsocketData? Data { get; set; } +} \ No newline at end of file diff --git a/Form1.Designer.cs b/Form1.Designer.cs new file mode 100644 index 0000000..04bd876 --- /dev/null +++ b/Form1.Designer.cs @@ -0,0 +1,57 @@ +namespace PteroController +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panel1 = new Panel(); + SuspendLayout(); + // + // panel1 + // + panel1.Location = new Point(83, 83); + panel1.Name = "panel1"; + panel1.Size = new Size(591, 260); + panel1.TabIndex = 0; + // + // Form1 + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(panel1); + Name = "Form1"; + Text = "Form1"; + Load += Form1_Load; + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + } +} \ No newline at end of file diff --git a/Form1.cs b/Form1.cs new file mode 100644 index 0000000..299afbc --- /dev/null +++ b/Form1.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace PteroController +{ + public partial class Form1 : Form + { + [DllImport("kernel32.dll")] + public static extern bool AllocConsole(); + + [DllImport("kernel32.dll")] + public static extern bool FreeConsole(); + private Process externalProcess; + + public Form1() + { + InitializeComponent(); + } + private void EmbedExternalApplication() + { + string externalAppPath = "C:\\Users\\NaysKutzu\\Documents\\GitHub\\PteroController\\Controller\\bin\\Debug\\net6.0-windows\\PteroConsole.exe"; + externalProcess = new Process(); + externalProcess.StartInfo.FileName = externalAppPath; + externalProcess.StartInfo.UseShellExecute = false; + externalProcess.StartInfo.CreateNoWindow = true; + + // Redirect standard input, output, and error streams + externalProcess.StartInfo.RedirectStandardInput = true; + externalProcess.StartInfo.RedirectStandardOutput = true; + externalProcess.StartInfo.RedirectStandardError = true; + + externalProcess.OutputDataReceived += (sender, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + { + // Write the console output to the console window + Console.WriteLine(e.Data); + } + }; + + externalProcess.ErrorDataReceived += (sender, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + { + // Write the console error output to the console window + Console.Error.WriteLine(e.Data); + } + }; + + externalProcess.Start(); + + // Attach the console window to the form + AllocConsole(); + + // Start reading the standard output and error streams + externalProcess.BeginOutputReadLine(); + externalProcess.BeginErrorReadLine(); + } + + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + + // Close the external console application and free the console when the form is closed + if (externalProcess != null && !externalProcess.HasExited) + { + externalProcess.CloseMainWindow(); + externalProcess.WaitForExit(); + FreeConsole(); + } + } + + private void Form1_Load(object sender, EventArgs e) + { + EmbedExternalApplication(); + } + } +} diff --git a/Form1.resx b/Form1.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FrmServerController.cs b/FrmServerController.cs index 7907eb2..a36ec35 100644 --- a/FrmServerController.cs +++ b/FrmServerController.cs @@ -1,18 +1,17 @@ -namespace PteroController +namespace PteroController; + +public partial class FrmServerController : Form { - public partial class FrmServerController : Form - { - private string serverIdentifier; + private string serverIdentifier; - public FrmServerController(string serverIdentifier) - { - InitializeComponent(); - this.serverIdentifier = serverIdentifier; - } + public FrmServerController(string serverIdentifier) + { + InitializeComponent(); + this.serverIdentifier = serverIdentifier; + } - private void FrmServerController_Load(object sender, EventArgs e) - { + private void FrmServerController_Load(object sender, EventArgs e) + { - } } } diff --git a/FrmServerSelector.cs b/FrmServerSelector.cs index d3eef10..e3f472a 100644 --- a/FrmServerSelector.cs +++ b/FrmServerSelector.cs @@ -1,91 +1,90 @@ using Newtonsoft.Json; -namespace PteroController +namespace PteroController; + +public partial class FrmServerSelector : Form { - public partial class FrmServerSelector : Form - { - private HttpClient httpClient; + private HttpClient httpClient; - public FrmServerSelector() - { - InitializeComponent(); - serverListBox.SelectedIndex = -1; - httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {FrmLogin.panel_api_key}"); - } - private bool isFirstLoad = true; + public FrmServerSelector() + { + InitializeComponent(); + serverListBox.SelectedIndex = -1; + httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {FrmLogin.panel_api_key}"); + } + private bool isFirstLoad = true; - private async void FrmServerSelector_Load(object sender, EventArgs e) + private async void FrmServerSelector_Load(object sender, EventArgs e) + { + try { - try - { - HttpResponseMessage response = await httpClient.GetAsync(FrmLogin.panel_url + "/api/client"); - response.EnsureSuccessStatusCode(); - string jsonResponse = await response.Content.ReadAsStringAsync(); - PterodactylApiResponse apiResponse = JsonConvert.DeserializeObject(jsonResponse); - List serverDataList = apiResponse.Data; - List serverNames = new List(); - foreach (var serverData in serverDataList) - { - string serverName = $"{serverData.Attributes.Name} ({serverData.Attributes.Identifier})"; - serverNames.Add(serverName); - } - serverListBox.DataSource = serverNames; - serverListBox.ClearSelected(); - isFirstLoad = true; - } - catch (Exception ex) - { - Console.WriteLine("[{0:HH:mm:ss}] (SERVER LIST) An error occurred: " + ex.Message + "", DateTime.Now); - } - } - - private void serverListBox_KeyDown(object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) + HttpResponseMessage response = await httpClient.GetAsync(FrmLogin.panel_url + "/api/client"); + response.EnsureSuccessStatusCode(); + string jsonResponse = await response.Content.ReadAsStringAsync(); + PterodactylApiResponse apiResponse = JsonConvert.DeserializeObject(jsonResponse); + List serverDataList = apiResponse.Data; + List serverNames = new List(); + foreach (var serverData in serverDataList) { - e.Handled = true; - e.SuppressKeyPress = true; + string serverName = $"{serverData.Attributes.Name} ({serverData.Attributes.Identifier})"; + serverNames.Add(serverName); } + serverListBox.DataSource = serverNames; + serverListBox.ClearSelected(); + isFirstLoad = true; } - public class ServerData + catch (Exception ex) { - public ServerAttributes? Attributes { get; set; } + Console.WriteLine("[{0:HH:mm:ss}] (SERVER LIST) An error occurred: " + ex.Message + "", DateTime.Now); } + } - public class ServerAttributes - { - public string? Identifier { get; set; } - public string? Name { get; set; } - } - public class PterodactylApiResponse + private void serverListBox_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) { - public List? Data { get; set; } + e.Handled = true; + e.SuppressKeyPress = true; } - private void serverListBox_SelectedIndexChanged(object sender, EventArgs e) + } + public class ServerData + { + public ServerAttributes? Attributes { get; set; } + } + + public class ServerAttributes + { + public string? Identifier { get; set; } + public string? Name { get; set; } + } + public class PterodactylApiResponse + { + public List? Data { get; set; } + } + private void serverListBox_SelectedIndexChanged(object sender, EventArgs e) + { + try { - try + if (isFirstLoad) { - if (isFirstLoad) - { - isFirstLoad = false; - return; - } - - if (serverListBox.SelectedItem != null && serverListBox.SelectedItem is string selectedServerName) - { - string identifier = selectedServerName.Substring(selectedServerName.IndexOf('(') + 1); - identifier = identifier.TrimEnd(')'); - FrmServerController serverDetailsForm = new FrmServerController(identifier); - serverDetailsForm.Show(); - this.Hide(); - } + isFirstLoad = false; + return; } - catch (Exception ex) + + if (serverListBox.SelectedItem != null && serverListBox.SelectedItem is string selectedServerName) { - MessageBox.Show("We are sorry but we can't load the servers"); + string identifier = selectedServerName.Substring(selectedServerName.IndexOf('(') + 1); + identifier = identifier.TrimEnd(')'); + FrmServerController serverDetailsForm = new FrmServerController(identifier); + serverDetailsForm.Show(); + this.Hide(); } } + catch (Exception ex) + { + MessageBox.Show("We are sorry but we can't load the servers"); + } } } diff --git a/PteroController.csproj b/PteroController.csproj index 357ff18..2cae527 100644 --- a/PteroController.csproj +++ b/PteroController.csproj @@ -24,7 +24,10 @@ + + + @@ -35,8 +38,4 @@ - - - - \ No newline at end of file diff --git a/PteroController.csproj.user b/PteroController.csproj.user index 685525b..e8546ef 100644 --- a/PteroController.csproj.user +++ b/PteroController.csproj.user @@ -1,6 +1,9 @@  + + Form + Form diff --git a/PteroController.sln b/PteroController.sln index 70bd9d2..8e300dd 100644 --- a/PteroController.sln +++ b/PteroController.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.6.33815.320 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PteroController", "PteroController.csproj", "{E8F91865-3849-4773-B205-2DCFF069F11F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PteroController", "PteroController.csproj", "{E8F91865-3849-4773-B205-2DCFF069F11F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution