PanicSafe()
This commit is contained in:
7
auth.go
7
auth.go
@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func reqPass(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
go http.Redirect(w, r, "/login", 302)
|
||||
username := r.FormValue("UserName")
|
||||
var userID string
|
||||
@ -28,6 +29,7 @@ func reqPass(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func tryLogin(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
session, err := store.Get(r, "2fa")
|
||||
if err != nil {
|
||||
log.LogWarn("Error opening session for 2fa store")
|
||||
@ -61,6 +63,7 @@ func tryLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func usePassword(user string, pass string, ip string) bool {
|
||||
defer log.PanicSafe()
|
||||
tok := toks[user]
|
||||
delete(toks, user)
|
||||
if time.Since(tok.timestamp) > (time.Minute * 5) {
|
||||
@ -80,6 +83,7 @@ func usePassword(user string, pass string, ip string) bool {
|
||||
}
|
||||
|
||||
func genPassword(length int) string {
|
||||
defer log.PanicSafe()
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||
"abcdefghijklmnopqrstuvwxyz" +
|
||||
@ -91,6 +95,7 @@ func genPassword(length int) string {
|
||||
return b.String() // E.g. "ExcbsVQs"
|
||||
}
|
||||
func sendPassword(user string, ipaddr string) {
|
||||
defer log.PanicSafe()
|
||||
str := genPassword(8)
|
||||
m, _ := dg.GuildMember(config.GuildID, user)
|
||||
toks[m.User.Username] = tokens{
|
||||
@ -106,6 +111,7 @@ func sendPassword(user string, ipaddr string) {
|
||||
}
|
||||
|
||||
func getSessionIdentifier(r *http.Request) string {
|
||||
defer log.PanicSafe()
|
||||
ipaddr := r.Header.Get("X-Real-IP")
|
||||
if ipaddr == "" {
|
||||
ipaddr = r.RemoteAddr
|
||||
@ -115,6 +121,7 @@ func getSessionIdentifier(r *http.Request) string {
|
||||
}
|
||||
|
||||
func detectUser(r *http.Request, callFunc string) (bool, string) {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called detectUser", getSessionIdentifier(r)))
|
||||
session, err := store.Get(r, "2fa")
|
||||
if err != nil {
|
||||
|
||||
10
site-api.go
10
site-api.go
@ -19,6 +19,7 @@ var (
|
||||
)
|
||||
|
||||
func topWrapper(r *http.Request) string {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called topWrapper", getSessionIdentifier(r)))
|
||||
headerTemplate, err := ioutil.ReadFile("./static/header.tpl")
|
||||
if err != nil {
|
||||
@ -36,6 +37,7 @@ func topWrapper(r *http.Request) string {
|
||||
}
|
||||
|
||||
func bodyWrapper(r *http.Request, template string) string {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called bodyWrapper", getSessionIdentifier(r)))
|
||||
bodyTemplate, err := ioutil.ReadFile(fmt.Sprintf("./static/%+v.tpl", template))
|
||||
if err != nil {
|
||||
@ -46,12 +48,14 @@ func bodyWrapper(r *http.Request, template string) string {
|
||||
|
||||
}
|
||||
func pageBuilder(r *http.Request, pageName string) string {
|
||||
defer log.PanicSafe()
|
||||
pageCode := topWrapper(r)
|
||||
pageCode += bodyWrapper(r, pageName)
|
||||
return pageCode
|
||||
}
|
||||
|
||||
func greetUser(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called greetUser", getSessionIdentifier(r)))
|
||||
loggedIn, username := detectUser(r, "greetUser")
|
||||
fmt.Fprintf(w, pageBuilder(r, "home"))
|
||||
@ -61,10 +65,12 @@ func greetUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func passPage(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called passPage", getSessionIdentifier(r)))
|
||||
fmt.Fprintf(w, pageBuilder(r, "pass"))
|
||||
}
|
||||
func loginPage(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo(fmt.Sprintf("%s called loginPage", getSessionIdentifier(r)))
|
||||
session, err := store.Get(r, "2fa")
|
||||
if err != nil {
|
||||
@ -84,6 +90,7 @@ func loginPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func notFoundPage(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
go log.LogWarn(fmt.Sprintf("%s triggered notFoundPage", getSessionIdentifier(r)))
|
||||
fmt.Fprintf(w, topWrapper(r))
|
||||
|
||||
@ -94,6 +101,7 @@ func notFoundPage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
func card(title string, content string, footer string) string {
|
||||
defer log.PanicSafe()
|
||||
cardTemplate, err := ioutil.ReadFile("./static/card.tpl")
|
||||
if err != nil {
|
||||
log.LogError("Unable to open card template")
|
||||
@ -108,6 +116,7 @@ func card(title string, content string, footer string) string {
|
||||
}
|
||||
|
||||
func getPending(w http.ResponseWriter, r *http.Request) {
|
||||
defer log.PanicSafe()
|
||||
loggedIn, _ := detectUser(r, "getPending")
|
||||
if loggedIn {
|
||||
pending, err := json.Marshal(config.Verifications)
|
||||
@ -122,6 +131,7 @@ func getPending(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func runWeb() {
|
||||
defer log.PanicSafe()
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
log.LogInfo("Adding HandleFuncs to router")
|
||||
router.NotFoundHandler = http.HandlerFunc(notFoundPage)
|
||||
|
||||
Reference in New Issue
Block a user