From 6b645f7b8858b8557d7168fe17f84c57ed30d8a9 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 26 Mar 2021 09:06:35 +1000 Subject: [PATCH] Add Config.DisableBadRouteLog setting. Surface more errors in template_init.go Dump unknown UAs properly when DisableSuspLog is enabled. --- common/site.go | 5 +++-- common/template_init.go | 17 +++++++++++++---- docs/configuration.md | 4 +++- gen_router.go | 16 +++++++++++----- router_gen/main.go | 12 +++++++++--- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/common/site.go b/common/site.go index d9f4a7a0..5ce3817e 100644 --- a/common/site.go +++ b/common/site.go @@ -135,8 +135,9 @@ type config struct { WriteTimeout int IdleTimeout int - LogDir string - DisableSuspLog bool + LogDir string + DisableSuspLog bool + DisableBadRouteLog bool } type devConfig struct { diff --git a/common/template_init.go b/common/template_init.go index c5c32608..d3492d80 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -249,7 +249,10 @@ func compileCommons(c *tmpl.CTemplateSet, head, head2 *Header, forumList []Forum var replyList []*ReplyUser reply := Reply{1, 1, "Yo!", 1 /*, Config.DefaultGroup*/, now, 0, 0, 1, "::1", true, 1, 1, ""} ru := &ReplyUser{ClassName: "", Reply: reply, CreatedByName: "Alice", Avatar: avatar, Group: Config.DefaultGroup, Level: 0, Attachments: miniAttach} - ru.Init(user2) + _, err := ru.Init(user2) + if err != nil { + return err + } replyList = append(replyList, ru) tpage := TopicPage{htitle("Topic Name"), replyList, tu, &Forum{ID: 1, Name: "Hahaha"}, &poll, Paginator{[]int{1}, 1, 1}} tpage.Forum.Link = BuildForumURL(NameToSlug(tpage.Forum.Name), tpage.Forum.ID) @@ -279,7 +282,10 @@ func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string //avatar, microAvatar = BuildAvatar(0, "") reply := Reply{1, 1, "Yo!", 1 /*, Config.DefaultGroup*/, now, 0, 0, 1, "::1", true, 1, 1, ""} ru := &ReplyUser{ClassName: "", Reply: reply, CreatedByName: "Alice", Avatar: "", Group: Config.DefaultGroup, Level: 0, Attachments: miniAttach} - ru.Init(user) + _, err := ru.Init(user) + if err != nil { + return err + } replyList = append(replyList, ru) // TODO: Use a dummy forum list to avoid o(n) problems @@ -302,7 +308,7 @@ func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string return header } t := TItemHold(make(map[string]TItem)) - err := compileCommons(c, header, header2, forumList, t) + err = compileCommons(c, header, header2, forumList, t) if err != nil { return err } @@ -556,7 +562,10 @@ func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName stri avatar, microAvatar = BuildAvatar(0, "") reply := Reply{1, 1, "Yo!", 1 /*, Config.DefaultGroup*/, now, 0, 0, 1, "::1", true, 1, 1, ""} ru := &ReplyUser{ClassName: "", Reply: reply, CreatedByName: "Alice", Avatar: avatar, Group: Config.DefaultGroup, Level: 0, Attachments: miniAttach} - ru.Init(user) + _, err = ru.Init(user) + if err != nil { + return err + } replyList = append(replyList, ru) varList = make(map[string]tmpl.VarItem) diff --git a/docs/configuration.md b/docs/configuration.md index 15014b1d..f3b22551 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -148,7 +148,9 @@ IdleTimeout - The number of seconds that a Keep-Alive connection will be kept op LogDir - The directory in which logs are stored, with the exception of ops log, until a related bug is resolved. Default: ./logs/ -DisableSuspLog - Whether suspicious requests are logged in the suspicious request logs. Enabling this may make a site faster. Defaults to false. +DisableSuspLog - Whether suspicious requests are logged in the suspicious request logs. Enabling this may make a site faster. Defaults: false. + +DisableBadRouteLog - Whether requests referencing routes which don't exist should be individually logged. Enabling this may make a site faster. Default: false Related: https://support.cloudflare.com/hc/en-us/articles/212794707-General-Best-Practices-for-Load-Balancing-at-your-origin-with-Cloudflare diff --git a/gen_router.go b/gen_router.go index 54d02f99..7535459d 100644 --- a/gen_router.go +++ b/gen_router.go @@ -1120,8 +1120,12 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } else { // TODO: Test this items = items[:0] - r.SuspiciousRequest(req,"Illegal char "+strconv.Itoa(int(it))+" in UA") - r.reqLogger.Print("UA Buf: ", buf,"\nUA Buf String: ", string(buf)) + if c.Config.DisableSuspLog { + r.reqLogger.Print("Illegal char "+strconv.Itoa(int(it))+" in UA\nUA Buf: ", buf,"\nUA Buf String: ", string(buf)) + } else { + r.SuspiciousRequest(req,"Illegal char "+strconv.Itoa(int(it))+" in UA") + r.reqLogger.Print("UA Buf: ", buf,"\nUA Buf String: ", string(buf)) + } break } } @@ -2610,12 +2614,12 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user * co.RouteViewCounter.Bump3(150, cn) } case "/profile": - err = c.NoSessionMismatch(w,req,user) + err = c.MemberOnly(w,req,user) if err != nil { return err } - err = c.MemberOnly(w,req,user) + err = c.NoSessionMismatch(w,req,user) if err != nil { return err } @@ -2809,7 +2813,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user * } } - r.DumpRequest(req,"Bad Route") + if !c.Config.DisableBadRouteLog { + r.DumpRequest(req,"Bad Route") + } ae := req.Header.Get("Accept-Encoding") likelyBot := ae == "gzip" || ae == "" if likelyBot { diff --git a/router_gen/main.go b/router_gen/main.go index ca41676f..308a2a95 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -709,8 +709,12 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } else { // TODO: Test this items = items[:0] - r.SuspiciousRequest(req,"Illegal char "+strconv.Itoa(int(it))+" in UA") - r.reqLogger.Print("UA Buf: ", buf,"\nUA Buf String: ", string(buf)) + if c.Config.DisableSuspLog { + r.reqLogger.Print("Illegal char "+strconv.Itoa(int(it))+" in UA\nUA Buf: ", buf,"\nUA Buf String: ", string(buf)) + } else { + r.SuspiciousRequest(req,"Illegal char "+strconv.Itoa(int(it))+" in UA") + r.reqLogger.Print("UA Buf: ", buf,"\nUA Buf String: ", string(buf)) + } break } } @@ -973,7 +977,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user * } } - r.DumpRequest(req,"Bad Route") + if !c.Config.DisableBadRouteLog { + r.DumpRequest(req,"Bad Route") + } ae := req.Header.Get("Accept-Encoding") likelyBot := ae == "gzip" || ae == "" if likelyBot {