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>
?