From 4bca24fe97ba851778866b420a532be7c1b593e9 Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 27 Oct 2017 11:12:13 +0800 Subject: [PATCH] refactor code struct --- godns.go | 75 ------------------- handler.go | 19 ----- .../dnspod_handler.go | 37 +++++---- handler/handler.go | 22 ++++++ he_handler.go => handler/he_handler.go | 30 +++++--- logger.go | 2 +- settings.go | 2 +- settings_test.go | 2 +- utils.go | 23 ++++-- utils_test.go | 2 +- 10 files changed, 84 insertions(+), 130 deletions(-) delete mode 100644 godns.go delete mode 100644 handler.go rename dnspod_handler.go => handler/dnspod_handler.go (84%) create mode 100644 handler/handler.go rename he_handler.go => handler/he_handler.go (68%) diff --git a/godns.go b/godns.go deleted file mode 100644 index 3a2bdb2..0000000 --- a/godns.go +++ /dev/null @@ -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) - } - } -} diff --git a/handler.go b/handler.go deleted file mode 100644 index 81d8e1d..0000000 --- a/handler.go +++ /dev/null @@ -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 -} diff --git a/dnspod_handler.go b/handler/dnspod_handler.go similarity index 84% rename from dnspod_handler.go rename to handler/dnspod_handler.go index ead82a0..987bc43 100644 --- a/dnspod_handler.go +++ b/handler/dnspod_handler.go @@ -1,4 +1,4 @@ -package main +package handler import ( "encoding/json" @@ -12,20 +12,27 @@ import ( "strings" "time" + "github.com/TimothyYe/godns" "github.com/bitly/go-simplejson" "golang.org/x/net/proxy" ) // 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 -func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) { +func (handler *DNSPodHandler) 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()) - fmt.Println(identifyPanic()) - log.Print(identifyPanic()) + fmt.Println(godns.IdentifyPanic()) + log.Print(godns.IdentifyPanic()) panicChan <- *domain } }() @@ -38,7 +45,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain continue } - currentIP, err := getCurrentIP(configuration.IPUrl) + currentIP, err := godns.GetCurrentIP(handler.Configuration) if err != nil { log.Println("get_currentIP:", err) @@ -65,18 +72,18 @@ func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain } //Interval is 5 minutes - time.Sleep(time.Minute * INTERVAL) + time.Sleep(time.Minute * godns.INTERVAL) } } // GenerateHeader generates the request header for DNSPod API func (handler *DNSPodHandler) GenerateHeader(content url.Values) url.Values { header := url.Values{} - if configuration.LoginToken != "" { - header.Add("login_token", configuration.LoginToken) + if handler.Configuration.LoginToken != "" { + header.Add("login_token", handler.Configuration.LoginToken) } else { - header.Add("login_email", configuration.Email) - header.Add("login_password", configuration.Password) + header.Add("login_email", handler.Configuration.Email) + header.Add("login_password", handler.Configuration.Password) } header.Add("format", "json") 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) { 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 { fmt.Println("can't connect to the proxy:", 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.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) diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..3fbdfd6 --- /dev/null +++ b/handler/handler.go @@ -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 +} diff --git a/he_handler.go b/handler/he_handler.go similarity index 68% rename from he_handler.go rename to handler/he_handler.go index e72737e..5ce6dd1 100644 --- a/he_handler.go +++ b/handler/he_handler.go @@ -1,4 +1,4 @@ -package main +package handler import ( "fmt" @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/TimothyYe/godns" + "golang.org/x/net/proxy" ) @@ -19,21 +21,27 @@ var ( ) // 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 -func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) { +func (handler *HEHandler) 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()) - fmt.Println(identifyPanic()) - log.Print(identifyPanic()) + fmt.Println(godns.IdentifyPanic()) + log.Print(godns.IdentifyPanic()) panicChan <- *domain } }() for { - currentIP, err := getCurrentIP(configuration.IPUrl) + currentIP, err := godns.GetCurrentIP(handler.Configuration) if err != nil { log.Println("get_currentIP:", err) @@ -47,7 +55,7 @@ func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) { } // 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) { values := url.Values{} values.Add("hostname", fmt.Sprintf("%s.%s", subDomain, domain)) - values.Add("password", configuration.Password) + values.Add("password", handler.Configuration.Password) values.Add("myip", currentIP) client := &http.Client{} - if configuration.Socks5Proxy != "" { - log.Println("use socks5 proxy:" + configuration.Socks5Proxy) - dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct) + if handler.Configuration.Socks5Proxy != "" { + log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) + dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) if err != nil { log.Println("can't connect to the proxy:", err) return diff --git a/logger.go b/logger.go index 932c28d..84f71b5 100644 --- a/logger.go +++ b/logger.go @@ -1,4 +1,4 @@ -package main +package godns import ( "bufio" diff --git a/settings.go b/settings.go index bcefb8b..cd7e532 100644 --- a/settings.go +++ b/settings.go @@ -1,4 +1,4 @@ -package main +package godns import ( "encoding/json" diff --git a/settings_test.go b/settings_test.go index 3c9dd54..da7efb9 100644 --- a/settings_test.go +++ b/settings_test.go @@ -1,4 +1,4 @@ -package main +package godns import ( "testing" diff --git a/utils.go b/utils.go index 6b359b6..9aeea8f 100644 --- a/utils.go +++ b/utils.go @@ -1,4 +1,4 @@ -package main +package godns import ( "errors" @@ -13,7 +13,18 @@ import ( "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{} if configuration.Socks5Proxy != "" { @@ -31,7 +42,7 @@ func getCurrentIP(url string) (string, error) { httpTransport.Dial = dialer.Dial } - response, err := client.Get(url) + response, err := client.Get(configuration.IPUrl) if err != nil { log.Println("Cannot get IP...") @@ -44,7 +55,7 @@ func getCurrentIP(url string) (string, error) { return string(body), nil } -func identifyPanic() string { +func IdentifyPanic() string { var name, file string var line int var pc [16]uintptr @@ -72,12 +83,12 @@ func identifyPanic() string { return fmt.Sprintf("pc:%x", pc) } -func usage() { +func Usage() { log.Println("[command] -c=[config file path]") flag.PrintDefaults() } -func checkSettings(config *Settings) error { +func CheckSettings(config *Settings) error { if config.Provider == DNSPOD { if (config.Email == "" || config.Password == "") && config.LoginToken == "" { return errors.New("email/password or login token cannot be empty") diff --git a/utils_test.go b/utils_test.go index 21f3ee1..26a961b 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,4 +1,4 @@ -package main +package godns import ( "testing"