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

refactor code struct

This commit is contained in:
Timothy 2017-10-27 11:12:13 +08:00
parent d46fefa18b
commit 4bca24fe97
10 changed files with 84 additions and 130 deletions

View File

@ -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)
}
}
}

View File

@ -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
}

View File

@ -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)

22
handler/handler.go Normal file
View 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
}

View File

@ -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

View File

@ -1,4 +1,4 @@
package main
package godns
import (
"bufio"

View File

@ -1,4 +1,4 @@
package main
package godns
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package main
package godns
import (
"testing"

View File

@ -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")

View File

@ -1,4 +1,4 @@
package main
package godns
import (
"testing"