mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
feat: add No-IP provider
This commit is contained in:
parent
1ddd1dfb8b
commit
a7c2b0a56e
23
README.md
23
README.md
@ -33,6 +33,7 @@ Now I rewrite [DynDNS](https://github.com/TimothyYe/DynDNS) by Golang and call i
|
||||
* AliDNS ([https://help.aliyun.com/product/29697.html](https://help.aliyun.com/product/29697.html))
|
||||
* DuckDNS ([https://www.duckdns.org](https://www.duckdns.org))
|
||||
* Dreamhost ([https://www.dreamhost.com](https://www.dreamhost.com))
|
||||
* No-IP ([https://www.noip.com/](https://www.noip.com))
|
||||
|
||||
## Supported Platforms
|
||||
|
||||
@ -123,6 +124,7 @@ Supported provider(s):
|
||||
* DuckDNS
|
||||
* Google Domains
|
||||
* HE.net
|
||||
* No-IP
|
||||
|
||||
To enable the `IPv6` support of GoDNS, there are 2 solutions you can choose:
|
||||
* Get IPv6 address online
|
||||
@ -323,6 +325,27 @@ For DuckDNS, only need to provide the `token`, config 1 default domain & subdoma
|
||||
}
|
||||
```
|
||||
|
||||
### Config example for No-IP
|
||||
|
||||
```json
|
||||
{
|
||||
"provider": "NoIP",
|
||||
"email": "mail@example.com",
|
||||
"password": "YourPassword",
|
||||
"domains": [
|
||||
{
|
||||
"domain_name": "ddns.net",
|
||||
"sub_domains": ["timothyye6"]
|
||||
}
|
||||
],
|
||||
"ip_type": "IPv4",
|
||||
"ip_url": "https://myip.biturl.top",
|
||||
"resolver": "8.8.8.8",
|
||||
"interval": 300,
|
||||
"socks5_proxy": ""
|
||||
}
|
||||
```
|
||||
|
||||
### Config example for HE.net
|
||||
|
||||
For HE, email is not needed, just fill DDNS key to password, and config all the domains & subdomains.
|
||||
|
@ -49,7 +49,7 @@ func main() {
|
||||
func dnsLoop() {
|
||||
panicChan := make(chan godns.Domain)
|
||||
|
||||
log.Println("Creating DNS h with provider:", configuration.Provider)
|
||||
log.Println("Creating DNS handler with provider:", configuration.Provider)
|
||||
h := handler.CreateHandler(configuration.Provider)
|
||||
h.SetConfiguration(&configuration)
|
||||
for i := range configuration.Domains {
|
||||
|
@ -48,7 +48,11 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
log.Println("currentIP is:", currentIP)
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
@ -63,7 +63,12 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
@ -57,7 +57,12 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
@ -51,10 +51,8 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
log.Println("get_currentIP:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Println("currentIP is:", currentIP)
|
||||
|
||||
//check against locally cached IP, if no change, skip update
|
||||
|
||||
client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
|
||||
var ip string
|
||||
|
||||
@ -66,7 +64,12 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
@ -54,7 +54,12 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
log.Println("currentIP is:", currentIP)
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/TimothyYe/godns/handler/duck"
|
||||
"github.com/TimothyYe/godns/handler/google"
|
||||
"github.com/TimothyYe/godns/handler/he"
|
||||
"github.com/TimothyYe/godns/handler/noip"
|
||||
)
|
||||
|
||||
// IHandler is the interface for all DNS handlers
|
||||
@ -36,6 +37,8 @@ func CreateHandler(provider string) IHandler {
|
||||
handler = IHandler(&google.Handler{})
|
||||
case godns.DUCK:
|
||||
handler = IHandler(&duck.Handler{})
|
||||
case godns.NOIP:
|
||||
handler = IHandler(&noip.Handler{})
|
||||
}
|
||||
|
||||
return handler
|
||||
|
@ -58,7 +58,12 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
|
114
handler/noip/noip_handler.go
Normal file
114
handler/noip/noip_handler.go
Normal file
@ -0,0 +1,114 @@
|
||||
package noip
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/TimothyYe/godns"
|
||||
)
|
||||
|
||||
var (
|
||||
// NoIPUrl the API address for NoIP
|
||||
NoIPUrl = "https://%s:%s@dynupdate.no-ip.com/nic/update?hostname=%s&%s"
|
||||
)
|
||||
|
||||
// Handler struct
|
||||
type Handler struct {
|
||||
Configuration *godns.Settings
|
||||
}
|
||||
|
||||
// SetConfiguration pass dns settings and store it to handler instance
|
||||
func (handler *Handler) SetConfiguration(conf *godns.Settings) {
|
||||
handler.Configuration = conf
|
||||
}
|
||||
|
||||
// DomainLoop the main logic loop
|
||||
func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.Domain) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
|
||||
panicChan <- *domain
|
||||
}
|
||||
}()
|
||||
|
||||
looping := false
|
||||
|
||||
for {
|
||||
if looping {
|
||||
// Sleep with interval
|
||||
log.Printf("Going to sleep, will start next checking in %d seconds...\r\n", handler.Configuration.Interval)
|
||||
time.Sleep(time.Second * time.Duration(handler.Configuration.Interval))
|
||||
}
|
||||
|
||||
looping = true
|
||||
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||
|
||||
if err != nil {
|
||||
log.Println("get_currentIP:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Println("currentIP is:", currentIP)
|
||||
client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
|
||||
|
||||
var ip string
|
||||
if strings.ToUpper(handler.Configuration.IPType) == godns.IPV4 {
|
||||
ip = fmt.Sprintf("myip=%s", currentIP)
|
||||
} else if strings.ToUpper(handler.Configuration.IPType) == godns.IPV6 {
|
||||
ip = fmt.Sprintf("myipv6=%s", currentIP)
|
||||
}
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
hostname := subDomain + "." + domain.DomainName
|
||||
lastIP, err := godns.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
//check against currently known IP, if no change, skip update
|
||||
if currentIP == lastIP {
|
||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
||||
} else {
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
||||
NoIPUrl,
|
||||
handler.Configuration.Email,
|
||||
handler.Configuration.Password,
|
||||
hostname,
|
||||
ip), nil)
|
||||
|
||||
if handler.Configuration.UserAgent != "" {
|
||||
req.Header.Add("User-Agent", handler.Configuration.UserAgent)
|
||||
}
|
||||
|
||||
// update IP with HTTP GET request
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// handle error
|
||||
log.Print("Failed to update sub domain:", subDomain)
|
||||
continue
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil || !strings.Contains(string(body), "good") {
|
||||
log.Println("Failed to update the IP")
|
||||
continue
|
||||
} else {
|
||||
log.Print("IP updated to:", currentIP)
|
||||
}
|
||||
|
||||
// Send notification
|
||||
if err := godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP); err != nil {
|
||||
log.Println("Failed to send notification")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -83,18 +83,26 @@ func (r *DNSResolver) lookupHost(host string, dnsType uint16, triesLeft int) ([]
|
||||
}
|
||||
|
||||
if dnsType == dns.TypeA {
|
||||
for _, record := range in.Answer {
|
||||
if t, ok := record.(*dns.A); ok {
|
||||
result = append(result, t.A)
|
||||
if len(in.Answer) > 0 {
|
||||
for _, record := range in.Answer {
|
||||
if t, ok := record.(*dns.A); ok {
|
||||
result = append(result, t.A)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return result, errors.New("empty result")
|
||||
}
|
||||
}
|
||||
|
||||
if dnsType == dns.TypeAAAA {
|
||||
for _, record := range in.Answer {
|
||||
if t, ok := record.(*dns.AAAA); ok {
|
||||
result = append(result, t.AAAA)
|
||||
if len(in.Answer) > 0 {
|
||||
for _, record := range in.Answer {
|
||||
if t, ok := record.(*dns.AAAA); ok {
|
||||
result = append(result, t.AAAA)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return result, errors.New("Cannot resolve this domain, please make sure the IP type is right")
|
||||
}
|
||||
}
|
||||
|
||||
|
22
utils.go
22
utils.go
@ -54,6 +54,8 @@ const (
|
||||
DUCK = "DuckDNS"
|
||||
// DREAMHOST for Dreamhost
|
||||
DREAMHOST = "Dreamhost"
|
||||
// NOIP for NoIP
|
||||
NOIP = "NoIP"
|
||||
// IPV4 for IPV4 mode
|
||||
IPV4 = "IPV4"
|
||||
// IPV6 for IPV6 mode
|
||||
@ -218,6 +220,8 @@ func CheckSettings(config *Settings) error {
|
||||
return errors.New("login token cannot be empty")
|
||||
}
|
||||
case GOOGLE:
|
||||
fallthrough
|
||||
case NOIP:
|
||||
if config.Email == "" {
|
||||
return errors.New("email cannot be empty")
|
||||
}
|
||||
@ -427,7 +431,7 @@ func buildTemplate(currentIP, domain string, tplsrc string) string {
|
||||
}
|
||||
|
||||
// ResolveDNS will query DNS for a given hostname.
|
||||
func ResolveDNS(hostname, resolver, ipType string) string {
|
||||
func ResolveDNS(hostname, resolver, ipType string) (string, error) {
|
||||
var dnsType uint16
|
||||
if ipType == "" || strings.ToUpper(ipType) == IPV4 {
|
||||
dnsType = dns.TypeA
|
||||
@ -439,12 +443,10 @@ func ResolveDNS(hostname, resolver, ipType string) string {
|
||||
if resolver == "" {
|
||||
dnsAdress, err := net.LookupHost(hostname)
|
||||
if err != nil {
|
||||
if strings.HasSuffix(err.Error(), "no such host") {
|
||||
return "<nil>"
|
||||
}
|
||||
log.Fatalf(err.Error())
|
||||
return "<nil>", err
|
||||
}
|
||||
return dnsAdress[0]
|
||||
|
||||
return dnsAdress[0], nil
|
||||
}
|
||||
res := dnsResolver.New([]string{resolver})
|
||||
// In case of i/o timeout
|
||||
@ -452,11 +454,9 @@ func ResolveDNS(hostname, resolver, ipType string) string {
|
||||
|
||||
ip, err := res.LookupHost(hostname, dnsType)
|
||||
if err != nil {
|
||||
if strings.HasSuffix(err.Error(), "NXDOMAIN") {
|
||||
return "<nil>"
|
||||
}
|
||||
log.Fatalf(err.Error())
|
||||
return "<nil>", err
|
||||
}
|
||||
return ip[0].String()
|
||||
|
||||
return ip[0].String(), nil
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user