diff --git a/ClashYamlParser/ClashYamlParser.csproj b/ClashYamlParser/ClashYamlParser.csproj
index bc512c7..8f547c3 100644
--- a/ClashYamlParser/ClashYamlParser.csproj
+++ b/ClashYamlParser/ClashYamlParser.csproj
@@ -11,7 +11,7 @@
zhangbudademao.com
Copyrights © 2024 zhangbudademao.com all rights reserved.
$(AssemblyVersion)
- 1.1.5.8
+ 1.1.6.9
$(AssemblyVersion)
$(AssemblyVersion)
diff --git a/ClashYamlParser/Program.cs b/ClashYamlParser/Program.cs
index acdad78..7491d54 100644
--- a/ClashYamlParser/Program.cs
+++ b/ClashYamlParser/Program.cs
@@ -8,9 +8,11 @@
ConcurrentQueue log_temp = new();
string log_file = $"logs\\{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}.log";
+List blacklist = [];
await log($"{Assembly.GetExecutingAssembly().GetName().Version}", "version");
Timer log_wraper = new(async (o) => { await WrapLogAsync(); }, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
+Timer blacklist_refresher = new(async (o) => { await LoadBlacklist(); }, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));
HttpListener server = new HttpListener();
server.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
@@ -33,6 +35,38 @@ await Task.Run(async () =>
await log("Application exited.", flush: true);
});
+async Task LoadBlacklist()
+{
+ if (!File.Exists("blacklist.txt"))
+ {
+ return;
+ }
+
+ string[] backup = new string[blacklist.Capacity];
+ blacklist.CopyTo(backup);
+ blacklist = new(blacklist.Capacity);
+ try
+ {
+ using HttpClient httpClient = new();
+ Regex regex = RegexDomain();
+ foreach (string line in await File.ReadAllLinesAsync("blacklist.txt"))
+ {
+ HttpResponseMessage response = await httpClient.GetAsync(line);
+ if (response.IsSuccessStatusCode)
+ {
+ foreach (Match m in regex.Matches(await response.Content.ReadAsStringAsync()))
+ {
+ blacklist.Add(string.Format("DOMAIN-SUFFIX,{0},REJECT", m.Value));
+ }
+ }
+ }
+ }
+ catch
+ {
+ blacklist = [.. backup];
+ }
+}
+
async Task WrapLogAsync()
{
StringBuilder sb = new StringBuilder();
@@ -160,11 +194,10 @@ void SendResponseString(HttpListenerContext context, string? str_data = null, in
async Task<(string, string, string)> GetOriginYAMLAsync(string url)
{
- using (HttpClient httpClient = new HttpClient())
- {
- var response = await httpClient.GetAsync(url);
- return (await response.Content.ReadAsStringAsync(), response.Content.Headers.ContentDisposition?.FileName ?? "clash.xaml", response.Headers.GetValues("Subscription-Userinfo").First());
- }
+ using HttpClient httpClient = new();
+ var response = await httpClient.GetAsync(url);
+ response.EnsureSuccessStatusCode();
+ return (await response.Content.ReadAsStringAsync(), response.Content.Headers.ContentDisposition?.FileName ?? "clash.xaml", response.Headers.GetValues("Subscription-Userinfo").First());
}
string CombineYAML(string origin_YAML, string parser_YAML)
@@ -199,9 +232,23 @@ string CombineYAML(string origin_YAML, string parser_YAML)
MergeConfig("rule-providers", parserDict, ref originDict);
MixObject(parserDict, ref originDict);
+ AddAdBlocker(ref originDict);
+
return serializer.Serialize(originDict);
}
+void AddAdBlocker(ref Dictionary originDict)
+{
+ if (!originDict.TryGetValue("rules", out object? value))
+ {
+ originDict.Add("rules", blacklist);
+ }
+ else
+ {
+ (value as List