12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package main
- import (
- "flag"
- "fmt"
- "io/ioutil"
- "log"
- "net"
- "net/http"
- "strings"
- )
- var (
- verbal = flag.Bool("v", true, "Enable verbal output")
- )
- func main() {
- flag.Parse()
- publicIp := strings.TrimSpace(getPublicIP())
- domains := GetFreeDomain(publicIp)
- if *verbal {
- fmt.Println("Your public IP address is: ", publicIp)
- fmt.Println("Your free domain assigned by ISP is: ", domains)
- } else {
- if len(domains) > 0 {
- out := domains[0]
- if out[len(out)-1:] == "." {
- out = out[:len(out)-1]
- }
- fmt.Println(out)
- }
- }
- }
- func getPublicIP() string {
- //http://checkip.amazonaws.com/
- resp, err := http.Get("https://api.ipify.org/")
- if err != nil {
- log.Fatalln(err)
- }
- //We Read the response body on the line below.
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Fatalln(err)
- }
- //Convert the body to type string
- sb := string(body)
- return sb
- }
- func GetFreeDomain(ipaddr string) []string {
- ptr, _ := net.LookupAddr(ipaddr)
- return ptr
- }
|