|
| 1 | +package myip |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/jetkvm/kvm/pkg/nmlite/link" |
| 15 | +) |
| 16 | + |
| 17 | +func (ps *PublicIPState) request(ctx context.Context, url string, family int) ([]byte, error) { |
| 18 | + client := ps.httpClient(family) |
| 19 | + |
| 20 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("error creating request: %w", err) |
| 23 | + } |
| 24 | + |
| 25 | + resp, err := client.Do(req) |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("error sending request: %w", err) |
| 28 | + } |
| 29 | + defer resp.Body.Close() |
| 30 | + |
| 31 | + if resp.StatusCode != http.StatusOK { |
| 32 | + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 33 | + } |
| 34 | + |
| 35 | + body, err := io.ReadAll(resp.Body) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("error reading response body: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + return body, err |
| 41 | +} |
| 42 | + |
| 43 | +// checkCloudflare uses cdn-cgi/trace to get the public IP address |
| 44 | +func (ps *PublicIPState) checkCloudflare(ctx context.Context, family int) (*PublicIP, error) { |
| 45 | + u, err := url.JoinPath(ps.cloudflareEndpoint, "/cdn-cgi/trace") |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("error joining path: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + body, err := ps.request(ctx, u, family) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + for line := range strings.SplitSeq(string(body), "\n") { |
| 56 | + key, value, ok := strings.Cut(line, "=") |
| 57 | + if !ok || key != "ip" { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + return &PublicIP{ |
| 62 | + IPAddress: net.ParseIP(value), |
| 63 | + LastUpdated: time.Now(), |
| 64 | + }, nil |
| 65 | + } |
| 66 | + |
| 67 | + return nil, fmt.Errorf("no IP address found") |
| 68 | +} |
| 69 | + |
| 70 | +// checkAPI uses the API endpoint to get the public IP address |
| 71 | +func (ps *PublicIPState) checkAPI(_ context.Context, _ int) (*PublicIP, error) { |
| 72 | + return nil, fmt.Errorf("not implemented") |
| 73 | +} |
| 74 | + |
| 75 | +// checkIPs checks both IPv4 and IPv6 public IP addresses in parallel |
| 76 | +// and updates the IPAddresses slice with the results |
| 77 | +func (ps *PublicIPState) checkIPs(ctx context.Context, checkIPv4, checkIPv6 bool) error { |
| 78 | + var wg sync.WaitGroup |
| 79 | + var mu sync.Mutex |
| 80 | + var ips []PublicIP |
| 81 | + var errors []error |
| 82 | + |
| 83 | + checkFamily := func(family int, familyName string) { |
| 84 | + wg.Add(1) |
| 85 | + go func(f int, name string) { |
| 86 | + defer wg.Done() |
| 87 | + |
| 88 | + ip, err := ps.checkIPForFamily(ctx, f) |
| 89 | + mu.Lock() |
| 90 | + defer mu.Unlock() |
| 91 | + if err != nil { |
| 92 | + errors = append(errors, fmt.Errorf("%s check failed: %w", name, err)) |
| 93 | + return |
| 94 | + } |
| 95 | + if ip != nil { |
| 96 | + ips = append(ips, *ip) |
| 97 | + } |
| 98 | + }(family, familyName) |
| 99 | + } |
| 100 | + |
| 101 | + if checkIPv4 { |
| 102 | + checkFamily(link.AfInet, "IPv4") |
| 103 | + } |
| 104 | + |
| 105 | + if checkIPv6 { |
| 106 | + checkFamily(link.AfInet6, "IPv6") |
| 107 | + } |
| 108 | + |
| 109 | + wg.Wait() |
| 110 | + |
| 111 | + if len(ips) > 0 { |
| 112 | + ps.mu.Lock() |
| 113 | + defer ps.mu.Unlock() |
| 114 | + |
| 115 | + ps.addresses = ips |
| 116 | + ps.lastUpdated = time.Now() |
| 117 | + } |
| 118 | + |
| 119 | + if len(errors) > 0 && len(ips) == 0 { |
| 120 | + return errors[0] |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func (ps *PublicIPState) checkIPForFamily(ctx context.Context, family int) (*PublicIP, error) { |
| 127 | + if ps.apiEndpoint != "" { |
| 128 | + ip, err := ps.checkAPI(ctx, family) |
| 129 | + if err == nil && ip != nil { |
| 130 | + return ip, nil |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if ps.cloudflareEndpoint != "" { |
| 135 | + ip, err := ps.checkCloudflare(ctx, family) |
| 136 | + if err == nil && ip != nil { |
| 137 | + return ip, nil |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return nil, fmt.Errorf("all IP check methods failed for family %d", family) |
| 142 | +} |
0 commit comments