initial announcements
Signed-off-by: Odyssey346 <odyssey346@disroot.org>
This commit is contained in:
111
api/announcements.go
Normal file
111
api/announcements.go
Normal file
@ -0,0 +1,111 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
var (
|
||||
authToken = os.Getenv("SEGFAUTILS_AUTHTOKEN")
|
||||
)
|
||||
|
||||
func Announcements() {
|
||||
http.HandleFunc("/api/announcements", getAnnouncements)
|
||||
http.HandleFunc("/api/announcements/post", handleAnnouncements)
|
||||
http.HandleFunc("/api/announcements/delete", handleAnnouncementDeleteRequest)
|
||||
}
|
||||
|
||||
func handleAnnouncements(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if r.FormValue("token") != authToken {
|
||||
http.Error(w, "You need to provide the authorization token given to you by your system administrator in order to post an announcement.", http.StatusUnauthorized)
|
||||
return
|
||||
} else {
|
||||
if r.FormValue("title") == "" || r.FormValue("link") == "" || r.FormValue("severity") == "" {
|
||||
http.Error(w, "Your request is not proper. Please add a title, link, and severity.", http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
now := time.Now()
|
||||
feed := &feeds.Feed{
|
||||
Title: r.FormValue("title"),
|
||||
Link: &feeds.Link{Href: r.FormValue("link")},
|
||||
Description: r.FormValue("severity"),
|
||||
Created: now,
|
||||
}
|
||||
|
||||
json, err := feed.ToJSON()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
f, err := os.Create("./static/announcements.json")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
_, err2 := f.WriteString(json)
|
||||
|
||||
if err2 != nil {
|
||||
log.Fatal(err2)
|
||||
}
|
||||
|
||||
w.Write([]byte("Announcement posted!"))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func handleAnnouncementDeleteRequest(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if r.FormValue("token") != authToken {
|
||||
http.Error(w, "You need to provide the authorization token given to you by your system administrator in order to delete an announcement.", http.StatusUnauthorized)
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := os.Stat("./static/announcements.json"); errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, "If you're gonna delete the annoucement, there has to be an announcement in the first place.", http.StatusNotFound)
|
||||
return
|
||||
} else {
|
||||
err := os.Remove("./static/announcements.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
w.Write([]byte("Announcement deleted!"))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getAnnouncements(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat("./static/announcements.json"); errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, "There are no announcements.", http.StatusNotFound)
|
||||
return
|
||||
} else {
|
||||
f, err := os.Open("./static/announcements.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
io.Copy(w, f)
|
||||
}
|
||||
}
|
10
api/form.go
10
api/form.go
@ -12,7 +12,7 @@ import (
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"github.com/ProjectSegfault/segfautils/otherthings"
|
||||
"github.com/ProjectSegfault/segfautils/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -34,7 +34,7 @@ func theActualFormCode(w http.ResponseWriter, r *http.Request) {
|
||||
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)},
|
||||
"content": {"IP " + utils.GetUserIP(r) + "failed captcha!\nhttps://abuseipdb.com/check/" + utils.GetUserIP(r)},
|
||||
}
|
||||
req, err := http.PostForm(webhookURL, postData)
|
||||
if err != nil {
|
||||
@ -45,12 +45,12 @@ func theActualFormCode(w http.ResponseWriter, r *http.Request) {
|
||||
} 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)},
|
||||
"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)},
|
||||
}
|
||||
if r.FormValue("webhook") != "" {
|
||||
fmt.Fprintf(w, "\nThanks for trying Segfautils 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/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)},
|
||||
"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)},
|
||||
}
|
||||
req, err := http.PostForm(r.FormValue("webhook"), postData)
|
||||
if err != nil {
|
||||
@ -68,5 +68,5 @@ func theActualFormCode(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
log.Println("[HTTP] " + utils.GetUserIP(r) + " accessed /api/form with method " + r.Method)
|
||||
}
|
||||
|
Reference in New Issue
Block a user