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
with:
push: true
tags: projectsegfault/segfautilities:latest
tags: projectsegfault/segfautils:latest

View File

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

View File

@ -1,25 +1,25 @@
package api
import (
"net/http"
"log"
"net/http"
"github.com/kataras/hcaptcha"
"github.com/kataras/hcaptcha"
"os"
"fmt"
"os"
"net/url"
"io"
"net/url"
"github.com/ProjectSegfault/segfautilities/otherthings"
"github.com/ProjectSegfault/segfautils/otherthings"
)
var (
siteKey = os.Getenv("HCAPTCHA_SITE_KEY")
secretKey = os.Getenv("HCAPTCHA_SECRET_KEY")
webhookURL = os.Getenv("SEGFAUTILITIES_WEBHOOK_URL")
client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */
siteKey = os.Getenv("HCAPTCHA_SITE_KEY")
secretKey = os.Getenv("HCAPTCHA_SECRET_KEY")
webhookURL = os.Getenv("SEGFAUTILS_WEBHOOK_URL")
client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */
)
func Form() {
@ -27,46 +27,46 @@ func Form() {
}
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.\nYour message has not been sent.")
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{
"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{
"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)
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 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.
log.Fatal("Something went terribly wrong!", 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] " + 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_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

65
main.go
View File

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

View File

@ -2,39 +2,39 @@
package otherthings
import (
"os"
"log"
"os"
)
var (
unused string
ok1 bool
ok1 bool
)
func CheckEnv() {
unused, ok1 = os.LookupEnv("SEGFAUTILITIES_PORT")
unused, ok1 = os.LookupEnv("SEGFAUTILS_PORT")
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 {
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")
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 {
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")
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 {
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" {
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 {
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
## 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 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.
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)
```
git clone https://github.com/ProjectSegfault/segfautilities
cd segfautilities/
# You need to add the environment HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY, SEGFAUTILITIES_WEBHOOK_URL and SEGFAUTILITIES_PORT.
go run main.go # Run this when you've done above!
git clone https://github.com/ProjectSegfault/segfautils
cd segfautils/
# 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, and you're planning on developing, if not, do below
go build . -o segfautils
./segfautils
```
#### NixOS
```
git clone https://github.com/ProjectSegfault/segfautilities
cd segfautilities/
git clone https://github.com/ProjectSegfault/segfautils
cd segfautils/
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!
go run main.go # I wonder if this is good practice or not. If this isn't good practice, make a GitHub issue please.
# You still need the environment variables HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY and SEGFAUTILS_WEBHOOK_URL though!
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
];
shellHook = ''
export SEGFAUTILITIES_PORT=6893
export SEGFAUTILS_PORT=6893
echo "Go installed, have your fun"
'';
}

View File

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

View File

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