Commit b9cf02b
Changed files (5)
cmd/get/get_ipinfo.go
@@ -0,0 +1,19 @@
+package get
+
+import (
+ "github.com/kahnwong/swissknife/internal/get"
+ "github.com/spf13/cobra"
+)
+
+var getIPInfoCmd = &cobra.Command{
+ Use: "ipinfo [ip]",
+ Short: "Get detailed IP information (location, ISP, etc.)",
+ Args: cobra.MaximumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ get.IPInfo(args)
+ },
+}
+
+func init() {
+ Cmd.AddCommand(getIPInfoCmd)
+}
internal/get/ip.go
@@ -104,7 +104,7 @@ func getPublicIP() PublicIPResponse {
func getIPLocation(ip string) IPLocationResponse {
var response IPLocationResponse
err := requests.
- URL(fmt.Sprintf("http://ip-api.com/json/%s", ip)).
+ URL(fmt.Sprintf("http://ip-api.com/json/%s?fields=query,country,regionName", ip)).
ToJSON(&response).
Fetch(context.Background())
internal/get/ipinfo.go
@@ -0,0 +1,61 @@
+package get
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/carlmjohnson/requests"
+ "github.com/kahnwong/swissknife/configs/color"
+ "github.com/kahnwong/swissknife/internal/utils"
+ "github.com/rs/zerolog/log"
+)
+
+type IPInfoResponse struct {
+ Query string `json:"query"`
+ Status string `json:"status"`
+ Country string `json:"country"`
+ CountryCode string `json:"countryCode"`
+ Region string `json:"region"`
+ RegionName string `json:"regionName"`
+ City string `json:"city"`
+ Zip string `json:"zip"`
+ Lat float64 `json:"lat"`
+ Lon float64 `json:"lon"`
+ Timezone string `json:"timezone"`
+ Isp string `json:"isp"`
+ Org string `json:"org"`
+ As string `json:"as"`
+}
+
+func getIPInfo(ip string) IPInfoResponse {
+ var response IPInfoResponse
+ err := requests.
+ URL(fmt.Sprintf("http://ip-api.com/json/%s", ip)).
+ ToJSON(&response).
+ Fetch(context.Background())
+
+ if err != nil {
+ log.Fatal().Msg("Error getting detailed ip info")
+ }
+
+ return response
+}
+
+func IPInfo(args []string) {
+ ip := utils.SetIP(args)
+
+ var targetIP string
+ if ip == "" {
+ publicIP := getPublicIP()
+ targetIP = publicIP.Ip
+ } else {
+ targetIP = ip
+ }
+
+ ipInfo := getIPInfo(targetIP)
+
+ fmt.Printf("%s: %s\n", color.Green("IP Address"), ipInfo.Query)
+ fmt.Printf("%s: %s, %s, %s\n", color.Green("Location"), ipInfo.City, ipInfo.RegionName, color.Blue(ipInfo.Country))
+ fmt.Printf("%s: %s\n", color.Green("ISP"), ipInfo.Isp)
+ fmt.Printf("%s: %s\n", color.Green("Organization"), ipInfo.Org)
+}
internal/utils/ip.go
@@ -0,0 +1,29 @@
+package utils
+
+import (
+ "net"
+ "strings"
+)
+
+func SetIP(args []string) string {
+ var ip string
+ if len(args) == 0 {
+ ipFromClipboard := ReadFromClipboard()
+ if ipFromClipboard != "" {
+ // Check if it's a valid IP address
+ if net.ParseIP(strings.TrimSpace(ipFromClipboard)) != nil {
+ ip = strings.TrimSpace(ipFromClipboard)
+ }
+ }
+ }
+ if ip == "" {
+ if len(args) == 0 {
+ // Return empty string to use public IP
+ return ""
+ } else if len(args) == 1 {
+ ip = args[0]
+ }
+ }
+
+ return ip
+}
go.sum
@@ -91,8 +91,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
-github.com/projectdiscovery/wappalyzergo v0.2.61 h1:TxiYJvXqReiscuWKtGKhFx3VxbVVjHOgECNX709AEX4=
-github.com/projectdiscovery/wappalyzergo v0.2.61/go.mod h1:8FtSVcmPRZU0g1euBpdSYEBHIvB7Zz9MOb754ZqZmfU=
github.com/projectdiscovery/wappalyzergo v0.2.62 h1:SMZ70bLCj6jHnFgjanuiaQpqUXY6aiEC3YoM0ZSvYes=
github.com/projectdiscovery/wappalyzergo v0.2.62/go.mod h1:8FtSVcmPRZU0g1euBpdSYEBHIvB7Zz9MOb754ZqZmfU=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=