Rename to Segfautils

Signed-off-by: Odyssey346 <odyssey346@disroot.org>
This commit is contained in:
Odyssey346 2022-08-02 10:41:17 +02:00
parent 68e2c6475e
commit 2f6fbed9c0
11 changed files with 122 additions and 118 deletions

View File

@ -26,4 +26,4 @@ jobs:
uses: docker/build-push-action@v3 uses: docker/build-push-action@v3
with: with:
push: true push: true
tags: projectsegfault/segfautilities:latest tags: projectsegfault/segfautils:latest

View File

@ -1,15 +1,15 @@
FROM golang:1.18-alpine3.16 FROM golang:1.18-alpine3.16
ENV SEGFAUTILITIES_PORT 6893 ENV SEGFAUTILS_PORT 6893
RUN mkdir /segfautilities RUN mkdir /segfautils
WORKDIR /segfautilities WORKDIR /segfautils
COPY . /segfautilities/ COPY . /segfautils/
RUN go mod download RUN go mod download
EXPOSE 6893 EXPOSE 6893
RUN go build -o segfautilities RUN go build -o segfautils
RUN chmod +x segfautilities RUN chmod +x segfautils
RUN go clean -modcache RUN go clean -modcache
CMD ["./segfautilities"] CMD ["./segfautils"]

View File

@ -1,25 +1,25 @@
package api package api
import ( import (
"net/http"
"log" "log"
"net/http"
"github.com/kataras/hcaptcha" "github.com/kataras/hcaptcha"
"os"
"fmt" "fmt"
"os"
"net/url"
"io" "io"
"net/url"
"github.com/ProjectSegfault/segfautilities/otherthings" "github.com/ProjectSegfault/segfautils/otherthings"
) )
var ( var (
siteKey = os.Getenv("HCAPTCHA_SITE_KEY") siteKey = os.Getenv("HCAPTCHA_SITE_KEY")
secretKey = os.Getenv("HCAPTCHA_SECRET_KEY") secretKey = os.Getenv("HCAPTCHA_SECRET_KEY")
webhookURL = os.Getenv("SEGFAUTILITIES_WEBHOOK_URL") webhookURL = os.Getenv("SEGFAUTILS_WEBHOOK_URL")
client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */ client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */
) )
func Form() { func Form() {
@ -27,46 +27,46 @@ func Form() {
} }
func theActualFormCode(w http.ResponseWriter, r *http.Request) { func theActualFormCode(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case http.MethodPost: case http.MethodPost:
hcaptchaResp, ok := hcaptcha.Get(r) hcaptchaResp, ok := hcaptcha.Get(r)
if !ok { if !ok {
w.WriteHeader(http.StatusUnauthorized) 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.") 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{
"content": {"IP " + otherthings.GetUserIP(r) + "failed captcha!\nhttps://abuseipdb.com/check/" + otherthings.GetUserIP(r)},
}
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{
"content": {"IP " + otherthings.GetUserIP(r) + "\nFrom " + r.FormValue("email") + " with feedback type " + r.FormValue("commentType") + ":\n" + "**" + r.FormValue("message") + "**\n https://abuseipdb.com/check/" + otherthings.GetUserIP(r)},
}
if r.FormValue("webhook") != "" {
fmt.Fprintf(w, "\nThanks for trying Segfautils Contact Form :)")
postData := url.Values{ postData := url.Values{
"content": {"IP " + otherthings.GetUserIP(r) + "failed captcha!\nhttps://abuseipdb.com/check/" + otherthings.GetUserIP(r)}, "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 " + otherthings.GetUserIP(r) + "\nFrom " + r.FormValue("email") + " with feedback type " + r.FormValue("commentType") + ":\n" + "**" + r.FormValue("message") + "**\n https://abuseipdb.com/check/" + otherthings.GetUserIP(r)},
} }
req, err := http.PostForm(r.FormValue("webhook"), postData)
if err != nil {
log.Println("Someone tried to send a webhook, but it failed!")
}
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) req, err := http.PostForm(webhookURL, postData)
if err != nil { if err != nil {
log.Fatal("Something went terribly wrong!", err) 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{
"content": {"IP " + otherthings.GetUserIP(r) + "\nFrom " + r.FormValue("email") + " with feedback type " + r.FormValue("commentType") + ":\n" + "**" + r.FormValue("message") + "**\n https://abuseipdb.com/check/" + otherthings.GetUserIP(r)},
}
if r.FormValue("webhook") != "" {
fmt.Fprintf(w, "\nThanks for trying Segfautilities Contact Form :)")
postData := url.Values{
"content": {"**Note: you are currently testing our form example. Please check out the actual project at https://github.com/ProjectSegfault/segfautilities! It's not hard to self-host :)**\n" + "IP " + otherthings.GetUserIP(r) + "\nFrom " + r.FormValue("email") + " with feedback type " + r.FormValue("commentType") + ":\n" + "**" + r.FormValue("message") + "**\n https://abuseipdb.com/check/" + otherthings.GetUserIP(r)},
}
req, err := http.PostForm(r.FormValue("webhook"), postData)
if err != nil {
log.Println("Someone tried to send a webhook, but it failed!")
}
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)
}
fmt.Fprint(io.Discard, req) // Out with your request! I don't want it.
} }
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] " + otherthings.GetUserIP(r) + " accessed /api/form with method " + r.Method) default:
} http.Error(w, "Method isn't allowed!\nYou may only POST here, not "+r.Method, http.StatusMethodNotAllowed)
}
log.Println("[HTTP] " + otherthings.GetUserIP(r) + " accessed /api/form with method " + r.Method)
}

