name-dyndns/dyndns/external.go

45 lines
873 B
Go
Raw Normal View History

2015-05-02 21:56:08 -04:00
package dyndns
import (
"errors"
"io/ioutil"
"net/http"
)
2015-05-04 20:20:11 -04:00
// Urls contains a set of mirrors in which a
// raw IP string can be retreived. It is exported
// for the intent of modification.
2015-05-02 21:56:08 -04:00
var (
2019-05-16 21:43:15 -04:00
Urls = []string{"https://ip.darkcloud.ca"}
2015-05-02 21:56:08 -04:00
)
func tryMirror(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(contents), nil
}
// GetExternalIP retrieves the external facing IP Address.
2015-05-04 20:20:11 -04:00
// If multiple mirrors are provided in Urls,
// it will try each one (in order), should
// preceding mirrors fail.
2015-05-02 21:56:08 -04:00
func GetExternalIP() (string, error) {
for _, url := range Urls {
resp, err := tryMirror(url)
if err == nil {
return resp, err
}
}
return "", errors.New("Could not retreive external IP")
}