C# Exe + Web 自动化 (BitComet 绿灯 自动化配置、设置)

server/2025/3/16 10:35:27/

BitComet GreenLight,内网黄灯转绿灯 (HighID), 增加p2p连接率提速下载-CSDN博客
前两天写个这个,每次开机关机后要重来一遍很麻烦的索性写个自动化


先还是按照上面的教程自己制作一遍,留下Luck 以及 路由器相关的 端口记录信息。

(因为自动化我没写创建的逻辑,只是写了更改端口号的逻辑,所以基础信息条目需要存在。)

基于更改信息,自动化主要逻辑如下

1、取得Luck 设定好的端口

2、复制到路由器相关端口映射页面保存

3、启动BT,设置新的端口映射数据


完整代码如下:

 public class NetworkManagerExt{private string HostPort { get; set; }private string RemotePort { get; set; }private const string BitCometPath = @"D:\Program Files\BitComet\BitComet.exe";private const string LuckyPath = @"D:\Lucky\lucky.exe";private const string RouterUrl = "http://192.168.0.1/";private const string RouterPassword = "你自己的密码";private const string LuckyUrl = "http://127.0.0.1:16601/#/login";private const string LuckyPassword = "666"; //luck 默认密码private const int DefaultTimeoutSeconds = 30;public void ConfigureNetwork(){StartLuckyAndConfigureRouter();}public void StartBitComet(){if (!string.IsNullOrEmpty(RemotePort)){ConfigureBitComet(RemotePort);}else{Console.WriteLine("Error: RemotePort not set. Cannot configure BitComet.");}}private Process GetOrStartProcess(string exePath){string processName = System.IO.Path.GetFileNameWithoutExtension(exePath);Process[] processes = Process.GetProcessesByName(processName);return processes.Length > 0 ? processes[0] : Process.Start(exePath);}private void ConfigureBitComet(string port){Process calc = Process.Start(BitCometPath);AutomationElement mainExe = null;// 等待BitComet窗口出现DateTime startTime = DateTime.Now;TimeSpan timeout = TimeSpan.FromSeconds(DefaultTimeoutSeconds);while (mainExe == null){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for BitComet window to appear");}AutomationElementCollection elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));foreach (AutomationElement item in elementCollection){if (item.Current.Name.Contains("BitComet", StringComparison.OrdinalIgnoreCase)){mainExe = item;break;}}if (mainExe == null){System.Threading.Thread.Sleep(500); // 短暂等待后重试}}WindowPattern windowPattern = (WindowPattern)mainExe.GetCurrentPattern(WindowPattern.Pattern);windowPattern.SetWindowVisualState(WindowVisualState.Normal);System.Windows.Forms.SendKeys.SendWait("^p");bool setOperated = false;startTime = DateTime.Now;while (!setOperated){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for BitComet settings dialog");}var tempWindow = mainExe.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));if (tempWindow != null){var tempPane = tempWindow.FindFirst(TreeScope.Subtree,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));var onButtonPane = tempWindow.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));if (tempPane != null){ValuePattern valuePattern = (ValuePattern)tempPane.GetCurrentPattern(ValuePattern.Pattern);valuePattern.SetValue(port);setOperated = true;InvokePattern invokePattern = (InvokePattern)onButtonPane.GetCurrentPattern(InvokePattern.Pattern);invokePattern.Invoke();}}if (!setOperated){System.Threading.Thread.Sleep(500); // 短暂等待后重试}}}private void StartLuckyAndConfigureRouter(){Process calc = GetOrStartProcess(LuckyPath);AutomationElement mainExe = null;// 等待Lucky窗口出现DateTime startTime = DateTime.Now;TimeSpan timeout = TimeSpan.FromSeconds(DefaultTimeoutSeconds);while (mainExe == null){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for Lucky window to appear");}var element = AutomationElement.RootElement.FindFirst(TreeScope.Subtree,new PropertyCondition(AutomationElement.NameProperty, "万吉"));if (element != null){mainExe = element;}else{System.Threading.Thread.Sleep(500); // 短暂等待后重试}}using (IWebDriver driver = new EdgeDriver()){// 设置隐式等待,适用于所有元素查找driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);// 创建显式等待对象WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(DefaultTimeoutSeconds));// 登录Luckydriver.Navigate().GoToUrl(LuckyUrl);// 等待输入框出现并输入密码var inputElements = wait.Until(d => d.FindElements(By.CssSelector("input[placeholder='默认666']")));foreach (var item in inputElements){item.Clear();item.SendKeys(LuckyPassword);}// 等待登录按钮出现并点击IWebElement loginButton = wait.Until(d => {var button = d.FindElement(By.CssSelector("button.el-button.el-button--primary.is-round"));return button.Text == "登录" ? button : null;});loginButton.Click();// 获取端口信息driver.Navigate().GoToUrl("http://127.0.0.1:16601/#/stun");// 等待第一个IP端口信息出现IWebElement firstIpSpan = wait.Until(d => d.FindElement(By.XPath("/html/body/div[1]/div/section/section/section/main/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div/div/div/table/tbody/tr[2]/td[2]/span[1]/span")));// 等待第二个IP端口信息出现IWebElement secondIpSpan = wait.Until(d => d.FindElement(By.XPath("/html/body/div[1]/div/section/section/section/main/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div/div/div/table/tbody/tr[2]/td[2]/span[3]/span")));string firstIpPort = firstIpSpan.Text;string secondIpPort = secondIpSpan.Text;if (!string.IsNullOrEmpty(firstIpPort) && !string.IsNullOrEmpty(secondIpPort)){HostPort = firstIpPort.Split(':')[1];RemotePort = secondIpPort.Split(':')[1];// 配置路由器ConfigureRouter(driver, wait);}}}private void ConfigureRouter(IWebDriver driver, WebDriverWait wait){driver.Navigate().GoToUrl(RouterUrl);// 等待密码输入框出现IWebElement routerPwd = wait.Until(d => d.FindElement(By.XPath("/html/body/div[6]/div[2]/ul/li[2]/ul/li[1]/input")));routerPwd.Clear();routerPwd.SendKeys(RouterPassword);// 等待登录按钮出现并点击IWebElement loginButton = wait.Until(d => d.FindElement(By.XPath("/html/body/div[6]/div[2]/ul/li[3]/input")));loginButton.Click();// 等待功能1按钮出现并点击IWebElement func1 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[2]/ul/li[3]/div/i[2]")));func1.Click();// 等待功能2按钮出现并点击IWebElement func2 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div/div[2]/div[1]/div[8]/div/div/input[3]")));func2.Click();// 等待功能3按钮出现并点击IWebElement func3 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[7]/i")));func3.Click();// 设置外部端口IWebElement outport = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[3]/input")));outport.Clear();outport.SendKeys(HostPort);// 设置内部端口IWebElement selfport = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[4]/input")));selfport.Clear();selfport.SendKeys(RemotePort);// 保存路由器设置IWebElement saveButton = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[7]/input[1]")));saveButton.Click();}}

路由器 Web 部分(ConfigureRouter()方法),需要自行Web 页面 元素 XPath 取得慢慢调整,我的路由器是 TPLink ,TL-WDR8500,配置界面大致如下:

调用代码
 

   static void Main(string[] args){var networkManager = new NetworkManagerExt();networkManager.ConfigureNetwork();networkManager.StartBitComet();}

这样 每次开机执行一下这个exe,保障BT 是绿灯,前面配置设置那些步骤全自动化

需要的包和引用如下:(基于 .net 9.0 编译通过,测试通过)

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><ApplicationManifest>app.manifest</ApplicationManifest></PropertyGroup><ItemGroup><FrameworkReference Include="Microsoft.WindowsDesktop.App" /><PackageReference Include="Selenium.WebDriver" Version="4.29.0" /></ItemGroup>
</Project>



 


http://www.ppmy.cn/server/175400.html

相关文章

全球领先的光学方案设计公司:倚光科技

在光学技术革新的浪潮中&#xff0c;倚光&#xff08;深圳&#xff09;科技有限公司以创新者的姿态迅速崛起&#xff0c;成为全球光学领域的标杆企业。自 2021 年成立以来&#xff0c;公司始终聚焦纳米光学技术研发与超精密加工&#xff0c;凭借顶尖的技术实力和前瞻性的市场布…

组件通信框架ARouter原理剖析

组件通信框架ARouter原理剖析 一、前言 随着Android应用规模的不断扩大&#xff0c;模块化和组件化开发变得越来越重要。ARouter作为一个用于帮助Android应用进行组件化改造的框架&#xff0c;提供了一套完整的路由解决方案。本文将深入分析ARouter的核心原理和实现机制。 二…

2025 linux系统资源使用率统计docker容器使用率统计docker监控软件Weave Scope安装weavescope

1.Weave Scope介绍 Weave Scope 是一款用于监控和可视化 Docker 容器、Kubernetes 集群以及分布式应用的强大工具。它的设计目标是帮助开发者和运维人员更好地理解和管理复杂的微服务架构。以下是 Weave Scope 的主要优点&#xff1a; 1. 实时可视化 Weave Scope 提供了一个直…

AI时代研究卷积神经网络(CNN)工具与方法

在AI时代&#xff0c;作为研究卷积神经网络&#xff08;CNN&#xff09;和视觉网络的程序员&#xff0c;合理选择工具、技术和学习资源是提升效率与专业能力的关键。以下结合2025年最新技术动态与实践经验&#xff0c;从工具链、技术方向、学习资料及效率方法四个维度进行系统推…

【Go语言圣经1.5】

目标 概念 要点&#xff08;案例&#xff09; 实现了一个简单的 HTTP 客户端程序&#xff0c;主要功能是&#xff1a; 读取命令行参数&#xff1a;程序从命令行获取一个或多个 URL。发送 HTTP GET 请求&#xff1a;使用 Go 内置的 net/http 包&#xff0c;通过 http.Get 函…

打包当前Ubuntu镜像 制作Ubuntu togo系统

我的系统的基本情况说明&#xff1a; 我原来的系统的具体型号如下&#xff1a; uname -rLinux Engine 5.15.0-134-generic #145~20.04.1-Ubuntu SMP Mon Feb 17 13:27:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux我原来的硬盘以及分区策略如下&#xff1a; 可以看到我的分区…

Scala语言的数据库编程

Scala语言的数据库编程 Scala是一种现代的多用途编程语言&#xff0c;它融合了面向对象和函数式编程的特性。近年来&#xff0c;Scala逐渐在大数据处理、分布式计算和Web开发等领域获得了广泛的关注。在这些应用中&#xff0c;数据库编程是不可或缺的一部分。本文将探讨Scala语…

【蓝桥杯】省赛:连连看(暴力 非AC)

对角线 遍历每个元素的左下、右下对角线&#xff0c;检查是否值相等 n,m map(int,input().split()) A [] for i in range(n):ls list(map(int,input().split()))A.append(ls)cnt 0 for i in range(n):for j in range(m):# zuoxiafor p in range(1, min(n-1-i 1,j1)):if A…