1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

enable socks for domain update and telegram notification seperately

- add configuration to enable socks for domain update
  and telegram notification seperately.
- sleep to reduce cpu usage while connection error occurred.
This commit is contained in:
Jemy Zhang 2020-05-05 15:11:41 +08:00
parent 09503d0c97
commit 9186772d50
11 changed files with 76 additions and 35 deletions

View File

@ -398,7 +398,8 @@ Update config file and provide your Telegram options, a notification message wil
"enabled": true, "enabled": true,
"bot_api_key": "11111:aaaa-bbbb", "bot_api_key": "11111:aaaa-bbbb",
"chat_id": "-123456", "chat_id": "-123456",
"message_template": "Domain *{{ .Domain }}* is updated to %0A{{ .CurrentIP }}" "message_template": "Domain *{{ .Domain }}* is updated to %0A{{ .CurrentIP }}",
"use_proxy": false
}, },
} }
``` ```
@ -410,6 +411,7 @@ You can also use SOCKS5 proxy, just fill SOCKS5 address to the ```socks5_proxy``
```json ```json
"socks5_proxy": "127.0.0.1:7070" "socks5_proxy": "127.0.0.1:7070"
"use_proxy": true
``` ```
Now all the queries will go through the specified SOCKS5 proxy. Now all the queries will go through the specified SOCKS5 proxy.

View File

@ -26,12 +26,14 @@
"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": "",
"use_proxy": false,
"notify": { "notify": {
"telegram": { "telegram": {
"enabled": false, "enabled": false,
"bot_api_key": "", "bot_api_key": "",
"chat_id": "", "chat_id": "",
"message_template": "" "message_template": "",
"use_proxy": false
}, },
"mail": { "mail": {
"enabled": false, "enabled": false,

View File

@ -28,9 +28,17 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
aliDNS := NewAliDNS(handler.Configuration.Email, handler.Configuration.Password) aliDNS := NewAliDNS(handler.Configuration.Email, handler.Configuration.Password)
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
@ -68,9 +76,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }

View File

@ -77,7 +77,15 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
}() }()
var lastIP string var lastIP string
looping := false
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
log.Println("Error in GetCurrentIP:", err) log.Println("Error in GetCurrentIP:", err)
@ -117,9 +125,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
log.Println("Failed to find zone for domain:", domain.DomainName) log.Println("Failed to find zone for domain:", domain.DomainName)
} }
} }
// 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))
} }
} }
@ -137,7 +142,7 @@ func recordTracked(domain *godns.Domain, record *DNSRecord) bool {
// Create a new request with auth in place and optional proxy // Create a new request with auth in place and optional proxy
func (handler *Handler) newRequest(method, url string, body io.Reader) (*http.Request, *http.Client) { func (handler *Handler) newRequest(method, url string, body io.Reader) (*http.Request, *http.Client) {
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
if client == nil { if client == nil {
log.Println("cannot create HTTP client") log.Println("cannot create HTTP client")
} }

View File

@ -36,7 +36,16 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
for { 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
log.Printf("Checking IP for domain %s \r\n", domain.DomainName) log.Printf("Checking IP for domain %s \r\n", domain.DomainName)
domainID := handler.GetDomain(domain.DomainName) domainID := handler.GetDomain(domain.DomainName)
@ -82,9 +91,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }
@ -245,7 +251,7 @@ func (handler *Handler) UpdateIP(domainID int64, subDomainID string, subDomainNa
// PostData post data and invoke DNSPod API // PostData post data and invoke DNSPod API
func (handler *Handler) PostData(url string, content url.Values) (string, error) { func (handler *Handler) PostData(url string, content url.Values) (string, error) {
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
if client == nil { if client == nil {
return "", errors.New("failed to create HTTP client") return "", errors.New("failed to create HTTP client")

View File

@ -38,7 +38,15 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
@ -63,10 +71,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }
@ -106,7 +110,7 @@ func (handler *Handler) updateDNS(dns, ip, hostname, action string) {
log.Fatalf("Unknown action %s\n", action) log.Fatalf("Unknown action %s\n", action)
} }
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
req, _ := http.NewRequest("POST", DreamhostURL, strings.NewReader(values.Encode())) req, _ := http.NewRequest("POST", DreamhostURL, strings.NewReader(values.Encode()))
req.SetBasicAuth(handler.Configuration.Email, handler.Configuration.Password) req.SetBasicAuth(handler.Configuration.Email, handler.Configuration.Password)

View File

@ -35,7 +35,16 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
@ -46,7 +55,7 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
//check against locally cached IP, if no change, skip update //check against locally cached IP, if no change, skip update
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
var ip string var ip string
if strings.ToUpper(handler.Configuration.IPType) == godns.IPV4 { if strings.ToUpper(handler.Configuration.IPType) == godns.IPV4 {
@ -86,9 +95,5 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }

View File

@ -36,7 +36,16 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
log.Println("get_currentIP:", err) log.Println("get_currentIP:", err)
@ -59,16 +68,13 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }
// UpdateIP update subdomain with current IP // UpdateIP update subdomain with current IP
func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) {
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
resp, err := client.Get(fmt.Sprintf(GoogleURL, resp, err := client.Get(fmt.Sprintf(GoogleURL,
handler.Configuration.Email, handler.Configuration.Email,
handler.Configuration.Password, handler.Configuration.Password,

View File

@ -37,7 +37,15 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
}() }()
looping := false
for { 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) currentIP, err := godns.GetCurrentIP(handler.Configuration)
if err != nil { if err != nil {
@ -64,10 +72,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
} }
} }
// 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))
} }
} }
@ -79,7 +83,7 @@ func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) {
values.Add("password", handler.Configuration.Password) values.Add("password", handler.Configuration.Password)
values.Add("myip", currentIP) values.Add("myip", currentIP)
client := godns.GetHttpClient(handler.Configuration) client := godns.GetHttpClient(handler.Configuration, handler.Configuration.UseProxy)
req, _ := http.NewRequest("POST", HEUrl, strings.NewReader(values.Encode())) req, _ := http.NewRequest("POST", HEUrl, strings.NewReader(values.Encode()))
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@ -18,6 +18,7 @@ type TelegramNotify struct {
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"`
UseProxy bool `json:"use_proxy"`
} }
// Notify struct for SMTP notification // Notify struct for SMTP notification
@ -53,6 +54,7 @@ type Settings struct {
IPInterface string `json:"ip_interface"` IPInterface string `json:"ip_interface"`
IPType string `json:"ip_type"` IPType string `json:"ip_type"`
Resolver string `json:"resolver"` Resolver string `json:"resolver"`
UseProxy bool `json:"use_proxy"`
} }
// LoadSettings -- Load settings from config file // LoadSettings -- Load settings from config file

