diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 1ba5beb..c78dcdf 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -26,4 +26,4 @@ jobs: uses: docker/build-push-action@v3 with: push: true - tags: projectsegfault/segfautilities:latest \ No newline at end of file + tags: projectsegfault/segfautils:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f3e05e2..2d4d54d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +CMD ["./segfautils"] \ No newline at end of file diff --git a/api/form.go b/api/form.go index 0cce691..b96c6ce 100644 --- a/api/form.go +++ b/api/form.go @@ -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) -} \ No newline at end of file + 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) +} diff --git a/docker.env b/docker.env index 6feab05..1340b31 100644 --- a/docker.env +++ b/docker.env @@ -1,3 +1,3 @@ HCAPTCHA_SITE_KEY=YOURSITEKEY HCAPTCHA_SECRET_KEY=YOURSECRETKEY -SEGFAUTILITIES_WEBHOOK_URL=YOURWEBHOOKURL +SEGFAUTILS_WEBHOOK_URL=YOURWEBHOOKURL diff --git a/go.mod b/go.mod index 511ba6b..4e424d0 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ProjectSegfault/segfautilities +module github.com/ProjectSegfault/segfautils go 1.18 diff --git a/main.go b/main.go index a8e9431..a44e58f 100644 --- a/main.go +++ b/main.go @@ -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)) -} \ No newline at end of file + io.WriteString(w, "welcome to hell") + }) + api.Form() + log.Println("[HTTP] HTTP server is now running at " + port + "!") + log.Println(http.ListenAndServe(":"+port, nil)) +} diff --git a/otherthings/checkenv.go b/otherthings/checkenv.go index 787aecd..1d45be9 100644 --- a/otherthings/checkenv.go +++ b/otherthings/checkenv.go @@ -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") -} \ No newline at end of file + log.Println("[Segfautils] ✅ Passed the Environment Variables check") +} diff --git a/readme.md b/readme.md index 9f554ce..ce48ad0 100644 --- a/readme.md +++ b/readme.md @@ -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. ``` diff --git a/shell.nix b/shell.nix index 9597d12..797efd9 100644 --- a/shell.nix +++ b/shell.nix @@ -6,7 +6,7 @@ pkgs.mkShell { pkgs.go ]; shellHook = '' - export SEGFAUTILITIES_PORT=6893 + export SEGFAUTILS_PORT=6893 echo "Go installed, have your fun" ''; } \ No newline at end of file diff --git a/static/form.html b/static/form.html index 310c452..cb7f591 100644 --- a/static/form.html +++ b/static/form.html @@ -4,7 +4,7 @@ - Segfautilites form implementation example + Segfautils form implementation example -

Welcome to Segfautilities

-

Running at port {{.Port}} | GitHub

+

Welcome to Segfautils

+

Running at port {{.Port}} | GitHub

Click here for an example form implementation

\ No newline at end of file