Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions App/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ partial class NetworkConfigGroup
[ConfigItem<int>("SystemDebugAnim", 9)] public partial int AnimationSpeed { get; set; }
[ConfigItem<bool>("SystemDebugDelay", false)] public partial bool AddRandomDelay { get; set; }
[ConfigItem<bool>("SystemDebugSkipCopy", false)] public partial bool DontCopy { get; set; }
[ConfigItem<bool>("SystemDebugAllowRestrictedFeature", false)] public partial bool AllowRestrictedFeature { get; set; }
}
}

Expand Down
40 changes: 40 additions & 0 deletions Net/NetworkHelper.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,4 +17,40 @@ public static int NewTcpPort()
listener.Stop();
return port;
}

/// <summary>
/// 测试 HTTP 连通性
/// </summary>
/// <param name="url">目标 Url,留空则为 baidu.com</param>
/// <param name="timeout">超时时间 (默认 3000 ms)</param>
/// <returns>若 HTTP 连接成功,则返回 true;反之,则返回 false</returns>
public static async Task<bool> 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;
}
}
}
15 changes: 15 additions & 0 deletions Utils/RegionUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Globalization;
using PCL.Core.App;

namespace PCL.Core.Utils;

public static class RegionUtils
{
/// <summary>
/// 获取区域限制状态
/// </summary>
public static bool IsRestrictedFeatAllowed =>
Config.System.Debug.AllowRestrictedFeature || (TimeZoneInfo.Local.Id == "China Standard Time" &&
(CultureInfo.CurrentCulture.Name == "zh-CN" || CultureInfo.CurrentUICulture.Name == "zh-CN"));
}
Loading