From cdd7bfbea79fbff277c7e4755d4c5923d841ad8e Mon Sep 17 00:00:00 2001 From: Odyssey346 Date: Wed, 27 Jul 2022 19:00:48 +0200 Subject: [PATCH] new stuff cool ig Signed-off-by: Odyssey346 --- api/form.go | 53 +++++++++++++++++++++++++++++++---------- otherthings/checkenv.go | 21 ++++++++++++---- static/testform.html | 7 ++++-- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/api/form.go b/api/form.go index 22129be..8346209 100644 --- a/api/form.go +++ b/api/form.go @@ -3,12 +3,15 @@ package api import ( "net/http" "html/template" - "io" "log" "github.com/kataras/hcaptcha" "os" + "fmt" + + "net/url" + "io" ) var ( @@ -22,23 +25,47 @@ var ( ) func Form() { - http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case http.MethodGet: - io.WriteString(w, "I got your ") - io.WriteString(w, "\n" + r.RemoteAddr) - case http.MethodPost: - io.WriteString(w, "You have no mail to POST!") - default: - http.Error(w, "Method isn't allowed!\nYou may only GET or POST here, not " + r.Method, http.StatusMethodNotAllowed) - } - log.Println("[HTTP] " + r.RemoteAddr + " accessed /api/form with method " + r.Method) - }) + http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode)) http.HandleFunc("/form", renderTestForm) } +func theActualFormCode(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + hcaptchaResp, ok := hcaptcha.Get(r) + if !ok { + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprint(w, "Seems like captcha failed, you didn't complete the captcha or you are a bot. Please try again.\nPlease note that your IP has been logged in our systems for manual review to check if you're an abusive user. If you're seen as abusive, you will be blacklisted.") + postData := url.Values{ + "content": {"IP " + r.RemoteAddr + "failed captcha!"}, + } + req, err := http.PostForm("https://canary.discord.com/api/webhooks/1001546472971587674/XpxdpvlrEvEo7jnRmWyrEudTW0eNC_LpNVd-uC20R44e_efgwvhzSNQ26IaGeO9yan6f", postData) + if err != nil { + log.Fatal(err) + } + + fmt.Fprint(io.Discard, req) // I don't want the result of the request in stdout + return + } + log.Printf(r.FormValue("aisdjaoidjoiajsidaoj")) + fmt.Fprintf(w, "Thanks for your message, and thanks for doing the captcha!\n%#+v", hcaptchaResp) + postData := url.Values{ + "content": {"IP " + r.RemoteAddr + "\nFrom " + r.FormValue("email") + " with feedback type " + r.FormValue("commentType") + ":\n" + "**" + r.FormValue("message") + "**"}, + } + req, err := http.PostForm("https://discord.com/api/webhooks/1001546472971587674/XpxdpvlrEvEo7jnRmWyrEudTW0eNC_LpNVd-uC20R44e_efgwvhzSNQ26IaGeO9yan6f", postData) + if err != nil { + log.Fatal(err) + } + + fmt.Fprint(io.Discard, req) // Out with your request! I don't want it. + default: + http.Error(w, "Method isn't allowed!\nYou may only POST here, not " + r.Method, http.StatusMethodNotAllowed) + } + log.Println("[HTTP] " + r.RemoteAddr + " accessed /api/form with method " + r.Method) +} + func renderTestForm(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") testForm.Execute(w, map[string]string{ diff --git a/otherthings/checkenv.go b/otherthings/checkenv.go index 1a77769..9333619 100644 --- a/otherthings/checkenv.go +++ b/otherthings/checkenv.go @@ -1,4 +1,4 @@ -// Please ignore this :))))))) :( +// Please ignore my terrible code :) It works package otherthings import ( @@ -6,8 +6,10 @@ import ( "log" ) -var unused string -var ok1 bool +var ( + unused string + ok1 bool +) func CheckEnv() { unused, ok1 = os.LookupEnv("SEGFAUTILITIES_PORT") @@ -16,6 +18,17 @@ func CheckEnv() { } else { log.Fatal("[Segfautilities] Environment variable SEGFAUTILITIES_PORT is not set! Please set it to a number, for example 6893") } - + unused, ok1 = os.LookupEnv("HCAPTCHA_SITE_KEY") + if !ok1 { + log.Fatal("[Segfautilities] Environment variable HCAPTCHA_SITE_KEY is not set! Please set it to the site key you got from hCaptcha.") + } else { + log.Println("[Segfautilities] Environment variable HCAPTCHA_SITE_KEY is set as " + unused) + } + unused, ok1 = os.LookupEnv("HCAPTCHA_SECRET_KEY") + if !ok1 { + log.Fatal("[Segfautilities] Environment variable HCAPTCHA_SECRET_KEY is not set! Please set it to the secret key you got from hCaptcha.") + } else { + log.Println("[Segfautilities] Environment variable HCAPTCHA_SECRET_KEY is set!") + } log.Println("[Segfautilities] ✅ Passed the Environment Variables check") } \ No newline at end of file diff --git a/static/testform.html b/static/testform.html index 3713078..a54d3a5 100644 --- a/static/testform.html +++ b/static/testform.html @@ -8,14 +8,17 @@
- - +
+