View File

@ -1,3 +1,3 @@
HCAPTCHA_SITE_KEY=YOURSITEKEY HCAPTCHA_SITE_KEY=YOURSITEKEY
HCAPTCHA_SECRET_KEY=YOURSECRETKEY HCAPTCHA_SECRET_KEY=YOURSECRETKEY
SEGFAUTILITIES_WEBHOOK_URL=YOURWEBHOOKURL SEGFAUTILS_WEBHOOK_URL=YOURWEBHOOKURL

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/ProjectSegfault/segfautilities module github.com/ProjectSegfault/segfautils
go 1.18 go 1.18

65
main.go
View File

@ -1,49 +1,50 @@
package main package main
import ( import (
"net/http" "html/template"
"html/template" "io"
"io" "log"
"log" "net/http"
"github.com/ProjectSegfault/segfautilities/otherthings" "os"
"os"
"github.com/ProjectSegfault/segfautilities/api" "github.com/ProjectSegfault/segfautils/api"
"github.com/ProjectSegfault/segfautils/otherthings"
) )
type StaticThingy struct { type StaticThingy struct {
Port string Port string
HCaptchaSiteKey string HCaptchaSiteKey string
} }
var port string var port string
var shit bool var shit bool
func main() { func main() {
log.Println("[Segfautilities] Starting") log.Println("[Segfautils] Starting")
otherthings.CheckEnv() otherthings.CheckEnv()
log.Println("[HTTP] Starting server") log.Println("[HTTP] Starting server")
port := os.Getenv("SEGFAUTILITIES_PORT") port := os.Getenv("SEGFAUTILS_PORT")
hcaptcha_site_key := os.Getenv("HCAPTCHA_SITE_KEY") hcaptcha_site_key := os.Getenv("HCAPTCHA_SITE_KEY")
tmpl := template.Must(template.ParseFiles("static/index.html")) tmpl := template.Must(template.ParseFiles("static/index.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := StaticThingy{ data := StaticThingy{
Port: port, Port: port,
} }
tmpl.Execute(w, data) tmpl.Execute(w, data)
}) })
tmpl_form := template.Must(template.ParseFiles("static/form.html")) tmpl_form := template.Must(template.ParseFiles("static/form.html"))
http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) {
data := StaticThingy{ data := StaticThingy{
HCaptchaSiteKey: hcaptcha_site_key, HCaptchaSiteKey: hcaptcha_site_key,
} }
tmpl_form.Execute(w, data) tmpl_form.Execute(w, data)
}) })
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "welcome to hell") io.WriteString(w, "welcome to hell")
}) })
api.Form() api.Form()
log.Println("[HTTP] HTTP server is now running at " + port + "!") log.Println("[HTTP] HTTP server is now running at " + port + "!")
log.Println(http.ListenAndServe(":" + port, nil)) log.Println(http.ListenAndServe(":"+port, nil))
} }

