Skip to content

Commit

Permalink
每次启动本地节点前会更新BT的Trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Aug 12, 2024
1 parent 4fae5fd commit def6d6c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Aria2Fast/Assets/Config/aria2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 两个参数一致
Expand Down
98 changes: 77 additions & 21 deletions Aria2Fast/Service/Aria2ApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,43 @@ public void Init()
SetupEvent();
}

private async Task<bool> 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;
}




/// <summary>
/// 启动本地Aria2并管理,退出时跟随退出
/// </summary>
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -585,39 +625,55 @@ internal async Task<bool> 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;
}

Expand Down

0 comments on commit def6d6c

Please sign in to comment.