Compare commits

5 Commits
Author SHA1 Message Date
da e0f183b5b7 add Dockerfile 2021-04-14 15:14:31 +02:00
da 2062819b65 use yaml config file instead of flags 2021-04-14 15:07:13 +02:00
da 00971bcca0 add question to utilGetMentioned for future refactoring 2021-04-14 15:05:44 +02:00
da ab21a9d0cf add config.yaml to .gitignore for future use 2021-04-14 13:09:39 +02:00
da ff1f5eed89 remove go.sum from .gitignore 2021-04-14 13:09:39 +02:00
4 changed files with 9 additions and 67 deletions
+1 -1
View File
@@ -22,4 +22,4 @@ Coming soon.
## Known issues ## Known issues
- `https://discordapp.com/api/oauth2` needs to be added to `brokenAuthHeaderProviders` in `golang.org/x/oauth2/internal/token.go` in order for oauth2 to work again. - `https://discordapp.com/api/oauth2` needs to be added to `brokenAuthHeaderProviders` in `golang.org/x/oauth2/internal/token.go` in order for oauth2 to work again.
Please use the issue tracker. Thank you! Please use the issue tracker.
-1
View File
@@ -7,7 +7,6 @@ require (
github.com/dustin/go-humanize v1.0.0 github.com/dustin/go-humanize v1.0.0
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/gorilla/sessions v1.2.1 github.com/gorilla/sessions v1.2.1
github.com/simplesurance/go-ip-anonymizer v0.0.0-20200429124537-35a880f8e87d
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
gopkg.in/yaml.v2 v2.2.2 gopkg.in/yaml.v2 v2.2.2
-2
View File
@@ -123,8 +123,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/simplesurance/go-ip-anonymizer v0.0.0-20200429124537-35a880f8e87d h1:4FkGkGts6gLznca6fgclIvbupwbq543mb/fFkog4VIg=
github.com/simplesurance/go-ip-anonymizer v0.0.0-20200429124537-35a880f8e87d/go.mod h1:fTTj1EOmRdtuwYw3jF/1X2dTa0N1BdbZhrpA21N/S4I=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+8 -63
View File
@@ -4,20 +4,15 @@ import (
"bufio" "bufio"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"html/template" "html/template"
"net"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"strconv"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/simplesurance/go-ip-anonymizer/ipanonymizer"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@@ -42,14 +37,8 @@ var (
TokenURL: "https://discordapp.com/api/oauth2/token", TokenURL: "https://discordapp.com/api/oauth2/token",
} }
tmpls = map[string]*template.Template{} tmpls = map[string]*template.Template{}
// TODO change secret // TODO change secret
store *sessions.CookieStore store *sessions.CookieStore
ipAnonymizer = ipanonymizer.NewWithMask(
net.CIDRMask(16, 32),
net.CIDRMask(64, 128),
)
) )
// SoundItem is used to represent a sound of our COLLECTIONS for html generation // SoundItem is used to represent a sound of our COLLECTIONS for html generation
@@ -78,8 +67,8 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", handleMain) r.HandleFunc("/", handleMain)
r.HandleFunc("/logout", handleLogout) r.HandleFunc("/logout", handleLogout)
r.HandleFunc("/discordLogin", handleDiscordLogin) r.HandleFunc("/discordLogin", handlediscordLogin)
r.HandleFunc("/discordCallback", handleDiscordCallback) r.HandleFunc("/discordCallback", handlediscordCallback)
r.HandleFunc("/playsound", handlePlaySound) r.HandleFunc("/playsound", handlePlaySound)
http.Handle("/", r) http.Handle("/", r)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
@@ -87,7 +76,6 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
} }
func handlePlaySound(w http.ResponseWriter, r *http.Request) { func handlePlaySound(w http.ResponseWriter, r *http.Request) {
log.Infoln("WebUI /playsound Request from " + r.RemoteAddr)
r.ParseForm() r.ParseForm()
sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname")) sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname"))
session, _ := store.Get(r, "gidbig-session") session, _ := store.Get(r, "gidbig-session")
@@ -118,13 +106,13 @@ func handlePlaySound(w http.ResponseWriter, r *http.Request) {
} }
func handleMain(w http.ResponseWriter, r *http.Request) { func handleMain(w http.ResponseWriter, r *http.Request) {
logWebRequests(r)
session, _ := store.Get(r, "gidbig-session") session, _ := store.Get(r, "gidbig-session")
if session.Values["discordUsername"] != nil { if session.Values["discordUsername"] != nil {
var prefixes []string var prefixes []string
var si []SoundItem var si []SoundItem
for _, sc := range COLLECTIONS { for _, sc := range COLLECTIONS {
newSoundItemRandom := SoundItem{ var newSoundItemRandom SoundItem
newSoundItemRandom = SoundItem{
Itemprefix: sc.Prefix, Itemprefix: sc.Prefix,
Itemcommand: "!" + sc.Prefix, Itemcommand: "!" + sc.Prefix,
Itemsoundname: "", Itemsoundname: "",
@@ -134,7 +122,8 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
prefixes = append(prefixes, sc.Prefix) prefixes = append(prefixes, sc.Prefix)
si = append(si, newSoundItemRandom) si = append(si, newSoundItemRandom)
for _, snd := range sc.Sounds { for _, snd := range sc.Sounds {
newSoundItem := SoundItem{ var newSoundItem SoundItem
newSoundItem = SoundItem{
Itemprefix: sc.Prefix, Itemprefix: sc.Prefix,
Itemcommand: "!" + sc.Prefix, Itemcommand: "!" + sc.Prefix,
Itemsoundname: snd.Name, Itemsoundname: snd.Name,
@@ -219,7 +208,6 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
tmpls["home.html"].ExecuteTemplate(w, "footer", map[string]interface{}{}) tmpls["home.html"].ExecuteTemplate(w, "footer", map[string]interface{}{})
} }
func handleLogout(w http.ResponseWriter, r *http.Request) { func handleLogout(w http.ResponseWriter, r *http.Request) {
log.Infoln("WebUI /logout Request from " + r.RemoteAddr)
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: "gidbig-session", Name: "gidbig-session",
Value: "", Value: "",
@@ -229,8 +217,7 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
func handleDiscordLogin(w http.ResponseWriter, r *http.Request) { func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
logWebRequests(r)
b := make([]byte, 16) b := make([]byte, 16)
rand.Read(b) rand.Read(b)
oauthStateString = base64.URLEncoding.EncodeToString(b) oauthStateString = base64.URLEncoding.EncodeToString(b)
@@ -243,8 +230,7 @@ func handleDiscordLogin(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, url, http.StatusTemporaryRedirect) http.Redirect(w, r, url, http.StatusTemporaryRedirect)
} }
func handleDiscordCallback(w http.ResponseWriter, r *http.Request) { func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
logWebRequests(r)
session, err := store.Get(r, "gidbig-session") session, err := store.Get(r, "gidbig-session")
if err != nil { if err != nil {
fmt.Fprintln(w, "aborted") fmt.Fprintln(w, "aborted")
@@ -289,44 +275,3 @@ func handleDiscordCallback(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
} }
func parseIPPort(s string) (ip net.IP, port, space string, err error) {
ip = net.ParseIP(s)
if ip == nil {
var host string
host, port, err = net.SplitHostPort(s)
if err != nil {
return
}
if port != "" {
// This check only makes sense if service names are not allowed
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
return
}
}
ip = net.ParseIP(host)
}
if ip == nil {
err = errors.New("invalid address format")
} else {
space = "IPv6"
if ip4 := ip.To4(); ip4 != nil {
space = "IPv4"
ip = ip4
}
}
return
}
func logWebRequests(r *http.Request) {
ip, port, _, err := parseIPPort(r.RemoteAddr)
if err != nil {
log.Warnln("Error parsing IP address for WebUI Request to " + r.RequestURI)
}
anonIP, err := ipAnonymizer.IPString(ip.String())
if err != nil {
log.Warnln("Could not anonymize IP address for WebUI Request to " + r.RequestURI)
} else {
log.Infoln("WebUI Request to " + r.RequestURI + " from " + anonIP + port)
}
}