rewrite in go and add support for ipv6
This commit is contained in:
24
public_ip.go
Normal file
24
public_ip.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetPublicIP(version int8) string {
|
||||
url := fmt.Sprintf("https://ipv%d.icanhazip.com", version)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Panic("Failed to get public IP: ", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
|
||||
return strings.TrimSpace(string(body))
|
||||
}
|
||||
Reference in New Issue
Block a user