2024-11-07 06:46:27 +00:00
|
|
|
|
using CliWrap;
|
2024-10-08 09:40:25 +00:00
|
|
|
|
using CliWrap.Buffered;
|
|
|
|
|
using System.Collections.Specialized;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Reflection;
|
2024-08-26 10:45:41 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-01-01 11:42:01 +00:00
|
|
|
|
using System.Security.Cryptography;
|
2024-10-06 02:23:44 +00:00
|
|
|
|
using System.Security.Principal;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
2024-08-19 10:15:54 +00:00
|
|
|
|
namespace ServiceLib.Common
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-08-19 10:15:54 +00:00
|
|
|
|
public class Utils
|
2021-01-06 11:54:15 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
private static readonly string _tag = "Utils";
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
#region 资源操作
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取嵌入文本资源
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="res"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetEmbedText(string res)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var result = string.Empty;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
using var stream = assembly.GetManifestResourceStream(res);
|
2023-02-19 04:18:08 +00:00
|
|
|
|
ArgumentNullException.ThrowIfNull(stream);
|
2023-02-17 06:36:28 +00:00
|
|
|
|
using StreamReader reader = new(stream);
|
|
|
|
|
result = reader.ReadToEnd();
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 取得存储资源
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2024-05-04 05:54:22 +00:00
|
|
|
|
public static string? LoadResource(string? res)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (File.Exists(res))
|
2024-05-04 05:54:22 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return File.ReadAllText(res);
|
2024-05-04 05:54:22 +00:00
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-02-20 10:16:30 +00:00
|
|
|
|
return null;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
#endregion 资源操作
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
#region 转换函数
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
/// 转逗号分隔的字符串
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="lst"></param>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
/// <param name="wrap"></param>
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <returns></returns>
|
2024-06-22 08:44:15 +00:00
|
|
|
|
public static string List2String(List<string>? lst, bool wrap = false)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-08 12:27:36 +00:00
|
|
|
|
if (lst == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
if (wrap)
|
|
|
|
|
{
|
2023-02-17 07:17:01 +00:00
|
|
|
|
return string.Join("," + Environment.NewLine, lst);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-20 10:16:30 +00:00
|
|
|
|
return string.Join(",", lst);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return string.Empty;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <summary>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
/// 逗号分隔的字符串
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
public static List<string>? String2List(string? str)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (str == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 11:49:27 +00:00
|
|
|
|
str = str.Replace(Environment.NewLine, "");
|
2023-02-17 07:17:01 +00:00
|
|
|
|
return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return null;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 05:21:22 +00:00
|
|
|
|
/// <summary>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
/// 逗号分隔的字符串,先排序后转List
|
2022-04-01 05:21:22 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
public static List<string>? String2ListSorted(string str)
|
2022-04-01 05:21:22 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
str = str.Replace(Environment.NewLine, "");
|
2023-02-17 07:17:01 +00:00
|
|
|
|
List<string> list = new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
|
list.Sort();
|
|
|
|
|
return list;
|
2022-04-01 05:21:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2022-04-01 05:21:22 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return null;
|
2022-04-01 05:21:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base64编码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="plainText"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string Base64Encode(string plainText)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
return Convert.ToBase64String(plainTextBytes);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return string.Empty;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base64解码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="plainText"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-09-18 11:54:38 +00:00
|
|
|
|
public static string Base64Decode(string? plainText)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-09-18 11:54:38 +00:00
|
|
|
|
if (plainText.IsNullOrEmpty()) return "";
|
2024-01-10 02:43:48 +00:00
|
|
|
|
plainText = plainText.Trim()
|
2024-11-01 06:54:23 +00:00
|
|
|
|
.Replace(Environment.NewLine, "")
|
|
|
|
|
.Replace("\n", "")
|
|
|
|
|
.Replace("\r", "")
|
|
|
|
|
.Replace('_', '/')
|
|
|
|
|
.Replace('-', '+')
|
|
|
|
|
.Replace(" ", "");
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
if (plainText.Length % 4 > 0)
|
|
|
|
|
{
|
2024-11-07 06:46:27 +00:00
|
|
|
|
plainText = plainText.PadRight(plainText.Length + 4 - (plainText.Length % 4), '=');
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var data = Convert.FromBase64String(plainText);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
return Encoding.UTF8.GetString(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return string.Empty;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 09:11:00 +00:00
|
|
|
|
public static int ToInt(object? obj)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-01-15 09:11:00 +00:00
|
|
|
|
return Convert.ToInt32(obj ?? string.Empty);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-10-14 02:42:05 +00:00
|
|
|
|
catch
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2022-03-13 02:41:04 +00:00
|
|
|
|
public static bool ToBool(object obj)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBoolean(obj);
|
|
|
|
|
}
|
2024-10-14 02:42:05 +00:00
|
|
|
|
catch
|
2022-03-13 02:41:04 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
public static string ToString(object? obj)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-02-17 06:36:28 +00:00
|
|
|
|
return obj?.ToString() ?? string.Empty;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-10-14 02:42:05 +00:00
|
|
|
|
catch
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
private static void ToHumanReadable(long amount, out double result, out string unit)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var factor = 1024u;
|
2023-01-01 11:42:01 +00:00
|
|
|
|
//long KBs = amount / factor;
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var KBs = amount;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
if (KBs > 0)
|
|
|
|
|
{
|
|
|
|
|
// multi KB
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var MBs = KBs / factor;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
if (MBs > 0)
|
|
|
|
|
{
|
|
|
|
|
// multi MB
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var GBs = MBs / factor;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
if (GBs > 0)
|
|
|
|
|
{
|
|
|
|
|
// multi GB
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var TBs = GBs / factor;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
if (TBs > 0)
|
|
|
|
|
{
|
2023-04-16 12:45:47 +00:00
|
|
|
|
result = TBs + ((GBs % factor) / (factor + 0.0));
|
2019-10-11 06:15:20 +00:00
|
|
|
|
unit = "TB";
|
|
|
|
|
return;
|
2023-04-10 09:30:27 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-04-16 12:45:47 +00:00
|
|
|
|
result = GBs + ((MBs % factor) / (factor + 0.0));
|
2019-10-11 06:15:20 +00:00
|
|
|
|
unit = "GB";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-04-16 12:45:47 +00:00
|
|
|
|
result = MBs + ((KBs % factor) / (factor + 0.0));
|
2019-10-11 06:15:20 +00:00
|
|
|
|
unit = "MB";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-04-16 12:45:47 +00:00
|
|
|
|
result = KBs + ((amount % factor) / (factor + 0.0));
|
2019-10-11 06:15:20 +00:00
|
|
|
|
unit = "KB";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = amount;
|
|
|
|
|
unit = "B";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public static string HumanFy(long amount)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
ToHumanReadable(amount, out var result, out var unit);
|
|
|
|
|
return $"{result:f1} {unit}";
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 11:54:15 +00:00
|
|
|
|
public static string UrlEncode(string url)
|
|
|
|
|
{
|
2022-03-31 12:19:15 +00:00
|
|
|
|
return Uri.EscapeDataString(url);
|
2021-01-06 11:54:15 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2021-01-06 11:54:15 +00:00
|
|
|
|
public static string UrlDecode(string url)
|
|
|
|
|
{
|
2024-01-16 03:05:02 +00:00
|
|
|
|
return Uri.UnescapeDataString(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static NameValueCollection ParseQueryString(string query)
|
|
|
|
|
{
|
|
|
|
|
var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
if (IsNullOrEmpty(query))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var parts = query[1..].Split('&', StringSplitOptions.RemoveEmptyEntries);
|
2024-01-16 03:05:02 +00:00
|
|
|
|
foreach (var part in parts)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var keyValue = part.Split('=');
|
2024-01-16 03:05:02 +00:00
|
|
|
|
if (keyValue.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-12-01 03:17:36 +00:00
|
|
|
|
var key = Uri.UnescapeDataString(keyValue.First());
|
|
|
|
|
var val = Uri.UnescapeDataString(keyValue.Last());
|
2024-01-16 03:05:02 +00:00
|
|
|
|
|
2024-02-21 02:17:28 +00:00
|
|
|
|
if (result[key] is null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(key, val);
|
|
|
|
|
}
|
2024-01-16 03:05:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-01-06 11:54:15 +00:00
|
|
|
|
}
|
2022-05-12 00:52:40 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
public static string GetMd5(string str)
|
2022-05-12 00:52:40 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var byteOld = Encoding.UTF8.GetBytes(str);
|
|
|
|
|
var byteNew = MD5.HashData(byteOld);
|
2023-02-17 06:36:28 +00:00
|
|
|
|
StringBuilder sb = new(32);
|
2024-10-14 02:42:05 +00:00
|
|
|
|
foreach (var b in byteNew)
|
2022-05-12 00:52:40 +00:00
|
|
|
|
{
|
|
|
|
|
sb.Append(b.ToString("x2"));
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2022-05-12 00:52:40 +00:00
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-04-06 07:09:49 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// idn to idc
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-12-13 12:02:45 +00:00
|
|
|
|
public static string GetPunycode(string url)
|
|
|
|
|
{
|
2024-03-26 06:26:03 +00:00
|
|
|
|
if (Utils.IsNullOrEmpty(url))
|
2022-12-13 12:02:45 +00:00
|
|
|
|
{
|
|
|
|
|
return url;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2022-12-13 12:02:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-02-17 06:36:28 +00:00
|
|
|
|
Uri uri = new(url);
|
2024-03-07 01:06:35 +00:00
|
|
|
|
if (uri.Host == uri.IdnHost || uri.Host == $"[{uri.IdnHost}]")
|
2022-12-13 12:02:45 +00:00
|
|
|
|
{
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return url.Replace(uri.Host, uri.IdnHost);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 11:54:38 +00:00
|
|
|
|
public static bool IsBase64String(string? plainText)
|
2023-04-06 03:40:28 +00:00
|
|
|
|
{
|
2024-08-20 06:30:45 +00:00
|
|
|
|
if (plainText.IsNullOrEmpty()) return false;
|
2023-04-06 03:40:28 +00:00
|
|
|
|
var buffer = new Span<byte>(new byte[plainText.Length]);
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Convert.TryFromBase64String(plainText, buffer, out var _);
|
2023-04-06 03:40:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 02:22:55 +00:00
|
|
|
|
public static string Convert2Comma(string text)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (IsNullOrEmpty(text))
|
2023-04-28 02:22:55 +00:00
|
|
|
|
{
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-04-28 02:22:55 +00:00
|
|
|
|
return text.Replace(",", ",").Replace(Environment.NewLine, ",");
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:49:36 +00:00
|
|
|
|
#endregion 转换函数
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
#region 数据检查
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断输入的是否是数字
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="oText"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-02-19 09:43:36 +00:00
|
|
|
|
public static bool IsNumeric(string oText)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return oText.All(char.IsNumber);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public static bool IsNullOrEmpty(string? text)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-03-23 10:26:04 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(text))
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return text == "null";
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 08:52:41 +00:00
|
|
|
|
public static bool IsNotEmpty(string? text)
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrEmpty(text);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证Domain地址是否合法
|
|
|
|
|
/// </summary>
|
2023-04-14 12:49:36 +00:00
|
|
|
|
/// <param name="domain"></param>
|
2024-05-12 07:42:28 +00:00
|
|
|
|
public static bool IsDomain(string? domain)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsNullOrEmpty(domain))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 08:20:13 +00:00
|
|
|
|
return Uri.CheckHostName(domain) == UriHostNameType.Dns;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 02:09:39 +00:00
|
|
|
|
public static bool IsIpv6(string ip)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (IPAddress.TryParse(ip, out var address))
|
2021-03-21 02:09:39 +00:00
|
|
|
|
{
|
2023-02-17 06:36:28 +00:00
|
|
|
|
return address.AddressFamily switch
|
2021-03-21 02:09:39 +00:00
|
|
|
|
{
|
2023-02-17 06:36:28 +00:00
|
|
|
|
AddressFamily.InterNetwork => false,
|
|
|
|
|
AddressFamily.InterNetworkV6 => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
2021-03-21 02:09:39 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2021-03-21 02:09:39 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 09:09:07 +00:00
|
|
|
|
public static Uri? TryUri(string url)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return new Uri(url);
|
|
|
|
|
}
|
|
|
|
|
catch (UriFormatException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsPrivateNetwork(string ip)
|
|
|
|
|
{
|
|
|
|
|
if (IPAddress.TryParse(ip, out var address))
|
|
|
|
|
{
|
|
|
|
|
var ipBytes = address.GetAddressBytes();
|
|
|
|
|
if (ipBytes[0] == 10) return true;
|
|
|
|
|
if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) return true;
|
|
|
|
|
if (ipBytes[0] == 192 && ipBytes[1] == 168) return true;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-24 09:09:07 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:49:36 +00:00
|
|
|
|
#endregion 数据检查
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
#region 测速
|
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
private static bool PortInUse(int port)
|
2022-01-12 13:05:03 +00:00
|
|
|
|
{
|
2022-03-17 11:07:06 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
|
|
|
|
var ipEndPoints = ipProperties.GetActiveTcpListeners();
|
|
|
|
|
//var lstIpEndPoints = new List<IPEndPoint>(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
|
|
|
|
|
return ipEndPoints.Any(endPoint => endPoint.Port == port);
|
2022-01-12 13:05:03 +00:00
|
|
|
|
}
|
2022-03-17 11:07:06 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2022-03-17 11:07:06 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return false;
|
2022-01-12 13:05:03 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2024-03-02 02:00:28 +00:00
|
|
|
|
public static int GetFreePort(int defaultPort = 9090)
|
2024-02-08 06:01:33 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-03-26 06:26:03 +00:00
|
|
|
|
if (!Utils.PortInUse(defaultPort))
|
2024-02-08 06:01:33 +00:00
|
|
|
|
{
|
|
|
|
|
return defaultPort;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TcpListener l = new(IPAddress.Loopback, 0);
|
|
|
|
|
l.Start();
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var port = ((IPEndPoint)l.LocalEndpoint).Port;
|
2024-02-08 06:01:33 +00:00
|
|
|
|
l.Stop();
|
|
|
|
|
return port;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-03-02 02:00:28 +00:00
|
|
|
|
return 59090;
|
2024-02-08 06:01:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:49:36 +00:00
|
|
|
|
#endregion 测速
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
#region 杂项
|
|
|
|
|
|
2025-01-04 09:03:12 +00:00
|
|
|
|
public static bool UpgradeAppExists(out string upgradeFileName)
|
2024-10-15 01:31:30 +00:00
|
|
|
|
{
|
2025-01-18 08:58:44 +00:00
|
|
|
|
upgradeFileName = Path.Combine(GetBaseDirectory(), GetExeName("AmazTool"));
|
2025-01-04 09:03:12 +00:00
|
|
|
|
return File.Exists(upgradeFileName);
|
2024-10-15 01:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 取得版本
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-02-20 07:16:00 +00:00
|
|
|
|
public static string GetVersion(bool blFull = true)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-18 02:27:58 +00:00
|
|
|
|
return blFull
|
2025-01-01 02:38:55 +00:00
|
|
|
|
? $"{Global.AppName} - V{GetVersionInfo()} - {RuntimeInformation.ProcessArchitecture}"
|
2024-12-18 02:27:58 +00:00
|
|
|
|
: $"{Global.AppName}/{GetVersionInfo()}";
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Global.AppName;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 07:19:03 +00:00
|
|
|
|
public static string GetVersionInfo()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-09-16 07:34:35 +00:00
|
|
|
|
return Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString(3) ?? "0.0";
|
2024-08-29 07:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2024-08-29 07:19:03 +00:00
|
|
|
|
return "0.0";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 02:38:55 +00:00
|
|
|
|
public static string GetRuntimeInfo()
|
|
|
|
|
{
|
2025-01-10 12:12:42 +00:00
|
|
|
|
return $"{Utils.GetVersion()} | {Utils.StartupPath()} | {Utils.GetExePath()} | {Environment.OSVersion}";
|
2025-01-01 02:38:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 取得GUID
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2024-10-14 02:42:05 +00:00
|
|
|
|
public static string GetGuid(bool full = true)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-28 13:02:25 +00:00
|
|
|
|
if (full)
|
|
|
|
|
{
|
|
|
|
|
return Guid.NewGuid().ToString("D");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0).ToString();
|
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2021-10-16 12:16:50 +00:00
|
|
|
|
catch (Exception ex)
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2019-10-11 06:15:20 +00:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 11:07:06 +00:00
|
|
|
|
public static bool IsGuidByParse(string strSrc)
|
|
|
|
|
{
|
2024-10-08 05:47:13 +00:00
|
|
|
|
return Guid.TryParse(strSrc, out _);
|
2022-03-17 11:07:06 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2024-01-28 11:53:20 +00:00
|
|
|
|
public static Dictionary<string, string> GetSystemHosts()
|
|
|
|
|
{
|
|
|
|
|
var systemHosts = new Dictionary<string, string>();
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var hostFile = @"C:\Windows\System32\drivers\etc\hosts";
|
2024-01-28 11:53:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (File.Exists(hostFile))
|
2024-01-28 11:53:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var hosts = File.ReadAllText(hostFile).Replace("\r", "");
|
2024-01-28 11:53:20 +00:00
|
|
|
|
var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
foreach (var host in hostsList)
|
|
|
|
|
{
|
|
|
|
|
if (host.StartsWith("#")) continue;
|
|
|
|
|
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
2024-12-01 03:17:36 +00:00
|
|
|
|
if (hostItem.Length != 2) continue;
|
|
|
|
|
systemHosts.Add(hostItem.Last(), hostItem.First());
|
2024-01-28 11:53:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2024-01-28 11:53:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-01-28 11:53:20 +00:00
|
|
|
|
return systemHosts;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 12:45:56 +00:00
|
|
|
|
public static async Task<string?> GetCliWrapOutput(string filePath, string? arg)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return await GetCliWrapOutput(filePath, arg != null ? new List<string>() { arg } : null);
|
2024-10-08 12:45:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 12:22:26 +00:00
|
|
|
|
public static async Task<string?> GetCliWrapOutput(string filePath, IEnumerable<string>? args)
|
2024-10-08 12:45:56 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var cmd = Cli.Wrap(filePath);
|
|
|
|
|
if (args != null)
|
|
|
|
|
{
|
|
|
|
|
if (args.Count() == 1)
|
|
|
|
|
{
|
|
|
|
|
cmd = cmd.WithArguments(args.First());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cmd = cmd.WithArguments(args);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-08 12:45:56 +00:00
|
|
|
|
var result = await cmd.ExecuteBufferedAsync();
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return result.StandardOutput.ToString();
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-08 12:45:56 +00:00
|
|
|
|
Logging.SaveLog(result.ToString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logging.SaveLog("GetCliWrapOutput", ex);
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-08 12:45:56 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:49:36 +00:00
|
|
|
|
#endregion 杂项
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
|
|
|
|
#region TempPath
|
|
|
|
|
|
2024-12-18 02:27:58 +00:00
|
|
|
|
public static bool HasWritePermission()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-30 10:16:07 +00:00
|
|
|
|
//When this file exists, it is equivalent to having no permission to read and write
|
2025-01-18 08:58:44 +00:00
|
|
|
|
if (File.Exists(Path.Combine(GetBaseDirectory(), "NotStoreConfigHere.txt")))
|
2024-12-30 10:16:07 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 08:58:44 +00:00
|
|
|
|
var tempPath = Path.Combine(GetBaseDirectory(), "guiTemps");
|
2024-12-18 02:27:58 +00:00
|
|
|
|
if (!Directory.Exists(tempPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
|
}
|
|
|
|
|
var fileName = Path.Combine(tempPath, GetGuid());
|
|
|
|
|
File.Create(fileName).Close();
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
}
|
2024-12-18 07:27:00 +00:00
|
|
|
|
catch (Exception)
|
2024-12-18 02:27:58 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 01:56:47 +00:00
|
|
|
|
public static string GetPath(string fileName)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var startupPath = StartupPath();
|
2024-01-31 01:56:47 +00:00
|
|
|
|
if (IsNullOrEmpty(fileName))
|
|
|
|
|
{
|
|
|
|
|
return startupPath;
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-01-31 01:56:47 +00:00
|
|
|
|
return Path.Combine(startupPath, fileName);
|
|
|
|
|
}
|
2024-10-24 09:09:07 +00:00
|
|
|
|
|
2025-01-18 08:58:44 +00:00
|
|
|
|
public static string GetBaseDirectory(string fileName = "")
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 01:56:47 +00:00
|
|
|
|
public static string GetExePath()
|
|
|
|
|
{
|
2024-10-09 12:13:25 +00:00
|
|
|
|
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
|
2024-01-31 01:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string StartupPath()
|
|
|
|
|
{
|
2025-01-18 08:58:44 +00:00
|
|
|
|
if (Environment.GetEnvironmentVariable(Global.LocalAppData) == "1")
|
2024-12-18 02:27:58 +00:00
|
|
|
|
{
|
|
|
|
|
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "v2rayN");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 08:58:44 +00:00
|
|
|
|
return GetBaseDirectory();
|
2024-01-31 01:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 12:40:07 +00:00
|
|
|
|
public static string GetTempPath(string filename = "")
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "guiTemps");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2019-10-11 06:15:20 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (IsNullOrEmpty(filename))
|
2022-03-20 12:40:07 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return tempPath;
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
2020-11-23 13:18:44 +00:00
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
|
2022-03-20 12:40:07 +00:00
|
|
|
|
public static string GetBackupPath(string filename)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "guiBackups");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2022-03-20 12:40:07 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2022-03-20 12:40:07 +00:00
|
|
|
|
public static string GetConfigPath(string filename = "")
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "guiConfigs");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2022-03-20 12:40:07 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-03-26 06:26:03 +00:00
|
|
|
|
if (Utils.IsNullOrEmpty(filename))
|
2022-03-20 12:40:07 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return tempPath;
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2022-03-20 12:40:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2024-01-10 02:43:48 +00:00
|
|
|
|
public static string GetBinPath(string filename, string? coreType = null)
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "bin");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
if (coreType != null)
|
|
|
|
|
{
|
2024-10-25 01:27:11 +00:00
|
|
|
|
tempPath = Path.Combine(tempPath, coreType.ToLower().ToString());
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (!Directory.Exists(tempPath))
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-10-14 02:42:05 +00:00
|
|
|
|
if (IsNullOrEmpty(filename))
|
2023-02-10 03:22:03 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return tempPath;
|
2023-02-10 03:22:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2023-02-10 03:22:03 +00:00
|
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-01-01 11:42:01 +00:00
|
|
|
|
public static string GetLogPath(string filename = "")
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "guiLogs");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-03-26 06:26:03 +00:00
|
|
|
|
if (Utils.IsNullOrEmpty(filename))
|
2023-01-01 11:42:01 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return tempPath;
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2023-01-01 11:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-02-04 04:50:38 +00:00
|
|
|
|
public static string GetFontsPath(string filename = "")
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var tempPath = Path.Combine(StartupPath(), "guiFonts");
|
|
|
|
|
if (!Directory.Exists(tempPath))
|
2023-02-04 04:50:38 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
Directory.CreateDirectory(tempPath);
|
2023-02-04 04:50:38 +00:00
|
|
|
|
}
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
2024-03-26 06:26:03 +00:00
|
|
|
|
if (Utils.IsNullOrEmpty(filename))
|
2023-02-04 04:50:38 +00:00
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return tempPath;
|
2023-02-04 04:50:38 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return Path.Combine(tempPath, filename);
|
2023-02-04 04:50:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-20 12:40:07 +00:00
|
|
|
|
|
2023-04-14 12:49:36 +00:00
|
|
|
|
#endregion TempPath
|
2024-10-06 02:23:44 +00:00
|
|
|
|
|
|
|
|
|
#region Platform
|
|
|
|
|
|
|
|
|
|
public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
|
|
|
|
|
public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
|
|
|
|
|
|
|
|
|
public static bool IsOSX() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
|
|
|
|
|
2024-12-19 03:24:52 +00:00
|
|
|
|
public static bool IsNonWindows() => !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
|
2024-10-06 02:23:44 +00:00
|
|
|
|
public static string GetExeName(string name)
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
return IsWindows() ? $"{name}.exe" : name;
|
2024-10-06 02:23:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsAdministrator()
|
|
|
|
|
{
|
|
|
|
|
if (IsWindows())
|
|
|
|
|
{
|
|
|
|
|
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
|
|
}
|
2024-12-31 08:30:27 +00:00
|
|
|
|
return false;
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// var id = GetLinuxUserId().Result ?? "1000";
|
|
|
|
|
// if (int.TryParse(id, out var userId))
|
|
|
|
|
// {
|
|
|
|
|
// return userId == 0;
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2024-10-08 09:40:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<string?> GetLinuxUserId()
|
|
|
|
|
{
|
2024-10-14 02:42:05 +00:00
|
|
|
|
var arg = new List<string>() { "-c", "id -u" };
|
|
|
|
|
return await GetCliWrapOutput("/bin/bash", arg);
|
2024-10-06 02:23:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 12:08:33 +00:00
|
|
|
|
public static async Task<string?> SetLinuxChmod(string? fileName)
|
|
|
|
|
{
|
|
|
|
|
if (fileName.IsNullOrEmpty()) return null;
|
2025-01-01 04:27:49 +00:00
|
|
|
|
if (fileName.Contains(' ')) fileName = fileName.AppendQuotes();
|
2024-12-18 02:27:58 +00:00
|
|
|
|
//File.SetUnixFileMode(fileName, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
|
2024-10-22 12:08:33 +00:00
|
|
|
|
var arg = new List<string>() { "-c", $"chmod +x {fileName}" };
|
|
|
|
|
return await GetCliWrapOutput("/bin/bash", arg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 06:59:06 +00:00
|
|
|
|
public static async Task<string?> GetLinuxFontFamily(string lang)
|
|
|
|
|
{
|
2024-11-01 06:54:23 +00:00
|
|
|
|
// var arg = new List<string>() { "-c", $"fc-list :lang={lang} family" };
|
2024-10-27 06:59:06 +00:00
|
|
|
|
var arg = new List<string>() { "-c", $"fc-list : family" };
|
|
|
|
|
return await GetCliWrapOutput("/bin/bash", arg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 06:54:23 +00:00
|
|
|
|
public static string? GetHomePath()
|
|
|
|
|
{
|
|
|
|
|
return IsWindows()
|
|
|
|
|
? Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%")
|
|
|
|
|
: Environment.GetEnvironmentVariable("HOME");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 04:27:49 +00:00
|
|
|
|
public static async Task<string?> GetListNetworkServices()
|
|
|
|
|
{
|
|
|
|
|
var arg = new List<string>() { "-c", $"networksetup -listallnetworkservices" };
|
|
|
|
|
return await GetCliWrapOutput("/bin/bash", arg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 02:23:44 +00:00
|
|
|
|
#endregion Platform
|
2019-10-11 06:15:20 +00:00
|
|
|
|
}
|
2024-04-10 05:32:50 +00:00
|
|
|
|
}
|