|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gosuda/portal/v2/types" |
| 13 | +) |
| 14 | + |
| 15 | +// ResolvePublicIP attempts to determine the caller's public IP address |
| 16 | +// using well-known external services. Returns empty string on failure. |
| 17 | +// Best-effort with a short timeout to avoid blocking registration. |
| 18 | +func ResolvePublicIP(ctx context.Context) string { |
| 19 | + ctx, cancel := context.WithTimeout(ctx, 3*time.Second) |
| 20 | + defer cancel() |
| 21 | + |
| 22 | + endpoints := []string{ |
| 23 | + "https://api.ipify.org", |
| 24 | + "https://ifconfig.me/ip", |
| 25 | + } |
| 26 | + client := &http.Client{Timeout: 3 * time.Second} |
| 27 | + |
| 28 | + for _, endpoint := range endpoints { |
| 29 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) |
| 30 | + if err != nil { |
| 31 | + continue |
| 32 | + } |
| 33 | + req.Header.Set("User-Agent", "portal-tunnel") |
| 34 | + |
| 35 | + resp, err := client.Do(req) |
| 36 | + if err != nil { |
| 37 | + continue |
| 38 | + } |
| 39 | + |
| 40 | + body, readErr := io.ReadAll(io.LimitReader(resp.Body, 256)) |
| 41 | + _ = resp.Body.Close() |
| 42 | + if resp.StatusCode != http.StatusOK || readErr != nil { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + if candidate := SanitizeReportedIP(string(body)); candidate != "" { |
| 47 | + return candidate |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return "" |
| 52 | +} |
| 53 | + |
| 54 | +func SanitizeReportedIP(raw string) string { |
| 55 | + candidate := strings.TrimSpace(raw) |
| 56 | + if candidate == "" { |
| 57 | + return "" |
| 58 | + } |
| 59 | + if net.ParseIP(candidate) == nil { |
| 60 | + return "" |
| 61 | + } |
| 62 | + return candidate |
| 63 | +} |
| 64 | + |
| 65 | +func ResolvePortalRelayURLs(ctx context.Context, explicit []string, includeDefaults bool) ([]string, error) { |
| 66 | + explicit, err := NormalizeRelayURLs(explicit...) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + if !includeDefaults { |
| 71 | + return explicit, nil |
| 72 | + } |
| 73 | + |
| 74 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, types.PortalRelayRegistryURL, nil) |
| 75 | + if err != nil { |
| 76 | + return explicit, nil |
| 77 | + } |
| 78 | + |
| 79 | + client := &http.Client{Timeout: 5 * time.Second} |
| 80 | + resp, err := client.Do(req) |
| 81 | + if err != nil { |
| 82 | + return explicit, nil |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + |
| 86 | + if resp.StatusCode != http.StatusOK { |
| 87 | + return explicit, nil |
| 88 | + } |
| 89 | + |
| 90 | + var registry struct { |
| 91 | + Relays []string `json:"relays"` |
| 92 | + } |
| 93 | + if err := json.NewDecoder(resp.Body).Decode(®istry); err != nil { |
| 94 | + return explicit, nil |
| 95 | + } |
| 96 | + |
| 97 | + defaults, err := NormalizeRelayURLs(registry.Relays...) |
| 98 | + if err != nil { |
| 99 | + return explicit, nil |
| 100 | + } |
| 101 | + if len(defaults) == 0 { |
| 102 | + return explicit, nil |
| 103 | + } |
| 104 | + return MergeRelayURLs(defaults, nil, explicit) |
| 105 | +} |
0 commit comments