Files
go-scripts/myip-go/main.go
2024-12-29 16:33:18 +08:00

40 lines
798 B
Go
Executable File

/// 2>/dev/null ; gorun "$0" "$@" ; exit $?
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
const GetIpUrl = "https://hatter.ink/ip/ip.jsonp"
type IpResponse struct {
Status int16 `json:"status"`
Message string `json:"message"`
IP string `json:"ip"`
UserAgent string `json:"userAgent"`
}
func main() {
response, err := http.Get(GetIpUrl)
if err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(1)
}
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(2)
}
var ipReponse IpResponse
if err := json.Unmarshal(body, &ipReponse); err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(3)
}
fmt.Printf("[INFO] Your IP address: %s\n", ipReponse.IP)
}