func PrintIp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, r.Header.Get("X-Real-IP")) }
package main import ( "fmt" "github.com/julienschmidt/httprouter" "log" "net/http" "flag" ) // var ( port = flag.Int("port", 80, "port") // , host = flag.String("host", "", "host") // prefix = flag.String("prefix", "/ip", "uri prefix") // ) func PrintIp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, r.Header.Get("X-Real-IP")) } func main() { // flag.Parse() // addr := fmt.Sprintf("%s:%d", *host, *port) log.Println("listening on", addr) router := httprouter.New() // url router.GET(*prefix, PrintIp) // log.Fatal(http.ListenAndServe(addr, router)) }
upstream yourip { server locahost:888; # } server { listen 80; location /ip { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://yourip; } }
curl https://www.cloudflare.com/api_json.html \ -d 'a=rec_load_all' \ -d 'tkn=8afbe6dea02407989af4dd4c97bb6e25' \ -d 'email=sample@example.com' \ -d 'z=example.com'
// , func Url() (u url.URL) { u.Host = "www.cloudflare.com" u.Scheme = "https" u.Path = "api_json.html" return }
u := Url() // // () url values := u.Query() values.Add("email", *email) values.Add("tkn", *token) values.Add("a", "rec_load_all") values.Add("z", *domain) // RawQuery , u.RawQuery = values.Encode() reqUrl := u.String()
client = http.Client{} req, _ := http.NewRequest("POST", reqUrl, nil) res, err := client.Do(req)
type AllResponse struct { Response struct { Records struct { Objects []struct { Id string `json:"rec_id"` Name string `json:"name"` Type string `json:"type"` Content string `json:"content"` } `json:"objs"` } `json:"recs"` } `json:"response"` }
// , response := &AllResponse{} // decoder := json.NewDecoder(res.Body) // err = decoder.Decode(response)
for _, v := range response.Response.Records.Objects { // if v.Name == *target && v.Type == "A" { // id, _ := strconv.Atoi(v.Id) return id, v.Content, nil } }
u := Url() values := u.Query() values.Add("email", *email) values.Add("tkn", *token) values.Add("a", "rec_edit") values.Add("z", *domain) values.Add("type", "A") values.Add("name", *target) values.Add("service_mode", "0") values.Add("content", ip) values.Add("id", strconv.Itoa(id)) values.Add("ttl", fmt.Sprint(*ttl))
req, _ := http.NewRequest("POST", reqUrl, nil) res, err := client.Do(req)
func main() { flag.Parse() // id ip id, previousIp, err := GetDnsId() if err != nil { log.Fatalln("unable to get dns record id:", err) } // , // 5 ip ticker := time.NewTicker(time.Second * 5) // for _ = range ticker.C { ip, err := GetIp() if err != nil { continue } if previousIp != ip { err = SetIp(ip, id) if err != nil { continue } } log.Println("updated to", ip) previousIp = ip } }
package main import ( "encoding/json" "errors" "flag" "fmt" "io/ioutil" "log" "net/http" "net/url" "strconv" "time" ) // api type AllResponse struct { Response struct { Records struct { Objects []struct { Id string `json:"rec_id"` Name string `json:"name"` Type string `json:"type"` Content string `json:"content"` } `json:"objs"` } `json:"recs"` } `json:"response"` } // var ( yourIpUrl = flag.String("url", "https://cydev.ru/ip", "Yourip service url") domain = flag.String("domain", "cydev.ru", "Cloudflare domain") target = flag.String("target", "me.cydev.ru", "Target domain") email = flag.String("email", "ernado@ya.ru", "The e-mail address associated with the API key") token = flag.String("token", "-", "This is the API key made available on your Account page") ttl = flag.Int("ttl", 120, "TTL of record in seconds. 1 = Automatic, otherwise, value must in between 120 and 86400 seconds") // http - Do, client = http.Client{} ) // , func Url() (u url.URL) { u.Host = "www.cloudflare.com" u.Scheme = "https" u.Path = "api_json.html" return } // SetIp id func SetIp(ip string, id int) error { u := Url() values := u.Query() values.Add("email", *email) values.Add("tkn", *token) values.Add("a", "rec_edit") values.Add("z", *domain) values.Add("type", "A") values.Add("name", *target) values.Add("service_mode", "0") values.Add("content", ip) values.Add("id", strconv.Itoa(id)) values.Add("ttl", fmt.Sprint(*ttl)) u.RawQuery = values.Encode() reqUrl := u.String() log.Println("POST", reqUrl) req, err := http.NewRequest("POST", reqUrl, nil) if err != nil { return err } res, err := client.Do(req) if err != nil { return err } if res.StatusCode != http.StatusOK { return errors.New(fmt.Sprintf("bad status %d", res.StatusCode)) } return nil } // GetDnsId id func GetDnsId() (int, string, error) { log.Println("getting dns record id") // url u := Url() // values := u.Query() values.Add("email", *email) values.Add("tkn", *token) values.Add("a", "rec_load_all") values.Add("z", *domain) u.RawQuery = values.Encode() reqUrl := u.String() // , log.Println("POST", reqUrl) req, err := http.NewRequest("POST", reqUrl, nil) res, err := client.Do(req) if err != nil { return 0, "", err } if res.StatusCode != http.StatusOK { return 0, "", errors.New(fmt.Sprintf("bad status %d", res.StatusCode)) } response := &AllResponse{} // decoder := json.NewDecoder(res.Body) // err = decoder.Decode(response) if err != nil { return 0, "", err } // for _, v := range response.Response.Records.Objects { // if v.Name == *target && v.Type == "A" { // id, _ := strconv.Atoi(v.Id) return id, v.Content, nil } } // return 0, "", errors.New("not found") } // GetIp() yourip ip func GetIp() (string, error) { res, err := client.Get(*yourIpUrl) if err != nil { return "", err } if res.StatusCode != http.StatusOK { return "", errors.New(fmt.Sprintf("bad status %d", res.StatusCode)) } body, err := ioutil.ReadAll(res.Body) if err != nil { return "", err } return string(body), nil } func main() { flag.Parse() id, previousIp, err := GetDnsId() if err != nil { log.Fatalln("unable to get dns record id:", err) } log.Println("found record", id, "=", previousIp) // , // 5 ip ticker := time.NewTicker(time.Second * 5) // for _ = range ticker.C { ip, err := GetIp() if err != nil { log.Println("err", err) continue } if previousIp != ip { err = SetIp(ip, id) if err != nil { log.Println("unable to set ip:", err) continue } } log.Println("updated to", ip) previousIp = ip } }
Source: https://habr.com/ru/post/229179/
All Articles