mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
muti domain support
Signed-off-by: jerrylou <gunsluo@gmail.com>
This commit is contained in:
parent
5d6f76b136
commit
63f39bad94
@ -1,9 +1,15 @@
|
|||||||
{
|
{
|
||||||
"email": "",
|
"email": "example@gmail.com",
|
||||||
"password": "",
|
"password": "",
|
||||||
"login_token": "",
|
"login_token": "",
|
||||||
"domain": "",
|
"domains": [{
|
||||||
"sub_domain": "",
|
"domain_name":"example.com",
|
||||||
|
"sub_domains":["www","test"]
|
||||||
|
},{
|
||||||
|
"domain_name":"example2.com",
|
||||||
|
"sub_domains":["www","test"]
|
||||||
|
}
|
||||||
|
],
|
||||||
"ip_url": "http://members.3322.org/dyndns/getip",
|
"ip_url": "http://members.3322.org/dyndns/getip",
|
||||||
"log_path":"./godns.log",
|
"log_path":"./godns.log",
|
||||||
"log_size":16,
|
"log_size":16,
|
||||||
|
35
godns.go
35
godns.go
@ -36,13 +36,17 @@ func main() {
|
|||||||
Email: os.Getenv("EMAIL"),
|
Email: os.Getenv("EMAIL"),
|
||||||
Password: os.Getenv("PASSWORD"),
|
Password: os.Getenv("PASSWORD"),
|
||||||
LoginToken: os.Getenv("TOKEN"),
|
LoginToken: os.Getenv("TOKEN"),
|
||||||
Domain: os.Getenv("DOMAIN"),
|
|
||||||
SubDomain: os.Getenv("SUB_DOMAIN"),
|
|
||||||
IPUrl: "http://members.3322.org/dyndns/getip",
|
IPUrl: "http://members.3322.org/dyndns/getip",
|
||||||
LogPath: "./godns.log",
|
LogPath: "./godns.log",
|
||||||
LogSize: 16,
|
LogSize: 16,
|
||||||
LogNum: 3,
|
LogNum: 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := LoadDomains(os.Getenv("DOMAINS"), &configuration.Domains); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
log.Println(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
//Load settings from configurations file
|
//Load settings from configurations file
|
||||||
if err := LoadSettings(*optConf, &configuration); err != nil {
|
if err := LoadSettings(*optConf, &configuration); err != nil {
|
||||||
@ -79,9 +83,18 @@ func dnsLoop() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
for _, domain := range configuration.Domains {
|
||||||
|
go DomainLoop(&domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DomainLoop(domain *Domain) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
domainID := getDomain(configuration.Domain)
|
domainID := getDomain(domain.DomainName)
|
||||||
|
|
||||||
if domainID == -1 {
|
if domainID == -1 {
|
||||||
continue
|
continue
|
||||||
@ -93,22 +106,24 @@ func dnsLoop() {
|
|||||||
log.Println("get_currentIP:", err)
|
log.Println("get_currentIP:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
log.Println("currentIp is:", currentIP)
|
||||||
|
|
||||||
subDomainID, ip := getSubDomain(domainID, configuration.SubDomain)
|
for _, subDomain := range domain.SubDomains {
|
||||||
|
|
||||||
|
subDomainID, ip := getSubDomain(domainID, subDomain)
|
||||||
|
|
||||||
if subDomainID == "" || ip == "" {
|
if subDomainID == "" || ip == "" {
|
||||||
log.Println("sub_domain:", subDomainID, ip)
|
log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("currentIp is:", currentIP)
|
|
||||||
|
|
||||||
//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.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName)
|
||||||
updateIP(domainID, subDomainID, configuration.SubDomain, currentIP)
|
updateIP(domainID, subDomainID, subDomain, currentIP)
|
||||||
} else {
|
} else {
|
||||||
log.Println("Current IP is same as domain IP, no need to update...")
|
log.Println("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Interval is 5 minutes
|
//Interval is 5 minutes
|
||||||
|
20
settings.go
20
settings.go
@ -6,13 +6,17 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Domain struct {
|
||||||
|
DomainName string `json:"domain_name"`
|
||||||
|
SubDomains []string `json:"sub_domains"`
|
||||||
|
}
|
||||||
|
|
||||||
//Settings struct
|
//Settings struct
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
LoginToken string `json:"login_token"`
|
LoginToken string `json:"login_token"`
|
||||||
Domain string `json:"domain"`
|
Domains []Domain `json:"domains"`
|
||||||
SubDomain string `json:"sub_domain"`
|
|
||||||
IPUrl string `json:"ip_url"`
|
IPUrl string `json:"ip_url"`
|
||||||
LogPath string `json:"log_path"`
|
LogPath string `json:"log_path"`
|
||||||
LogSize int `json:"log_size"`
|
LogSize int `json:"log_size"`
|
||||||
@ -36,3 +40,15 @@ func LoadSettings(configPath string, settings *Settings) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//LoadDomains -- Load domains from domains json
|
||||||
|
func LoadDomains(domainsJson string, domains *[]Domain) error {
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(domainsJson), domains)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error occurs while unmarshal domains json, please make sure domains json config correct!")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user