mirror of
				https://github.com/taigrr/godns
				synced 2025-01-18 04:03:25 -08:00 
			
		
		
		
	integrate with he dns
This commit is contained in:
		
							parent
							
								
									a4dbb864bf
								
							
						
					
					
						commit
						9936fbc4e3
					
				| @ -7,13 +7,73 @@ import ( | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"os" | ||||
| 	"runtime/debug" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"github.com/bitly/go-simplejson" | ||||
| 	"golang.org/x/net/proxy" | ||||
| ) | ||||
| 
 | ||||
| type DNSPodHandler struct{} | ||||
| 
 | ||||
| func (handler *DNSPodHandler) DomainLoop(domain *Domain) { | ||||
| 	defer func() { | ||||
| 		if err := recover(); err != nil { | ||||
| 			panicCount++ | ||||
| 			log.Printf("Recovered in %v: %v\n", err, debug.Stack()) | ||||
| 			fmt.Println(identifyPanic()) | ||||
| 			log.Print(identifyPanic()) | ||||
| 			if panicCount < PANIC_MAX { | ||||
| 				log.Println("Got panic in goroutine, will start a new one... :", panicCount) | ||||
| 				go handler.DomainLoop(domain) | ||||
| 			} else { | ||||
| 				os.Exit(1) | ||||
| 			} | ||||
| 		} | ||||
| 	}() | ||||
| 
 | ||||
| 	for { | ||||
| 
 | ||||
| 		domainID := getDomain(domain.DomainName) | ||||
| 
 | ||||
| 		if domainID == -1 { | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		currentIP, err := getCurrentIP(configuration.IPUrl) | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			log.Println("get_currentIP:", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		log.Println("currentIp is:", currentIP) | ||||
| 
 | ||||
| 		for _, subDomain := range domain.SubDomains { | ||||
| 
 | ||||
| 			subDomainID, ip := getSubDomain(domainID, subDomain) | ||||
| 
 | ||||
| 			if subDomainID == "" || ip == "" { | ||||
| 				log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip) | ||||
| 				continue | ||||
| 			} | ||||
| 
 | ||||
| 			//Continue to check the IP of sub-domain | ||||
| 			if len(ip) > 0 && !strings.Contains(currentIP, ip) { | ||||
| 				log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) | ||||
| 				updateIP(domainID, subDomainID, subDomain, currentIP) | ||||
| 			} else { | ||||
| 				log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName) | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		//Interval is 5 minutes | ||||
| 		time.Sleep(time.Minute * INTERVAL) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func getCurrentIP(url string) (string, error) { | ||||
| 	response, err := http.Get(url) | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										62
									
								
								godns.go
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								godns.go
									
									
									
									
									
								
							| @ -5,9 +5,6 @@ import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"runtime/debug" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| @ -52,65 +49,10 @@ func main() { | ||||
| } | ||||
| 
 | ||||
| func dnsLoop() { | ||||
| 
 | ||||
| 	handler := createHandler(configuration.Type) | ||||
| 	for _, domain := range configuration.Domains { | ||||
| 		go domainLoop(&domain) | ||||
| 		go handler.DomainLoop(&domain) | ||||
| 	} | ||||
| 
 | ||||
| 	select {} | ||||
| } | ||||
| 
 | ||||
| func domainLoop(domain *Domain) { | ||||
| 	defer func() { | ||||
| 		if err := recover(); err != nil { | ||||
| 			panicCount++ | ||||
| 			log.Printf("Recovered in %v: %v\n", err, debug.Stack()) | ||||
| 			fmt.Println(identifyPanic()) | ||||
| 			log.Print(identifyPanic()) | ||||
| 			if panicCount < PANIC_MAX { | ||||
| 				log.Println("Got panic in goroutine, will start a new one... :", panicCount) | ||||
| 				go domainLoop(domain) | ||||
| 			} else { | ||||
| 				os.Exit(1) | ||||
| 			} | ||||
| 		} | ||||
| 	}() | ||||
| 
 | ||||
| 	for { | ||||
| 
 | ||||
| 		domainID := getDomain(domain.DomainName) | ||||
| 
 | ||||
| 		if domainID == -1 { | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		currentIP, err := getCurrentIP(configuration.IPUrl) | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			log.Println("get_currentIP:", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		log.Println("currentIp is:", currentIP) | ||||
| 
 | ||||
| 		for _, subDomain := range domain.SubDomains { | ||||
| 
 | ||||
| 			subDomainID, ip := getSubDomain(domainID, subDomain) | ||||
| 
 | ||||
| 			if subDomainID == "" || ip == "" { | ||||
| 				log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip) | ||||
| 				continue | ||||
| 			} | ||||
| 
 | ||||
| 			//Continue to check the IP of sub-domain | ||||
| 			if len(ip) > 0 && !strings.Contains(currentIP, ip) { | ||||
| 				log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) | ||||
| 				updateIP(domainID, subDomainID, subDomain, currentIP) | ||||
| 			} else { | ||||
| 				log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName) | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		//Interval is 5 minutes | ||||
| 		time.Sleep(time.Minute * INTERVAL) | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										18
									
								
								handler.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								handler.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| package main | ||||
| 
 | ||||
| type IHandler interface { | ||||
| 	DomainLoop(domain *Domain) | ||||
| } | ||||
| 
 | ||||
| func createHandler(provider string) IHandler { | ||||
| 	var handler IHandler | ||||
| 
 | ||||
| 	switch provider { | ||||
| 	case "DNSPod": | ||||
| 		handler = IHandler(&DNSPodHandler{}) | ||||
| 	case "HE": | ||||
| 		handler = IHandler(&HEHandler{}) | ||||
| 	} | ||||
| 
 | ||||
| 	return handler | ||||
| } | ||||
							
								
								
									
										6
									
								
								he_handler.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								he_handler.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | ||||
| package main | ||||
| 
 | ||||
| type HEHandler struct{} | ||||
| 
 | ||||
| func (handler *HEHandler) DomainLoop(domain *Domain) { | ||||
| } | ||||
| @ -15,6 +15,7 @@ type Domain struct { | ||||
| 
 | ||||
| //Settings struct | ||||
| type Settings struct { | ||||
| 	Type        string   `json:"type"` | ||||
| 	Email       string   `json:"email"` | ||||
| 	Password    string   `json:"password"` | ||||
| 	LoginToken  string   `json:"login_token"` | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user