1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

refactor post logic

This commit is contained in:
Timothy 2014-05-12 23:19:32 +08:00
parent 8d0c8e624c
commit 936efcbdca
3 changed files with 33 additions and 17 deletions

View File

@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
func GetCurrentIP(url string) (string, error) { func get_currentIP(url string) (string, error) {
response, err := http.Get(url) response, err := http.Get(url)
defer response.Body.Close() defer response.Body.Close()
@ -21,37 +21,47 @@ func GetCurrentIP(url string) (string, error) {
return string(body), nil return string(body), nil
} }
func generate_header(content url.Values, setting Settings) url.Values { func generate_header(content url.Values) url.Values {
header := url.Values{} header := url.Values{}
header.Add("login_email", setting.Email) header.Add("login_email", Configuration.Email)
header.Add("login_password", setting.Password) header.Add("login_password", Configuration.Password)
header.Add("format", "json") header.Add("format", "json")
header.Add("lang", "en") header.Add("lang", "en")
header.Add("error_on_empty", "no") header.Add("error_on_empty", "no")
if content != nil {
for k, _ := range content { for k, _ := range content {
header.Add(k, content.Get(k)) header.Add(k, content.Get(k))
} }
}
return header return header
} }
func post_data(url string, content url.Values, setting Settings) (string, error) { func api_version() {
fmt.Println(Configuration.Email)
post_data("/Info.Version", nil)
}
func post_data(url string, content url.Values) (string, error) {
client := &http.Client{} client := &http.Client{}
req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(content.Encode())) values := generate_header(content)
req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", setting.Email)) req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", Configuration.Email))
response, err := client.Do(req) response, err := client.Do(req)
defer response.Body.Close() defer response.Body.Close()
if err != nil { if err != nil {
fmt.Println("Post failed...") fmt.Println("Post failed...")
fmt.Println(err.Error())
return "", err return "", err
} }
resp, _ := ioutil.ReadAll(response.Body) resp, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(resp))
return string(resp), nil return string(resp), nil
} }

View File

@ -6,14 +6,15 @@ import (
"time" "time"
) )
var Configuration Settings
func main() { func main() {
fmt.Println("Starting...") fmt.Println("Starting...")
setting := LoadSettings() Configuration = LoadSettings()
fmt.Println(setting.IP_Url)
loop := make(chan bool) loop := make(chan bool)
go dns_loop(setting, loop) go dns_loop(loop)
ret := <-loop ret := <-loop
@ -25,11 +26,18 @@ func main() {
} }
} }
func dns_loop(setting Settings, loop chan bool) { func dns_loop(loop chan bool) {
fmt.Println("Inside the loop...") fmt.Println("Inside the loop...")
//time.Sleep(time.Second * 60 * 5) //time.Sleep(time.Second * 60 * 5)
time.Sleep(time.Second * 5)
fmt.Println(GetCurrentIP(setting.IP_Url)) currentIP, _ := get_currentIP(Configuration.IP_Url)
fmt.Println("Current IP is" + currentIP)
//Continue to check the IP of sub-domain
if len(currentIP) > 0 {
}
api_version()
loop <- false loop <- false
} }

View File

@ -28,7 +28,5 @@ func LoadSettings() Settings {
var setting Settings var setting Settings
json.Unmarshal(file, &setting) json.Unmarshal(file, &setting)
fmt.Println(setting.Email)
return setting return setting
} }