Files
elmprodvpn/selective-vpn-api/app/egress_identity_probe_external.go

45 lines
1.2 KiB
Go

package app
import (
"fmt"
"strings"
egressutilpkg "selective-vpn-api/app/egressutil"
)
func egressProbeExternalIP() (string, error) {
endpoints := egressIPEndpoints()
ip, errs := egressutilpkg.ProbeFirstSuccess(endpoints, func(rawURL string) (string, error) {
body, err := egressutilpkg.HTTPGetBody(egressHTTPClient, rawURL, egressIdentityProbeTimeout, "selective-vpn-api/egress-identity", 8*1024)
if err != nil {
return "", err
}
return egressutilpkg.ParseIPFromBody(body)
})
if strings.TrimSpace(ip) != "" {
return ip, nil
}
if len(errs) == 0 {
return "", fmt.Errorf("egress probe endpoints are not configured")
}
return "", fmt.Errorf("%s", strings.Join(errs, "; "))
}
func egressProbeExternalIPViaInterface(iface string) (string, error) {
iface = strings.TrimSpace(iface)
if iface == "" {
return egressProbeExternalIP()
}
endpoints := egressIPEndpoints()
ip, errs := egressutilpkg.ProbeFirstSuccess(endpoints, func(rawURL string) (string, error) {
return egressProbeURLViaInterface(rawURL, iface, egressIdentityProbeTimeout)
})
if strings.TrimSpace(ip) != "" {
return ip, nil
}
if len(errs) == 0 {
return "", fmt.Errorf("egress probe endpoints are not configured")
}
return "", fmt.Errorf("%s", strings.Join(errs, "; "))
}