mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
Replaced cached IP with remote resolver
This commit is contained in:
parent
41d54f1354
commit
f0a6291406
@ -22,6 +22,7 @@
|
|||||||
"ipv6_url": "https://api-ipv6.ip.sb/ip",
|
"ipv6_url": "https://api-ipv6.ip.sb/ip",
|
||||||
"ip_type": "IPv4",
|
"ip_type": "IPv4",
|
||||||
"interval": 300,
|
"interval": 300,
|
||||||
|
"resolver": "8.8.8.8",
|
||||||
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36",
|
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36",
|
||||||
"ip_interface": "eth0",
|
"ip_interface": "eth0",
|
||||||
"socks5_proxy": "",
|
"socks5_proxy": "",
|
||||||
|
@ -37,24 +37,21 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var lastIP string
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get_currentIP:", err)
|
log.Println("get_currentIP:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Println("currentIP is:", currentIP)
|
log.Println("currentIP is:", currentIP)
|
||||||
|
for _, subDomain := range domain.SubDomains {
|
||||||
|
hostname := subDomain + "." + domain.DomainName
|
||||||
|
lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver)
|
||||||
|
//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 {
|
||||||
|
|
||||||
//check against locally cached IP, if no change, skip update
|
|
||||||
if currentIP == lastIP {
|
|
||||||
log.Printf("IP is the same as cached one. Skip update.\n")
|
|
||||||
} else {
|
|
||||||
lastIP = currentIP
|
|
||||||
|
|
||||||
for _, subDomain := range domain.SubDomains {
|
|
||||||
log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName)
|
log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName)
|
||||||
handler.UpdateIP(domain.DomainName, subDomain, currentIP)
|
handler.UpdateIP(domain.DomainName, subDomain, currentIP)
|
||||||
|
|
||||||
|
13
settings.go
13
settings.go
@ -14,10 +14,10 @@ type Domain struct {
|
|||||||
|
|
||||||
// Notify struct for telegram notification
|
// Notify struct for telegram notification
|
||||||
type TelegramNotify struct {
|
type TelegramNotify struct {
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
BotApiKey string `json:"bot_api_key"`
|
BotApiKey string `json:"bot_api_key"`
|
||||||
ChatId string `json:"chat_id"`
|
ChatId string `json:"chat_id"`
|
||||||
MsgTemplate string `json:"message_template"`
|
MsgTemplate string `json:"message_template"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify struct for SMTP notification
|
// Notify struct for SMTP notification
|
||||||
@ -32,8 +32,8 @@ type MailNotify struct {
|
|||||||
|
|
||||||
// Notify struct
|
// Notify struct
|
||||||
type Notify struct {
|
type Notify struct {
|
||||||
Telegram TelegramNotify `json:"telegram"`
|
Telegram TelegramNotify `json:"telegram"`
|
||||||
Mail MailNotify `json:"mail"`
|
Mail MailNotify `json:"mail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settings struct
|
// Settings struct
|
||||||
@ -52,6 +52,7 @@ type Settings struct {
|
|||||||
Notify Notify `json:"notify"`
|
Notify Notify `json:"notify"`
|
||||||
IPInterface string `json:"ip_interface"`
|
IPInterface string `json:"ip_interface"`
|
||||||
IPType string `json:"ip_type"`
|
IPType string `json:"ip_type"`
|
||||||
|
Resolver string `json:"resolver"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadSettings -- Load settings from config file
|
// LoadSettings -- Load settings from config file
|
||||||
|
30
utils.go
30
utils.go
@ -12,6 +12,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bogdanovich/dns_resolver"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
gomail "gopkg.in/gomail.v2"
|
gomail "gopkg.in/gomail.v2"
|
||||||
)
|
)
|
||||||
@ -346,3 +347,32 @@ func buildTemplate(currentIP, domain string, tplsrc string) string {
|
|||||||
|
|
||||||
return tpl.String()
|
return tpl.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResolveDNS will query DNS for a given hostname.
|
||||||
|
func ResolveDNS(hostname, resolver string) string {
|
||||||
|
|
||||||
|
// If no DNS server is set in config file, falls back to default resolver.
|
||||||
|
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 dnsAdress[0]
|
||||||
|
}
|
||||||
|
res := dns_resolver.New([]string{resolver})
|
||||||
|
// In case of i/o timeout
|
||||||
|
res.RetryTimes = 5
|
||||||
|
|
||||||
|
ip, err := res.LookupHost(hostname)
|
||||||
|
if err != nil {
|
||||||
|
if strings.HasSuffix(err.Error(), "NXDOMAIN") {
|
||||||
|
return "<nil>"
|
||||||
|
}
|
||||||
|
log.Fatalf(err.Error())
|
||||||
|
}
|
||||||
|
return ip[0].String()
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user