FetchDomainsFromURL 从URL获取域名列表
(url string)
| 337 | |
| 338 | // FetchDomainsFromURL 从URL获取域名列表 |
| 339 | func FetchDomainsFromURL(url string) ([]string, error) { |
| 340 | resp, err := http.Get(url) |
| 341 | if err != nil { |
| 342 | return nil, fmt.Errorf("获取URL内容失败: %v", err) |
| 343 | } |
| 344 | defer resp.Body.Close() |
| 345 | |
| 346 | if resp.StatusCode != http.StatusOK { |
| 347 | return nil, fmt.Errorf("HTTP请求失败,状态码: %d", resp.StatusCode) |
| 348 | } |
| 349 | |
| 350 | body, err := io.ReadAll(resp.Body) |
| 351 | if err != nil { |
| 352 | return nil, fmt.Errorf("读取响应内容失败: %v", err) |
| 353 | } |
| 354 | |
| 355 | // 使用正则表达式提取域名 |
| 356 | re := regexp.MustCompile(`(http|https)://(.*?)[/"\s<>]+`) |
| 357 | matches := re.FindAllStringSubmatch(string(body), -1) |
| 358 | |
| 359 | domains := make(map[string]bool) // 使用map去重 |
| 360 | for _, match := range matches { |
| 361 | if len(match) >= 3 { |
| 362 | domain := strings.TrimSpace(match[2]) |
| 363 | if ValidateDomainName(domain) { |
| 364 | domains[domain] = true |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // 转换为切片 |
| 370 | result := make([]string, 0, len(domains)) |
| 371 | for domain := range domains { |
| 372 | result = append(result, domain) |
| 373 | } |
| 374 | |
| 375 | return result, nil |
| 376 | } |
| 377 | |
| 378 | // ResolveDomain 解析域名为IP地址 |
| 379 | func ResolveDomain(domain string) ([]net.IP, error) { |
nothing calls this directly
no test coverage detected