//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()) } }
//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 }
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()) } } }
func Getdomainlist() (Result, error) { var data Result res, err := http.Get(Conf.Userinfo.Domain_list) if err != nil { dlog.Errorf("Parse domainlist url failed, error: %s\n", err.Error()) return data, err } var result []byte result, err = ioutil.ReadAll(res.Body) if err != nil { dlog.Errorf("Get domainlist failed, error: %s\n", err.Error()) return data, err } defer res.Body.Close() err = json.Unmarshal(result, &data) if err != nil { dlog.Errorf("Parse domainlist failed, error: %s\n", err.Error()) return data, err } return data, err }
func main() { data, err := Getdomainlist() if err != nil { dlog.Errorf("Fail to get data from Getdomainlist, error: %s\n", err.Error()) return } var inner_datas []Inner_result inner_datas, err = Getdomainrecord(data) if err != nil { dlog.Errorf("Fail to get data from Getdomainrecord, error: %s\n", err.Error()) return } var gitname string gitname, err = Writefile(data, inner_datas) if err != nil { dlog.Errorf("Fail to Wirte file, error: %s\n", err.Error()) return } add_cmd := exec.Command("git", "add", gitname) err = add_cmd.Run() if err != nil { dlog.Errorf("Git add file failed, error: %s\n", err.Error()) return } commit_cmd := exec.Command("git", "commit", "-m", gitname) err = commit_cmd.Run() if err != nil { dlog.Errorf("Git commit failed, error: %s\n", err.Error()) return } }
func Getdomainrecord(data Result) ([]Inner_result, error) { var inner_datas []Inner_result var err error for _, domain := range data.Domains { var inner_data Inner_result id := domain.Id url := fmt.Sprintf("%v?id=%v", Conf.Userinfo.Record_list, id) var res *http.Response res, err = http.Get(url) if err != nil { dlog.Errorf("Parse domainrecord url failed, error: %s\n", err.Error()) return inner_datas, err } var result []byte result, err = ioutil.ReadAll(res.Body) if err != nil { dlog.Errorf("Get domainrecord failed, error: %s\n", err.Error()) return inner_datas, err } defer res.Body.Close() err = json.Unmarshal(result, &inner_data) if err != nil { dlog.Errorf("Parse domainrecord failed, error: %s \n", err.Error()) return inner_datas, err } inner_datas = append(inner_datas, inner_data) } return inner_datas, err }
func init() { basePath, _ := os.Getwd() logPath := fmt.Sprintf("%s/log/", basePath) logBackend, err := dlog.NewFileBackend(logPath) if err != nil { fmt.Println("failed to new log backend") os.Exit(1) } dlog.SetLogging(dlog.INFO, logBackend) //loading config.toml _, err = toml.DecodeFile("config.toml", &Conf) if err != nil { dlog.Errorf("unable to decode config file. ") } }
//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 }
func Writefile(data Result, inner_datas []Inner_result) (string, error) { t := time.Now() parse_time := t.Format("2006-01-02") filename := fmt.Sprintf("%vdnspod.%v", Conf.Userinfo.Path, parse_time) gitname := fmt.Sprintf("dnspod.%v", parse_time) f, err := os.Create(filename) if err != nil { dlog.Errorf("Create file failed, error: %s\n", err.Error()) return gitname, err } defer f.Close() for _, domain := range data.Domains { id := fmt.Sprintf("id: %v", domain.Id) name := fmt.Sprintf("name: %v", domain.Name) vipstart := fmt.Sprintf("vipstart: %v", domain.VipStart) vipend := fmt.Sprintf("vipend: %v", domain.VipEnd) remark := fmt.Sprintf("remark: %v", domain.Remark) restday := fmt.Sprintf("restday: %v", domain.RestDay) result := fmt.Sprintf("|%-20s|%-30s|%-30s|%-30s|%-20s|%-20s|\n", id, name, vipstart, vipend, remark, restday) _, err = f.WriteString(result) if err != nil { dlog.Errorf("Write domainlist to file failed, error: %s\n", err.Error()) return gitname, err } } for _, record := range inner_datas { domain_name := fmt.Sprintf("domain: %v\n", record.Domain) _, err = f.WriteString(domain_name) if err != nil { dlog.Errorf("Write domain_name to file failed, error: %s\n", err.Error()) return gitname, err } for _, inner_domain := range record.Records { name := fmt.Sprintf("name: %v", inner_domain.Name) domain_type := fmt.Sprintf("type: %v", inner_domain.Type) line := fmt.Sprintf("line: %v", inner_domain.Line) value := fmt.Sprintf("value: %v", inner_domain.Value) mx := fmt.Sprintf("mx: %v", inner_domain.Mx) ttl := fmt.Sprintf("ttl: %v", inner_domain.Ttl) remark := fmt.Sprintf("|remark: %v|\n\n", inner_domain.Remark) result := fmt.Sprintf("|%-25s|%-15s|%-25s|%-45s|%-15s|%-15s|\n", name, domain_type, line, value, mx, ttl) _, err = f.WriteString(result) if err != nil { dlog.Errorf("Write domainrecord to file failed, error: %s\n", err.Error()) return gitname, err } _, err = f.WriteString(remark) if err != nil { dlog.Errorf("Write domain record remark to file failed, error: %s\n", err.Error()) return gitname, err } } } return gitname, err }