2022-07-26 16:27:30 +05:30
package api
import (
"net/http"
2022-07-26 18:44:52 +05:30
"html/template"
2022-07-26 16:27:30 +05:30
"log"
2022-07-26 18:44:52 +05:30
"github.com/kataras/hcaptcha"
"os"
2022-07-27 22:30:48 +05:30
"fmt"
"net/url"
"io"
2022-07-26 18:44:52 +05:30
)
var (
siteKey = os . Getenv ( "HCAPTCHA_SITE_KEY" )
secretKey = os . Getenv ( "HCAPTCHA_SECRET_KEY" )
2022-07-27 23:37:18 +05:30
webhookURL = os . Getenv ( "SEGFAUTILITIES_WEBHOOK_URL" )
2022-07-26 18:44:52 +05:30
)
var (
client = hcaptcha . New ( secretKey ) /* See `Client.FailureHandler` too. */
testForm = template . Must ( template . ParseFiles ( "./static/testform.html" ) )
2022-07-26 16:27:30 +05:30
)
func Form ( ) {
2022-07-27 22:30:48 +05:30
http . HandleFunc ( "/api/form" , client . HandlerFunc ( theActualFormCode ) )
http . HandleFunc ( "/form" , renderTestForm )
}
func theActualFormCode ( w http . ResponseWriter , r * http . Request ) {
2022-07-26 16:27:30 +05:30
switch r . Method {
case http . MethodPost :
2022-07-27 22:30:48 +05:30
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 {
2022-07-27 23:37:18 +05:30
"content" : { "IP " + r . RemoteAddr + "failed captcha! [AbuseIPDB](https://abuseipdb.com/check/" + r . RemoteAddr } ,
2022-07-27 22:30:48 +05:30
}
2022-07-27 23:37:18 +05:30
req , err := http . PostForm ( webhookURL , postData )
2022-07-27 22:30:48 +05:30
if err != nil {
2022-07-27 23:37:18 +05:30
log . Fatal ( "Something went terribly wrong!" , err )
2022-07-27 22:30:48 +05:30
}
fmt . Fprint ( io . Discard , req ) // I don't want the result of the request in stdout
2022-07-27 23:37:18 +05:30
} else {
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 ( webhookURL , postData )
if err != nil {
log . Fatal ( "Something went terribly wrong!" , err )
}
2022-07-27 22:30:48 +05:30
2022-07-27 23:37:18 +05:30
fmt . Fprint ( io . Discard , req ) // Out with your request! I don't want it.
}
2022-07-26 16:27:30 +05:30
default :
2022-07-27 22:30:48 +05:30
http . Error ( w , "Method isn't allowed!\nYou may only POST here, not " + r . Method , http . StatusMethodNotAllowed )
2022-07-26 16:27:30 +05:30
}
log . Println ( "[HTTP] " + r . RemoteAddr + " accessed /api/form with method " + r . Method )
2022-07-26 18:44:52 +05:30
}
func renderTestForm ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , "text/html; charset=utf-8" )
testForm . Execute ( w , map [ string ] string {
"SiteKey" : siteKey ,
} )
}
// testForm is only used in development. I will remove it when I've added it to the website
// Oh also, you need to add the following to your hosts file:
// 127.0.0.1 epicwebsite.com
// and visit epicwebsite.com:(yourport)/form. hCaptcha doesn't work in localhost unfortunately :(