Block data uploads

This commit is contained in:
Anonymous Proxy 2024-05-22 23:49:10 +08:00
parent ca70c3536f
commit 7b54f443ab
5 changed files with 56 additions and 7 deletions

4
.gitignore vendored
View file

@ -1,3 +1,7 @@
config.json
# -----------------
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##

View file

@ -7,6 +7,7 @@ namespace FireflySR.Tool.Proxy
{
private const string Title = "FreeSR Proxy (Alter)";
private const string ConfigPath = "config.json";
private const string ConfigTemplatePath = "config.tmpl.json";
private static ProxyService s_proxyService = null!;
@ -15,6 +16,7 @@ namespace FireflySR.Tool.Proxy
Console.Title = Title;
Console.WriteLine($"Firefly.Tool.Proxy - Credits for original FreeSR Proxy");
CheckProxy();
InitConfig();
var conf = JsonSerializer.Deserialize<ProxyConfig>(File.ReadAllText(ConfigPath)) ?? throw new FileLoadException("Please correctly configure config.json.");
s_proxyService = new ProxyService(conf.DestinationHost, conf.DestinationPort, conf);
@ -24,6 +26,14 @@ namespace FireflySR.Tool.Proxy
Thread.Sleep(-1);
}
private static void InitConfig()
{
if (!File.Exists(ConfigPath))
{
File.Copy(ConfigTemplatePath, ConfigPath);
}
}
private static void OnProcessExit(object? sender, EventArgs args)
{
s_proxyService.Shutdown();
@ -68,4 +78,4 @@ namespace FireflySR.Tool.Proxy
}
}
}
}
}

View file

@ -5,7 +5,8 @@ public class ProxyConfig
public List<string> RedirectDomains { get; set; } = [];
public List<string> AlwaysIgnoreDomains { get; set; } = [];
public List<string> ForceRedirectOnUrlContains { get; set; } = [];
public HashSet<string> BlockUrls { get; set; } = [];
public required string DestinationHost { get; set; }
public required int DestinationPort { get; set; }
public int ProxyBindPort { get; set; }
}
}

View file

@ -3,6 +3,7 @@
using System;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
@ -38,8 +39,11 @@
_webProxyServer.AddEndPoint(explicitEP);
_webProxyServer.Start();
_webProxyServer.SetAsSystemHttpProxy(explicitEP);
_webProxyServer.SetAsSystemHttpsProxy(explicitEP);
if (OperatingSystem.IsWindows())
{
_webProxyServer.SetAsSystemHttpProxy(explicitEP);
_webProxyServer.SetAsSystemHttpsProxy(explicitEP);
}
}
public void Shutdown()
@ -74,6 +78,12 @@
return false;
}
private bool ShouldBlock(Uri uri)
{
var path = uri.AbsolutePath;
return _conf.BlockUrls.Contains(path);
}
private Task BeforeRequest(object sender, SessionEventArgs args)
{
string hostname = args.HttpClient.Request.RequestUri.Host;
@ -82,12 +92,24 @@
string requestUrl = args.HttpClient.Request.Url;
Uri local = new Uri($"http://{_targetRedirectHost}:{_targetRedirectPort}/");
string replacedUrl = new UriBuilder(requestUrl)
Uri builtUrl = new UriBuilder(requestUrl)
{
Scheme = local.Scheme,
Host = local.Host,
Port = local.Port
}.Uri.ToString();
}.Uri;
string replacedUrl = builtUrl.ToString();
if (ShouldBlock(builtUrl))
{
Console.WriteLine($"Blocked: {replacedUrl}");
args.Respond(new Titanium.Web.Proxy.Http.Response(Encoding.UTF8.GetBytes("Fuck off"))
{
StatusCode = 404,
StatusDescription = "Oh no!!!",
}, true);
return Task.CompletedTask;
}
Console.WriteLine("Redirecting: " + replacedUrl);
args.HttpClient.Request.Url = replacedUrl;

View file

@ -10,6 +10,7 @@
".juequling.com",
".zenlesszonezero.com",
".bh3.com",
".honkaiimpact3.com",
".mob.com",
".hyg.com"
],
@ -21,10 +22,21 @@
"autopatchcn.bhsr.com",
"autopatchos.starrails.com"
],
"BlockUrls": [
"/sdk/upload",
"/sdk/dataUpload",
"/common/h5log/log/batch",
"/crash/dataUpload",
"/crashdump/dataUpload",
"/client/event/dataUpload",
"/log",
"/asm/dataUpload",
"/sophon/dataUpload"
],
"ForceRedirectOnUrlContains": [
"query_dispatch",
"query_gateway",
"query_region_list",
"query_cur_region"
]
}
}