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

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.mzph.cn/pingmian/72425.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于 Docker 搭建 FRP 内网穿透开源项目

有些配置项不知道该不该用,不知道该在哪用,不知道怎么用,所以我自己写个文章简单记录一下做个笔记 本文介绍的是基于 Docker 运行 frps 和 frpc,并通过 TCP 协议简单穿透 SSH 和 HTTP,在观看本文之前请确保你的机器已经安装 Docker 服务端搭建 frps# 连接拥有公网 IP 的…

python---序列 (str,list,tuple)

一、 序列类型入门 python的数据类型&#xff1a;int float bool str 运算符 - * / % > < and or not 流程控制ifelsewhilefor掌握python的2大容器类型数值类型&#xff08;3个&#xff09;&#xff1a;int float bool序列类型容器(3个)&#xff1a;str &#xff1a; …

CSS元素层叠顺序规则

CSS元素层叠顺序规则 看图说话总结: background/borderz-index(<0)blockfloatinline/inline-blockz-index(0,auto)z-index (>0)

删除有序数组中的重复项(26)

26. 删除有序数组中的重复项 - 力扣&#xff08;LeetCode&#xff09; 解法&#xff1a; class Solution { public:int removeDuplicates(vector<int>& nums) {auto first nums.begin();auto last nums.end();auto result first;if (first last) {return std::…

Vue 概念、历史、发展和Vue简介

一、Vue概念 官方定义&#xff1a; 渐进式JavaScript 框架&#xff0c;易学易用&#xff0c;性能出色&#xff0c;适用场景丰富的 Web 前端框架。 Vue.js 是一个流行的前端JavaScript框架&#xff0c;由尤雨溪&#xff08;Evan You&#xff09;开发并维护。 它最初于2014年发…

ArcGIS Pro将有文字标注底图切换为无标注底图(在线地图图源)

今天介绍一下在ArcGIS Pro将有标注的地形底图换成无标注的底图。 大家在这项目底图时候会经常调用ArcGIS Pro自带的地形图&#xff0c;但是这个地形图自带是有注记的&#xff0c;如下图。 如何更改&#xff0c;才可以调用无文字注记的呢&#xff1f; 对于一个已经切好图的有注记…

Xxl-Job学习笔记

目录 概述 核心架构 核心特点 应用场景 什么是任务调度 快速入门 获取源码 初始化调度数据库 基本配置 数据源datasource 邮箱email&#xff08;可选&#xff09; 会话令牌access token 启动调度中心 启动执行器 依赖 yaml基本配置 XxlJobConfig类配置 定义执…

让双向链表不在云里雾里

又来博客留下我的足迹了&#xff0c;哈哈哈&#xff0c;这次是对于双向链表的理解 目录 创建双向链表&#xff1a; 申请结点&#xff1a; 双向链表初始化&#xff1a; 双向链表插入结点&#xff1a; 双向链表删除结点&#xff1a; 双向链表的打印&#xff1a; 双向链表…

java虚拟机(JVM)以及各种参数详解

Java 虚拟机&#xff08;JVM&#xff09;提供了许多参数来调整其行为和性能&#xff0c;以便更好地适应不同的应用场景。理解和使用这些参数对于优化 Java 应用程序的性能非常重要。以下是一些常用的 JVM 参数及其详细说明&#xff1a; 1. 内存管理参数 -Xms<size>&…

如何搭配 AI 量化策略选股

AI 量化选股策略结合了 技术指标、基本面数据、市场情绪&#xff0c;利用 机器学习、深度学习、因子分析 等方法&#xff0c;提高选股精准度和交易决策效率。下面介绍 如何搭配 AI 量化策略选股。 1. AI 量化选股的核心方法 AI 量化选股主要依靠 数据驱动&#xff0c;包括&…

Python 爬虫:一文掌握 SVG 映射反爬虫

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 1. SVG 概述1.1 SVG的优点1.1 映射反爬虫的原理2. SVG 映射反爬虫的示例3. 应对 SVG 映射反爬虫的方法3.1 解析 SVG 图像3.2 处理自定义字体3.3 使用 OCR 技术3.4 动态生成 SVG 的处理4. 实战案例4.1 使用 SVG 映射显示…

前端工程化之前端工程化详解 包管理工具

前端工程化详解 & 包管理工具 前端工程化什么是前端工程化前端工程化发展脚手架能力 体验度量规范流程效能流程扭转 稳定性建设针对整体稳定性建设 可监控&#xff1a;前端监控系统 包管理工具npm包详解package.jsonname 模块名description 模块描述信息keywords&#xff1…

《Python实战进阶》No24: PyAutoGUI 实现桌面自动化

No24: PyAutoGUI 实现桌面自动化 摘要 PyAutoGUI 是一个跨平台的桌面自动化工具&#xff0c;能够模拟鼠标点击、键盘输入、屏幕截图与图像识别&#xff0c;适用于重复性桌面任务&#xff08;如表单填写、游戏操作、批量文件处理&#xff09;。本集通过代码截图输出日志的实战形…

一周学会Flask3 Python Web开发-SQLAlchemy查询所有数据操作-班级模块

锋哥原创的Flask3 Python Web开发 Flask3视频教程&#xff1a; 2025版 Flask3 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili 我们来新建一个的蓝图模块-班级模块&#xff0c;后面可以和学生模块&#xff0c;实现一对多的数据库操作。 blueprint下新建g…

Neural Architecture Search for Transformers:A Survey

摘要 基于 Transformer 的深度神经网络架构因其在自然语言处理 (NLP) 和计算机视觉 (CV) 领域的各种应用中的有效性而引起了极大的兴趣。这些模型是多种语言任务&#xff08;例如情绪分析和文本摘要&#xff09;的实际选择&#xff0c;取代了长短期记忆 (LSTM) 模型。视觉 Tr…

TCP 全连接队列 内核层理解socket

TCP 全连接队列 理解 listen 的第二个参数 int listen(int sockfd, int backlog);backlog 参数表示 全连接队列&#xff08;accept 队列&#xff09;的最大长度。 那什么是全连接队列呢&#xff1f; 三次握手 & accept() 处理流程 客户端发送 SYN&#xff0c;服务器收到并…

程序化广告行业(18/89):交易模式与关键概念解析

程序化广告行业&#xff08;18/89&#xff09;&#xff1a;交易模式与关键概念解析 大家好呀&#xff01;一直以来&#xff0c;我都在深入研究程序化广告这个充满挑战与机遇的领域&#xff0c;在学习过程中收获了很多&#xff0c;也迫不及待想和大家分享。写这篇博客&#xff…

在离线情况下如何使用 Python 翻译文本

以下是在离线环境下使用Python进行文本翻译的两种主流方案&#xff0c;包含本地模型部署和轻量级词典两种方法&#xff1a; 方案一&#xff1a;使用本地神经网络翻译模型&#xff08;推荐&#xff09; # 安装依赖&#xff08;需提前下载&#xff09; # pip install argos-tra…

OpenEuler-22.03-LTS上利用Ansible轻松部署MySQL 5.7

一、需求 使用ansible自动化部署mysql二进制部署mysql部署mysql并创建JDBC用户 二、环境信息 本文涉及的代码&#xff0c;配置文件地址&#xff1a; 链接&#xff1a;百度网盘 请输入提取码 提取码&#xff1a;1g6y 软件名称版本备注Ansible2.9.27All modules — Ansible Doc…

基于javaweb的SpringBoot农资商城购物商城系统设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论…