mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
Refactor
This commit is contained in:
parent
f04b725ba1
commit
48892dfd5e
61
godns.go
61
godns.go
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -24,43 +23,10 @@ var (
|
|||||||
panicCount = 0
|
panicCount = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
func identifyPanic() string {
|
|
||||||
var name, file string
|
|
||||||
var line int
|
|
||||||
var pc [16]uintptr
|
|
||||||
|
|
||||||
n := runtime.Callers(3, pc[:])
|
|
||||||
for _, pc := range pc[:n] {
|
|
||||||
fn := runtime.FuncForPC(pc)
|
|
||||||
if fn == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
file, line = fn.FileLine(pc)
|
|
||||||
name = fn.Name()
|
|
||||||
if !strings.HasPrefix(name, "runtime.") {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case name != "":
|
|
||||||
return fmt.Sprintf("%v:%v", name, line)
|
|
||||||
case file != "":
|
|
||||||
return fmt.Sprintf("%v:%v", file, line)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("pc:%x", pc)
|
|
||||||
}
|
|
||||||
|
|
||||||
func usage() {
|
|
||||||
log.Println("[command] -c=[config file path]")
|
|
||||||
flag.PrintDefaults()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *optHelp {
|
if *optHelp {
|
||||||
usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +37,11 @@ func main() {
|
|||||||
Password: os.Getenv("PASSWORD"),
|
Password: os.Getenv("PASSWORD"),
|
||||||
LoginToken: os.Getenv("TOKEN"),
|
LoginToken: os.Getenv("TOKEN"),
|
||||||
Domain: os.Getenv("DOMAIN"),
|
Domain: os.Getenv("DOMAIN"),
|
||||||
Sub_domain: os.Getenv("SUB_DOMAIN"),
|
SubDomain: os.Getenv("SUB_DOMAIN"),
|
||||||
IP_Url: "http://members.3322.org/dyndns/getip",
|
IPUrl: "http://members.3322.org/dyndns/getip",
|
||||||
Log_Path: "./godns.log",
|
LogPath: "./godns.log",
|
||||||
Log_Size: 16,
|
LogSize: 16,
|
||||||
Log_Num: 3,
|
LogNum: 3,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//Load settings from configurations file
|
//Load settings from configurations file
|
||||||
@ -86,8 +52,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := InitLogger(configuration.Log_Path, configuration.Log_Size, configuration.Log_Num); err != nil {
|
if err := checkSettings(&configuration); err != nil {
|
||||||
log.Println("InitLogger error:", err)
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,14 +87,14 @@ func dnsLoop() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
currentIP, err := getCurrentIP(configuration.IP_Url)
|
currentIP, err := getCurrentIP(configuration.IPUrl)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get_currentIP:", err)
|
log.Println("get_currentIP:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
subDomainID, ip := getSubDomain(domainID, configuration.Sub_domain)
|
subDomainID, ip := getSubDomain(domainID, configuration.SubDomain)
|
||||||
|
|
||||||
if subDomainID == "" || ip == "" {
|
if subDomainID == "" || ip == "" {
|
||||||
log.Println("sub_domain:", subDomainID, ip)
|
log.Println("sub_domain:", subDomainID, ip)
|
||||||
@ -135,7 +106,7 @@ func dnsLoop() {
|
|||||||
//Continue to check the IP of sub-domain
|
//Continue to check the IP of sub-domain
|
||||||
if len(ip) > 0 && !strings.Contains(currentIP, ip) {
|
if len(ip) > 0 && !strings.Contains(currentIP, ip) {
|
||||||
log.Println("Start to update record IP...")
|
log.Println("Start to update record IP...")
|
||||||
updateIP(domainID, subDomainID, configuration.Sub_domain, currentIP)
|
updateIP(domainID, subDomainID, configuration.SubDomain, currentIP)
|
||||||
} else {
|
} else {
|
||||||
log.Println("Current IP is same as domain IP, no need to update...")
|
log.Println("Current IP is same as domain IP, no need to update...")
|
||||||
}
|
}
|
||||||
|
18
settings.go
18
settings.go
@ -8,15 +8,15 @@ import (
|
|||||||
|
|
||||||
//Settings struct
|
//Settings struct
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
Email string
|
Email string `json:"email"`
|
||||||
Password string
|
Password string `json:"password"`
|
||||||
LoginToken string
|
LoginToken string `json:"login_token"`
|
||||||
Domain string
|
Domain string `json:"domain"`
|
||||||
Sub_domain string
|
SubDomain string `json:"sub_domain"`
|
||||||
IP_Url string
|
IPUrl string `json:"ip_url"`
|
||||||
Log_Path string
|
LogPath string `json:"log_path"`
|
||||||
Log_Size int
|
LogSize int `json:"log_size"`
|
||||||
Log_Num int
|
LogNum int `json:"log_num"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//LoadSettings -- Load settings from config file
|
//LoadSettings -- Load settings from config file
|
||||||
|
@ -12,7 +12,7 @@ func TestLoadSetting(t *testing.T) {
|
|||||||
t.Error(err.Error())
|
t.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if settings.IP_Url == "" {
|
if settings.IPUrl == "" {
|
||||||
t.Error("Cannot load ip_url from config file")
|
t.Error("Cannot load ip_url from config file")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
51
utils.go
Normal file
51
utils.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func identifyPanic() string {
|
||||||
|
var name, file string
|
||||||
|
var line int
|
||||||
|
var pc [16]uintptr
|
||||||
|
|
||||||
|
n := runtime.Callers(3, pc[:])
|
||||||
|
for _, pc := range pc[:n] {
|
||||||
|
fn := runtime.FuncForPC(pc)
|
||||||
|
if fn == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
file, line = fn.FileLine(pc)
|
||||||
|
name = fn.Name()
|
||||||
|
if !strings.HasPrefix(name, "runtime.") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case name != "":
|
||||||
|
return fmt.Sprintf("%v:%v", name, line)
|
||||||
|
case file != "":
|
||||||
|
return fmt.Sprintf("%v:%v", file, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("pc:%x", pc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func usage() {
|
||||||
|
log.Println("[command] -c=[config file path]")
|
||||||
|
flag.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkSettings(config *Settings) error {
|
||||||
|
if (config.Email == "" || config.Password == "") && config.LoginToken == "" {
|
||||||
|
return errors.New("Input email/password or login token cannot be empty!")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user