From 82149cda0c4a992b891f7efe8216712e602c1b49 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Fri, 22 Mar 2019 21:39:55 -0400 Subject: [PATCH 1/9] Start adding room access restrictions So far only PIN and Open modes are implemented. It uses a session cookie to store the validity of the pin/password. The "Enter pin" page has some unreadable messages on it right now, but it kinda works. --- chatcommands.go | 69 ++++++++++++++++++++++++ common/chatcommands.go | 7 ++- handlers.go | 118 +++++++++++++++++++++++++++++++++++++++++ main.go | 3 ++ settings.go | 56 ++++++++++++++++++- static/thedoor.html | 13 +++++ 6 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 static/thedoor.html diff --git a/chatcommands.go b/chatcommands.go index 1f86f41..f043355 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -241,6 +241,19 @@ var commands = &CommandControl{ return "" }, }, + + common.CNPin.String(): Command{ + HelpText: "Display the current room access type and pin/password (if applicable).", + Function: func(cl *Client, args []string) string { + switch settings.RoomAccess { + case AccessPin: + return "Room is secured via PIN. Current PIN: " + settings.RoomAccessPin + case AccessRequest: + return "Room is secured via access requests. Users must request to be granted access." + } + return "Room is open access. Anybody can join." + }, + }, }, admin: map[string]Command{ @@ -291,6 +304,62 @@ var commands = &CommandControl{ return "Single use password: " + password }, }, + + common.CNNewPin.String(): Command{ + HelpText: "Generate a room acces new pin", + Function: func(cl *Client, args []string) string { + if settings.RoomAccess != AccessPin { + return "Room is not restricted by Pin. (" + string(settings.RoomAccess) + ")" + } + + pin, err := settings.generateNewPin() + if err != nil { + return "Unable to generate new pin: " + err.Error() + } + + fmt.Println("New room access pin: ", pin) + return "New access pin: " + pin + }, + }, + + common.CNRoomAccess.String(): Command{ + HelpText: "Change the room access type.", + Function: func(cl *Client, args []string) string { + // Print current access type if no arguments given + if len(args) == 0 { + return "Current room access type: " + string(settings.RoomAccess) + } + + switch AccessMode(strings.ToLower(args[0])) { + case AccessOpen: + settings.RoomAccess = AccessOpen + return "Room access set to open" + + case AccessPin: + // A pin/password was provided, use it. + if len(args) == 2 { + settings.RoomAccessPin = args[1] + + // A pin/password was not provided, generate a new one. + } else { + _, err := settings.generateNewPin() + if err != nil { + fmt.Println("Error generating new access pin: ", err.Error()) + return "Unable to generate a new pin, access unchanged: " + err.Error() + } + } + settings.RoomAccess = AccessPin + return "Room access set to Pin: " + settings.RoomAccessPin + + case AccessRequest: + settings.RoomAccess = AccessRequest + return "Room access set to request. WARNING: this isn't implemented yet." + + default: + return "Invalid access mode" + } + }, + }, }, } diff --git a/common/chatcommands.go b/common/chatcommands.go index c44f689..d928b43 100644 --- a/common/chatcommands.go +++ b/common/chatcommands.go @@ -29,20 +29,23 @@ var ( CNBan ChatCommandNames = []string{"ban"} CNUnban ChatCommandNames = []string{"unban"} CNPurge ChatCommandNames = []string{"purge"} + CNPin ChatCommandNames = []string{"pin", "password"} // Admin Commands CNMod ChatCommandNames = []string{"mod"} CNReloadPlayer ChatCommandNames = []string{"reloadplayer"} CNReloadEmotes ChatCommandNames = []string{"reloademotes"} CNModpass ChatCommandNames = []string{"modpass"} + CNNewPin ChatCommandNames = []string{"newpin", "newpassword"} + CNRoomAccess ChatCommandNames = []string{"changeaccess", "hodor"} ) var ChatCommands = []ChatCommandNames{ // User CNMe, CNHelp, CNCount, CNColor, CNWhoAmI, CNAuth, CNUsers, CNNick, // Mod - CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, + CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin, // Admin - CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, + CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, } func GetFullChatCommand(c string) string { diff --git a/handlers.go b/handlers.go index 2589c0a..ae9aa93 100644 --- a/handlers.go +++ b/handlers.go @@ -153,6 +153,94 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { }() } +// returns if it's OK to proceed +func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool { + session, err := sstore.Get(r, "moviesession") + if err != nil { + fmt.Printf("Unable to get session for client %s: %v\n", r.RemoteAddr, err) + http.Error(w, "Unable to get session data", http.StatusInternalServerError) + return false + } + + if settings.RoomAccess == AccessPin { + pin := session.Values["pin"] + // No pin found in session + if pin == nil || len(pin.(string)) == 0 { + if r.Method == "POST" { + // Check for correct pin + err = r.ParseForm() + if err != nil { + fmt.Printf("Error parsing form") + http.Error(w, "Unable to get session data", http.StatusInternalServerError) + } + + postPin := r.Form.Get("txtInput") + fmt.Printf("Received pin: %s\n", postPin) + if postPin == settings.RoomAccessPin { + // Pin is correct. Save it to session and return true. + session.Values["pin"] = settings.RoomAccessPin + session.Save(r, w) + return true + } + // Pin is incorrect. + handlePinTemplate(w, r, "Incorrect PIN") + return false + } + // nope. display pin entry and return + handlePinTemplate(w, r, "") + return false + } + + // Pin found in session, but it has changed since last time. + if pin.(string) != settings.RoomAccessPin { + // Clear out the old pin. + session.Values["pin"] = nil + session.Save(r, w) + + // Prompt for new one. + handlePinTemplate(w, r, "Pin has changed. Enter new PIN.") + return false + } + + // Correct pin found in session + return true + } + + // TODO: this. + if settings.RoomAccess == AccessRequest { + http.Error(w, "Requesting access not implemented yet", http.StatusNotImplemented) + return false + } + + // Room is open. + return true +} + +func handlePinTemplate(w http.ResponseWriter, r *http.Request, errorMessage string) { + t, err := template.ParseFiles("./static/base.html", "./static/thedoor.html") + if err != nil { + fmt.Printf("Error parsing template file: %v", err) + return + } + + type Data struct { + Title string + SubmitText string + Error string + } + + data := Data{ + Title: "Enter Pin", + SubmitText: "Submit Pin", + Error: errorMessage, + } + + err = t.Execute(w, data) + if err != nil { + fmt.Printf("Error executing file, %v", err) + } +} + func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("./static/base.html", "./static/help.html") if err != nil { @@ -186,7 +274,37 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { } } +func handlePin(w http.ResponseWriter, r *http.Request) { + session, err := sstore.Get(r, "moviesession") + if err != nil { + fmt.Printf("Unable to get session: %v\n", err) + } + + val := session.Values["pin"] + if val == nil { + session.Values["pin"] = "1234" + err := session.Save(r, w) + if err != nil { + fmt.Fprintf(w, "unable to save session: %v", err) + } + fmt.Fprint(w, "Pin was not set") + fmt.Println("pin was not set") + } else { + fmt.Fprintf(w, "pin set: %v", val) + fmt.Printf("pin is set: %v\n", val) + } +} + func handleIndexTemplate(w http.ResponseWriter, r *http.Request) { + fmt.Printf("RoomAcces: %s\n", settings.RoomAccess) + if settings.RoomAccess != AccessOpen { + if !checkRoomAccess(w, r) { + fmt.Println("Denied access") + return + } + fmt.Println("Granted access") + } + t, err := template.ParseFiles("./static/base.html", "./static/main.html") if err != nil { fmt.Printf("Error parsing template file, %v\n", err) diff --git a/main.go b/main.go index 42c260f..121d75b 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,8 @@ func main() { fmt.Println("Stream key: ", settings.GetStreamKey()) fmt.Println("Admin password: ", settings.AdminPassword) + fmt.Println("RoomAccess: ", settings.RoomAccess) + fmt.Println("RoomAccessPin: ", settings.RoomAccessPin) fmt.Println("Listen and serve ", addr) go startServer() @@ -82,6 +84,7 @@ func startServer() { http.HandleFunc("/chat", handleIndexTemplate) http.HandleFunc("/video", handleIndexTemplate) http.HandleFunc("/help", handleHelpTemplate) + http.HandleFunc("/pin", handlePin) http.HandleFunc("/", handleDefault) diff --git a/settings.go b/settings.go index 7fa4626..28f6926 100644 --- a/settings.go +++ b/settings.go @@ -6,13 +6,17 @@ import ( "fmt" "io/ioutil" "math/big" + "net/http" "strings" "sync" "time" + + "github.com/gorilla/sessions" ) var settings *Settings var settingsMtx sync.Mutex +var sstore *sessions.CookieStore type Settings struct { // Non-Saved settings @@ -24,11 +28,22 @@ type Settings struct { MaxMessageCount int TitleLength int // maximum length of the title that can be set with the /playing AdminPassword string + Bans []BanInfo StreamKey string ListenAddress string - Bans []BanInfo + SessionKey string // key for session data + RoomAccess AccessMode + RoomAccessPin string // auto generate this, } +type AccessMode string + +const ( + AccessOpen AccessMode = "open" + AccessPin AccessMode = "pin" + AccessRequest AccessMode = "request" +) + type BanInfo struct { IP string Names []string @@ -49,6 +64,36 @@ func init() { settings.TitleLength = 50 } + // Is this a good way to do this? Probably not... + if len(settings.SessionKey) == 0 { + out := "" + large := big.NewInt(int64(1 << 60)) + large = large.Add(large, large) + for len(out) < 50 { + num, err := rand.Int(rand.Reader, large) + if err != nil { + panic("Error generating session key: " + err.Error()) + } + out = fmt.Sprintf("%s%X", out, num) + } + settings.SessionKey = out + } + + if len(settings.RoomAccess) == 0 { + settings.RoomAccess = AccessOpen + } + + if settings.RoomAccess != AccessOpen && len(settings.RoomAccessPin) == 0 { + settings.RoomAccessPin = "1234" + } + + sstore = sessions.NewCookieStore([]byte(settings.SessionKey)) + sstore.Options = &sessions.Options{ + Path: "/", + MaxAge: 60 * 60 * 24, // one day + SameSite: http.SameSiteStrictMode, + } + // Save admin password to file if err = settings.Save(); err != nil { panic("Unable to save settings: " + err.Error()) @@ -174,3 +219,12 @@ func (s *Settings) GetStreamKey() string { } return s.StreamKey } + +func (s *Settings) generateNewPin() (string, error) { + num, err := rand.Int(rand.Reader, big.NewInt(int64(9999))) + if err != nil { + return "", err + } + settings.RoomAccessPin = fmt.Sprintf("%04d", num) + return settings.RoomAccessPin, nil +} diff --git a/static/thedoor.html b/static/thedoor.html new file mode 100644 index 0000000..9051a8a --- /dev/null +++ b/static/thedoor.html @@ -0,0 +1,13 @@ +{{define "header"}} + +{{end}} + +{{define "body"}} +
+ {{if .Error}}
{{.Error}}
{{end}} +
+
+ +
+
+{{end}} From 970321b92af1580672f5a2be1212b59c1598ac0a Mon Sep 17 00:00:00 2001 From: joeyak Date: Fri, 22 Mar 2019 22:38:58 -0400 Subject: [PATCH 2/9] Adding example template of access requests and css --- static/css/site.css | 23 +++++++++++++++++++++++ static/main.html | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/static/css/site.css b/static/css/site.css index b2de941..7d17dac 100644 --- a/static/css/site.css +++ b/static/css/site.css @@ -111,6 +111,25 @@ span.svmsg { color: #B1B1B1; } +.access-grant { + font-weight: normal; + color: greenyellow; +} + +.access-ignore { + font-weight: normal; + color: red; +} + +.access-name { + color: #f7b11b; +} + +.access-title { + color: #e5e0e5; + font-weight: bold; +} + #videoElement { position: relative; top: 50%; @@ -151,6 +170,10 @@ span.svmsg { text-align: center; } +#accessRequest div { + margin-bottom: 5px; +} + #playing { color: #288a85; font-size: x-Large; diff --git a/static/main.html b/static/main.html index 01d7e49..85d7ef6 100644 --- a/static/main.html +++ b/static/main.html @@ -58,6 +58,24 @@ {{ end }}
+
+
Grant Access?
+
+ + + User1 +
+
+ + + User2 +
+
+ + + User3 +
+
From 5c1fbe2a0b03dc86b7de1588c251fafb3e4be823 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 23 Mar 2019 13:30:04 -0400 Subject: [PATCH 3/9] Write correct IP in WriteData() error Return the correct IP address on error during chatConnection.WirteData(). If the server is behind a reverse proxy, the connection object will have "127.0.0.1" as the remote address. --- connection.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 82ac88c..7e1e19e 100644 --- a/connection.go +++ b/connection.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net" "sync" @@ -24,7 +25,11 @@ func (cc *chatConnection) WriteData(data interface{}) error { defer cc.mutex.Unlock() cc.mutex.Lock() stats.msgOutInc() - return cc.WriteJSON(data) + err := cc.WriteJSON(data) + if err != nil { + return fmt.Errorf("Error writing data to %s: %v", cc.Host(), err) + } + return nil } func (cc *chatConnection) Host() string { From 44e8947329b44ce82245eb97ffad3e0b00b4b000 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 23 Mar 2019 18:08:22 -0400 Subject: [PATCH 4/9] Print the new modes and pins to the console - When the access mode changes, print the new mode (and pin, if applicable) to the server console. - Don't return an HTTP error when getting a session fails, just make a new session. - Save pins to the settings --- chatcommands.go | 3 +++ handlers.go | 3 +-- settings.go | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/chatcommands.go b/chatcommands.go index b4b5927..e938d92 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -335,6 +335,7 @@ var commands = &CommandControl{ switch AccessMode(strings.ToLower(args[0])) { case AccessOpen: settings.RoomAccess = AccessOpen + fmt.Println("[access] Room set to open") return "Room access set to open" case AccessPin: @@ -351,10 +352,12 @@ var commands = &CommandControl{ } } settings.RoomAccess = AccessPin + fmt.Println("[access] Room set to pin: " + settings.RoomAccessPin) return "Room access set to Pin: " + settings.RoomAccessPin case AccessRequest: settings.RoomAccess = AccessRequest + fmt.Println("[access] Room set to request") return "Room access set to request. WARNING: this isn't implemented yet." default: diff --git a/handlers.go b/handlers.go index ae9aa93..f8bf85e 100644 --- a/handlers.go +++ b/handlers.go @@ -157,9 +157,8 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool { session, err := sstore.Get(r, "moviesession") if err != nil { + // Don't return as server error here, just make a new session. fmt.Printf("Unable to get session for client %s: %v\n", r.RemoteAddr, err) - http.Error(w, "Unable to get session data", http.StatusInternalServerError) - return false } if settings.RoomAccess == AccessPin { diff --git a/settings.go b/settings.go index 28f6926..22a7d6d 100644 --- a/settings.go +++ b/settings.go @@ -226,5 +226,8 @@ func (s *Settings) generateNewPin() (string, error) { return "", err } settings.RoomAccessPin = fmt.Sprintf("%04d", num) + if err = s.Save(); err != nil { + return "", err + } return settings.RoomAccessPin, nil } From 8e27ce93321d5a641486bd89b22e748990b4ee41 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Tue, 26 Mar 2019 11:58:46 -0400 Subject: [PATCH 5/9] Add /iplist to admin commands Lists all clients' UUID's, names, and IPs to console. # Conflicts: # chatcommands.go # common/chatcommands.go --- chatcommands.go | 18 ++++++++++++++++++ common/chatcommands.go | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/chatcommands.go b/chatcommands.go index e938d92..7ff728c 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -365,6 +365,24 @@ var commands = &CommandControl{ } }, }, + + common.CNIP.String(): Command{ + HelpText: "list users and IP in the server console", + Function: func(cl *Client, args []string) string { + cl.belongsTo.clientsMtx.Lock() + fmt.Println("Clients:") + for uuid, client := range cl.belongsTo.clients { + fmt.Printf(" [%s] %s %s\n", uuid, client.name, client.conn.Host()) + } + + fmt.Println("TmpConn:") + for uuid, conn := range cl.belongsTo.tempConn { + fmt.Printf(" [%s] %s\n", uuid, conn.Host()) + } + cl.belongsTo.clientsMtx.Unlock() + return "see console for output" + }, + }, }, } diff --git a/common/chatcommands.go b/common/chatcommands.go index d928b43..0d61b07 100644 --- a/common/chatcommands.go +++ b/common/chatcommands.go @@ -35,6 +35,7 @@ var ( CNReloadPlayer ChatCommandNames = []string{"reloadplayer"} CNReloadEmotes ChatCommandNames = []string{"reloademotes"} CNModpass ChatCommandNames = []string{"modpass"} + CNIP ChatCommandNames = []string{"iplist"} CNNewPin ChatCommandNames = []string{"newpin", "newpassword"} CNRoomAccess ChatCommandNames = []string{"changeaccess", "hodor"} ) @@ -45,7 +46,7 @@ var ChatCommands = []ChatCommandNames{ // Mod CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin, // Admin - CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, + CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, CNIP, } func GetFullChatCommand(c string) string { From 6b48322626d7677bdaccfceb6296ce87d707a341 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 30 Mar 2019 16:15:22 -0400 Subject: [PATCH 6/9] Use the logger Replace fmt.Print[f|ln]() with the logger equivalent. --- chatcommands.go | 10 +++++----- handlers.go | 21 ++++++++++----------- main.go | 4 ++-- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/chatcommands.go b/chatcommands.go index 22cb63e..87c2116 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -454,7 +454,7 @@ var commands = &CommandControl{ return "Unable to generate new pin: " + err.Error() } - fmt.Println("New room access pin: ", pin) + common.LogInfoln("New room access pin: ", pin) return "New access pin: " + pin }, }, @@ -470,7 +470,7 @@ var commands = &CommandControl{ switch AccessMode(strings.ToLower(args[0])) { case AccessOpen: settings.RoomAccess = AccessOpen - fmt.Println("[access] Room set to open") + common.LogInfoln("[access] Room set to open") return "Room access set to open" case AccessPin: @@ -482,17 +482,17 @@ var commands = &CommandControl{ } else { _, err := settings.generateNewPin() if err != nil { - fmt.Println("Error generating new access pin: ", err.Error()) + common.LogErrorln("Error generating new access pin: ", err.Error()) return "Unable to generate a new pin, access unchanged: " + err.Error() } } settings.RoomAccess = AccessPin - fmt.Println("[access] Room set to pin: " + settings.RoomAccessPin) + common.LogInfoln("[access] Room set to pin: " + settings.RoomAccessPin) return "Room access set to Pin: " + settings.RoomAccessPin case AccessRequest: settings.RoomAccess = AccessRequest - fmt.Println("[access] Room set to request") + common.LogInfoln("[access] Room set to request") return "Room access set to request. WARNING: this isn't implemented yet." default: diff --git a/handlers.go b/handlers.go index a53e89e..7d00de4 100644 --- a/handlers.go +++ b/handlers.go @@ -160,7 +160,7 @@ func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool { session, err := sstore.Get(r, "moviesession") if err != nil { // Don't return as server error here, just make a new session. - fmt.Printf("Unable to get session for client %s: %v\n", r.RemoteAddr, err) + common.LogErrorf("Unable to get session for client %s: %v\n", r.RemoteAddr, err) } if settings.RoomAccess == AccessPin { @@ -171,12 +171,12 @@ func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool { // Check for correct pin err = r.ParseForm() if err != nil { - fmt.Printf("Error parsing form") + common.LogErrorf("Error parsing form") http.Error(w, "Unable to get session data", http.StatusInternalServerError) } postPin := r.Form.Get("txtInput") - fmt.Printf("Received pin: %s\n", postPin) + common.LogDebugf("Received pin: %s\n", postPin) if postPin == settings.RoomAccessPin { // Pin is correct. Save it to session and return true. session.Values["pin"] = settings.RoomAccessPin @@ -220,7 +220,7 @@ func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool { func handlePinTemplate(w http.ResponseWriter, r *http.Request, errorMessage string) { t, err := template.ParseFiles("./static/base.html", "./static/thedoor.html") if err != nil { - fmt.Printf("Error parsing template file: %v", err) + common.LogErrorf("Error parsing template file: %v", err) return } @@ -238,7 +238,7 @@ func handlePinTemplate(w http.ResponseWriter, r *http.Request, errorMessage stri err = t.Execute(w, data) if err != nil { - fmt.Printf("Error executing file, %v", err) + common.LogErrorf("Error executing file, %v", err) } } @@ -278,7 +278,7 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { func handlePin(w http.ResponseWriter, r *http.Request) { session, err := sstore.Get(r, "moviesession") if err != nil { - fmt.Printf("Unable to get session: %v\n", err) + common.LogDebugf("Unable to get session: %v\n", err) } val := session.Values["pin"] @@ -289,21 +289,20 @@ func handlePin(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "unable to save session: %v", err) } fmt.Fprint(w, "Pin was not set") - fmt.Println("pin was not set") + common.LogDebugln("pin was not set") } else { fmt.Fprintf(w, "pin set: %v", val) - fmt.Printf("pin is set: %v\n", val) + common.LogDebugf("pin is set: %v\n", val) } } func handleIndexTemplate(w http.ResponseWriter, r *http.Request) { - fmt.Printf("RoomAcces: %s\n", settings.RoomAccess) if settings.RoomAccess != AccessOpen { if !checkRoomAccess(w, r) { - fmt.Println("Denied access") + common.LogDebugln("Denied access") return } - fmt.Println("Granted access") + common.LogDebugln("Granted access") } t, err := template.ParseFiles("./static/base.html", "./static/main.html") diff --git a/main.go b/main.go index a387715..40c7dd4 100644 --- a/main.go +++ b/main.go @@ -74,8 +74,8 @@ func main() { common.LogInfoln("Stream key: ", settings.GetStreamKey()) common.LogInfoln("Admin password: ", settings.AdminPassword) common.LogInfoln("Listen and serve ", addr) - fmt.Println("RoomAccess: ", settings.RoomAccess) - fmt.Println("RoomAccessPin: ", settings.RoomAccessPin) + common.LogInfoln("RoomAccess: ", settings.RoomAccess) + common.LogInfoln("RoomAccessPin: ", settings.RoomAccessPin) go startServer() go startRmtpServer() From b3b63b999db1f58c079e7e3ca9a27e5ae99e75c8 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 30 Mar 2019 16:18:26 -0400 Subject: [PATCH 7/9] Replace some fmt.Printf()'s that were missed --- emotes.go | 2 +- stats.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/emotes.go b/emotes.go index 242f8c0..a936a16 100644 --- a/emotes.go +++ b/emotes.go @@ -47,7 +47,7 @@ func (tc *twitchChannel) downloadEmotes() (*EmoteSet, error) { b := emote.Code[i] if b >= 'A' && b <= 'Z' { es.Prefix = emote.Code[0 : i-1] - fmt.Printf("Found prefix for channel %q: %q (%q)\n", es.Channel, es.Prefix, emote) + common.LogDebugf("Found prefix for channel %q: %q (%q)\n", es.Channel, es.Prefix, emote) break } } diff --git a/stats.go b/stats.go index a5b6bbc..3aef4de 100644 --- a/stats.go +++ b/stats.go @@ -29,7 +29,7 @@ func (s *streamStats) msgOutInc() { } func (s *streamStats) Print() { - fmt.Printf("Messages In: %d\n", s.messageIn) - fmt.Printf("Messages Out: %d\n", s.messageOut) - fmt.Printf("Total Time: %s\n", time.Since(s.start)) + common.LogInfof("Messages In: %d\n", s.messageIn) + common.LogInfof("Messages Out: %d\n", s.messageOut) + common.LogInfof("Total Time: %s\n", time.Since(s.start)) } From de2131dd55a19802e3ed27fd4068ab98a3288e6f Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 30 Mar 2019 16:20:06 -0400 Subject: [PATCH 8/9] Fix some imports Whoops. --- emotes.go | 2 ++ stats.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/emotes.go b/emotes.go index a936a16..dcd6335 100644 --- a/emotes.go +++ b/emotes.go @@ -7,6 +7,8 @@ import ( "net/http" "os" "strings" + + "github.com/zorchenhimer/MovieNight/common" ) type twitchChannel struct { diff --git a/stats.go b/stats.go index 3aef4de..c96ef3d 100644 --- a/stats.go +++ b/stats.go @@ -1,9 +1,10 @@ package main import ( - "fmt" "sync" "time" + + "github.com/zorchenhimer/MovieNight/common" ) type streamStats struct { From 635b4f75c0493ac34196d3366509e9e9de9f569f Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 30 Mar 2019 17:46:31 -0400 Subject: [PATCH 9/9] Unbreak settings Don't try and use the global 'settings' variable when it should be using the local *Settings variable. --- settings.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/settings.go b/settings.go index 4d777f9..032e5c0 100644 --- a/settings.go +++ b/settings.go @@ -128,12 +128,12 @@ func LoadSettings(filename string) (*Settings, error) { common.LogInfof("RateLimitColor: %v", s.RateLimitColor) common.LogInfof("RateLimitAuth: %v", s.RateLimitAuth) - if len(settings.RoomAccess) == 0 { - settings.RoomAccess = AccessOpen + if len(s.RoomAccess) == 0 { + s.RoomAccess = AccessOpen } - if settings.RoomAccess != AccessOpen && len(settings.RoomAccessPin) == 0 { - settings.RoomAccessPin = "1234" + if s.RoomAccess != AccessOpen && len(s.RoomAccessPin) == 0 { + s.RoomAccessPin = "1234" } // Don't use LogInfof() here. Log isn't setup yet when LoadSettings() is called from init(). @@ -144,7 +144,7 @@ func LoadSettings(filename string) (*Settings, error) { } // Is this a good way to do this? Probably not... - if len(settings.SessionKey) == 0 { + if len(s.SessionKey) == 0 { out := "" large := big.NewInt(int64(1 << 60)) large = large.Add(large, large) @@ -155,11 +155,11 @@ func LoadSettings(filename string) (*Settings, error) { } out = fmt.Sprintf("%s%X", out, num) } - settings.SessionKey = out + s.SessionKey = out } // Save admin password to file - if err = settings.Save(); err != nil { + if err = s.Save(); err != nil { return nil, fmt.Errorf("Unable to save settings: %s", err) } @@ -202,11 +202,11 @@ func (s *Settings) AddBan(host string, names []string) error { IP: host, When: time.Now(), } - settings.Bans = append(settings.Bans, b) + s.Bans = append(s.Bans, b) common.LogInfof("[BAN] %q (%s) has been banned.\n", strings.Join(names, ", "), host) - return settings.Save() + return s.Save() } func (s *Settings) RemoveBan(name string) error { @@ -225,7 +225,7 @@ func (s *Settings) RemoveBan(name string) error { } } s.Bans = newBans - return settings.Save() + return s.Save() } func (s *Settings) IsBanned(host string) (bool, []string) { @@ -262,9 +262,9 @@ func (s *Settings) generateNewPin() (string, error) { if err != nil { return "", err } - settings.RoomAccessPin = fmt.Sprintf("%04d", num) + s.RoomAccessPin = fmt.Sprintf("%04d", num) if err = s.Save(); err != nil { return "", err } - return settings.RoomAccessPin, nil + return s.RoomAccessPin, nil }