From e523c9e8f7559682ccc6192d21dd97a83582085a Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 13 Jul 2020 20:59:50 +0200 Subject: [PATCH 01/14] Enable TLS 1.2 in launcher --- USBHelperLauncher/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 925f78c..038de02 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -255,6 +255,7 @@ static void Main(string[] args) running.Kill(); } + ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; Proxy.Start(); // Update translations From bf8f6e6a5d2f0aef4f0f271e5de758e2e6912747 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 24 Jul 2020 14:50:50 +0200 Subject: [PATCH 02/14] Automatically delete user.config when file is corrupted --- .../Patches/InvalidConfigPatch.cs | 40 +++++++++++++++++++ USBHelperInjector/USBHelperInjector.csproj | 1 + 2 files changed, 41 insertions(+) create mode 100644 USBHelperInjector/Patches/InvalidConfigPatch.cs diff --git a/USBHelperInjector/Patches/InvalidConfigPatch.cs b/USBHelperInjector/Patches/InvalidConfigPatch.cs new file mode 100644 index 0000000..d7bb5c6 --- /dev/null +++ b/USBHelperInjector/Patches/InvalidConfigPatch.cs @@ -0,0 +1,40 @@ +using System; +using System.Configuration; +using System.IO; +using System.Reflection; +using System.Windows.Forms; +using System.Xml; +using HarmonyLib; +using USBHelperInjector.Patches.Attributes; + +namespace USBHelperInjector.Patches +{ + [Optional] + [HarmonyPatch] + class InvalidConfigPatch + { + static MethodBase TargetMethod() + { + return ReflectionHelper.Settings.GetConstructor(new Type[] { }); + } + + static void Postfix() + { + try + { + // try to load user.config + ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); + } + catch (ConfigurationErrorsException e) when (e.GetBaseException() is XmlException) + { + // can't use `ConfigurationManager.OpenExeConfiguration` for getting the config path here, + // as it would preload the file and throw again + var configPathsType = Assembly.GetAssembly(typeof(Configuration)).GetType("System.Configuration.ClientConfigPaths"); + var configPaths = AccessTools.Property(configPathsType, "Current").GetValue(null); + var fileName = (string)AccessTools.Property(configPathsType, "LocalConfigFilename").GetValue(configPaths); + File.Delete(fileName); + MessageBox.Show("A corrupted configuration file has been deleted, you'll need to go through the setup again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + } +} diff --git a/USBHelperInjector/USBHelperInjector.csproj b/USBHelperInjector/USBHelperInjector.csproj index 9532962..fc49172 100644 --- a/USBHelperInjector/USBHelperInjector.csproj +++ b/USBHelperInjector/USBHelperInjector.csproj @@ -66,6 +66,7 @@ + From 65b8dedf219719b997eeb9a8239e0c5eec75e58a Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 26 Jul 2020 20:04:27 +0200 Subject: [PATCH 03/14] Add timestamps to logger --- USBHelperLauncher/Logger.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/USBHelperLauncher/Logger.cs b/USBHelperLauncher/Logger.cs index 03f7239..9cd2867 100644 --- a/USBHelperLauncher/Logger.cs +++ b/USBHelperLauncher/Logger.cs @@ -19,6 +19,8 @@ public Logger() override public void Write(string output) { + var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); + output = $"[{timestamp}] {output}"; // Capture the output and also send it to StdOut Captured.Write(output); stdOutWriter.Write(output); @@ -26,9 +28,7 @@ override public void Write(string output) override public void WriteLine(string output) { - // Capture the output and also send it to StdOut - Captured.WriteLine(output); - stdOutWriter.WriteLine(output); + Write(output + Environment.NewLine); } public string GetLog() From 56ca53ec535236c3aeece5eedb1e133d9b3de520 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 26 Jul 2020 20:16:17 +0200 Subject: [PATCH 04/14] Revert "Fix issues with 3DS TMD downloads" This reverts commit c66c9cd82915e5b1b5d139841e1734fb74357528. --- USBHelperLauncher/Net/Proxy.cs | 1 - USBHelperLauncher/Net/WiiUShopEndpoint.cs | 20 -------------------- USBHelperLauncher/USBHelperLauncher.csproj | 1 - 3 files changed, 22 deletions(-) delete mode 100644 USBHelperLauncher/Net/WiiUShopEndpoint.cs diff --git a/USBHelperLauncher/Net/Proxy.cs b/USBHelperLauncher/Net/Proxy.cs index 13a4165..f825572 100644 --- a/USBHelperLauncher/Net/Proxy.cs +++ b/USBHelperLauncher/Net/Proxy.cs @@ -25,7 +25,6 @@ class Proxy : IDisposable new RegistrationEndpoint(), new CloudEndpoint(), new SiteEndpoint(), - new WiiUShopEndpoint(), new TitlekeysWiiUEndpoint(), new Titlekeys3DSEndpoint() }; diff --git a/USBHelperLauncher/Net/WiiUShopEndpoint.cs b/USBHelperLauncher/Net/WiiUShopEndpoint.cs deleted file mode 100644 index 6a66c0c..0000000 --- a/USBHelperLauncher/Net/WiiUShopEndpoint.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Fiddler; - -namespace USBHelperLauncher.Net -{ - class WiiUShopEndpoint : Endpoint - { - public WiiUShopEndpoint() : base("ccs.cdn.wup.shop.nintendo.net") { } - - protected WiiUShopEndpoint(string hostName) : base(hostName) { } - - [Request("/*")] - public void Get(Session oS) - { - oS.utilCreateResponseAndBypassServer(); - oS.oResponse.headers.SetStatus(307, "Redirect"); - oS.oResponse["Location"] = "http://ccs.cdn.c.shop.nintendowifi.net" + oS.PathAndQuery; - Proxy.LogRequest(oS, this, "Redirecting to http://ccs.cdn.c.shop.nintendowifi.net" + oS.PathAndQuery); - } - } -} diff --git a/USBHelperLauncher/USBHelperLauncher.csproj b/USBHelperLauncher/USBHelperLauncher.csproj index 5ea9459..b69328a 100644 --- a/USBHelperLauncher/USBHelperLauncher.csproj +++ b/USBHelperLauncher/USBHelperLauncher.csproj @@ -132,7 +132,6 @@ - From e881926c3e6bea1002a8336bbbf22283587153f4 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 27 Jul 2020 16:26:20 +0200 Subject: [PATCH 05/14] Use WCF TCP bindings instead of named pipes --- .../{Contracts => IPC}/IInjectorService.cs | 2 +- .../{Contracts => IPC}/ILauncherService.cs | 8 +- USBHelperInjector/IPC/IPCType.cs | 12 +++ USBHelperInjector/IPC/IPCUtils.cs | 78 +++++++++++++++++++ USBHelperInjector/InjectorService.cs | 20 +++-- .../Patches/CommandLineArgsPatch.cs | 21 +++++ USBHelperInjector/USBHelperInjector.csproj | 7 +- USBHelperLauncher/App.config | 3 + USBHelperLauncher/Configuration/Settings.cs | 4 + USBHelperLauncher/LauncherService.cs | 13 ++-- USBHelperLauncher/Program.cs | 15 ++-- USBHelperLauncher/WiiU_USB_Helper_.exe.config | 3 + 12 files changed, 160 insertions(+), 26 deletions(-) rename USBHelperInjector/{Contracts => IPC}/IInjectorService.cs (97%) rename USBHelperInjector/{Contracts => IPC}/ILauncherService.cs (57%) create mode 100644 USBHelperInjector/IPC/IPCType.cs create mode 100644 USBHelperInjector/IPC/IPCUtils.cs create mode 100644 USBHelperInjector/Patches/CommandLineArgsPatch.cs diff --git a/USBHelperInjector/Contracts/IInjectorService.cs b/USBHelperInjector/IPC/IInjectorService.cs similarity index 97% rename from USBHelperInjector/Contracts/IInjectorService.cs rename to USBHelperInjector/IPC/IInjectorService.cs index 12abcd5..18bf24f 100644 --- a/USBHelperInjector/Contracts/IInjectorService.cs +++ b/USBHelperInjector/IPC/IInjectorService.cs @@ -1,6 +1,6 @@ using System.ServiceModel; -namespace USBHelperInjector.Contracts +namespace USBHelperInjector.IPC { [ServiceContract] public interface IInjectorService diff --git a/USBHelperInjector/Contracts/ILauncherService.cs b/USBHelperInjector/IPC/ILauncherService.cs similarity index 57% rename from USBHelperInjector/Contracts/ILauncherService.cs rename to USBHelperInjector/IPC/ILauncherService.cs index c0adde2..c18f80c 100644 --- a/USBHelperInjector/Contracts/ILauncherService.cs +++ b/USBHelperInjector/IPC/ILauncherService.cs @@ -1,7 +1,7 @@ -using System.ServiceModel; -using System.Threading.Tasks; +using System; +using System.ServiceModel; -namespace USBHelperInjector.Contracts +namespace USBHelperInjector.IPC { [ServiceContract] public interface ILauncherService @@ -10,6 +10,6 @@ public interface ILauncherService void SetKeySite(string site, string url); [OperationContract] - void SendInjectorSettings(); + void SendInjectorSettings(Uri uri); } } diff --git a/USBHelperInjector/IPC/IPCType.cs b/USBHelperInjector/IPC/IPCType.cs new file mode 100644 index 0000000..bd53b47 --- /dev/null +++ b/USBHelperInjector/IPC/IPCType.cs @@ -0,0 +1,12 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace USBHelperInjector.IPC +{ + [JsonConverter(typeof(StringEnumConverter))] + public enum IPCType + { + NamedPipe, + TCP + } +} diff --git a/USBHelperInjector/IPC/IPCUtils.cs b/USBHelperInjector/IPC/IPCUtils.cs new file mode 100644 index 0000000..c0783d0 --- /dev/null +++ b/USBHelperInjector/IPC/IPCUtils.cs @@ -0,0 +1,78 @@ +using System; +using System.ComponentModel; +using System.Linq; +using System.ServiceModel; +using System.ServiceModel.Channels; +using System.ServiceModel.Description; + +namespace USBHelperInjector.IPC +{ + public class IPCUtils + { + public static ServiceHost CreateService(IPCType ipcType, Type serviceType, Type contractType, out Uri uri) + { + switch (ipcType) + { + case IPCType.NamedPipe: + return CreateNamedPipeService(serviceType, contractType, out uri); + case IPCType.TCP: + return CreateTcpService(serviceType, contractType, out uri); + default: + throw new InvalidEnumArgumentException(nameof(ipcType), (int)ipcType, ipcType.GetType()); + } + } + + public static ServiceHost CreateNamedPipeService(Type serviceType, Type contractType, out Uri uri) + { + var guid = Guid.NewGuid().ToString("D"); + var host = new ServiceHost(serviceType, new Uri($"net.pipe://localhost/{guid}")); + host.AddServiceEndpoint(contractType, GetNamedPipeBinding(), ""); + host.Open(); + uri = host.ChannelDispatchers.First().Listener.Uri; + return host; + } + + public static ServiceHost CreateTcpService(Type serviceType, Type contractType, out Uri uri) + { + var host = new ServiceHost(serviceType, new Uri("net.tcp://127.0.0.1:0")); + var endpoint = host.AddServiceEndpoint(contractType, GetTcpBinding(), "", new Uri("net.tcp://127.0.0.1")); + endpoint.ListenUriMode = ListenUriMode.Unique; + host.Open(); + uri = host.ChannelDispatchers.First().Listener.Uri; + return host; + } + + + public static TContract CreateChannel(IPCType ipcType, string address) + { + Binding binding; + switch (ipcType) + { + case IPCType.NamedPipe: + binding = GetNamedPipeBinding(); + break; + case IPCType.TCP: + binding = GetTcpBinding(); + break; + default: + throw new InvalidEnumArgumentException(nameof(ipcType), (int)ipcType, ipcType.GetType()); + } + + var factory = new ChannelFactory(binding, address); + return factory.CreateChannel(); + } + + + private static NetNamedPipeBinding GetNamedPipeBinding() + { + return new NetNamedPipeBinding(""); + } + private static NetTcpBinding GetTcpBinding() + { + return new NetTcpBinding("") + { + Security = { Mode = SecurityMode.None } + }; + } + } +} diff --git a/USBHelperInjector/InjectorService.cs b/USBHelperInjector/InjectorService.cs index 3df6a49..a3d0961 100644 --- a/USBHelperInjector/InjectorService.cs +++ b/USBHelperInjector/InjectorService.cs @@ -6,11 +6,12 @@ using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.ServiceModel; -using USBHelperInjector.Contracts; +using USBHelperInjector.IPC; using USBHelperInjector.Patches.Attributes; namespace USBHelperInjector { + [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class InjectorService : IInjectorService { public static Harmony Harmony { get; private set; } @@ -27,14 +28,19 @@ public class InjectorService : IInjectorService public static void Init() { - var factory = new ChannelFactory(new NetNamedPipeBinding(""), "net.pipe://localhost/LauncherService"); - LauncherService = factory.CreateChannel(); + var args = Environment.GetCommandLineArgs(); + var ipcType = (IPCType)Enum.Parse(typeof(IPCType), args[2]); + var launcherUri = args[3]; - ServiceHost host = new ServiceHost(typeof(InjectorService), new Uri("net.pipe://localhost/InjectorService")); - host.AddServiceEndpoint(typeof(IInjectorService), new NetNamedPipeBinding(""), ""); - host.Open(); + IPCUtils.CreateService( + ipcType, + typeof(InjectorService), + typeof(IInjectorService), + out var serviceUri + ); - LauncherService.SendInjectorSettings(); + LauncherService = IPCUtils.CreateChannel(ipcType, launcherUri); + LauncherService.SendInjectorSettings(serviceUri); Harmony = new Harmony("me.failedshack.usbhelperinjector"); var assembly = Assembly.GetExecutingAssembly(); diff --git a/USBHelperInjector/Patches/CommandLineArgsPatch.cs b/USBHelperInjector/Patches/CommandLineArgsPatch.cs new file mode 100644 index 0000000..9771586 --- /dev/null +++ b/USBHelperInjector/Patches/CommandLineArgsPatch.cs @@ -0,0 +1,21 @@ + +using System.Linq; +using System.Reflection; +using HarmonyLib; + +namespace USBHelperInjector.Patches +{ + [HarmonyPatch] + class CommandLineArgsPatch + { + static MethodBase TargetMethod() + { + return ReflectionHelper.EntryPoint; + } + + static void Prefix(ref string[] __0) + { + __0 = __0.Take(1).ToArray(); + } + } +} diff --git a/USBHelperInjector/USBHelperInjector.csproj b/USBHelperInjector/USBHelperInjector.csproj index fc49172..2d8516f 100644 --- a/USBHelperInjector/USBHelperInjector.csproj +++ b/USBHelperInjector/USBHelperInjector.csproj @@ -52,11 +52,14 @@ - + + + + @@ -82,7 +85,7 @@ - + diff --git a/USBHelperLauncher/App.config b/USBHelperLauncher/App.config index 29cb102..c968351 100644 --- a/USBHelperLauncher/App.config +++ b/USBHelperLauncher/App.config @@ -8,6 +8,9 @@ + + + \ No newline at end of file diff --git a/USBHelperLauncher/Configuration/Settings.cs b/USBHelperLauncher/Configuration/Settings.cs index 8e42a1a..75f8a49 100644 --- a/USBHelperLauncher/Configuration/Settings.cs +++ b/USBHelperLauncher/Configuration/Settings.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Reflection; +using USBHelperInjector.IPC; using USBHelperLauncher.Net; namespace USBHelperLauncher.Configuration @@ -92,6 +93,9 @@ private static List> Properties [Setting("Injector", true)] public static bool SplitUnpackDirectories { get; set; } + [Setting("Launcher", IPCType.TCP)] + public static IPCType IPCType { get; set; } + public static void Save() { DoNotModify = Program.GetVersion(); diff --git a/USBHelperLauncher/LauncherService.cs b/USBHelperLauncher/LauncherService.cs index 368fa3d..533c371 100644 --- a/USBHelperLauncher/LauncherService.cs +++ b/USBHelperLauncher/LauncherService.cs @@ -1,10 +1,12 @@ -using Fiddler; +using System; +using Fiddler; using System.ServiceModel; -using USBHelperInjector.Contracts; +using USBHelperInjector.IPC; using USBHelperLauncher.Configuration; namespace USBHelperLauncher { + [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] class LauncherService : ILauncherService { public void SetKeySite(string site, string url) @@ -13,11 +15,10 @@ public void SetKeySite(string site, string url) Settings.Save(); } - public void SendInjectorSettings() + public void SendInjectorSettings(Uri uri) { - Program.Logger.WriteLine("Sending information to injector..."); - var factory = new ChannelFactory(new NetNamedPipeBinding(""), "net.pipe://localhost/InjectorService"); - var channel = factory.CreateChannel(); + Program.Logger.WriteLine($"Sending information to injector ({uri})..."); + var channel = IPCUtils.CreateChannel(Settings.IPCType, uri.ToString()); if (Program.OverridePublicKey) { channel.SetDonationKey(Program.GenerateDonationKey()); diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 038de02..1bc24c6 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -13,14 +13,13 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; -using System.ServiceModel; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Threading; -using USBHelperInjector.Contracts; +using USBHelperInjector.IPC; using USBHelperLauncher.Configuration; using USBHelperLauncher.Emulator; using USBHelperLauncher.Utils; @@ -282,9 +281,13 @@ static void Main(string[] args) } }).Wait(); - ServiceHost host = new ServiceHost(typeof(LauncherService), new Uri("net.pipe://localhost/LauncherService")); - host.AddServiceEndpoint(typeof(ILauncherService), new NetNamedPipeBinding(""), ""); - host.Open(); + IPCUtils.CreateService( + Settings.IPCType, + typeof(LauncherService), + typeof(ILauncherService), + out var serviceUri + ); + Logger.WriteLine($"WCF host uri: {serviceUri}"); // Patching dialog.Invoke(new Action(() => @@ -314,7 +317,7 @@ static void Main(string[] args) var startInfo = new ProcessStartInfo() { FileName = executable, - Arguments = HelperVersion, + Arguments = string.Join(" ", HelperVersion, Settings.IPCType, serviceUri), UseShellExecute = false, RedirectStandardError = true, StandardErrorEncoding = Encoding.Default diff --git a/USBHelperLauncher/WiiU_USB_Helper_.exe.config b/USBHelperLauncher/WiiU_USB_Helper_.exe.config index 2c03b64..c605c0a 100644 --- a/USBHelperLauncher/WiiU_USB_Helper_.exe.config +++ b/USBHelperLauncher/WiiU_USB_Helper_.exe.config @@ -20,6 +20,9 @@ + + + \ No newline at end of file From 67b207d0c1cc3f8b9051880fd4389f39f544d8e9 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 27 Jul 2020 21:46:54 +0200 Subject: [PATCH 06/14] Fix access violation caused by RivaTuner overlay --- .../Patches/DownloadManagerOverlayPatch.cs | 30 +++++++++++++++++++ USBHelperInjector/USBHelperInjector.csproj | 1 + 2 files changed, 31 insertions(+) create mode 100644 USBHelperInjector/Patches/DownloadManagerOverlayPatch.cs diff --git a/USBHelperInjector/Patches/DownloadManagerOverlayPatch.cs b/USBHelperInjector/Patches/DownloadManagerOverlayPatch.cs new file mode 100644 index 0000000..11275df --- /dev/null +++ b/USBHelperInjector/Patches/DownloadManagerOverlayPatch.cs @@ -0,0 +1,30 @@ +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using HarmonyLib; +using USBHelperInjector.Patches.Attributes; + +namespace USBHelperInjector.Patches +{ + [Optional] + [HarmonyPatch] + class DownloadManagerOverlayPatch + { + static MethodBase TargetMethod() + { + return AccessTools.DeclaredProperty(ReflectionHelper.Settings, "ShowDownloadManagerTip").GetGetMethod(true); + } + + static bool Prefix(ref bool __result) + { + // Check if RivaTuner hooks are loaded into the process; RivaTuner can sometimes break d3d9 + var modules = Process.GetCurrentProcess().Modules.Cast(); + if (modules.Any(m => m.ModuleName.ToLowerInvariant().StartsWith("rtsshooks"))) + { + __result = false; + return false; + } + return true; + } + } +} diff --git a/USBHelperInjector/USBHelperInjector.csproj b/USBHelperInjector/USBHelperInjector.csproj index 2d8516f..8097b84 100644 --- a/USBHelperInjector/USBHelperInjector.csproj +++ b/USBHelperInjector/USBHelperInjector.csproj @@ -68,6 +68,7 @@ + From 877ac3bf7f7b0d9fd767bc19ff499791cd109a1a Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jul 2020 00:40:37 +0200 Subject: [PATCH 07/14] Fix custom certificate validation --- USBHelperLauncher/Net/Proxy.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/USBHelperLauncher/Net/Proxy.cs b/USBHelperLauncher/Net/Proxy.cs index f825572..5055960 100644 --- a/USBHelperLauncher/Net/Proxy.cs +++ b/USBHelperLauncher/Net/Proxy.cs @@ -68,9 +68,20 @@ public void Start() policy.RevocationMode = X509RevocationMode.NoCheck; policy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; policy.ExtraStore.AddRange(CertificateStore); - if (chain.Build(remoteChain.ChainElements[0].Certificate)) + + if (!chain.Build(remoteChain.ChainElements[0].Certificate)) + { + return; + } + var chainRoot = chain.ChainElements[chain.ChainElements.Count - 1].Certificate; + foreach (var trusted in CertificateStore) { + if (!chainRoot.RawData.SequenceEqual(trusted.RawData)) + { + continue; + } args.ValidityState = CertificateValidity.ForceValid; + break; } }; FiddlerApplication.Prefs.SetBoolPref("fiddler.certmaker.CleanupServerCertsOnExit", true); From 4ac74519da2d619abd7c4b99ad6aa232924d0ca0 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jul 2020 16:15:29 +0200 Subject: [PATCH 08/14] Improve 'missing USB Helper' error message --- USBHelperLauncher/Program.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 1bc24c6..3095d25 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -199,9 +199,14 @@ static void Main(string[] args) Environment.Exit(-1); } - if (!File.Exists("ver") || !File.Exists("WiiU_USB_Helper.exe")) - { - MessageBox.Show("Could not find Wii U USB Helper, please make sure this executable is in the correct folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + if (!File.Exists("WiiU_USB_Helper.exe")) + { + MessageBox.Show( + File.Exists("ver") + ? $"Could not find Wii U USB Helper, your Antivirus software probably deleted it. Try adding the install directory ({GetLauncherPath()}) to your Antivirus' exclusions or disable your Antivirus, then reinstall USB Helper." + : "Could not find Wii U USB Helper, please make sure you unpacked the launcher's files (e.g. USBHelperLauncher.exe) and Wii U USB Helper's files (e.g. WiiU_USB_Helper.exe) into the same directory.", + "Error", MessageBoxButtons.OK, MessageBoxIcon.Error + ); Environment.Exit(-1); } HelperVersion = File.ReadAllLines("ver")[0]; From caf5d2dc6dcf6f36cc15a4d7bec14ac73c3133fa Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jul 2020 19:17:09 +0200 Subject: [PATCH 09/14] Skip module initializer injection if it's already up-to-date --- .../ModuleInitInjectedAttribute.cs | 15 ++++++++ USBHelperInjector/USBHelperInjector.csproj | 1 + USBHelperLauncher/ModuleInitInjector.cs | 36 ++++++++++++++++++- USBHelperLauncher/Program.cs | 11 ++++-- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 USBHelperInjector/ModuleInitInjectedAttribute.cs diff --git a/USBHelperInjector/ModuleInitInjectedAttribute.cs b/USBHelperInjector/ModuleInitInjectedAttribute.cs new file mode 100644 index 0000000..7195bd5 --- /dev/null +++ b/USBHelperInjector/ModuleInitInjectedAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace USBHelperInjector +{ + [AttributeUsage(AttributeTargets.Assembly)] + public class ModuleInitInjectedAttribute : Attribute + { + public string Version { get; } + + public ModuleInitInjectedAttribute(string version) + { + Version = version; + } + } +} diff --git a/USBHelperInjector/USBHelperInjector.csproj b/USBHelperInjector/USBHelperInjector.csproj index 8097b84..28f3283 100644 --- a/USBHelperInjector/USBHelperInjector.csproj +++ b/USBHelperInjector/USBHelperInjector.csproj @@ -56,6 +56,7 @@ + diff --git a/USBHelperLauncher/ModuleInitInjector.cs b/USBHelperLauncher/ModuleInitInjector.cs index b71a002..92b1df3 100644 --- a/USBHelperLauncher/ModuleInitInjector.cs +++ b/USBHelperLauncher/ModuleInitInjector.cs @@ -1,7 +1,9 @@ -using dnlib.DotNet; +using System.IO; +using dnlib.DotNet; using dnlib.DotNet.Emit; using dnlib.DotNet.Writer; using System.Linq; +using USBHelperInjector; namespace USBHelperLauncher { @@ -14,6 +16,29 @@ public ModuleInitInjector(string path) this.path = path; } + public bool RequiresInject(string outputPath) + { + if (!File.Exists(outputPath)) + { + return true; + } + try + { + using (var module = ModuleDefMD.Load(outputPath)) + { + // skip injection if file already exists and is up-to-date + var attr = module.Assembly.CustomAttributes.FirstOrDefault( + a => a.TypeFullName == typeof(ModuleInitInjectedAttribute).FullName + ); + return attr == null || (attr.ConstructorArguments[0].Value as UTF8String).String != Program.GetVersion(); + } + } + catch + { + return true; + } + } + public void Inject(string outputPath) { using (var module = ModuleDefMD.Load(path)) @@ -52,6 +77,15 @@ public void Inject(string outputPath) cctor.Body.Instructions.Insert(0, OpCodes.Call.ToInstruction(testMethodRef)); } + // add injected ModuleInitInjected attribute + var injectAttrDef = typeof(ModuleInitInjectedAttribute).GetConstructor(new[] { typeof(string) }); + var injectAttrRef = module.Import(injectAttrDef); + var newAttribute = new CustomAttribute(injectAttrRef as MemberRef, new[] + { + new CAArgument(injectAttrRef.GetParam(0), Program.GetVersion()) + }); + module.Assembly.CustomAttributes.Add(newAttribute); + // write new file var options = new ModuleWriterOptions(module); options.MetadataOptions.PreserveHeapOrder(module, true); diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 3095d25..5781207 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -303,8 +303,15 @@ out var serviceUri })); var injector = new ModuleInitInjector(executable); executable = Path.Combine(GetLauncherPath(), "WiiU_USB_Helper_.exe"); - injector.Inject(executable); - Logger.WriteLine("Injected module initializer."); + if (injector.RequiresInject(executable)) + { + injector.Inject(executable); + Logger.WriteLine("Injected module initializer."); + } + else + { + Logger.WriteLine("Module initializer already injected."); + } dialog.Invoke(new Action(() => dialog.Close())); if (OverridePublicKey) From 216f269adf7e50f71bad643429f34d80ab387ec3 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jul 2020 22:19:24 +0200 Subject: [PATCH 10/14] Fix detection of running USB Helper instances --- USBHelperLauncher/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 5781207..8a894fb 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -247,7 +247,8 @@ static void Main(string[] args) string executable = Path.Combine(GetLauncherPath(), "WiiU_USB_Helper.exe"); - var running = Process.GetProcessesByName("Patched").FirstOrDefault(p => p.GetMainModuleFileName().StartsWith(GetLauncherPath(), StringComparison.OrdinalIgnoreCase)); + var running = Process.GetProcessesByName("WiiU_USB_Helper_") + .FirstOrDefault(p => p.GetMainModuleFileName().StartsWith(GetLauncherPath(), StringComparison.OrdinalIgnoreCase)); if (running != default(Process)) { From ce532791bc9c022e0cd0d90c95fec49936acde35 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 5 Aug 2020 23:50:15 +0200 Subject: [PATCH 11/14] Add --portable flag --- USBHelperLauncher/Program.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 8a894fb..0424e38 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -67,6 +67,10 @@ static void Main(string[] args) case "showconsole": showConsole = true; break; + case "portable": + Settings.Portable = true; + Settings.Save(); + break; } } } From 0b14bb7a7006c2bc7d71c9bc5f891ebc4dc1c631 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 10 Aug 2020 15:36:43 +0200 Subject: [PATCH 12/14] Fix formatting/naming --- .../IPC/{IPCUtils.cs => IPCUtil.cs} | 39 ++++++++++--------- USBHelperInjector/InjectorService.cs | 4 +- .../Patches/InvalidConfigPatch.cs | 4 +- USBHelperInjector/USBHelperInjector.csproj | 2 +- USBHelperLauncher/LauncherService.cs | 2 +- USBHelperLauncher/Program.cs | 4 +- 6 files changed, 29 insertions(+), 26 deletions(-) rename USBHelperInjector/IPC/{IPCUtils.cs => IPCUtil.cs} (75%) diff --git a/USBHelperInjector/IPC/IPCUtils.cs b/USBHelperInjector/IPC/IPCUtil.cs similarity index 75% rename from USBHelperInjector/IPC/IPCUtils.cs rename to USBHelperInjector/IPC/IPCUtil.cs index c0783d0..a3cd265 100644 --- a/USBHelperInjector/IPC/IPCUtils.cs +++ b/USBHelperInjector/IPC/IPCUtil.cs @@ -7,7 +7,7 @@ namespace USBHelperInjector.IPC { - public class IPCUtils + public class IPCUtil { public static ServiceHost CreateService(IPCType ipcType, Type serviceType, Type contractType, out Uri uri) { @@ -26,7 +26,7 @@ public static ServiceHost CreateNamedPipeService(Type serviceType, Type contract { var guid = Guid.NewGuid().ToString("D"); var host = new ServiceHost(serviceType, new Uri($"net.pipe://localhost/{guid}")); - host.AddServiceEndpoint(contractType, GetNamedPipeBinding(), ""); + host.AddServiceEndpoint(contractType, new NetNamedPipeBinding(""), ""); host.Open(); uri = host.ChannelDispatchers.First().Listener.Uri; return host; @@ -34,8 +34,16 @@ public static ServiceHost CreateNamedPipeService(Type serviceType, Type contract public static ServiceHost CreateTcpService(Type serviceType, Type contractType, out Uri uri) { - var host = new ServiceHost(serviceType, new Uri("net.tcp://127.0.0.1:0")); - var endpoint = host.AddServiceEndpoint(contractType, GetTcpBinding(), "", new Uri("net.tcp://127.0.0.1")); + var localUri = new Uri("net.tcp://127.0.0.1"); + var host = new ServiceHost(serviceType, localUri); + var binding = new NetTcpBinding("") + { + Security = + { + Mode = SecurityMode.None + } + }; + var endpoint = host.AddServiceEndpoint(contractType, binding, "", localUri); endpoint.ListenUriMode = ListenUriMode.Unique; host.Open(); uri = host.ChannelDispatchers.First().Listener.Uri; @@ -49,10 +57,16 @@ public static TContract CreateChannel(IPCType ipcType, string address switch (ipcType) { case IPCType.NamedPipe: - binding = GetNamedPipeBinding(); + binding = new NetNamedPipeBinding(""); break; case IPCType.TCP: - binding = GetTcpBinding(); + binding = new NetTcpBinding("") + { + Security = + { + Mode = SecurityMode.None + } + }; break; default: throw new InvalidEnumArgumentException(nameof(ipcType), (int)ipcType, ipcType.GetType()); @@ -61,18 +75,5 @@ public static TContract CreateChannel(IPCType ipcType, string address var factory = new ChannelFactory(binding, address); return factory.CreateChannel(); } - - - private static NetNamedPipeBinding GetNamedPipeBinding() - { - return new NetNamedPipeBinding(""); - } - private static NetTcpBinding GetTcpBinding() - { - return new NetTcpBinding("") - { - Security = { Mode = SecurityMode.None } - }; - } } } diff --git a/USBHelperInjector/InjectorService.cs b/USBHelperInjector/InjectorService.cs index a3d0961..5e08f87 100644 --- a/USBHelperInjector/InjectorService.cs +++ b/USBHelperInjector/InjectorService.cs @@ -32,14 +32,14 @@ public static void Init() var ipcType = (IPCType)Enum.Parse(typeof(IPCType), args[2]); var launcherUri = args[3]; - IPCUtils.CreateService( + IPCUtil.CreateService( ipcType, typeof(InjectorService), typeof(IInjectorService), out var serviceUri ); - LauncherService = IPCUtils.CreateChannel(ipcType, launcherUri); + LauncherService = IPCUtil.CreateChannel(ipcType, launcherUri); LauncherService.SendInjectorSettings(serviceUri); Harmony = new Harmony("me.failedshack.usbhelperinjector"); diff --git a/USBHelperInjector/Patches/InvalidConfigPatch.cs b/USBHelperInjector/Patches/InvalidConfigPatch.cs index d7bb5c6..a2fbb43 100644 --- a/USBHelperInjector/Patches/InvalidConfigPatch.cs +++ b/USBHelperInjector/Patches/InvalidConfigPatch.cs @@ -15,7 +15,7 @@ class InvalidConfigPatch { static MethodBase TargetMethod() { - return ReflectionHelper.Settings.GetConstructor(new Type[] { }); + return ReflectionHelper.Settings.GetConstructor(Type.EmptyTypes); } static void Postfix() @@ -33,7 +33,7 @@ static void Postfix() var configPaths = AccessTools.Property(configPathsType, "Current").GetValue(null); var fileName = (string)AccessTools.Property(configPathsType, "LocalConfigFilename").GetValue(configPaths); File.Delete(fileName); - MessageBox.Show("A corrupted configuration file has been deleted, you'll need to go through the setup again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("The configuration file has been corrupted. You'll need to go through the setup again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } diff --git a/USBHelperInjector/USBHelperInjector.csproj b/USBHelperInjector/USBHelperInjector.csproj index 28f3283..63714ef 100644 --- a/USBHelperInjector/USBHelperInjector.csproj +++ b/USBHelperInjector/USBHelperInjector.csproj @@ -55,7 +55,7 @@ - + diff --git a/USBHelperLauncher/LauncherService.cs b/USBHelperLauncher/LauncherService.cs index 533c371..b18266c 100644 --- a/USBHelperLauncher/LauncherService.cs +++ b/USBHelperLauncher/LauncherService.cs @@ -18,7 +18,7 @@ public void SetKeySite(string site, string url) public void SendInjectorSettings(Uri uri) { Program.Logger.WriteLine($"Sending information to injector ({uri})..."); - var channel = IPCUtils.CreateChannel(Settings.IPCType, uri.ToString()); + var channel = IPCUtil.CreateChannel(Settings.IPCType, uri.ToString()); if (Program.OverridePublicKey) { channel.SetDonationKey(Program.GenerateDonationKey()); diff --git a/USBHelperLauncher/Program.cs b/USBHelperLauncher/Program.cs index 0424e38..2739549 100644 --- a/USBHelperLauncher/Program.cs +++ b/USBHelperLauncher/Program.cs @@ -264,7 +264,9 @@ static void Main(string[] args) running.Kill(); } + // The target .NET version (4.5) only uses TLS 1.0 and 1.1 by default ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; + Proxy.Start(); // Update translations @@ -291,7 +293,7 @@ static void Main(string[] args) } }).Wait(); - IPCUtils.CreateService( + IPCUtil.CreateService( Settings.IPCType, typeof(LauncherService), typeof(ILauncherService), From ee67970b7a2f72624f858ee0cfffa1165766c3f4 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 10 Aug 2020 15:38:20 +0200 Subject: [PATCH 13/14] Remove other SecurityProtocol overrides --- USBHelperLauncher/Emulator/Package.cs | 1 - USBHelperLauncher/Emulator/Project64Configuration.cs | 1 - USBHelperLauncher/Utils/CitraRepoUtil.cs | 1 - USBHelperLauncher/Utils/GithubUtil.cs | 1 - USBHelperLauncher/Utils/SourceforgeUtil.cs | 1 - 5 files changed, 5 deletions(-) diff --git a/USBHelperLauncher/Emulator/Package.cs b/USBHelperLauncher/Emulator/Package.cs index 649a725..0fc1a19 100644 --- a/USBHelperLauncher/Emulator/Package.cs +++ b/USBHelperLauncher/Emulator/Package.cs @@ -93,7 +93,6 @@ public async Task Download(WebClient client, string path) string fileName = await GetFileName(); string file = Path.Combine(path, fileName); ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // Use non-browser user agent to avoid being redirected to HTML page (e.g. SourceForge) client.Headers.Add("User-Agent", "USBHelperLauncher"); await client.DownloadFileTaskAsync(uri, file); diff --git a/USBHelperLauncher/Emulator/Project64Configuration.cs b/USBHelperLauncher/Emulator/Project64Configuration.cs index b44f7e9..56bf963 100644 --- a/USBHelperLauncher/Emulator/Project64Configuration.cs +++ b/USBHelperLauncher/Emulator/Project64Configuration.cs @@ -20,7 +20,6 @@ public async Task GetPackageAsync() using (WebClient client = new WebClient()) { ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"); try { diff --git a/USBHelperLauncher/Utils/CitraRepoUtil.cs b/USBHelperLauncher/Utils/CitraRepoUtil.cs index 4b1d3a7..ab6c0c1 100644 --- a/USBHelperLauncher/Utils/CitraRepoUtil.cs +++ b/USBHelperLauncher/Utils/CitraRepoUtil.cs @@ -18,7 +18,6 @@ public static async Task GetPackageAsync(string branch, string platform using (WebClient client = new WebClient()) { ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"); try { diff --git a/USBHelperLauncher/Utils/GithubUtil.cs b/USBHelperLauncher/Utils/GithubUtil.cs index f8008c0..bf79d4b 100644 --- a/USBHelperLauncher/Utils/GithubUtil.cs +++ b/USBHelperLauncher/Utils/GithubUtil.cs @@ -13,7 +13,6 @@ public static async Task GetRelease(string user, string repo, string re using (WebClient client = new WebClient()) { ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"); try { diff --git a/USBHelperLauncher/Utils/SourceforgeUtil.cs b/USBHelperLauncher/Utils/SourceforgeUtil.cs index 8842357..40db950 100644 --- a/USBHelperLauncher/Utils/SourceforgeUtil.cs +++ b/USBHelperLauncher/Utils/SourceforgeUtil.cs @@ -13,7 +13,6 @@ public static async Task GetLatestRelease(string project, string platfor using (WebClient client = new WebClient()) { ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"); try { From a7f39cafda27681ed8d5f12b9bf9c6b4bb26023c Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 10 Aug 2020 19:09:25 +0200 Subject: [PATCH 14/14] Remove try/catch in ModuleInitInjector::RequiresInject --- USBHelperLauncher/ModuleInitInjector.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/USBHelperLauncher/ModuleInitInjector.cs b/USBHelperLauncher/ModuleInitInjector.cs index 92b1df3..9e1a75e 100644 --- a/USBHelperLauncher/ModuleInitInjector.cs +++ b/USBHelperLauncher/ModuleInitInjector.cs @@ -22,20 +22,13 @@ public bool RequiresInject(string outputPath) { return true; } - try + using (var module = ModuleDefMD.Load(outputPath)) { - using (var module = ModuleDefMD.Load(outputPath)) - { - // skip injection if file already exists and is up-to-date - var attr = module.Assembly.CustomAttributes.FirstOrDefault( - a => a.TypeFullName == typeof(ModuleInitInjectedAttribute).FullName - ); - return attr == null || (attr.ConstructorArguments[0].Value as UTF8String).String != Program.GetVersion(); - } - } - catch - { - return true; + // skip injection if file already exists and is up-to-date + var attr = module.Assembly.CustomAttributes.FirstOrDefault( + a => a.TypeFullName == typeof(ModuleInitInjectedAttribute).FullName + ); + return attr == null || (attr.ConstructorArguments[0].Value as UTF8String).String != Program.GetVersion(); } }