Compare commits

7 Commits
Author SHA1 Message Date
da 622e3cdb87 add logging to webserver 2021-04-23 11:16:31 +02:00
da 11d830e3a7 add Dockerfile 2021-04-23 11:16:31 +02:00
da 16a5f22448 use yaml config file instead of flags 2021-04-23 11:16:31 +02:00
da 9707a60934 add question to utilGetMentioned for future refactoring 2021-04-23 11:16:31 +02:00
da 3cf21ba510 add config.yaml to .gitignore for future use 2021-04-23 11:16:31 +02:00
da cd5ff317dc remove go.sum from .gitignore 2021-04-23 11:16:31 +02:00
da e85f491d99 README.md: add thank you to text 2021-04-23 10:23:56 +02:00
7 changed files with 130 additions and 28 deletions
+10
View File
@@ -0,0 +1,10 @@
FROM golang:1.16-alpine
WORKDIR /go/src/gidbig
COPY . .
RUN go get -d -v .
RUN go install -v .
EXPOSE 8080
CMD [ "gidbig" ]
+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. Please use the issue tracker. Thank you!
+8
View File
@@ -0,0 +1,8 @@
token: ""
shard: ""
shardcount: ""
owner: ""
port: 8080
redirecturl: "https://bot.example.org:1337"
ci: 0
cs: ""
+2
View File
@@ -7,6 +7,8 @@ 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
) )
+5
View File
@@ -114,13 +114,17 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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=
@@ -370,6 +374,7 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
+41 -19
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -20,6 +19,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
humanize "github.com/dustin/go-humanize" humanize "github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
) )
var ( var (
@@ -418,6 +418,7 @@ func displayBotStats(cid string) {
discord.ChannelMessageSend(cid, buf.String()) discord.ChannelMessageSend(cid, buf.String())
} }
// what did I start here?
func utilGetMentioned(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.User { func utilGetMentioned(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.User {
for _, mention := range m.Mentions { for _, mention := range m.Mentions {
if mention.ID != s.State.Ready.User.ID { if mention.ID != s.State.Ready.User.ID {
@@ -608,33 +609,54 @@ func deleteCommandMessage(s *discordgo.Session, channelID string, messageID stri
} }
} }
type config struct {
Token string `yaml:"token"`
Shard string `yaml:"shard"`
ShardCount string `yaml:"shardcount"`
Owner string `yaml:"owner"`
Port int `yaml:"port"`
RedirectURL string `yaml:"redirecturl"`
Ci int `yaml:"ci"`
Cs string `yaml:"cs"`
}
func loadConfigFile() *config {
config := &config{}
configFile, err := os.Open("config.yaml")
if err != nil {
log.Warningln("Could not load config file.", err)
return nil
}
defer configFile.Close()
d := yaml.NewDecoder(configFile)
if err := d.Decode(&config); err != nil {
return nil
}
return config
}
func main() { func main() {
config := loadConfigFile()
var ( var (
Token = flag.String("t", "", "Discord Authentication Token") err error
Shard = flag.String("s", "", "Shard ID")
ShardCount = flag.String("c", "", "Number of shards")
Owner = flag.String("o", "", "Owner ID")
Port = flag.Int("p", 0, "Web server port")
RedirectURL = flag.String("r", "", "Address where the web server will be available without slash at the end. For example: \"http://bot.example.org:12345\"")
Ci = flag.Int("ci", 0, "ClientID")
Cs = flag.String("cs", "", "ClientSecret")
err error
) )
flag.Parse()
// create SoundCollections by scanning the audio folder // create SoundCollections by scanning the audio folder
createCollections() createCollections()
// Start Webserver if a valid port is provided and if ClientID and ClientSecret are set // Start Webserver if a valid port is provided and if ClientID and ClientSecret are set
if *Port != 0 && *Port >= 1 && *Ci != 0 && *Cs != "" && *RedirectURL != "" { if config.Port != 0 && config.Port >= 1 && config.Ci != 0 && config.Cs != "" && config.RedirectURL != "" {
log.Infoln("Starting web server on port " + strconv.Itoa(*Port)) log.Infoln("Starting web server on port " + strconv.Itoa(config.Port))
go startWebServer(strconv.Itoa(*Port), strconv.Itoa(*Ci), *Cs, *RedirectURL) go startWebServer(strconv.Itoa(config.Port), strconv.Itoa(config.Ci), config.Cs, config.RedirectURL)
} else { } else {
log.Infoln("Required web server arguments missing or invalid. Skipping web server start.") log.Infoln("Required web server arguments missing or invalid. Skipping web server start.")
} }
if *Owner != "" { if config.Owner != "" {
OWNER = *Owner OWNER = config.Owner
} }
// Preload all the sounds // Preload all the sounds
@@ -645,7 +667,7 @@ func main() {
// Create a discord session // Create a discord session
log.Info("Starting discord session...") log.Info("Starting discord session...")
discord, err = discordgo.New("Bot " + *Token) discord, err = discordgo.New("Bot " + config.Token)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"error": err, "error": err,
@@ -654,8 +676,8 @@ func main() {
} }
// Set sharding info // Set sharding info
discord.ShardID, _ = strconv.Atoi(*Shard) discord.ShardID, _ = strconv.Atoi(config.Shard)
discord.ShardCount, _ = strconv.Atoi(*ShardCount) discord.ShardCount, _ = strconv.Atoi(config.ShardCount)
if discord.ShardCount <= 0 { if discord.ShardCount <= 0 {
discord.ShardCount = 1 discord.ShardCount = 1
+63 -8
View File
@@ -4,15 +4,20 @@ 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"
) )
@@ -37,8 +42,14 @@ 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
@@ -67,8 +78,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"))))
@@ -76,6 +87,7 @@ 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")
@@ -106,13 +118,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 {
var newSoundItemRandom SoundItem newSoundItemRandom := SoundItem{
newSoundItemRandom = SoundItem{
Itemprefix: sc.Prefix, Itemprefix: sc.Prefix,
Itemcommand: "!" + sc.Prefix, Itemcommand: "!" + sc.Prefix,
Itemsoundname: "", Itemsoundname: "",
@@ -122,8 +134,7 @@ 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 {
var newSoundItem SoundItem newSoundItem := SoundItem{
newSoundItem = SoundItem{
Itemprefix: sc.Prefix, Itemprefix: sc.Prefix,
Itemcommand: "!" + sc.Prefix, Itemcommand: "!" + sc.Prefix,
Itemsoundname: snd.Name, Itemsoundname: snd.Name,
@@ -208,6 +219,7 @@ 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: "",
@@ -217,7 +229,8 @@ 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)
@@ -230,7 +243,8 @@ 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")
@@ -275,3 +289,44 @@ 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)
}
}