2022-07-26 16:27:30 +05:30
package api
import (
2022-07-27 22:30:48 +05:30
"fmt"
"io"
2022-09-18 03:03:44 +05:30
"log"
"net/http"
2022-08-02 14:11:17 +05:30
"net/url"
2022-09-18 03:42:06 +05:30
"text/template"
2022-07-28 14:51:25 +05:30
2022-08-15 00:36:00 +05:30
"github.com/ProjectSegfault/segfautils/config"
2022-08-04 01:07:12 +05:30
"github.com/ProjectSegfault/segfautils/utils"
2022-09-18 03:03:44 +05:30
"github.com/kataras/hcaptcha"
2022-07-26 18:44:52 +05:30
)
var (
2022-08-15 00:36:00 +05:30
siteKey = config . HCaptchaSiteKey ( )
secretKey = config . HCaptchaSecretKey ( )
webhookURL = config . WebhookURL ( )
2022-08-02 14:11:17 +05:30
client = hcaptcha . New ( secretKey ) /* See `Client.FailureHandler` too. */
2022-09-18 03:03:44 +05:30
resForm = config . OptForm ( )
2022-07-26 16:27:30 +05:30
)
2022-09-18 02:48:21 +05:30
func FormCheck ( ) {
2022-09-18 03:03:44 +05:30
if resForm == "true" {
2022-09-18 03:42:06 +05:30
FormPage ( )
2022-09-18 02:48:21 +05:30
Form ( )
} else {
log . Println ( "Forms disabled" )
http . HandleFunc ( "/api/form" , func ( w http . ResponseWriter , r * http . Request ) {
io . WriteString ( w , "Disabled" )
} )
}
}
2022-09-18 03:42:06 +05:30
func FormPage ( ) {
type StaticThing struct {
HCaptchaSiteKey string
}
tmpl_form := template . Must ( template . ParseFiles ( "static/form.html" ) )
http . HandleFunc ( "/form/" , func ( w http . ResponseWriter , r * http . Request ) {
hcaptcha_site_key := config . HCaptchaSiteKey ( )
data := StaticThing {
HCaptchaSiteKey : hcaptcha_site_key ,
}
tmpl_form . Execute ( w , data )
} )
}
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 ) )
}
func theActualFormCode ( w http . ResponseWriter , r * http . Request ) {
2022-08-02 14:11:17 +05:30
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-04 01:07:12 +05:30
"content" : { "IP " + utils . GetUserIP ( r ) + "failed captcha!\nhttps://abuseipdb.com/check/" + utils . GetUserIP ( r ) } ,
2022-08-02 14:11:17 +05:30
}
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-04 01:07:12 +05:30
"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 14:11:17 +05:30
}
if r . FormValue ( "webhook" ) != "" {
fmt . Fprintf ( w , "\nThanks for trying Segfautils Contact Form :)" )
2022-07-27 22:30:48 +05:30
postData := url . Values {
2022-08-04 01:07:12 +05:30
"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 22:30:48 +05:30
}
2022-08-02 14:11:17 +05:30
req , err := http . PostForm ( r . FormValue ( "webhook" ) , postData )
2022-07-27 22:30:48 +05:30
if err != nil {
2022-08-02 14:11:17 +05:30
log . Println ( "Someone tried to send a webhook, but it failed!" )
2022-07-27 23:37:18 +05:30
}
2022-08-02 14:11:17 +05:30
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 23:37:18 +05:30
}
2022-08-02 14:11:17 +05:30
fmt . Fprint ( io . Discard , req ) // Out with your request! I don't want it.
2022-07-27 23:37:18 +05:30
}
2022-07-26 16:27:30 +05:30
}
2022-08-02 14:11:17 +05:30
default :
http . Error ( w , "Method isn't allowed!\nYou may only POST here, not " + r . Method , http . StatusMethodNotAllowed )
}
2022-08-04 01:07:12 +05:30
log . Println ( "[HTTP] " + utils . GetUserIP ( r ) + " accessed /api/form with method " + r . Method )
2022-08-02 14:11:17 +05:30
}