Created
October 26, 2024 08:33
-
-
Save armamini/5b98153e6fb31928adb0fedf442d8730 to your computer and use it in GitHub Desktop.
IP Address and Location Finder in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "net" | |
| "net/http" | |
| ) | |
| type Location struct { | |
| City string `json:"city"` | |
| Region string `json:"region"` | |
| Country string `json:"country"` | |
| IP string `json:"ip"` | |
| } | |
| func getPublicIP() (string, error) { | |
| resp, err := http.Get("https://api.ipify.org") | |
| if err != nil { | |
| return "", fmt.Errorf("failed to get public IP: %w", err) | |
| } | |
| defer resp.Body.Close() | |
| if resp.StatusCode != http.StatusOK { | |
| return "", fmt.Errorf("failed to get public IP: %s", resp.Status) | |
| } | |
| ip, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| return "", fmt.Errorf("failed to read public IP response: %w", err) | |
| } | |
| return string(ip), nil | |
| } | |
| func getLocation(ip string) (*Location, error) { | |
| resp, err := http.Get(fmt.Sprintf("https://ipinfo.io/%s/json", ip)) | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to get location: %w", err) | |
| } | |
| defer resp.Body.Close() | |
| if resp.StatusCode != http.StatusOK { | |
| return nil, fmt.Errorf("failed to get location: %s", resp.Status) | |
| } | |
| var location Location | |
| if err := json.NewDecoder(resp.Body).Decode(&location); err != nil { | |
| return nil, fmt.Errorf("failed to decode location response: %w", err) | |
| } | |
| return &location, nil | |
| } | |
| func getPrivateIPs() ([]string, error) { | |
| var privateIPs []string | |
| interfaces, err := net.Interfaces() | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to get network interfaces: %w", err) | |
| } | |
| for _, iface := range interfaces { | |
| addrs, err := iface.Addrs() | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err) | |
| } | |
| for _, addr := range addrs { | |
| if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.IsPrivate() { | |
| privateIPs = append(privateIPs, ipNet.IP.String()) | |
| } | |
| } | |
| } | |
| return privateIPs, nil | |
| } | |
| func main() { | |
| publicIP, err := getPublicIP() | |
| if err != nil { | |
| log.Fatalf("Error getting public IP: %v", err) | |
| } | |
| location, err := getLocation(publicIP) | |
| if err != nil { | |
| log.Fatalf("Error getting location: %v", err) | |
| } | |
| fmt.Printf("Public IP: %s\n", publicIP) | |
| fmt.Printf("Location: %s, %s, %s\n", location.City, location.Region, location.Country) | |
| privateIPs, err := getPrivateIPs() | |
| if err != nil { | |
| log.Fatalf("Error getting private IPs: %v", err) | |
| } | |
| fmt.Println("\nPrivate IPs:") | |
| for _, ip := range privateIPs { | |
| fmt.Printf(" - %s\n", ip) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment