From be8fa8a3d92ceed24122106d298db6b1696d8f93 Mon Sep 17 00:00:00 2001 From: Bitl Date: Fri, 30 Dec 2022 11:38:57 -0700 Subject: [PATCH] prototype web server --- Novetus/NovetusCore/Classes/WebProxy.cs | 135 ++++++++++++++++++ Novetus/NovetusCore/NovetusCore.projitems | 1 + .../StorageAndFunctions/GlobalVars.cs | 6 +- .../Compact/LauncherFormCompact.cs | 2 +- .../Extended/LauncherFormExtended.cs | 2 +- .../Forms/LauncherForm/LauncherFormShared.cs | 9 +- .../LauncherFormStylishInterface.xaml.cs | 1 + .../NovetusLauncher/Forms/NovetusConsole.cs | 6 + .../NovetusLauncher/Novetus.Launcher.csproj | 1 + Novetus/NovetusURI/Forms/LoaderForm.cs | 5 + 10 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 Novetus/NovetusCore/Classes/WebProxy.cs diff --git a/Novetus/NovetusCore/Classes/WebProxy.cs b/Novetus/NovetusCore/Classes/WebProxy.cs new file mode 100644 index 0000000..1754ee7 --- /dev/null +++ b/Novetus/NovetusCore/Classes/WebProxy.cs @@ -0,0 +1,135 @@ +#if LAUNCHER || URI +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Titanium.Web.Proxy; +using Titanium.Web.Proxy.EventArguments; +using Titanium.Web.Proxy.Http; +using Titanium.Web.Proxy.Models; + +public class WebProxy +{ + private static ProxyServer Server = new ProxyServer(); + private static ExplicitProxyEndPoint end; + + public bool HasStarted() + { + return Server.ProxyRunning; + } + + public void Start() + { + try + { + //load ext + Server.BeforeRequest += new AsyncEventHandler(OnRequest); + UpdateEndPoint(true); + Util.ConsolePrint("Web Proxy started on port " + GlobalVars.WebProxyPort, 3); + } + catch (Exception e) + { + Util.LogExceptions(e); + } + } + + public void UpdateEndPoint(bool shouldRunServer = false, bool decrypt = true) + { + if (Server.ProxyEndPoints.Count > 0) + { + Server.RemoveEndPoint(end); + } + + GlobalVars.WebProxyPort = GlobalVars.UserConfiguration.RobloxPort + 1; + end = new ExplicitProxyEndPoint(IPAddress.Any, GlobalVars.WebProxyPort, decrypt); + end.BeforeTunnelConnectRequest += new AsyncEventHandler(OnBeforeTunnelConnectRequest); + Server.AddEndPoint(end); + + if (!Server.ProxyRunning && shouldRunServer) + { + Server.Start(); + } + + if (Server.ProxyRunning) + { + foreach (ProxyEndPoint endPoint in Server.ProxyEndPoints) + { + Server.SetAsSystemHttpProxy(end); + Server.SetAsSystemHttpsProxy(end); + } + } + + Util.ConsolePrint("Web Proxy Endpoint updated with port " + GlobalVars.WebProxyPort, 3); + } + + private bool IsURIAllowed(HttpWebClient client) + { + string uri = client.Request.RequestUri.Host; + + if ((!uri.StartsWith("www.") && + !uri.StartsWith("web.") && + !uri.StartsWith("assetgame.") && + !uri.StartsWith("wiki.") && + !uri.EndsWith("api.roblox.com") && + !uri.StartsWith("roblox.com") || !uri.EndsWith("roblox.com")) && + !uri.EndsWith("robloxlabs.com")) + { + return false; + } + + //we check the header + HeaderCollection headers = client.Request.Headers; + List userAgents = headers.GetHeaders("User-Agent"); + + if (userAgents == null) + return false; + + if (string.IsNullOrWhiteSpace(userAgents.FirstOrDefault().Value)) + return false; + + string ua = userAgents.FirstOrDefault().Value.ToLowerInvariant(); + + Util.ConsolePrint(ua); + + return ua.Contains("roblox"); + } + + private Task OnBeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e) + { + if (!IsURIAllowed(e.HttpClient)) + { + e.DecryptSsl = false; + } + + return Task.CompletedTask; + } + +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + private async Task OnRequest(object sender, SessionEventArgs e) +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously + { + if (!IsURIAllowed(e.HttpClient)) + { + return; + } + + Util.ConsolePrint("we should be returning a 404 here."); + e.GenericResponse("", HttpStatusCode.NotFound); + } + + public void Stop() + { + Util.ConsolePrint("Web Proxy stopping on port " + GlobalVars.WebProxyPort, 3); + Server.BeforeRequest -= new AsyncEventHandler(OnRequest); + Server.Stop(); + } +} + +public interface IWebProxyExtention +{ + +} +#endif diff --git a/Novetus/NovetusCore/NovetusCore.projitems b/Novetus/NovetusCore/NovetusCore.projitems index ae91e91..2657b69 100644 --- a/Novetus/NovetusCore/NovetusCore.projitems +++ b/Novetus/NovetusCore/NovetusCore.projitems @@ -9,6 +9,7 @@ NovetusCore + diff --git a/Novetus/NovetusCore/StorageAndFunctions/GlobalVars.cs b/Novetus/NovetusCore/StorageAndFunctions/GlobalVars.cs index 6854e80..bf0ea19 100644 --- a/Novetus/NovetusCore/StorageAndFunctions/GlobalVars.cs +++ b/Novetus/NovetusCore/StorageAndFunctions/GlobalVars.cs @@ -97,7 +97,11 @@ public static class GlobalVars public static string ExternalIP = SecurityFuncs.GetExternalIPAddress(); public static ScriptType GameOpened = ScriptType.None; public static string PlayerTripcode = ""; - #endregion +#if LAUNCHER || URI + public static int WebProxyPort = 0; + public static WebProxy Proxy = new WebProxy(); +#endif +#endregion #if LAUNCHER #region Novetus Launcher diff --git a/Novetus/NovetusLauncher/Forms/LauncherForm/Compact/LauncherFormCompact.cs b/Novetus/NovetusLauncher/Forms/LauncherForm/Compact/LauncherFormCompact.cs index ff87595..63e2e34 100644 --- a/Novetus/NovetusLauncher/Forms/LauncherForm/Compact/LauncherFormCompact.cs +++ b/Novetus/NovetusLauncher/Forms/LauncherForm/Compact/LauncherFormCompact.cs @@ -183,7 +183,7 @@ namespace NovetusLauncher void Button22Click(object sender, EventArgs e) { - launcherForm.ResetCurPort(numericUpDown2, GlobalVars.UserConfiguration.RobloxPort); + launcherForm.ResetCurPort(numericUpDown2); } void TreeView1AfterSelect(object sender, TreeViewEventArgs e) diff --git a/Novetus/NovetusLauncher/Forms/LauncherForm/Extended/LauncherFormExtended.cs b/Novetus/NovetusLauncher/Forms/LauncherForm/Extended/LauncherFormExtended.cs index ffd2ed2..2c77821 100644 --- a/Novetus/NovetusLauncher/Forms/LauncherForm/Extended/LauncherFormExtended.cs +++ b/Novetus/NovetusLauncher/Forms/LauncherForm/Extended/LauncherFormExtended.cs @@ -192,7 +192,7 @@ namespace NovetusLauncher void Button22Click(object sender, EventArgs e) { - launcherForm.ResetCurPort(numericUpDown2, GlobalVars.UserConfiguration.RobloxPort); + launcherForm.ResetCurPort(numericUpDown2); } void TreeView1AfterSelect(object sender, TreeViewEventArgs e) diff --git a/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs b/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs index 82fc948..bab066e 100644 --- a/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs +++ b/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs @@ -186,6 +186,10 @@ namespace NovetusLauncher { DiscordRPC.Shutdown(); } + if (GlobalVars.Proxy.HasStarted()) + { + GlobalVars.Proxy.Stop(); + } if (!GlobalVars.AppClosed) { @@ -578,6 +582,7 @@ namespace NovetusLauncher Tree.Focus(); IPBox.Text = GlobalVars.CurrentServer.ToString(); HostPortBox.Value = Convert.ToDecimal(GlobalVars.UserConfiguration.RobloxPort); + GlobalVars.Proxy.UpdateEndPoint(); IPLabel.Text = GlobalVars.CurrentServer.ServerIP; PortLabel.Text = GlobalVars.CurrentServer.ServerPort.ToString(); DiscordPresenceCheckbox.Checked = GlobalVars.UserConfiguration.DiscordPresence; @@ -923,10 +928,9 @@ namespace NovetusLauncher IPBox.Text = GlobalVars.CurrentServer.ToString(); } - public void ResetCurPort(NumericUpDown box, int value) + public void ResetCurPort(NumericUpDown box) { box.Value = Convert.ToDecimal(GlobalVars.DefaultRobloxPort); - value = GlobalVars.DefaultRobloxPort; } public void ChangeServerAddress() @@ -953,6 +957,7 @@ namespace NovetusLauncher public void ChangeServerPort() { GlobalVars.UserConfiguration.RobloxPort = Convert.ToInt32(HostPortBox.Value); + GlobalVars.Proxy.UpdateEndPoint(); } public void ChangeClient() diff --git a/Novetus/NovetusLauncher/Forms/LauncherForm/Stylish/LauncherFormStylishInterface.xaml.cs b/Novetus/NovetusLauncher/Forms/LauncherForm/Stylish/LauncherFormStylishInterface.xaml.cs index 40800d6..5aa55c0 100644 --- a/Novetus/NovetusLauncher/Forms/LauncherForm/Stylish/LauncherFormStylishInterface.xaml.cs +++ b/Novetus/NovetusLauncher/Forms/LauncherForm/Stylish/LauncherFormStylishInterface.xaml.cs @@ -340,6 +340,7 @@ namespace NovetusLauncher if (!IsLoaded) return; GlobalVars.UserConfiguration.RobloxPort = Convert.ToInt32(serverPortBox.Text); + GlobalVars.Proxy.UpdateEndPoint(); } private void maxPlayersBox_TextChanged(object sender, TextChangedEventArgs e) diff --git a/Novetus/NovetusLauncher/Forms/NovetusConsole.cs b/Novetus/NovetusLauncher/Forms/NovetusConsole.cs index ec82ae9..3d4cd6c 100644 --- a/Novetus/NovetusLauncher/Forms/NovetusConsole.cs +++ b/Novetus/NovetusLauncher/Forms/NovetusConsole.cs @@ -125,6 +125,7 @@ namespace NovetusLauncher if (ConsoleArgs["hostport"] != null) { GlobalVars.UserConfiguration.RobloxPort = Convert.ToInt32(ConsoleArgs["hostport"]); + GlobalVars.Proxy.UpdateEndPoint(); } if (ConsoleArgs["upnp"] != null) @@ -356,6 +357,11 @@ namespace NovetusLauncher de.Show(); Util.ConsolePrint("???", 2); break; + case string proxy when proxy.Contains("proxytest", StringComparison.InvariantCultureIgnoreCase) == true: + GlobalVars.Proxy.Start(); + //Util.Delay(5000); + //GlobalVars.Proxy.Stop(); + break; default: Util.ConsolePrint("Command is either not registered or valid", 2); break; diff --git a/Novetus/NovetusLauncher/Novetus.Launcher.csproj b/Novetus/NovetusLauncher/Novetus.Launcher.csproj index b348647..563522b 100644 --- a/Novetus/NovetusLauncher/Novetus.Launcher.csproj +++ b/Novetus/NovetusLauncher/Novetus.Launcher.csproj @@ -127,6 +127,7 @@ ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + C:\Users\Bitl\Documents\GitHub\Novetus\Novetus_src\Novetus\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll diff --git a/Novetus/NovetusURI/Forms/LoaderForm.cs b/Novetus/NovetusURI/Forms/LoaderForm.cs index 30b48fe..6ebc1da 100644 --- a/Novetus/NovetusURI/Forms/LoaderForm.cs +++ b/Novetus/NovetusURI/Forms/LoaderForm.cs @@ -107,6 +107,11 @@ namespace NovetusURI { GlobalVars.GameOpened = ScriptType.None; } + + if (GlobalVars.Proxy.HasStarted()) + { + GlobalVars.Proxy.Stop(); + } ClientManagement.UpdateRichPresence(ClientManagement.GetStateForType(GlobalVars.GameOpened)); Close(); }