View File

@ -116,10 +116,10 @@ func isIPv4(ip string) bool {
} }
// GetHttpClient creates the HTTP client and return it // GetHttpClient creates the HTTP client and return it
func GetHttpClient(configuration *Settings) *http.Client { func GetHttpClient(configuration *Settings, use_proxy bool) *http.Client {
client := &http.Client{} client := &http.Client{}
if configuration.Socks5Proxy != "" { if use_proxy && configuration.Socks5Proxy != "" {
log.Println("use socks5 proxy:" + configuration.Socks5Proxy) log.Println("use socks5 proxy:" + configuration.Socks5Proxy)
dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct) dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct)
if err != nil { if err != nil {
@ -249,7 +249,7 @@ func SendTelegramNotify(configuration *Settings, domain, currentIP string) error
return errors.New("chat id cannot be empty") return errors.New("chat id cannot be empty")
} }
client := GetHttpClient(configuration) client := GetHttpClient(configuration, configuration.Notify.Telegram.UseProxy)
tpl := configuration.Notify.Telegram.MsgTemplate tpl := configuration.Notify.Telegram.MsgTemplate
if tpl == "" { if tpl == "" {
tpl = "_Your IP address is changed to_%0A%0A*{{ .CurrentIP }}*%0A%0ADomain *{{ .Domain }}* is updated" tpl = "_Your IP address is changed to_%0A%0A*{{ .CurrentIP }}*%0A%0ADomain *{{ .Domain }}* is updated"