Ejemplo n.º 1
0
func GetRecordList(domainId int) []g.RecordResult {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")
	data.Add("domain_id", strconv.Itoa(domainId))

	strResponse, err := Post("https://dnsapi.cn/Record.List", data)
	if nil == err && "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
		var rlr g.RecordListResult
		err = json.Unmarshal(bytes.NewBufferString(strResponse).Bytes(), &rlr)
		if nil != err {
			logger.Error("decode RECORD_LIST response fail %v\n", err)
		} else {
			var records []g.RecordResult
			for _, record := range rlr.Records {
				if "A" != record.Type {
					continue
				}
				records = append(records, record)
				logger.Info("domain_id:%d,name:%s,record_id:%s,name:%s,value:%s,status:%s\n",
					domainId, rlr.Domain.Name, record.Id, record.Name, record.Value, record.Status)
			}

			return records
		}
	} else {
		logger.Error("GET_RECORD_LIST RESPONSE<<<====%s, error: %v", strResponse, err)
	}
	return nil
}
Ejemplo n.º 2
0
func ModifyRecord(domainId int, recordId, subDomain, strIp string) bool {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")
	data.Add("domain_id", strconv.Itoa(domainId))
	data.Add("record_id", recordId)
	data.Add("sub_domain", subDomain)
	data.Add("record_type", "A")
	data.Add("record_line", "默认")
	data.Add("value", strIp)

	strResponse, err := Post("https://dnsapi.cn/Record.Modify", data)
	if nil != err {
		logger.Errorln("RECORD_MODIFY ERROR", err)
	} else {
		logger.Infoln("RECORD_MODIFY_RESPONSE:", strResponse, err)
		if "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
			return true
		} else {
			logger.Infoln("domainId:%d,recordId:%s,sub_domain:%s\n", domainId, recordId, subDomain)
		}
	}
	return false
}
Ejemplo n.º 3
0
func main() {
	cfg := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")

	flag.Parse()

	if *version {
		fmt.Println(g.VERSION)
		os.Exit(0)
	}

	g.ParseConfig(*cfg)
	log.Println(g.Config())

	logger.SetLevelWithDefault(g.Config().LogLevel, "info")

	if g.Config().Redis.Enabled {
		g.InitRedisConnPool()
	}

	go cron.SyncDomainRecord()
	go cron.Heartbeat()

	select {}
	// ticker1 := time.NewTicker(time.Duration(g.Config().Interval) * time.Second)
	// for {
	// 	select {
	// 	case <-ticker1.C:
	// 		go func() {
	// 			cron.UpdateIpRecord()
	// 		}()
	// 	}
	// }
}
Ejemplo n.º 4
0
func Heartbeat() {
	time.Sleep(time.Duration(5) * time.Second)
	for {
		heartbeat()
		d := time.Duration(g.Config().Interval) * time.Second
		time.Sleep(d)
	}
}
Ejemplo n.º 5
0
func existsRecordName(domainName, recordName string) bool {
	for _, domain := range g.Config().Domains {
		if domain.DomainName == domainName {
			for _, record := range domain.RecordNames {
				if record == recordName {
					return true
				}
			}
		}
	}
	return false
}
Ejemplo n.º 6
0
func heartbeat() {
	strIp := getIp()
	if "" == strIp || lastIp == strIp {
		logger.Debug("strIp:%s, lastIp: %s.\n", strIp, lastIp)
		return
	}

	logger.Debugln("domainMap's size:", len(domainMap))
	if 0 == len(domainMap) {
		return
	}

	var modifyResult bool
	for _, domain := range g.Config().Domains {
		if _, ok := domainMap[domain.DomainName]; ok {
			if recordResults, exists := recordMap[domain.DomainName]; exists {
				for _, recordResult := range recordResults {
					if existsRecordName(domain.DomainName, recordResult.Name) && strIp != recordResult.Value {
						modifyResult = ModifyRecord(domainMap[domain.DomainName], recordResult.Id, recordResult.Name, strIp)
					}
				}
			}
		}
	}

	if modifyResult {
		lastIp = strIp
		logger.Infoln("last ip have changed into ", strIp)

		if g.Config().Redis.Enabled {
			// send to redis
			rc := g.RedisConnPool.Get()
			defer rc.Close()
			rc.Do("LPUSH", "COMMAND_udai", g.Config().ExecuteCommand)
		}
	}
}
Ejemplo n.º 7
0
func GetDomainList() []g.DomainResult {
	data := url.Values{}

	data.Add("login_email", g.Config().LoginEmail)
	data.Add("login_password", g.Config().LoginPassword)
	data.Add("format", "json")

	strResponse, err := Post("https://dnsapi.cn/Domain.List", data)
	if nil == err && "" != strResponse && strings.Contains(strResponse, `"code":"1"`) {
		var dlr g.DomainListResult
		err = json.Unmarshal(bytes.NewBufferString(strResponse).Bytes(), &dlr)
		if nil != err {
			logger.Errorln("decode DOMAIN_LIST response fail %v\n", err)
		} else {
			for _, domain := range dlr.Domains {
				logger.Info("domain_id:%d,name:%s,created:%v,updated:%v\n", domain.Id, domain.Name, domain.Created, domain.Updated)
			}
			return dlr.Domains
		}
	} else {
		logger.Error("GET_DOMAIN_LIST RESPONSE<<<====%s, error: %v", strResponse, err)
	}
	return nil
}
Ejemplo n.º 8
0
func getIp() string {
	strUrl := g.Config().GetIpApi
	logger.Debug("REQUEST_URL:%s\n", strUrl)
	httpRequest := httplib.Get(strUrl).SetTimeout(3*time.Second, 10*time.Second)
	httpResponse, err := httpRequest.Bytes()
	if nil != err {
		logger.Errorln("GET_IP error", err)
		return ""
	}

	strIp := ""
	var resp g.ServletResponse
	err = json.Unmarshal(httpResponse, &resp)
	if err != nil {
		logger.Error("decode GET_IP response fail %v\n", err)
	} else if false == resp.Success {
		logger.Error("GET_IP fail %s\n", resp.Message)
	} else {
		strIp = resp.Message
	}

	logger.Infoln("RESPONSE_IP:", strIp)
	return strIp
}