Skip to content

Commit 13f1b30

Browse files
committed
## 优化
1. 生成缓存CIDR段;
1 parent e9d7bcd commit 13f1b30

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

Form1.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Generic;
1212
using System.Net.Http;
1313
using Newtonsoft.Json.Linq;
14+
using System.Text.RegularExpressions;
1415

1516
namespace cfnat.win.gui
1617
{
@@ -229,26 +230,27 @@ private async void button1_Click(object sender, EventArgs e)
229230
foreach (var line in lines.Skip(1)) // Skip(1) 跳过标题行
230231
{
231232
string[] columns = line.Split(','); // 按逗号分割列
232-
/*
233-
if (columns.Length >= 5)
234-
{
235-
string ip地址 = columns[0]; // IP地址
236-
string 数据中心名 = columns[1]; // 数据中心
237-
238-
// 如果该IP的 数据中心 在数据中心数组中
239-
if (数据中心数组.Contains(数据中心名))
240-
{
241-
IP库.AppendLine(ip地址); // 将符合条件的IP添加到IP库
242-
}
243-
}
244-
*/
245233
IP库.AppendLine(columns[0]); // 将符合条件的IP添加到IP库
246234
}
247235

248236
// 将IP库内容写入到程序目录的 ips-v4.txt 文件中
249237
string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"ips-v{IP类型}.txt");
250-
File.WriteAllText(outputPath, IP库.ToString());
251-
238+
string CIDR库;
239+
if (IP类型 == "4")
240+
{
241+
// 使用正则表达式修改每个 IP 的最后一段为 0 并加上 /24
242+
string pattern = @"(\d+\.\d+\.\d+)\.\d+";
243+
string replacement = "$1.0/24";
244+
CIDR库 = Regex.Replace(IP库.ToString(), pattern, replacement);
245+
}
246+
else
247+
{
248+
// 使用正则表达式匹配前 3 段 IPv6 地址,并添加 ::/48
249+
string pattern = @"^([0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}):.*";
250+
string replacement = "$1::/48";
251+
CIDR库 = Regex.Replace(IP库.ToString(), pattern, replacement, RegexOptions.Multiline);
252+
}
253+
File.WriteAllText(outputPath, CIDR库.ToString());
252254
log($"IP库已成功写入到 ips-v{IP类型}.txt 文件中。");
253255
}
254256
else

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("0.1.0.11")]
36-
[assembly: AssemblyFileVersion("0.1.0.11")]
35+
[assembly: AssemblyVersion("0.1.0.12")]
36+
[assembly: AssemblyFileVersion("0.1.0.12")]

0 commit comments

Comments
 (0)