コード例 #1
0
ファイル: server.go プロジェクト: guaiguaihw/dnspod
//Get records of every domain
func Getdomainrecord(domain_id string) []byte {

	//Read config.toml file
	var inner_data Inner_result
	var inner_body []byte
	timeout := time.Duration(Conf.Timeout.timeout) * time.Millisecond
	client := http.Client{
		Timeout: timeout,
	}
	count := Conf.Retry.Retry_count

	for i := 0; i < count; i++ {

		inner_req, err := client.PostForm(Conf.Userinfo.Record_list, url.Values{"login_email": {"%s", Conf.Userinfo.Login_email}, "login_password": {"%s", Conf.Userinfo.Login_password}, "format": {"json"}, "domain_id": {"%s", domain_id}})
		if err != nil {
			dlog.Errorf("post form error: %s\n", err.Error())
		}
		inner_body, err = ioutil.ReadAll(inner_req.Body)
		dlog.Info("post:", Conf.Userinfo.Record_list)
		dlog.Info("return:", string(inner_body))
		if err != nil {
			dlog.Errorf("read body failed. error: %s\n", err.Error())
		}

		defer inner_req.Body.Close()

		err = json.Unmarshal(inner_body, &inner_data)
		if err != nil {
			dlog.Errorf("Get records failed,check %s err\n", err.Error())
		}
		if len(inner_data.Records) == 0 {
			dlog.Errorf("query from api success, but conten length is zero! Try again...\n")
		} else {
			break
		}
	}

	var record_result []byte
	record_result, err := json.Marshal(inner_data)
	if err != nil {
		dlog.Errorf("marshal domain list failed. err:", err.Error())
	}
	return record_result
}
コード例 #2
0
ファイル: server.go プロジェクト: guaiguaihw/dnspod
//Http server
func Domainlist(w http.ResponseWriter, r *http.Request) {
	result := Getdomainlist()
	domainlist, err := json.Marshal(result)
	if err != nil {
		dlog.Errorf("marshal domain list failed. err:", err.Error())
	}
	dlog.Info("Get domianlist succeeded...")
	_, err = w.Write(domainlist)
	if err != nil {
		dlog.Errorf("write result to client failed, err:", err.Error())
	}
}
コード例 #3
0
ファイル: server.go プロジェクト: guaiguaihw/dnspod
func Record(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	if r.Method == "GET" {

		domain_id := r.FormValue("id")

		dlog.Info("Get records succeeded...")

		recordlist := Getdomainrecord(domain_id)
		_, err := w.Write(recordlist)
		if err != nil {
			dlog.Errorf("write result to client failed, err:", err.Error())
		}
	}
}
コード例 #4
0
ファイル: server.go プロジェクト: guaiguaihw/dnspod
//Get main domainlists
func Getdomainlist() Result {

	//Read config.toml file
	var body []byte
	var Data Result
	timeout := time.Duration(Conf.Timeout.timeout) * time.Millisecond
	client := http.Client{
		Timeout: timeout,
	}

	count := Conf.Retry.Retry_count
	for i := 0; i < count; i++ {

		rep, err := client.PostForm(Conf.Userinfo.Domain_list, url.Values{"login_email": {"%s", Conf.Userinfo.Login_email}, "login_password": {"%s", Conf.Userinfo.Login_password}, "format": {"json"}})
		if err != nil {
			dlog.Errorf("post form error: %s\n", err.Error())
		}
		body, err = ioutil.ReadAll(rep.Body)
		dlog.Info("post:", Conf.Userinfo.Domain_list)
		dlog.Info("return:", string(body))
		if err != nil {
			dlog.Errorf("read body failed. error: %s\n", err.Error())
		}

		defer rep.Body.Close()

		err = json.Unmarshal(body, &Data)
		if err != nil {
			dlog.Errorf("Get domainlist failed,check %s err\n", err.Error())
		}
		if len(Data.Domains) == 0 {
			dlog.Errorf("query from api success, but conten length is zero! Try again...\n")
		} else {
			break
		}
	}

	var days []int
	for _, v := range Data.Domains {
		nowtime := time.Now()
		end := v.VipEnd
		template := "2006-01-02"
		endtime, errend := time.Parse(template, end)
		if errend != nil {
			dlog.Errorf("Get vip_end_time failed,check %s err\n", errend.Error())
		}

		var result time.Duration
		if endtime.Sub(nowtime) < 0 {
			result = 0
		} else {
			result = endtime.Sub(nowtime)
		}

		restday := int(result.Hours() / 24)
		days = append(days, restday)
	}
	for i := 0; i < len(days); i++ {
		Data.Domains[i].RestDay = days[i]
	}
	return Data
}