package publicIp import ( "fmt" "io" "net/http" ) func ReadIp() (string, error) { fmt.Printf("Reading IP address ...\n") ip, errIpify := _readIpFromIpify() if errIpify == nil { fmt.Printf("IP address from ipify: %s\n", ip) return ip, nil } ip, errIdentMe := _readIpFromIdentMe() if errIdentMe == nil { fmt.Printf("IP address from identme: %s\n", ip) return ip, nil } return "", fmt.Errorf("error getting IP from all services: %v, %v", errIpify, errIdentMe) } func _readIpFromIpify() (string, error) { fmt.Printf("Getting IP address from ipify ...\n") resp, err := http.Get("https://api.ipify.org?format=text") if err != nil { return "", fmt.Errorf("error getting IP from ipify: %v", err) } defer resp.Body.Close() ip, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("error reading IP from ipify: %v", err) } fmt.Printf("IP address from ipify: %s\n", string(ip)) return string(ip), nil } func _readIpFromIdentMe() (string, error) { fmt.Printf("Getting IP address from identme ...\n") resp, err := http.Get("https://4.ident.me") if err != nil { return "", fmt.Errorf("error getting IP from identme: %v", err) } defer resp.Body.Close() ip, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("error reading IP from identme: %v", err) } fmt.Printf("IP address from identme: %s\n", string(ip)) return string(ip), nil }