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