Commit 9dbd48b
Changed files (1)
cmd/networking_get_current_ip.go → cmd/networking_get_ip.go
@@ -1,9 +1,12 @@
package cmd
import (
+ "encoding/json"
"fmt"
+ "io"
"log"
"net"
+ "net/http"
"strings"
"github.com/fatih/color"
@@ -31,15 +34,40 @@ func getLocalIP() string {
return fmt.Sprintf("%v", localIP)
}
+func getPublicIP() string {
+ // make request
+ resp, err := http.Get("https://httpbin.org/ip")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // parse
+ type Response struct {
+ Origin string `json:"origin"`
+ }
+
+ var jsonResponse Response
+ err = json.Unmarshal(body, &jsonResponse)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return jsonResponse.Origin
+}
+
var getIPCmd = &cobra.Command{
Use: "get-ip",
Short: "Get IP information",
Long: `Get IP information`,
Run: func(cmd *cobra.Command, args []string) {
color.Green("Networking: get-ip")
- fmt.Printf("\tLocal IP: %s\n", getLocalIP())
-
- // public ip
+ fmt.Printf("\tLocal IP : %s\n", getLocalIP())
+ fmt.Printf("\tPublic IP : %s\n", getPublicIP())
},
}