|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const ianaTLDSource = "https://data.iana.org/TLD/tlds-alpha-by-domain.txt" |
| 14 | + |
| 15 | +var ( |
| 16 | + domainEndingsOnce sync.Once |
| 17 | + domainEndingsCache []string |
| 18 | + domainEndingsErr error |
| 19 | +) |
| 20 | + |
| 21 | +func loadDomainEndings() ([]string, error) { |
| 22 | + domainEndingsOnce.Do(func() { |
| 23 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 24 | + defer cancel() |
| 25 | + |
| 26 | + iana, err := fetchIANATLDs(ctx) |
| 27 | + if err != nil { |
| 28 | + domainEndingsErr = fmt.Errorf("konnte endungs-pool nicht laden: %w", err) |
| 29 | + domainEndingsCache = domainEndingPool |
| 30 | + return |
| 31 | + } |
| 32 | + domainEndingsCache = mergeUniqueEndings(iana, domainEndingPool) |
| 33 | + }) |
| 34 | + |
| 35 | + if domainEndingsErr != nil { |
| 36 | + return domainEndingsCache, domainEndingsErr |
| 37 | + } |
| 38 | + return domainEndingsCache, nil |
| 39 | +} |
| 40 | + |
| 41 | +func fetchIANATLDs(ctx context.Context) ([]string, error) { |
| 42 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, ianaTLDSource, nil) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + resp, err := http.DefaultClient.Do(req) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + defer resp.Body.Close() |
| 51 | + if resp.StatusCode != http.StatusOK { |
| 52 | + return nil, fmt.Errorf("iana antwort: %s", resp.Status) |
| 53 | + } |
| 54 | + |
| 55 | + scanner := bufio.NewScanner(resp.Body) |
| 56 | + list := make([]string, 0, 2000) |
| 57 | + for scanner.Scan() { |
| 58 | + line := strings.TrimSpace(scanner.Text()) |
| 59 | + if line == "" || strings.HasPrefix(line, "#") { |
| 60 | + continue |
| 61 | + } |
| 62 | + list = append(list, strings.ToLower(line)) |
| 63 | + } |
| 64 | + if err := scanner.Err(); err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + if len(list) == 0 { |
| 68 | + return nil, fmt.Errorf("keine endungen gefunden") |
| 69 | + } |
| 70 | + return list, nil |
| 71 | +} |
| 72 | + |
| 73 | +func mergeUniqueEndings(primary, secondary []string) []string { |
| 74 | + seen := make(map[string]struct{}, len(primary)+len(secondary)) |
| 75 | + list := make([]string, 0, len(primary)+len(secondary)) |
| 76 | + for _, value := range primary { |
| 77 | + value = normalizeEnding(value) |
| 78 | + if value == "" { |
| 79 | + continue |
| 80 | + } |
| 81 | + if _, ok := seen[value]; ok { |
| 82 | + continue |
| 83 | + } |
| 84 | + seen[value] = struct{}{} |
| 85 | + list = append(list, value) |
| 86 | + } |
| 87 | + for _, value := range secondary { |
| 88 | + value = normalizeEnding(value) |
| 89 | + if value == "" { |
| 90 | + continue |
| 91 | + } |
| 92 | + if _, ok := seen[value]; ok { |
| 93 | + continue |
| 94 | + } |
| 95 | + seen[value] = struct{}{} |
| 96 | + list = append(list, value) |
| 97 | + } |
| 98 | + return list |
| 99 | +} |
0 commit comments