This repository has been archived on 2026-05-30. You can view files and clone it, but cannot push or open issues or pull requests.
Files
undead-universal-patch-il2cpp/BasePatches/BestHTTP.cs
zombieb bc84ef04ed Add token expiry (bump minor)
Temp remove genuine patch for unity product name
Exporting and importing tokens (not tested very well)
Insecure HTTP warning on auth
2025-03-25 21:57:14 -04:00

98 lines
3.8 KiB
C#

using System;
using System.Reflection;
using HarmonyLib;
namespace undead_universal_patch_il2cpp.Patches
{
[HarmonyPatch]
public class BestHTTP_Unob
{
static string TargetTypeName = "BestHTTP.HTTPManager";
static string TargetMethodName = "SendRequest";
static string Description = "Unobfuscated BestHTTP request URL rewrite patch";
static readonly Type targetType = AccessTools.TypeByName(TargetTypeName);
static readonly Type requestType = AccessTools.TypeByName("BestHTTP.HTTPRequest");
static readonly MethodInfo targetMethod = AccessTools.Method(targetType, TargetMethodName, [requestType]);
static bool Prepare()
{
if (targetType == null)
{
Plugin.Log.LogWarning($"'{Description}' disabled. The type for this patch was not found.");
return false;
}
if (targetMethod == null)
{
Plugin.Log.LogWarning($"'{Description}' disabled. The method for this patch was not found.");
return false;
}
Plugin.Log.LogInfo($"'{Description}' succeeded validation.");
return true;
}
static MethodBase TargetMethod() => targetMethod;
[HarmonyPrefix]
static void Prefix(ref object request)
{
PropertyInfo uriProperty = AccessTools.Property(requestType, "Uri");
if (uriProperty == null)
{
Plugin.Log.LogFatal("BestHTTP_Unob failed: uriProperty was null.");
return;
}
var uriInstance = (Il2CppSystem.Uri)uriProperty.GetValue(request, null);
if (uriInstance == null)
{
Plugin.Log.LogFatal("BestHTTP_Unob failed: uriInstance was null.");
return;
}
if (GenericConfig.LogAllRequests.Value) Plugin.Log.LogInfo($"BestHTTP_Unob request b-URL: {uriInstance.ToString()}");
if (!NameserverConfig.Rewrite.Value) return;
Il2CppSystem.Uri newUri = new(uriInstance.ToString());
// Request changes below
if (newUri.ToString().Contains("ns.rec.net")) newUri = new Il2CppSystem.Uri(NameserverConfig.NewUrl.Value);
if (newUri.ToString().Contains("api.amplitude"))
{
Il2CppSystem.UriBuilder replaceUri = new(new Il2CppSystem.Uri(NameserverConfig.NewUrl.Value));
replaceUri.Path = "/amplitude";
newUri = replaceUri.Uri;
}
if (GalvanicConfig.Enabled.Value)
{
string[] applyHeader = [
"authservice/cachedlogin/forplatformid",
"accountservice/account/create",
"authservice/connect/token"
];
foreach (string header in applyHeader)
{
if (newUri.PathAndQuery.Contains(header))
{
// refresh the token if it expired
// this is somewhat inefficient, but we don't hook into many requests (see below) so it should be fine
GalvanicWebAuth.TokenExpiry();
Type httpRequestType = request.GetType();
MethodInfo addHeaderMethod = httpRequestType.GetMethod("AddHeader");
addHeaderMethod.Invoke(request, ["GalvanicAuth", GalvanicWebAuth.Token]);
break;
}
}
}
if (GenericConfig.LogAllRequests.Value) Plugin.Log.LogInfo($"BestHTTP_Unob request a-URL: {newUri.ToString()}");
uriProperty.SetValue(request, NameserverConfig.Rewrite.Value ? newUri : uriInstance, null);
}
}
}