mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
refactor code struct
This commit is contained in:
parent
d46fefa18b
commit
4bca24fe97
75
godns.go
75
godns.go
@ -1,75 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// PanicMax is the max allowed panic times
|
|
||||||
PanicMax = 5
|
|
||||||
// INTERVAL is minute
|
|
||||||
INTERVAL = 5
|
|
||||||
// DNSPOD for dnspod.cn
|
|
||||||
DNSPOD = "DNSPod"
|
|
||||||
// HE for he.net
|
|
||||||
HE = "HE"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
configuration Settings
|
|
||||||
optConf = flag.String("c", "./config.json", "Specify a config file")
|
|
||||||
optHelp = flag.Bool("h", false, "Show help")
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
if *optHelp {
|
|
||||||
flag.Usage()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load settings from configurations file
|
|
||||||
if err := LoadSettings(*optConf, &configuration); err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
log.Println(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := checkSettings(&configuration); err != nil {
|
|
||||||
log.Println("Settings is invalid! ", err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := InitLogger(configuration.LogPath, configuration.LogSize, configuration.LogNum); err != nil {
|
|
||||||
log.Println("InitLogger error:", err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
dnsLoop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func dnsLoop() {
|
|
||||||
panicChan := make(chan Domain)
|
|
||||||
|
|
||||||
handler := createHandler(configuration.Provider)
|
|
||||||
for _, domain := range configuration.Domains {
|
|
||||||
go handler.DomainLoop(&domain, panicChan)
|
|
||||||
}
|
|
||||||
|
|
||||||
panicCount := 0
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case failDomain := <-panicChan:
|
|
||||||
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
|
|
||||||
go handler.DomainLoop(&failDomain, panicChan)
|
|
||||||
}
|
|
||||||
|
|
||||||
panicCount++
|
|
||||||
if panicCount >= PanicMax {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
19
handler.go
19
handler.go
@ -1,19 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// IHandler is the interface for all DNS handlers
|
|
||||||
type IHandler interface {
|
|
||||||
DomainLoop(domain *Domain, panicChan chan<- Domain)
|
|
||||||
}
|
|
||||||
|
|
||||||
func createHandler(provider string) IHandler {
|
|
||||||
var handler IHandler
|
|
||||||
|
|
||||||
switch provider {
|
|
||||||
case DNSPOD:
|
|
||||||
handler = IHandler(&DNSPodHandler{})
|
|
||||||
case HE:
|
|
||||||
handler = IHandler(&HEHandler{})
|
|
||||||
}
|
|
||||||
|
|
||||||
return handler
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -12,20 +12,27 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/TimothyYe/godns"
|
||||||
"github.com/bitly/go-simplejson"
|
"github.com/bitly/go-simplejson"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DNSPodHandler struct definition
|
// DNSPodHandler struct definition
|
||||||
type DNSPodHandler struct{}
|
type DNSPodHandler struct {
|
||||||
|
Configuration *godns.Settings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *DNSPodHandler) SetConfiguration(conf *godns.Settings) {
|
||||||
|
handler.Configuration = conf
|
||||||
|
}
|
||||||
|
|
||||||
// DomainLoop the main logic loop
|
// DomainLoop the main logic loop
|
||||||
func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
|
func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.Domain) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
|
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
|
||||||
fmt.Println(identifyPanic())
|
fmt.Println(godns.IdentifyPanic())
|
||||||
log.Print(identifyPanic())
|
log.Print(godns.IdentifyPanic())
|
||||||
panicChan <- *domain
|
panicChan <- *domain
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -38,7 +45,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
currentIP, err := getCurrentIP(configuration.IPUrl)
|
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get_currentIP:", err)
|
log.Println("get_currentIP:", err)
|
||||||
@ -65,18 +72,18 @@ func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Interval is 5 minutes
|
//Interval is 5 minutes
|
||||||
time.Sleep(time.Minute * INTERVAL)
|
time.Sleep(time.Minute * godns.INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateHeader generates the request header for DNSPod API
|
// GenerateHeader generates the request header for DNSPod API
|
||||||
func (handler *DNSPodHandler) GenerateHeader(content url.Values) url.Values {
|
func (handler *DNSPodHandler) GenerateHeader(content url.Values) url.Values {
|
||||||
header := url.Values{}
|
header := url.Values{}
|
||||||
if configuration.LoginToken != "" {
|
if handler.Configuration.LoginToken != "" {
|
||||||
header.Add("login_token", configuration.LoginToken)
|
header.Add("login_token", handler.Configuration.LoginToken)
|
||||||
} else {
|
} else {
|
||||||
header.Add("login_email", configuration.Email)
|
header.Add("login_email", handler.Configuration.Email)
|
||||||
header.Add("login_password", configuration.Password)
|
header.Add("login_password", handler.Configuration.Password)
|
||||||
}
|
}
|
||||||
header.Add("format", "json")
|
header.Add("format", "json")
|
||||||
header.Add("lang", "en")
|
header.Add("lang", "en")
|
||||||
@ -220,11 +227,11 @@ func (handler *DNSPodHandler) UpdateIP(domainID int64, subDomainID string, subDo
|
|||||||
func (handler *DNSPodHandler) PostData(url string, content url.Values) (string, error) {
|
func (handler *DNSPodHandler) PostData(url string, content url.Values) (string, error) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
if configuration.Socks5Proxy != "" {
|
if handler.Configuration.Socks5Proxy != "" {
|
||||||
|
|
||||||
log.Println("use socks5 proxy:" + configuration.Socks5Proxy)
|
log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy)
|
||||||
|
|
||||||
dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct)
|
dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("can't connect to the proxy:", err)
|
fmt.Println("can't connect to the proxy:", err)
|
||||||
return "", err
|
return "", err
|
||||||
@ -239,7 +246,7 @@ func (handler *DNSPodHandler) PostData(url string, content url.Values) (string,
|
|||||||
req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(values.Encode()))
|
req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(values.Encode()))
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", configuration.Email))
|
req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", handler.Configuration.Email))
|
||||||
|
|
||||||
response, err := client.Do(req)
|
response, err := client.Do(req)
|
||||||
|
|
22
handler/handler.go
Normal file
22
handler/handler.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import "github.com/TimothyYe/godns"
|
||||||
|
|
||||||
|
// IHandler is the interface for all DNS handlers
|
||||||
|
type IHandler interface {
|
||||||
|
SetConfiguration(*godns.Settings)
|
||||||
|
DomainLoop(domain *godns.Domain, panicChan chan<- godns.Domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateHandler(provider string) IHandler {
|
||||||
|
var handler IHandler
|
||||||
|
|
||||||
|
switch provider {
|
||||||
|
case godns.DNSPOD:
|
||||||
|
handler = IHandler(&DNSPodHandler{})
|
||||||
|
case godns.HE:
|
||||||
|
handler = IHandler(&HEHandler{})
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -10,6 +10,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/TimothyYe/godns"
|
||||||
|
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,21 +21,27 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// HEHandler struct
|
// HEHandler struct
|
||||||
type HEHandler struct{}
|
type HEHandler struct {
|
||||||
|
Configuration *godns.Settings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *HEHandler) SetConfiguration(conf *godns.Settings) {
|
||||||
|
handler.Configuration = conf
|
||||||
|
}
|
||||||
|
|
||||||
// DomainLoop the main logic loop
|
// DomainLoop the main logic loop
|
||||||
func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
|
func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.Domain) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
|
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
|
||||||
fmt.Println(identifyPanic())
|
fmt.Println(godns.IdentifyPanic())
|
||||||
log.Print(identifyPanic())
|
log.Print(godns.IdentifyPanic())
|
||||||
panicChan <- *domain
|
panicChan <- *domain
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
currentIP, err := getCurrentIP(configuration.IPUrl)
|
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get_currentIP:", err)
|
log.Println("get_currentIP:", err)
|
||||||
@ -47,7 +55,7 @@ func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Interval is 5 minutes
|
// Interval is 5 minutes
|
||||||
time.Sleep(time.Minute * INTERVAL)
|
time.Sleep(time.Minute * godns.INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,14 +63,14 @@ func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
|
|||||||
func (handler *HEHandler) UpdateIP(domain, subDomain, currentIP string) {
|
func (handler *HEHandler) UpdateIP(domain, subDomain, currentIP string) {
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
values.Add("hostname", fmt.Sprintf("%s.%s", subDomain, domain))
|
values.Add("hostname", fmt.Sprintf("%s.%s", subDomain, domain))
|
||||||
values.Add("password", configuration.Password)
|
values.Add("password", handler.Configuration.Password)
|
||||||
values.Add("myip", currentIP)
|
values.Add("myip", currentIP)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
if configuration.Socks5Proxy != "" {
|
if handler.Configuration.Socks5Proxy != "" {
|
||||||
log.Println("use socks5 proxy:" + configuration.Socks5Proxy)
|
log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy)
|
||||||
dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct)
|
dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("can't connect to the proxy:", err)
|
log.Println("can't connect to the proxy:", err)
|
||||||
return
|
return
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package godns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package godns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
23
utils.go
23
utils.go
@ -1,4 +1,4 @@
|
|||||||
package main
|
package godns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -13,7 +13,18 @@ import (
|
|||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getCurrentIP(url string) (string, error) {
|
const (
|
||||||
|
// PanicMax is the max allowed panic times
|
||||||
|
PanicMax = 5
|
||||||
|
// INTERVAL is minute
|
||||||
|
INTERVAL = 5
|
||||||
|
// DNSPOD for dnspod.cn
|
||||||
|
DNSPOD = "DNSPod"
|
||||||
|
// HE for he.net
|
||||||
|
HE = "HE"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetCurrentIP(configuration *Settings) (string, error) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
if configuration.Socks5Proxy != "" {
|
if configuration.Socks5Proxy != "" {
|
||||||
@ -31,7 +42,7 @@ func getCurrentIP(url string) (string, error) {
|
|||||||
httpTransport.Dial = dialer.Dial
|
httpTransport.Dial = dialer.Dial
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := client.Get(url)
|
response, err := client.Get(configuration.IPUrl)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Cannot get IP...")
|
log.Println("Cannot get IP...")
|
||||||
@ -44,7 +55,7 @@ func getCurrentIP(url string) (string, error) {
|
|||||||
return string(body), nil
|
return string(body), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func identifyPanic() string {
|
func IdentifyPanic() string {
|
||||||
var name, file string
|
var name, file string
|
||||||
var line int
|
var line int
|
||||||
var pc [16]uintptr
|
var pc [16]uintptr
|
||||||
@ -72,12 +83,12 @@ func identifyPanic() string {
|
|||||||
return fmt.Sprintf("pc:%x", pc)
|
return fmt.Sprintf("pc:%x", pc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func Usage() {
|
||||||
log.Println("[command] -c=[config file path]")
|
log.Println("[command] -c=[config file path]")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkSettings(config *Settings) error {
|
func CheckSettings(config *Settings) error {
|
||||||
if config.Provider == DNSPOD {
|
if config.Provider == DNSPOD {
|
||||||
if (config.Email == "" || config.Password == "") && config.LoginToken == "" {
|
if (config.Email == "" || config.Password == "") && config.LoginToken == "" {
|
||||||
return errors.New("email/password or login token cannot be empty")
|
return errors.New("email/password or login token cannot be empty")
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package godns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user