main.go 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net"
  8. "net/http"
  9. "strings"
  10. )
  11. var (
  12. verbal = flag.Bool("v", true, "Enable verbal output")
  13. )
  14. func main() {
  15. flag.Parse()
  16. publicIp := strings.TrimSpace(getPublicIP())
  17. domains := GetFreeDomain(publicIp)
  18. if *verbal {
  19. fmt.Println("Your public IP address is: ", publicIp)
  20. fmt.Println("Your free domain assigned by ISP is: ", domains)
  21. } else {
  22. if len(domains) > 0 {
  23. out := domains[0]
  24. if out[len(out)-1:] == "." {
  25. out = out[:len(out)-1]
  26. }
  27. fmt.Println(out)
  28. }
  29. }
  30. }
  31. func getPublicIP() string {
  32. //http://checkip.amazonaws.com/
  33. resp, err := http.Get("https://api.ipify.org/")
  34. if err != nil {
  35. log.Fatalln(err)
  36. }
  37. //We Read the response body on the line below.
  38. body, err := ioutil.ReadAll(resp.Body)
  39. if err != nil {
  40. log.Fatalln(err)
  41. }
  42. //Convert the body to type string
  43. sb := string(body)
  44. return sb
  45. }
  46. func GetFreeDomain(ipaddr string) []string {
  47. ptr, _ := net.LookupAddr(ipaddr)
  48. return ptr
  49. }