feat: add myip-go
This commit is contained in:
39
myip-go/main.go
Executable file
39
myip-go/main.go
Executable file
@@ -0,0 +1,39 @@
|
||||
/// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user