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"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
@ -24,43 +23,10 @@ var (
|
||||
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() {
|
||||
flag.Parse()
|
||||
if *optHelp {
|
||||
usage()
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
@ -71,11 +37,11 @@ func main() {
|
||||
Password: os.Getenv("PASSWORD"),
|
||||
LoginToken: os.Getenv("TOKEN"),
|
||||
Domain: os.Getenv("DOMAIN"),
|
||||
Sub_domain: os.Getenv("SUB_DOMAIN"),
|
||||
IP_Url: "http://members.3322.org/dyndns/getip",
|
||||
Log_Path: "./godns.log",
|
||||
Log_Size: 16,
|
||||
Log_Num: 3,
|
||||
SubDomain: os.Getenv("SUB_DOMAIN"),
|
||||
IPUrl: "http://members.3322.org/dyndns/getip",
|
||||
LogPath: "./godns.log",
|
||||
LogSize: 16,
|
||||
LogNum: 3,
|
||||
}
|
||||
} else {
|
||||
//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 {
|
||||
log.Println("InitLogger error:", err)
|
||||
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)
|
||||
}
|
||||
|
||||
@ -116,14 +87,14 @@ func dnsLoop() {
|
||||
continue
|
||||
}
|
||||
|
||||
currentIP, err := getCurrentIP(configuration.IP_Url)
|
||||
currentIP, err := getCurrentIP(configuration.IPUrl)
|
||||
|
||||
if err != nil {
|
||||
log.Println("get_currentIP:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
subDomainID, ip := getSubDomain(domainID, configuration.Sub_domain)
|
||||
subDomainID, ip := getSubDomain(domainID, configuration.SubDomain)
|
||||
|
||||
if subDomainID == "" || ip == "" {
|
||||
log.Println("sub_domain:", subDomainID, ip)
|
||||
@ -135,7 +106,7 @@ func dnsLoop() {
|
||||
//Continue to check the IP of sub-domain
|
||||
if len(ip) > 0 && !strings.Contains(currentIP, ip) {
|
||||
log.Println("Start to update record IP...")
|
||||
updateIP(domainID, subDomainID, configuration.Sub_domain, currentIP)
|
||||
updateIP(domainID, subDomainID, configuration.SubDomain, currentIP)
|
||||
} else {
|
||||
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
|
||||
type Settings struct {
|
||||
Email string
|
||||
Password string
|
||||
LoginToken string
|
||||
Domain string
|
||||
Sub_domain string
|
||||
IP_Url string
|
||||
Log_Path string
|
||||
Log_Size int
|
||||
Log_Num int
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
LoginToken string `json:"login_token"`
|
||||
Domain string `json:"domain"`
|
||||
SubDomain string `json:"sub_domain"`
|
||||
IPUrl string `json:"ip_url"`
|
||||
LogPath string `json:"log_path"`
|
||||
LogSize int `json:"log_size"`
|
||||
LogNum int `json:"log_num"`
|
||||
}
|
||||
|
||||
//LoadSettings -- Load settings from config file
|
||||
|
@ -12,7 +12,7 @@ func TestLoadSetting(t *testing.T) {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
|
||||
if settings.IP_Url == "" {
|
||||
if settings.IPUrl == "" {
|
||||
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