View File

@ -2,39 +2,39 @@
package otherthings package otherthings
import ( import (
"os"
"log" "log"
"os"
) )
var ( var (
unused string unused string
ok1 bool ok1 bool
) )
func CheckEnv() { func CheckEnv() {
unused, ok1 = os.LookupEnv("SEGFAUTILITIES_PORT") unused, ok1 = os.LookupEnv("SEGFAUTILS_PORT")
if ok1 { if ok1 {
log.Println("[Segfautilities] Environment variable SEGFAUTILITIES_PORT is set as " + unused) log.Println("[Segfautils] Environment variable SEGFAUTILS_PORT is set as " + unused)
} else { } else {
log.Fatal("[Segfautilities] Environment variable SEGFAUTILITIES_PORT is not set! Please set it to a number, for example 6893") log.Fatal("[Segfautils] Environment variable SEGFAUTILS_PORT is not set! Please set it to a number, for example 6893")
} }
unused, ok1 = os.LookupEnv("HCAPTCHA_SITE_KEY") unused, ok1 = os.LookupEnv("HCAPTCHA_SITE_KEY")
if !ok1 || unused == "YOURSITEKEY" { if !ok1 || unused == "YOURSITEKEY" {
log.Fatal("[Segfautilities] Environment variable HCAPTCHA_SITE_KEY is not set! Please set it to the site key you got from hCaptcha.") log.Fatal("[Segfautils] Environment variable HCAPTCHA_SITE_KEY is not set! Please set it to the site key you got from hCaptcha.")
} else { } else {
log.Println("[Segfautilities] Environment variable HCAPTCHA_SITE_KEY is set as " + unused) log.Println("[Segfautils] Environment variable HCAPTCHA_SITE_KEY is set as " + unused)
} }
unused, ok1 = os.LookupEnv("HCAPTCHA_SECRET_KEY") unused, ok1 = os.LookupEnv("HCAPTCHA_SECRET_KEY")
if !ok1 || unused == "YOURSECRETKEY" { if !ok1 || unused == "YOURSECRETKEY" {
log.Fatal("[Segfautilities] Environment variable HCAPTCHA_SECRET_KEY is not set! Please set it to the secret key you got from hCaptcha.") log.Fatal("[Segfautils] Environment variable HCAPTCHA_SECRET_KEY is not set! Please set it to the secret key you got from hCaptcha.")
} else { } else {
log.Println("[Segfautilities] Environment variable HCAPTCHA_SECRET_KEY is set!") log.Println("[Segfautils] Environment variable HCAPTCHA_SECRET_KEY is set!")
} }
unused, ok1 = os.LookupEnv("SEGFAUTILITIES_WEBHOOK_URL") unused, ok1 = os.LookupEnv("SEGFAUTILS_WEBHOOK_URL")
if !ok1 || unused == "YOURWEBHOOKURL" { if !ok1 || unused == "YOURWEBHOOKURL" {
log.Fatal("[Segfautilities] Environment variable SEGFAUTILITIES_WEBHOOK_URL is not set! Please set it to your webhook URL. If that URL doesn't work, make an issue on GitHub!") log.Fatal("[Segfautils] Environment variable SEGFAUTILS_WEBHOOK_URL is not set! Please set it to your webhook URL. If that URL doesn't work, make an issue on GitHub!")
} else { } else {
log.Println("[Segfautilities] Environment variable SEGFAUTILITIES_WEBHOOK_URL is set!") log.Println("[Segfautils] Environment variable SEGFAUTILS_WEBHOOK_URL is set!")
} }
log.Println("[Segfautilities] ✅ Passed the Environment Variables check") log.Println("[Segfautils] ✅ Passed the Environment Variables check")
} }

