Last active
August 29, 2015 14:12
-
-
Save techjanitor/b75e5cdec809ef34822a to your computer and use it in GitHub Desktop.
Ban someone on cloudflare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
) | |
type Cloudflare struct { | |
Msg interface{} `json:"msg"` | |
Response struct { | |
Result struct { | |
Action string `json:"action"` | |
Ip string `json:"ip"` | |
} `json:"result"` | |
} `json:"response"` | |
Result string `json:"result"` | |
} | |
func CloudflareBanIp(ip string) (result string, err error) { | |
queryValues := url.Values{} | |
if ip == "" { | |
err = errors.New("no ip provided") | |
return | |
} | |
queryValues.Add("a", "ban") | |
queryValues.Add("tkn", CloudflareKey) | |
queryValues.Add("email", CloudflareEmail) | |
queryValues.Add("key", ip) | |
res, err := http.PostForm(CloudflareUrl, queryValues) | |
if err != nil { | |
err = errors.New("error reaching cloudflare") | |
return | |
} | |
defer res.Body.Close() | |
res.Header.Add("User-Agent", "Pram/1.2") | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
err = errors.New("error parsing cloudflare response") | |
return | |
} | |
cf_data := Cloudflare{} | |
err = json.Unmarshal(body, &cf_data) | |
if err != nil { | |
err = errors.New("error parsing cloudflare data") | |
return | |
} | |
result = cf_data.Result | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment