Commit 4dcfb2c

Karn Wong <[email protected]>
2024-11-15 12:27:43
feat(ip): add region to ip location
1 parent 5b5fbb6
Changed files (1)
cmd
cmd/get/get_ip.go
@@ -12,6 +12,16 @@ import (
 	"github.com/spf13/cobra"
 )
 
+type PublicIPResponse struct {
+	Ip string `json:"ip"`
+}
+
+type IPLocation struct {
+	Ip         string `json:"ip"`
+	Country    string `json:"country"`
+	RegionName string `json:"regionName"`
+}
+
 func getLocalIP() string {
 	conn, err := net.Dial("udp", "8.8.8.8:80")
 	if err != nil {
@@ -33,15 +43,10 @@ func getLocalIP() string {
 	return localIP
 }
 
-type PublicIPResponse struct {
-	Ip      string `json:"ip"`
-	Country string `json:"country"`
-}
-
 func getPublicIP() PublicIPResponse {
 	var response PublicIPResponse
 	err := requests.
-		URL("https://api.country.is").
+		URL("https://api.ipify.org?format=json").
 		ToJSON(&response).
 		Fetch(context.Background())
 
@@ -52,6 +57,20 @@ func getPublicIP() PublicIPResponse {
 	return response
 }
 
+func getIPLocation(ip string) IPLocation {
+	var response IPLocation
+	err := requests.
+		URL(fmt.Sprintf("http://ip-api.com/json/%s", ip)).
+		ToJSON(&response).
+		Fetch(context.Background())
+
+	if err != nil {
+		log.Fatal().Err(err).Msg("Error getting ip location")
+	}
+
+	return response
+}
+
 var getIPCmd = &cobra.Command{
 	Use:   "ip",
 	Short: "Get IP information",
@@ -61,7 +80,8 @@ var getIPCmd = &cobra.Command{
 		fmt.Printf("%s: %s\n", color.Green("Local IP"), localIP)
 
 		publicIP := getPublicIP()
-		fmt.Printf("%s: %s (%s)\n", color.Green("Public IP"), publicIP.Ip, color.Blue(publicIP.Country))
+		IPLocation := getIPLocation(publicIP.Ip)
+		fmt.Printf("%s: %s (%s, %s)\n", color.Green("Public IP"), publicIP.Ip, color.Blue(IPLocation.RegionName), color.Blue(IPLocation.Country))
 	},
 }