View File

@ -1,4 +1,4 @@
# Segfautilities # Segfautils
Web utilities for Project Segfault Web utilities for Project Segfault
## What does it do? ## What does it do?
@ -8,27 +8,30 @@ For now it powers our contact form. In the future we will expand our APIs so you
### Docker: ### Docker:
``` ```
docker run -d --restart=always -p 6893:6893 --name segfautilities projectsegfault/segfautilities:latest --env-file ./docker.env docker run -d --restart=always -p 6893:6893 --name segfautils projectsegfault/segfautils:latest --env-file ./docker.env
``` ```
docker.env should be the environment file located in this repository, customized to your settings. The env file is self-documenting so I don't need to go in any detail here. docker.env should be the environment file located in this repository, customized to your settings. The env file is self-documenting so I don't need to go in any detail here.
We recommend using Docker as it provides better security (we suck in security, so that's why) and we are constantly updating Segfautilities. Docker makes it easy to update the program. We recommend using Docker as it provides better security (we suck in security, so that's why) and we are constantly updating Segfautils. Docker makes it easy to update the program.
If you're using Portainer, you should know how to add Segfautilities. If you're using Portainer, you should know how to add Segfautils.
### Manual (recommended for development) ### Manual (recommended for development)
``` ```
git clone https://github.com/ProjectSegfault/segfautilities git clone https://github.com/ProjectSegfault/segfautils
cd segfautilities/ cd segfautils/
# You need to add the environment HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY, SEGFAUTILITIES_WEBHOOK_URL and SEGFAUTILITIES_PORT. # You need to add the environment HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY, SEGFAUTILS_WEBHOOK_URL and SEGFAUTILS_PORT.
go run main.go # Run this when you've done above! go run main.go # Run this when you've done above, and you're planning on developing, if not, do below
go build . -o segfautils
./segfautils
``` ```
#### NixOS #### NixOS
``` ```
git clone https://github.com/ProjectSegfault/segfautilities git clone https://github.com/ProjectSegfault/segfautils
cd segfautilities/ cd segfautils/
nix-shell # Avoid installing Go and setting up the web port, by just running a nix shell nix-shell # Avoid installing Go and setting up the web port, by just running a nix shell
# You still need the environment variables HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY and SEGFAUTILITIES_WEBHOOK_URL though! # You still need the environment variables HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY and SEGFAUTILS_WEBHOOK_URL though!
go run main.go # I wonder if this is good practice or not. If this isn't good practice, make a GitHub issue please. go run main.go # If you're developing
go build . -o segfautils && ./segfautils # If you're intending to use Segfautils for production.
``` ```

View File

@ -6,7 +6,7 @@ pkgs.mkShell {
pkgs.go pkgs.go
]; ];
shellHook = '' shellHook = ''
export SEGFAUTILITIES_PORT=6893 export SEGFAUTILS_PORT=6893
echo "Go installed, have your fun" echo "Go installed, have your fun"
''; '';
} }

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Segfautilites form implementation example</title> <title>Segfautils form implementation example</title>
<style> <style>
body { body {
background-color: #252525; background-color: #252525;

View File

@ -3,7 +3,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Segfautilites</title> <title>Segfautils</title>
<style> <style>
body { body {
background-color: #252525; background-color: #252525;
@ -21,7 +21,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>Welcome to Segfautilities</h1> <h1>Welcome to Segfautils</h1>
<h3>Running at port {{.Port}} | <a href="https://github.com/ProjectSegfault/segfautilities/" target="_blank">GitHub</a></h3> <h3>Running at port {{.Port}} | <a href="https://github.com/ProjectSegfault/segfautils/" target="_blank">GitHub</a></h3>
<h3><a href="/form">Click here for an example form implementation</a></h3> <h3><a href="/form">Click here for an example form implementation</a></h3>
</body> </body>