Skip to content

Commit

Permalink
Use library to detect external IP more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed Aug 1, 2022
1 parent cc735b6 commit 07bb450
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.16
require (
github.com/aws/aws-sdk-go v1.44.66
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/glendc/go-external-ip v0.1.0 // indirect
github.com/imroc/req v0.3.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/aws/aws-sdk-go v1.44.66/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/glendc/go-external-ip v0.1.0 h1:iX3xQ2Q26atAmLTbd++nUce2P5ht5P4uD4V7caSY/xg=
github.com/glendc/go-external-ip v0.1.0/go.mod h1:CNx312s2FLAJoWNdJWZ2Fpf5O4oLsMFwuYviHjS4uJE=
github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U=
github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw=
github.com/imroc/req v0.3.2 h1:M/JkeU6RPmX+WYvT2vaaOL0K+q8ufL5LxwvJc4xeB4o=
Expand Down
32 changes: 9 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/imroc/req"
externalip "github.com/glendc/go-external-ip"
)

func IsIPv4(address string) bool {
return strings.Count(address, ":") < 2
}

func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}

func main() {
hostname := flag.String("hostname", "", "Hostname of this machine")
zoneID := flag.String("zone-id", "", "The ZoneID of your Route53 zone")
Expand All @@ -41,9 +33,10 @@ func main() {
}

var changes []*route53.Change
consensus := externalip.DefaultConsensus(nil, nil)

for _, addr_type := range []string{"A", "AAAA"} {
addr := getPublicIP(addr_type)
addr := getPublicIP(addr_type, consensus)
if addr != "" {
changes = append(changes, createChange(*hostname, *domain, addr, addr_type))
}
Expand All @@ -60,25 +53,18 @@ func main() {

}

type ApiResponse struct {
IP string `json:"ip"`
}

func getPublicIP(addrType string) string {
var url string
func getPublicIP(addrType string, consensus *externalip.Consensus) string {
if addrType == "A" {
url = "https://api4.my-ip.io/ip.json"
consensus.UseIPProtocol(4)
} else {
url = "https://api6.my-ip.io/ip.json"
consensus.UseIPProtocol(6)
}
r, err := req.Get(url)
if err != nil || r.Response().StatusCode != 200 {
ip, err := consensus.ExternalIP()
if err != nil {
fmt.Printf("Could not determine your %s address\n", addrType)
return ""
}
data := ApiResponse{}
r.ToJSON(&data)
return data.IP
return ip.String()
}

func createChange(name string, domain string, addr string, addr_type string) *route53.Change {
Expand Down

0 comments on commit 07bb450

Please sign in to comment.