2022-07-26 12:57:30 +02:00
package api
import (
"log"
2022-08-02 10:41:17 +02:00
"net/http"
2022-07-26 15:14:52 +02:00
2022-08-02 10:41:17 +02:00
"github.com/kataras/hcaptcha"
2022-07-26 15:14:52 +02:00
2022-07-27 19:00:48 +02:00
"fmt"
"io"
2022-08-02 10:41:17 +02:00
"net/url"
2022-07-28 11:21:25 +02:00
2022-08-14 21:06:00 +02:00
"github.com/ProjectSegfault/segfautils/config"
2022-08-03 21:37:12 +02:00
"github.com/ProjectSegfault/segfautils/utils"
2022-07-26 15:14:52 +02:00
)
var (
2022-08-14 21:06:00 +02:00
siteKey = config . HCaptchaSiteKey ( )
secretKey = config . HCaptchaSecretKey ( )
webhookURL = config . WebhookURL ( )
2022-08-02 10:41:17 +02:00
client = hcaptcha . New ( secretKey ) /* See `Client.FailureHandler` too. */
2022-07-26 12:57:30 +02:00
)
func Form ( ) {
2022-07-27 19:00:48 +02:00
http . HandleFunc ( "/api/form" , client . HandlerFunc ( theActualFormCode ) )
}
func theActualFormCode ( w http . ResponseWriter , r * http . Request ) {
2022-08-02 10:41:17 +02:00
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 . \ nYour message has not been sent . " )
postData := url . Values {
2022-08-03 21:37:12 +02:00
"content" : { "IP " + utils . GetUserIP ( r ) + "failed captcha!\nhttps://abuseipdb.com/check/" + utils . GetUserIP ( r ) } ,
2022-08-02 10:41:17 +02:00
}
req , err := http . PostForm ( webhookURL , postData )
if err != nil {
log . Fatal ( "Something went terribly wrong!" , err )
}
fmt . Fprint ( io . Discard , req ) // I don't want the result of the request in stdout
} else {
fmt . Fprintf ( w , "Thanks for your message, and thanks for doing the captcha!\nPlease ignore how different this page looks to the page you were on earlier. I'll figure it out eventually!\n%#+v" , hcaptchaResp )
postData := url . Values {
2022-08-03 21:37:12 +02:00
"content" : { "IP " + utils . GetUserIP ( r ) + "\nFrom " + r . FormValue ( "email" ) + " with feedback type " + r . FormValue ( "commentType" ) + ":\n" + "**" + r . FormValue ( "message" ) + "**\n https://abuseipdb.com/check/" + utils . GetUserIP ( r ) } ,
2022-08-02 10:41:17 +02:00
}
if r . FormValue ( "webhook" ) != "" {
fmt . Fprintf ( w , "\nThanks for trying Segfautils Contact Form :)" )
2022-07-27 19:00:48 +02:00
postData := url . Values {
2022-08-03 21:37:12 +02:00
"content" : { "**Note: you are currently testing our form example. Please check out the actual project at https://github.com/ProjectSegfault/segfautils if you found this neat! It's not hard to self-host :)**\n" + "IP " + utils . GetUserIP ( r ) + "\nFrom " + r . FormValue ( "email" ) + " with feedback type " + r . FormValue ( "commentType" ) + ":\n" + "**" + r . FormValue ( "message" ) + "**\n https://abuseipdb.com/check/" + utils . GetUserIP ( r ) } ,
2022-07-27 19:00:48 +02:00
}
2022-08-02 10:41:17 +02:00
req , err := http . PostForm ( r . FormValue ( "webhook" ) , postData )
2022-07-27 19:00:48 +02:00
if err != nil {
2022-08-02 10:41:17 +02:00
log . Println ( "Someone tried to send a webhook, but it failed!" )
2022-07-27 20:07:18 +02:00
}
2022-08-02 10:41:17 +02:00
fmt . Fprint ( io . Discard , req ) // I don't want the result of the demo request in stdout at ALL.
} else {
req , err := http . PostForm ( webhookURL , postData )
if err != nil {
log . Fatal ( "Something went terribly wrong!" , err )
2022-07-27 20:07:18 +02:00
}
2022-08-02 10:41:17 +02:00
fmt . Fprint ( io . Discard , req ) // Out with your request! I don't want it.
2022-07-27 20:07:18 +02:00
}
2022-07-26 12:57:30 +02:00
}
2022-08-02 10:41:17 +02:00
default :
http . Error ( w , "Method isn't allowed!\nYou may only POST here, not " + r . Method , http . StatusMethodNotAllowed )
}
2022-08-03 21:37:12 +02:00
log . Println ( "[HTTP] " + utils . GetUserIP ( r ) + " accessed /api/form with method " + r . Method )
2022-08-02 10:41:17 +02:00
}