From 782e3acae299c3675527746365c83eea81a96965 Mon Sep 17 00:00:00 2001 From: Pigeon0v0 <60414767+Pigeon0v0@users.noreply.github.com> Date: Sat, 6 Dec 2025 22:33:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E8=BF=9B=E8=A1=8C=E5=8C=BA=E5=9F=9F=E9=99=90=E5=88=B6?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Config.cs | 1 + Net/NetworkHelper.cs | 40 ++++++++++++++++++++++++++++++++++++++++ Utils/RegionUtils.cs | 15 +++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 Utils/RegionUtils.cs diff --git a/App/Config.cs b/App/Config.cs index 3c0ec99c..85d4252a 100644 --- a/App/Config.cs +++ b/App/Config.cs @@ -292,6 +292,7 @@ partial class NetworkConfigGroup [ConfigItem("SystemDebugAnim", 9)] public partial int AnimationSpeed { get; set; } [ConfigItem("SystemDebugDelay", false)] public partial bool AddRandomDelay { get; set; } [ConfigItem("SystemDebugSkipCopy", false)] public partial bool DontCopy { get; set; } + [ConfigItem("SystemDebugAllowRestrictedFeature", false)] public partial bool AllowRestrictedFeature { get; set; } } } diff --git a/Net/NetworkHelper.cs b/Net/NetworkHelper.cs index 65a31411..f58040b8 100644 --- a/Net/NetworkHelper.cs +++ b/Net/NetworkHelper.cs @@ -1,5 +1,9 @@ +using System; using System.Net; +using System.Net.Http; using System.Net.Sockets; +using System.Threading.Tasks; +using PCL.Core.Logging; namespace PCL.Core.Net; @@ -13,4 +17,40 @@ public static int NewTcpPort() listener.Stop(); return port; } + + /// + /// 测试 HTTP 连通性 + /// + /// 目标 Url,留空则为 baidu.com + /// 超时时间 (默认 3000 ms) + /// 若 HTTP 连接成功,则返回 true;反之,则返回 false + public static async Task TestHttpConnectionAsync(string url = "http://www.baidu.com", int timeout = 3000) + { + try + { + using (var httpClient = new HttpClient()) + { + httpClient.Timeout = TimeSpan.FromMilliseconds(timeout); + + var request = new HttpRequestMessage(HttpMethod.Head, url); + var response = await httpClient.SendAsync(request); + + return response.IsSuccessStatusCode; + } + } + catch (HttpRequestException ex) + { + LogWrapper.Error($"HTTP 请求异常: {ex.Message}"); + return false; + } + catch (TaskCanceledException) + { + LogWrapper.Error("请求超时"); + return false; + } + catch + { + return false; + } + } } diff --git a/Utils/RegionUtils.cs b/Utils/RegionUtils.cs new file mode 100644 index 00000000..231b1f7e --- /dev/null +++ b/Utils/RegionUtils.cs @@ -0,0 +1,15 @@ +using System; +using System.Globalization; +using PCL.Core.App; + +namespace PCL.Core.Utils; + +public static class RegionUtils +{ + /// + /// 获取区域限制状态 + /// + public static bool IsRestrictedFeatAllowed => + Config.System.Debug.AllowRestrictedFeature || (TimeZoneInfo.Local.Id == "China Standard Time" && + (CultureInfo.CurrentCulture.Name == "zh-CN" || CultureInfo.CurrentUICulture.Name == "zh-CN")); +} \ No newline at end of file