|
11 | 11 | using System.Collections.Generic; |
12 | 12 | using System.Net.Http; |
13 | 13 | using Newtonsoft.Json.Linq; |
| 14 | +using System.Text.RegularExpressions; |
14 | 15 |
|
15 | 16 | namespace cfnat.win.gui |
16 | 17 | { |
@@ -229,26 +230,27 @@ private async void button1_Click(object sender, EventArgs e) |
229 | 230 | foreach (var line in lines.Skip(1)) // Skip(1) 跳过标题行 |
230 | 231 | { |
231 | 232 | 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 | | - */ |
245 | 233 | IP库.AppendLine(columns[0]); // 将符合条件的IP添加到IP库 |
246 | 234 | } |
247 | 235 |
|
248 | 236 | // 将IP库内容写入到程序目录的 ips-v4.txt 文件中 |
249 | 237 | 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()); |
252 | 254 | log($"IP库已成功写入到 ips-v{IP类型}.txt 文件中。"); |
253 | 255 | } |
254 | 256 | else |
|
0 commit comments