From def6d6ce9faffce7784377eb26a63fea64794d76 Mon Sep 17 00:00:00 2001 From: aiqinxuancai Date: Mon, 12 Aug 2024 16:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E5=90=AF=E5=8A=A8=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E8=8A=82=E7=82=B9=E5=89=8D=E4=BC=9A=E6=9B=B4=E6=96=B0?= =?UTF-8?q?BT=E7=9A=84Trackers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria2Fast/Assets/Config/aria2.conf | 2 +- Aria2Fast/Service/Aria2ApiManager.cs | 98 ++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 22 deletions(-) diff --git a/Aria2Fast/Assets/Config/aria2.conf b/Aria2Fast/Assets/Config/aria2.conf index 578b555..19e682a 100644 --- a/Aria2Fast/Assets/Config/aria2.conf +++ b/Aria2Fast/Assets/Config/aria2.conf @@ -280,7 +280,7 @@ bt-detach-seed-only=true ## 客户端伪装 ## # 自定义 User Agent -user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.4577.63 Safari/537.36 Edg/122.0.961.47 +user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 # BT 客户端伪装 # PT 下载需要保持 user-agent 和 peer-agent 两个参数一致 diff --git a/Aria2Fast/Service/Aria2ApiManager.cs b/Aria2Fast/Service/Aria2ApiManager.cs index 55b9e1d..0070def 100644 --- a/Aria2Fast/Service/Aria2ApiManager.cs +++ b/Aria2Fast/Service/Aria2ApiManager.cs @@ -95,6 +95,43 @@ public void Init() SetupEvent(); } + private async Task UpdateTrackersList() + { + try + { + var aria2Path = Path.Combine(Directory.GetCurrentDirectory(), "Aria2"); + var aria2Conf = Path.Combine(aria2Path, "aria2.conf"); + + string list = await "https://cf.trackerslist.com/best.txt".WithTimeout(10).GetStringAsync(); + list = list.Replace("\n\n", ","); + + + //更新内容到 + var lines = File.ReadAllLines(aria2Conf); + for (int i = 0; i < lines.Length; i++) + { + if (lines[i].StartsWith("bt-tracker=")) + { + lines[i] = $"bt-tracker={list}"; + } + } + + File.WriteAllLines(aria2Conf, lines); + Debug.WriteLine(list); + return true; + } + catch (Exception e) + { + + } + + + return false; + } + + + + /// /// 启动本地Aria2并管理,退出时跟随退出 /// @@ -125,6 +162,9 @@ private async void StartupLocalAria2() PathHelper.WriteResourceToFile("Aria2Fast.Assets.Config.dht6.dat", Path.Combine(aria2Path, "dht6.dat")); File.WriteAllBytes(Path.Combine(aria2Path, "aria2.session"), new byte[0]); } + + await UpdateTrackersList(); + //如果没配置,则写出配置,否则读取配置中存储路径 var conf = new ConfigFileManager(aria2Conf); var port = conf.GetValue("rpc-listen-port"); @@ -585,39 +625,55 @@ internal async Task UpdateRpc() { var rpc = AppConfig.Instance.ConfigData.Aria2RpcAuto; var token = AppConfig.Instance.ConfigData.Aria2TokenAuto; + int retryCount = 0; - try + while (retryCount < 3) { - _eventReceivedSubject.OnNext(new LoginStartEvent()); - _client = new Aria2NetClient(rpc, token); + try + { + var currentRpc = AppConfig.Instance.ConfigData.Aria2RpcAuto; + if (currentRpc != rpc) + { + return false; + } + _eventReceivedSubject.OnNext(new LoginStartEvent()); + _client = new Aria2NetClient(rpc, token); - var result = await _client.TellAllAsync(); + var result = await _client.TellAllAsync(); - if (result.Count >= 0) - { - Connected = true; - CurrentRpc = rpc; - _eventReceivedSubject.OnNext(new LoginResultEvent(true)); - UpdateTask(); - return true; + currentRpc = AppConfig.Instance.ConfigData.Aria2RpcAuto; + if (currentRpc != rpc) + { + return false; + } + + if (result.Count >= 0) + { + Connected = true; + CurrentRpc = rpc; + _eventReceivedSubject.OnNext(new LoginResultEvent(true)); + UpdateTask(); + return true; + } + else + { + Connected = false; + CurrentRpc = rpc; + _eventReceivedSubject.OnNext(new LoginResultEvent(false)); + } } - else + catch (Exception ex) { + Debug.WriteLine(ex.ToString()); Connected = false; CurrentRpc = rpc; _eventReceivedSubject.OnNext(new LoginResultEvent(false)); - - return false; } + retryCount++; + await Task.Delay(3000); } - catch (Exception ex) - { - Debug.WriteLine(ex.ToString()); - Connected = false; - CurrentRpc = rpc; - _eventReceivedSubject.OnNext(new LoginResultEvent(false)); - } + return false; }