Skip to content

Commit 97c2b4c

Browse files
committed
chore: 合并
2 parents 107b082 + 8c639fa commit 97c2b4c

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

SangServerTool.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ VisualStudioVersion = 17.1.32228.430
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SangServerTool", "SangServerTool\SangServerTool.csproj", "{E65ECACB-940B-4317-81D7-47BAFE201651}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{9E02EE0C-5F1F-4B11-9D3E-59C2F2F3413D}"
9+
ProjectSection(SolutionItems) = preProject
10+
README.md = README.md
11+
EndProjectSection
12+
EndProject
813
Global
914
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1015
Debug|Any CPU = Debug|Any CPU

SangServerTool/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@
4949
});
5050
rootCommand.AddCommand(ddnsCommand);
5151

52+
// 定义IP获取命令
53+
var ipCommand = new Command("ip", "Get IP.");
54+
ipCommand.AddOption(new Option<bool>("--web", () => false, "Is check from 'https://ifconfig.me/ip' to get you Internet IP?"));
55+
ipCommand.Handler = CommandHandler.Create<bool>((web) =>
56+
{
57+
var logger = loggerFactory.CreateLogger("SangServerTool_IP");
58+
if (web)
59+
{
60+
logger.LogInformation(Utils.CurrentIPAddressByWeb());
61+
logger.LogInformation(Utils.CurrentIPAddressByWeb(true));
62+
return 0;
63+
}
64+
else
65+
{
66+
logger.LogInformation(Utils.CurrentIPAddress());
67+
logger.LogInformation(Utils.CurrentIPAddress(true));
68+
return 0;
69+
}
70+
});
71+
rootCommand.AddCommand(ipCommand);
72+
5273
// 定义获取 https 站点证书命令
5374
var getcertCommand = new Command("sync", "Get SSL Cert from https site.");
5475
getcertCommand.AddOption(new Option<string>(new[] { "--config", "-c" }, "Set config json file.") { IsRequired = true });

SangServerTool/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"profiles": {
33
"SangServerTool": {
44
"commandName": "Project",
5-
"commandLineArgs": "sync -c \"test.json\""
5+
//"commandLineArgs": "sync -c \"test.json\""
6+
"commandLineArgs": "ip"
67
}
78
}
89
}

SangServerTool/Utils.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,23 @@ public static bool isCerWillExp(string certFilePath)
3737
.Where(p => !exps.Contains(p.Name)) // 排除docker、lo等
3838
.Select(p => p.GetIPProperties())
3939
.SelectMany(p => p.UnicastAddresses)
40-
.Where(p => p.Address.AddressFamily == family && !IPAddress.IsLoopback(p.Address));
41-
//IPv6 时去除本地的
42-
if (family == AddressFamily.InterNetworkV6)
43-
{
44-
ips = ips.Where(p => !p.Address.IsIPv6LinkLocal);
45-
}
40+
.Where(p => p.Address.AddressFamily == family && !IPAddress.IsLoopback(p.Address) &&
41+
(family == AddressFamily.InterNetwork || !IsNotGoodIPv6(p))
42+
);
4643
return ips.FirstOrDefault()?.Address.ToString();
4744
}
45+
46+
/// <summary>
47+
/// 判断IPv6地址是否不太可用
48+
/// 排除Dhcp,本地和随机的临时地址
49+
/// </summary>
50+
/// <param name="unicastAddress">单播地址信息</param>
51+
/// <returns>不用则返回true</returns>
52+
private static bool IsNotGoodIPv6(UnicastIPAddressInformation unicastAddress)
53+
{
54+
return unicastAddress.Address.IsIPv6LinkLocal || unicastAddress.PrefixOrigin == PrefixOrigin.Dhcp || unicastAddress.SuffixOrigin == SuffixOrigin.Random;
55+
}
56+
4857
/// <summary>
4958
/// 获取电脑外网IP
5059
/// </summary>

0 commit comments

Comments
 (0)