2017-04-12 10:10:36 +00:00
/* WIP Under Construction */
package main
2017-12-10 03:43:30 +00:00
import (
2017-12-19 03:53:13 +00:00
"bytes"
2017-12-10 03:43:30 +00:00
"log"
"os"
2017-12-19 03:53:13 +00:00
"strconv"
2019-04-19 07:16:09 +00:00
"strings"
2017-12-19 03:53:13 +00:00
"text/template"
2017-12-10 03:43:30 +00:00
)
2017-09-10 16:57:22 +00:00
2017-12-19 03:53:13 +00:00
type TmplVars struct {
2018-12-31 09:03:49 +00:00
RouteList [ ] * RouteImpl
RouteGroups [ ] * RouteGroup
2019-04-19 07:16:09 +00:00
AllRouteNames [ ] RouteName
2018-12-31 09:03:49 +00:00
AllRouteMap map [ string ] int
AllAgentNames [ ] string
AllAgentMap map [ string ] int
AllAgentMarkNames [ ] string
AllAgentMarks map [ string ] string
2020-03-08 04:15:27 +00:00
AllAgentMarkIDs map [ string ] int
2018-12-31 09:03:49 +00:00
AllOSNames [ ] string
AllOSMap map [ string ] int
2017-12-19 03:53:13 +00:00
}
2019-04-19 07:16:09 +00:00
type RouteName struct {
Plain string
Short string
}
2017-04-12 10:10:36 +00:00
func main ( ) {
2017-08-13 11:22:34 +00:00
log . Println ( "Generating the router..." )
2017-09-10 16:57:22 +00:00
2017-04-12 10:10:36 +00:00
// Load all the routes...
2018-11-13 04:43:05 +00:00
r := & Router { }
routes ( r )
2017-09-10 16:57:22 +00:00
2019-10-20 01:42:19 +00:00
tmplVars := TmplVars {
2018-11-13 04:43:05 +00:00
RouteList : r . routeList ,
RouteGroups : r . routeGroups ,
2017-12-19 03:53:13 +00:00
}
2019-04-19 07:16:09 +00:00
var allRouteNames [ ] RouteName
2019-10-20 01:42:19 +00:00
allRouteMap := make ( map [ string ] int )
2017-12-19 03:53:13 +00:00
var out string
2019-10-20 01:42:19 +00:00
mapIt := func ( name string ) {
2019-04-19 07:16:09 +00:00
allRouteNames = append ( allRouteNames , RouteName { name , strings . Replace ( name , "common." , "c." , - 1 ) } )
2017-12-19 03:53:13 +00:00
allRouteMap [ name ] = len ( allRouteNames ) - 1
2017-12-10 03:43:30 +00:00
}
2020-02-26 10:34:38 +00:00
mapIt ( "routes.Error" )
2021-03-18 00:22:28 +00:00
var indentCache [ 20 ] string
countToIndents := func ( ind int ) string {
out := indentCache [ ind ]
if out != "" {
return out
}
for i := 0 ; i < ind ; i ++ {
out += "\t"
2017-04-13 09:26:40 +00:00
}
2021-03-18 00:22:28 +00:00
if ind < 20 {
indentCache [ ind ] = out
}
return out
2017-12-10 03:43:30 +00:00
}
2021-03-18 05:10:20 +00:00
o := func ( indent int , str string ) {
2021-03-18 00:22:28 +00:00
out += countToIndents ( indent ) + str
2021-03-18 05:10:20 +00:00
}
2021-03-18 00:22:28 +00:00
on := func ( indent int , str string ) {
out += "\n" + countToIndents ( indent ) + str
}
iferrn := func ( indent int ) {
ind := countToIndents ( indent )
ind2 := countToIndents ( indent + 1 )
out += "\n" + ind + "if err != nil {"
out += "\n" + ind2 + "return err\n" + ind + "}"
}
2021-03-18 05:10:20 +00:00
runBefore := func ( runnables [ ] Runnable , ind int ) {
2017-12-10 03:43:30 +00:00
if len ( runnables ) > 0 {
for _ , runnable := range runnables {
2017-11-05 09:55:34 +00:00
if runnable . Literal {
2021-03-18 05:10:20 +00:00
on ( ind , runnable . Contents )
2017-11-05 09:55:34 +00:00
} else {
2021-03-18 05:10:20 +00:00
on ( ind , "err = c." + runnable . Contents + "(w,req,user)" )
iferrn ( ind )
o ( ind , "\n" )
2017-11-05 09:55:34 +00:00
}
}
2017-04-13 09:26:40 +00:00
}
2017-12-10 03:43:30 +00:00
}
2021-03-18 00:22:28 +00:00
userCheckNano := func ( indent int , route * RouteImpl ) {
on ( indent , "h, err := c.UserCheckNano(w,req,user,cn)" )
iferrn ( indent )
vcpy := route . Vars
route . Vars = [ ] string { "h" }
route . Vars = append ( route . Vars , vcpy ... )
}
writeRoute := func ( indent int , r * RouteImpl ) {
on ( indent , "err = " + strings . Replace ( r . Name , "common." , "c." , - 1 ) + "(w,req,user" )
for _ , item := range r . Vars {
out += "," + item
}
out += ` ) `
}
2017-12-10 03:43:30 +00:00
2018-11-13 04:43:05 +00:00
for _ , route := range r . routeList {
2017-12-19 03:53:13 +00:00
mapIt ( route . Name )
2019-10-20 01:42:19 +00:00
end := len ( route . Path ) - 1
2021-03-18 00:22:28 +00:00
on ( 2 , "case \"" + route . Path [ 0 : end ] + "\":" )
//on(3,"id = " + strconv.Itoa(allRouteMap[route.Name]))
2021-03-18 05:10:20 +00:00
runBefore ( route . RunBefore , 3 )
2018-11-12 09:23:36 +00:00
if ! route . Action && ! route . NoHead {
2021-03-18 00:22:28 +00:00
userCheckNano ( 3 , route )
2021-03-18 05:10:20 +00:00
}
2021-03-18 00:22:28 +00:00
writeRoute ( 3 , route )
2021-03-18 05:10:20 +00:00
if route . Name != "common.RouteWebsockets" {
2021-03-18 00:22:28 +00:00
on ( 3 , "co.RouteViewCounter.Bump3(" + strconv . Itoa ( allRouteMap [ route . Name ] ) + ", cn)" )
}
}
2021-03-18 05:10:20 +00:00
prec := NewPrec ( )
prec . AddSet ( "MemberOnly" , "SuperModOnly" , "AdminOnly" , "SuperAdminOnly" )
2021-03-18 00:22:28 +00:00
// Hoist runnables which appear on every route to the route group to avoid code duplication
2021-03-18 05:10:20 +00:00
dupeMap := make ( map [ string ] int )
//skipRunnableAntiDupe:
2021-03-18 00:22:28 +00:00
for _ , g := range r . routeGroups {
for _ , route := range g . RouteList {
if len ( route . RunBefore ) == 0 {
2021-03-18 05:10:20 +00:00
continue //skipRunnableAntiDupe
2021-03-18 00:22:28 +00:00
}
// TODO: What if there are duplicates of the same runnable on this route?
for _ , runnable := range route . RunBefore {
dupeMap [ runnable . Contents ] += 1
}
}
// Unset entries which are already set on the route group
for _ , gRunnable := range g . RunBefore {
delete ( dupeMap , gRunnable . Contents )
2021-03-18 05:10:20 +00:00
for _ , item := range prec . LessThanItem ( gRunnable . Contents ) {
delete ( dupeMap , item )
}
2021-03-18 00:22:28 +00:00
}
for runnable , count := range dupeMap {
if count == len ( g . RouteList ) {
g . Before ( runnable )
}
2020-02-26 10:34:38 +00:00
}
2021-03-18 05:10:20 +00:00
// This method is optimised in the compiler to do a bulk delete
for name , _ := range dupeMap {
delete ( dupeMap , name )
}
2017-04-13 09:26:40 +00:00
}
2017-09-10 16:57:22 +00:00
2018-11-13 04:43:05 +00:00
for _ , group := range r . routeGroups {
2019-10-20 01:42:19 +00:00
end := len ( group . Path ) - 1
2021-03-18 00:22:28 +00:00
on ( 2 , "case \"" + group . Path [ 0 : end ] + "\":" )
2021-03-18 05:10:20 +00:00
runBefore ( group . RunBefore , 3 )
2021-03-18 00:22:28 +00:00
on ( 3 , "switch(req.URL.Path) {" )
2017-10-30 09:57:08 +00:00
2019-10-20 01:42:19 +00:00
defaultRoute := blankRoute ( )
2017-10-30 09:57:08 +00:00
for _ , route := range group . RouteList {
2017-04-13 10:55:51 +00:00
if group . Path == route . Path {
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
defaultRoute = route
2017-04-13 10:55:51 +00:00
continue
}
2017-12-19 03:53:13 +00:00
mapIt ( route . Name )
2017-09-10 16:57:22 +00:00
2021-03-18 00:22:28 +00:00
on ( 4 , "case \"" + route . Path + "\":" )
//on(5,"id = " + strconv.Itoa(allRouteMap[route.Name]))
2017-11-05 09:55:34 +00:00
if len ( route . RunBefore ) > 0 {
2017-11-12 07:18:25 +00:00
skipRunnable :
2017-11-05 09:55:34 +00:00
for _ , runnable := range route . RunBefore {
2017-11-12 07:18:25 +00:00
for _ , gRunnable := range group . RunBefore {
if gRunnable . Contents == runnable . Contents {
2021-03-18 00:22:28 +00:00
continue skipRunnable
}
2021-03-18 05:10:20 +00:00
if prec . GreaterThan ( gRunnable . Contents , runnable . Contents ) {
2017-11-12 07:18:25 +00:00
continue skipRunnable
}
}
2017-11-05 09:55:34 +00:00
if runnable . Literal {
2021-03-18 00:22:28 +00:00
on ( 5 , runnable . Contents )
2017-11-05 09:55:34 +00:00
} else {
2021-03-18 00:22:28 +00:00
on ( 5 , "err = c." + runnable . Contents + "(w,req,user)" )
iferrn ( 5 )
on ( 5 , "" )
2017-11-05 09:55:34 +00:00
}
}
2017-04-13 09:26:40 +00:00
}
2018-11-12 09:23:36 +00:00
if ! route . Action && ! route . NoHead && ! group . NoHead {
2021-03-18 00:22:28 +00:00
userCheckNano ( 5 , route )
2017-04-13 09:26:40 +00:00
}
2021-03-18 00:22:28 +00:00
writeRoute ( 5 , route )
on ( 5 , "co.RouteViewCounter.Bump3(" + strconv . Itoa ( allRouteMap [ route . Name ] ) + ", cn)" )
2017-04-13 09:26:40 +00:00
}
2017-09-10 16:57:22 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
if defaultRoute . Name != "" {
2017-12-19 03:53:13 +00:00
mapIt ( defaultRoute . Name )
2021-03-18 00:22:28 +00:00
on ( 4 , "default:" )
//on(5,"id = " + strconv.Itoa(allRouteMap[defaultRoute.Name]))
2021-03-18 05:10:20 +00:00
runBefore ( defaultRoute . RunBefore , 4 )
2018-11-12 09:23:36 +00:00
if ! defaultRoute . Action && ! defaultRoute . NoHead && ! group . NoHead {
2021-03-18 00:22:28 +00:00
userCheckNano ( 5 , defaultRoute )
2017-04-13 10:55:51 +00:00
}
2021-03-18 00:22:28 +00:00
writeRoute ( 5 , defaultRoute )
on ( 5 , "co.RouteViewCounter.Bump3(" + strconv . Itoa ( allRouteMap [ defaultRoute . Name ] ) + ", cn)" )
2017-04-13 10:55:51 +00:00
}
2021-03-18 00:22:28 +00:00
on ( 3 , "}" )
2017-04-12 10:10:36 +00:00
}
2017-09-10 16:57:22 +00:00
2017-12-24 22:08:35 +00:00
// Stubs for us to refer to these routes through
2018-05-15 05:59:52 +00:00
mapIt ( "routes.DynamicRoute" )
mapIt ( "routes.UploadedFile" )
2018-02-19 04:26:01 +00:00
mapIt ( "routes.StaticFile" )
2018-03-17 08:16:43 +00:00
mapIt ( "routes.RobotsTxt" )
mapIt ( "routes.SitemapXml" )
2019-05-08 05:08:38 +00:00
mapIt ( "routes.OpenSearchXml" )
2020-02-26 10:34:38 +00:00
mapIt ( "routes.Favicon" )
2018-05-15 05:59:52 +00:00
mapIt ( "routes.BadRoute" )
2019-03-21 22:59:41 +00:00
mapIt ( "routes.HTTPSRedirect" )
2017-12-19 03:53:13 +00:00
tmplVars . AllRouteNames = allRouteNames
tmplVars . AllRouteMap = allRouteMap
2018-02-04 08:15:20 +00:00
tmplVars . AllOSNames = [ ] string {
"unknown" ,
"windows" ,
"linux" ,
"mac" ,
"android" ,
"iphone" ,
}
tmplVars . AllOSMap = make ( map [ string ] int )
for id , os := range tmplVars . AllOSNames {
tmplVars . AllOSMap [ os ] = id
}
2018-01-09 07:39:29 +00:00
tmplVars . AllAgentNames = [ ] string {
"unknown" ,
"opera" ,
2021-02-17 05:27:56 +00:00
"chrome" ,
"firefox" ,
2018-01-09 07:39:29 +00:00
"safari" ,
"edge" ,
2018-01-09 08:46:16 +00:00
"internetexplorer" ,
2018-02-04 08:15:20 +00:00
"trident" , // Hack to support IE11
2018-01-09 07:39:29 +00:00
2018-02-03 05:47:14 +00:00
"androidchrome" ,
2018-02-04 08:15:20 +00:00
"mobilesafari" ,
"samsung" ,
2018-02-03 05:47:14 +00:00
"ucbrowser" ,
2018-01-09 07:39:29 +00:00
"googlebot" ,
"yandex" ,
"bing" ,
2020-03-07 03:25:50 +00:00
"slurp" ,
"exabot" ,
2020-03-11 08:54:11 +00:00
"mojeek" ,
2020-03-13 06:37:18 +00:00
"cliqz" ,
2021-03-02 07:22:32 +00:00
"qwant" ,
2020-03-21 07:44:33 +00:00
"datenbank" ,
2018-01-09 07:39:29 +00:00
"baidu" ,
2020-03-07 03:25:50 +00:00
"sogou" ,
2020-03-07 05:28:25 +00:00
"toutiao" ,
2020-03-08 00:50:23 +00:00
"haosou" ,
2018-01-09 07:39:29 +00:00
"duckduckgo" ,
2018-02-04 08:15:20 +00:00
"seznambot" ,
2018-01-11 08:03:17 +00:00
"discord" ,
2020-05-28 22:41:19 +00:00
"telegram" ,
2018-02-04 08:15:20 +00:00
"twitter" ,
2019-03-03 02:28:17 +00:00
"facebook" ,
2018-02-03 05:47:14 +00:00
"cloudflare" ,
2020-03-07 03:49:44 +00:00
"archive_org" ,
2018-01-25 04:57:33 +00:00
"uptimebot" ,
2018-03-08 03:59:47 +00:00
"slackbot" ,
2019-06-01 02:03:04 +00:00
"apple" ,
2018-02-04 08:15:20 +00:00
"discourse" ,
2021-03-02 07:22:32 +00:00
"xenforo" ,
2020-03-28 05:39:05 +00:00
"mattermost" ,
2020-03-07 03:25:50 +00:00
"alexa" ,
2018-01-11 08:03:17 +00:00
"lynx" ,
"blank" ,
2018-01-25 04:57:33 +00:00
"malformed" ,
2018-01-26 05:53:34 +00:00
"suspicious" ,
2019-02-10 09:32:05 +00:00
"semrush" ,
2019-02-23 06:29:19 +00:00
"dotbot" ,
2020-03-07 11:08:25 +00:00
"ahrefs" ,
2020-03-08 00:50:23 +00:00
"proximic" ,
2020-04-09 09:30:51 +00:00
"megaindex" ,
2020-03-07 10:46:27 +00:00
"majestic" ,
2020-05-26 21:08:13 +00:00
"cocolyze" ,
"babbar" ,
"surdotly" ,
2020-05-28 22:41:19 +00:00
"domcop" ,
2020-04-04 09:03:11 +00:00
"netcraft" ,
2021-03-23 20:59:34 +00:00
"seostar" ,
"pandalytics" ,
2020-03-13 06:37:18 +00:00
"blexbot" ,
2021-02-17 05:27:56 +00:00
"wappalyzer" ,
2021-03-02 07:22:32 +00:00
"twingly" ,
"linkfluence" ,
2021-03-23 20:59:34 +00:00
"pagething" ,
2020-04-04 03:21:59 +00:00
"burf" ,
2020-03-06 09:12:41 +00:00
"aspiegel" ,
2020-03-08 00:50:23 +00:00
"mail_ru" ,
2020-03-31 13:10:29 +00:00
"ccbot" ,
2021-02-17 05:27:56 +00:00
"yacy" ,
2018-02-03 05:47:14 +00:00
"zgrab" ,
2020-04-04 03:10:18 +00:00
"cloudsystemnetworks" ,
2020-05-28 22:41:19 +00:00
"maui" ,
2020-03-13 06:37:18 +00:00
"curl" ,
2020-03-22 04:37:45 +00:00
"python" ,
2021-02-17 05:27:56 +00:00
//"go",
2020-04-12 08:33:24 +00:00
"headlesschrome" ,
2020-05-26 21:08:13 +00:00
"awesome_bot" ,
2018-01-09 07:39:29 +00:00
}
tmplVars . AllAgentMap = make ( map [ string ] int )
for id , agent := range tmplVars . AllAgentNames {
tmplVars . AllAgentMap [ agent ] = id
}
2017-12-19 03:53:13 +00:00
2021-02-17 00:07:11 +00:00
tmplVars . AllAgentMarkNames = [ ] string { }
tmplVars . AllAgentMarks = map [ string ] string { }
2018-12-31 09:03:49 +00:00
2021-02-17 00:07:11 +00:00
// Add agent marks
a := func ( mark , agent string ) {
tmplVars . AllAgentMarkNames = append ( tmplVars . AllAgentMarkNames , mark )
tmplVars . AllAgentMarks [ mark ] = agent
2018-12-31 09:03:49 +00:00
}
2021-02-17 00:07:11 +00:00
a ( "OPR" , "opera" )
a ( "Chrome" , "chrome" )
a ( "Firefox" , "firefox" )
a ( "Safari" , "safari" )
a ( "MSIE" , "internetexplorer" )
a ( "Trident" , "trident" ) // Hack to support IE11
a ( "Edge" , "edge" )
a ( "Lynx" , "lynx" ) // There's a rare android variant of lynx which isn't covered by this
a ( "SamsungBrowser" , "samsung" )
a ( "UCBrowser" , "ucbrowser" )
2018-12-31 09:03:49 +00:00
2021-02-17 00:07:11 +00:00
a ( "Google" , "googlebot" )
a ( "Googlebot" , "googlebot" )
a ( "yandex" , "yandex" ) // from the URL
a ( "DuckDuckBot" , "duckduckgo" )
a ( "DuckDuckGo" , "duckduckgo" )
a ( "Baiduspider" , "baidu" )
a ( "Sogou" , "sogou" )
a ( "ToutiaoSpider" , "toutiao" )
2021-03-02 07:22:32 +00:00
a ( "Bytespider" , "toutiao" )
2021-02-17 00:07:11 +00:00
a ( "360Spider" , "haosou" )
a ( "bingbot" , "bing" )
a ( "BingPreview" , "bing" )
a ( "msnbot" , "bing" )
a ( "Slurp" , "slurp" )
a ( "Exabot" , "exabot" )
a ( "MojeekBot" , "mojeek" )
a ( "Cliqzbot" , "cliqz" )
2021-03-02 07:22:32 +00:00
a ( "Qwantify" , "qwant" )
2021-02-17 00:07:11 +00:00
a ( "netEstate" , "datenbank" )
a ( "SeznamBot" , "seznambot" )
a ( "CloudFlare" , "cloudflare" ) // Track alwayson specifically in case there are other bots?
a ( "archive" , "archive_org" ) //archive.org_bot
a ( "Uptimebot" , "uptimebot" )
a ( "Slackbot" , "slackbot" )
a ( "Slack" , "slackbot" )
a ( "Discordbot" , "discord" )
a ( "TelegramBot" , "telegram" )
a ( "Twitterbot" , "twitter" )
a ( "facebookexternalhit" , "facebook" )
a ( "Facebot" , "facebook" )
a ( "Applebot" , "apple" )
a ( "Discourse" , "discourse" )
2021-03-02 07:22:32 +00:00
a ( "XenForo" , "xenforo" )
2021-02-17 00:07:11 +00:00
a ( "mattermost" , "mattermost" )
a ( "ia_archiver" , "alexa" )
2018-12-31 09:03:49 +00:00
2021-02-17 00:07:11 +00:00
a ( "SemrushBot" , "semrush" )
a ( "DotBot" , "dotbot" )
a ( "AhrefsBot" , "ahrefs" )
a ( "proximic" , "proximic" )
a ( "MegaIndex" , "megaindex" )
a ( "MJ12bot" , "majestic" ) // TODO: This isn't matching bots out in the wild
a ( "mj12bot" , "majestic" )
a ( "Cocolyzebot" , "cocolyze" )
a ( "Barkrowler" , "babbar" )
a ( "SurdotlyBot" , "surdotly" )
a ( "DomCopBot" , "domcop" )
a ( "NetcraftSurveyAgent" , "netcraft" )
2021-03-23 20:59:34 +00:00
a ( "seostar" , "seostar" )
a ( "Pandalytics" , "pandalytics" )
2021-02-17 00:07:11 +00:00
a ( "BLEXBot" , "blexbot" )
2021-02-17 05:27:56 +00:00
a ( "Wappalyzer" , "wappalyzer" )
2021-03-02 07:22:32 +00:00
a ( "Twingly" , "twingly" )
a ( "linkfluence" , "linkfluence" )
2021-03-23 20:59:34 +00:00
a ( "PageThing" , "pagething" )
2021-02-17 00:07:11 +00:00
a ( "Burf" , "burf" )
a ( "AspiegelBot" , "aspiegel" )
a ( "PetalBot" , "aspiegel" )
a ( "RU_Bot" , "mail_ru" ) // Mail.RU_Bot
a ( "CCBot" , "ccbot" )
2021-02-17 05:27:56 +00:00
a ( "yacybot" , "yacy" )
2021-02-17 00:07:11 +00:00
a ( "zgrab" , "zgrab" )
a ( "Nimbostratus" , "cloudsystemnetworks" )
a ( "MauiBot" , "maui" )
a ( "curl" , "curl" )
a ( "python" , "python" )
2021-02-17 05:27:56 +00:00
//a("Go", "go") // yacy has java as part of it's UA, try to avoid hitting crawlers written in go
2021-02-17 00:07:11 +00:00
a ( "HeadlessChrome" , "headlesschrome" )
a ( "awesome_bot" , "awesome_bot" )
// TODO: Detect Adsbot/3.1, it has a similar user agent to Google's Adsbot, but it is different. No Google fragments.
2018-12-31 09:03:49 +00:00
2020-03-08 04:15:27 +00:00
tmplVars . AllAgentMarkIDs = make ( map [ string ] int )
for mark , agent := range tmplVars . AllAgentMarks {
tmplVars . AllAgentMarkIDs [ mark ] = tmplVars . AllAgentMap [ agent ]
}
2019-10-20 01:42:19 +00:00
fileData := ` // Code generated by Gosora's Router Generator. DO NOT EDIT.
2017-12-10 03:43:30 +00:00
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main
2017-04-12 10:10:36 +00:00
2017-11-11 04:06:16 +00:00
import (
"strings"
2020-03-22 23:14:08 +00:00
//"bytes"
2018-03-08 03:59:47 +00:00
"strconv"
2018-08-21 08:00:35 +00:00
"compress/gzip"
2018-07-15 10:29:31 +00:00
"sync/atomic"
2017-11-11 04:06:16 +00:00
"errors"
"net/http"
2019-04-19 07:16:09 +00:00
c "github.com/Azareal/Gosora/common"
2019-10-29 07:13:04 +00:00
co "github.com/Azareal/Gosora/common/counters"
2020-02-28 23:11:07 +00:00
"github.com/Azareal/Gosora/uutils"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/routes"
"github.com/Azareal/Gosora/routes/panel"
2020-05-26 21:08:13 +00:00
//"github.com/andybalholm/brotli"
2017-11-11 04:06:16 +00:00
)
2017-04-12 10:10:36 +00:00
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
var ErrNoRoute = errors . New ( "That route doesn't exist." )
2017-12-19 03:53:13 +00:00
// TODO: What about the /uploads/ route? x.x
var RouteMap = map [ string ] interface { } { { { range . AllRouteNames } }
2019-04-19 07:16:09 +00:00
"{{.Plain}}" : { { . Short } } , { { end } }
2017-12-19 03:53:13 +00:00
}
// ! NEVER RELY ON THESE REMAINING THE SAME BETWEEN COMMITS
2020-05-28 22:41:19 +00:00
var routeMapEnum = map [ string ] int { { { range $ index , $ el := . AllRouteNames } }
"{{$el.Plain}}" : { { $ index } } , { { end } }
2017-12-19 03:53:13 +00:00
}
2020-05-28 22:41:19 +00:00
var reverseRouteMapEnum = map [ int ] string { { { range $ index , $ el := . AllRouteNames } }
{ { $ index } } : "{{$el.Plain}}" , { { end } }
2017-12-19 03:53:13 +00:00
}
2020-05-28 22:41:19 +00:00
var osMapEnum = map [ string ] int { { { range $ index , $ el := . AllOSNames } }
"{{$el}}" : { { $ index } } , { { end } }
2018-02-04 08:15:20 +00:00
}
2020-05-28 22:41:19 +00:00
var reverseOSMapEnum = map [ int ] string { { { range $ index , $ el := . AllOSNames } }
{ { $ index } } : "{{$el}}" , { { end } }
2018-02-04 08:15:20 +00:00
}
2020-05-28 22:41:19 +00:00
var agentMapEnum = map [ string ] int { { { range $ index , $ el := . AllAgentNames } }
"{{$el}}" : { { $ index } } , { { end } }
2018-01-09 07:39:29 +00:00
}
2020-05-28 22:41:19 +00:00
var reverseAgentMapEnum = map [ int ] string { { { range $ index , $ el := . AllAgentNames } }
{ { $ index } } : "{{$el}}" , { { end } }
2018-01-09 07:39:29 +00:00
}
2020-05-28 22:41:19 +00:00
var markToAgent = map [ string ] string { { { range $ index , $ el := . AllAgentMarkNames } }
"{{$el}}" : "{{index $.AllAgentMarks $el}}" , { { end } }
2018-02-03 05:47:14 +00:00
}
2020-05-28 22:41:19 +00:00
var markToID = map [ string ] int { { { range $ index , $ el := . AllAgentMarkNames } }
"{{$el}}" : { { index $ . AllAgentMarkIDs $ el } } , { { end } }
2020-03-08 04:15:27 +00:00
}
2018-02-03 05:47:14 +00:00
/ * var agentRank = map [ string ] int {
"opera" : 9 ,
"chrome" : 8 ,
"safari" : 1 ,
} * /
2017-12-19 03:53:13 +00:00
2019-03-21 22:59:41 +00:00
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
2019-07-12 08:33:25 +00:00
type HTTPSRedirect struct { }
2019-03-21 22:59:41 +00:00
func ( red * HTTPSRedirect ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
w . Header ( ) . Set ( "Connection" , "close" )
2019-10-29 07:13:04 +00:00
co . RouteViewCounter . Bump ( { { index . AllRouteMap "routes.HTTPSRedirect" } } )
2019-03-21 22:59:41 +00:00
dest := "https://" + req . Host + req . URL . String ( )
http . Redirect ( w , req , dest , http . StatusTemporaryRedirect )
}
2020-03-08 04:15:27 +00:00
func ( r * GenRouter ) SuspiciousRequest ( req * http . Request , pre string ) {
2021-03-24 08:08:37 +00:00
if c . Config . DisableSuspLog {
return
}
2021-03-18 05:10:20 +00:00
var sb strings . Builder
2020-03-08 04:15:27 +00:00
if pre != "" {
2021-03-23 09:57:50 +00:00
sb . WriteString ( "Suspicious Request\n" )
2021-02-27 06:52:51 +00:00
} else {
pre = "Suspicious Request"
2018-04-03 04:34:07 +00:00
}
2021-03-23 09:57:50 +00:00
r . ddumpRequest ( req , pre , r . suspLog , & sb )
2019-10-29 07:13:04 +00:00
co . AgentViewCounter . Bump ( { { . AllAgentMap . suspicious } } )
2018-01-27 07:30:44 +00:00
}
2018-06-17 07:28:18 +00:00
// TODO: Pass the default path or config struct to the router rather than accessing it via a package global
// TODO: SetDefaultPath
// TODO: GetDefaultPath
2018-11-13 06:51:34 +00:00
func ( r * GenRouter ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
2021-04-08 02:20:57 +00:00
// HTTP/1.1 hanging conn fix
2021-04-10 10:06:22 +00:00
/ * if req . ProtoMajor == 1 && c . Dev . ExpFix1 {
2021-04-08 02:20:57 +00:00
defer func ( ) {
2021-04-10 10:06:22 +00:00
//io.Copy(ioutil.Discard, req.Body)
2021-04-08 02:20:57 +00:00
req . Body . Close ( )
} ( )
2021-04-10 10:06:22 +00:00
} * /
2019-10-29 07:13:04 +00:00
malformedRequest := func ( typ int ) {
2019-04-01 05:44:38 +00:00
w . WriteHeader ( 200 ) // 400
w . Write ( [ ] byte ( "" ) )
2019-04-14 00:13:32 +00:00
r . DumpRequest ( req , "Malformed Request T" + strconv . Itoa ( typ ) )
2019-10-29 07:13:04 +00:00
co . AgentViewCounter . Bump ( { { . AllAgentMap . malformed } } )
2019-04-01 05:44:38 +00:00
}
// Split the Host and Port string
var shost , sport string
if req . Host [ 0 ] == '[' {
spl := strings . Split ( req . Host , "]" )
if len ( spl ) > 2 {
2019-04-14 00:13:32 +00:00
malformedRequest ( 0 )
2019-04-01 05:44:38 +00:00
return
}
shost = strings . TrimPrefix ( spl [ 0 ] , "[" )
sport = strings . TrimPrefix ( spl [ 1 ] , ":" )
2020-03-27 06:05:35 +00:00
} else if strings . Contains ( req . Host , ":" ) {
2019-04-01 05:44:38 +00:00
spl := strings . Split ( req . Host , ":" )
if len ( spl ) > 2 {
2019-04-14 00:13:32 +00:00
malformedRequest ( 1 )
2019-04-01 05:44:38 +00:00
return
}
shost = spl [ 0 ]
2020-03-27 06:05:35 +00:00
//if len(spl)==2 {
2019-04-01 05:44:38 +00:00
sport = spl [ 1 ]
2020-03-27 06:05:35 +00:00
//}
} else {
shost = req . Host
2019-04-01 05:44:38 +00:00
}
// TODO: Reject requests from non-local IPs, if the site host is set to localhost or a localhost IP
2019-04-19 07:16:09 +00:00
if ! c . Config . LoosePort && c . Site . PortInt != 80 && c . Site . PortInt != 443 && sport != c . Site . Port {
2019-04-14 00:13:32 +00:00
malformedRequest ( 2 )
2019-04-01 05:44:38 +00:00
return
}
// Redirect www. and local IP requests to the right place
2020-03-06 03:07:28 +00:00
if strings . HasPrefix ( shost , "www." ) || c . Site . LocalHost {
2019-04-19 07:16:09 +00:00
if shost == "www." + c . Site . Host || ( c . Site . LocalHost && shost != c . Site . Host && isLocalHost ( shost ) ) {
2019-03-31 01:50:59 +00:00
// TODO: Abstract the redirect logic?
2018-01-28 14:30:24 +00:00
w . Header ( ) . Set ( "Connection" , "close" )
2021-03-23 09:57:50 +00:00
var s , p string
2019-11-04 11:55:52 +00:00
if c . Config . SslSchema {
2018-01-28 14:30:24 +00:00
s = "s"
}
2019-04-19 07:16:09 +00:00
if c . Site . PortInt != 80 && c . Site . PortInt != 443 {
p = ":" + c . Site . Port
2019-04-01 05:44:38 +00:00
}
2019-04-19 07:16:09 +00:00
dest := "http" + s + "://" + c . Site . Host + p + req . URL . Path
2018-01-28 14:30:24 +00:00
if len ( req . URL . RawQuery ) > 0 {
dest += "?" + req . URL . RawQuery
}
http . Redirect ( w , req , dest , http . StatusMovedPermanently )
return
}
2020-03-06 03:07:28 +00:00
}
2018-01-28 14:30:24 +00:00
// Deflect malformed requests
2019-04-19 07:16:09 +00:00
if len ( req . URL . Path ) == 0 || req . URL . Path [ 0 ] != '/' || ( ! c . Config . LooseHost && shost != c . Site . Host ) {
2019-04-14 00:13:32 +00:00
malformedRequest ( 3 )
2017-04-12 13:39:03 +00:00
return
}
2021-03-23 09:57:50 +00:00
r . suspScan ( req )
2018-06-17 07:28:18 +00:00
// Indirect the default route onto a different one
if req . URL . Path == "/" {
2019-04-19 07:16:09 +00:00
req . URL . Path = c . Config . DefaultPath
2018-06-17 07:28:18 +00:00
}
2020-02-26 10:34:38 +00:00
//log.Print("URL.Path: ", req.URL.Path)
2020-03-22 06:24:31 +00:00
prefix := req . URL . Path [ 0 : strings . IndexByte ( req . URL . Path [ 1 : ] , '/' ) + 1 ]
2018-08-21 08:00:35 +00:00
2019-04-10 07:40:47 +00:00
// TODO: Use the same hook table as downstream
2019-04-19 07:16:09 +00:00
hTbl := c . GetHookTable ( )
2020-05-26 21:08:13 +00:00
skip , ferr := c . H_router_after_filters_hook ( hTbl , w , req , prefix )
2019-04-10 07:40:47 +00:00
if skip || ferr != nil {
return
}
2018-08-21 08:00:35 +00:00
if prefix != "/ws" {
h := w . Header ( )
h . Set ( "X-Frame-Options" , "deny" )
h . Set ( "X-XSS-Protection" , "1; mode=block" ) // TODO: Remove when we add a CSP? CSP's are horrendously glitchy things, tread with caution before removing
h . Set ( "X-Content-Type-Options" , "nosniff" )
2019-11-04 11:55:52 +00:00
if c . Config . RefNoRef || ! c . Config . SslSchema {
2019-08-14 10:39:04 +00:00
h . Set ( "Referrer-Policy" , "no-referrer" )
} else {
h . Set ( "Referrer-Policy" , "strict-origin" )
}
2021-04-10 10:06:22 +00:00
h . Set ( "Permissions-Policy" , "interest-cohort=()" )
2018-08-21 08:00:35 +00:00
}
2017-08-06 15:22:18 +00:00
2019-04-19 07:16:09 +00:00
if c . Dev . SuperDebug {
2018-11-13 06:51:34 +00:00
r . DumpRequest ( req , "before routes.StaticFile" )
2017-08-06 15:22:18 +00:00
}
2018-02-19 04:26:01 +00:00
// Increment the request counter
2020-02-26 10:34:38 +00:00
if ! c . Config . DisableAnalytics {
co . GlobalViewCounter . Bump ( )
}
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
2019-08-14 10:39:04 +00:00
if prefix == "/s" { //old prefix: /static
2020-02-26 10:34:38 +00:00
if ! c . Config . DisableAnalytics {
co . RouteViewCounter . Bump ( { { index . AllRouteMap "routes.StaticFile" } } )
}
2018-02-03 05:47:14 +00:00
routes . StaticFile ( w , req )
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
return
}
2020-03-27 06:05:35 +00:00
// TODO: Handle JS routes
2019-04-19 07:16:09 +00:00
if atomic . LoadInt32 ( & c . IsDBDown ) == 1 {
c . DatabaseError ( w , req )
2018-07-15 10:29:31 +00:00
return
}
2019-04-19 07:16:09 +00:00
if c . Dev . SuperDebug {
2021-03-23 09:57:50 +00:00
r . reqLogger . Print ( "before PreRoute" )
2017-07-17 10:23:42 +00:00
}
2017-12-10 03:43:30 +00:00
2020-02-25 04:25:50 +00:00
/ * if c . Dev . QuicPort != 0 {
2021-03-18 05:10:20 +00:00
sQuicPort := strconv . Itoa ( c . Dev . QuicPort )
w . Header ( ) . Set ( "Alt-Svc" , "quic=\":" + sQuicPort + "\"; ma=2592000; v=\"44,43,39\", h3-23=\":" + sQuicPort + "\"; ma=3600, h3-24=\":" + sQuicPort + "\"; ma=3600, h2=\":443\"; ma=3600" )
2020-02-25 04:25:50 +00:00
} * /
2018-01-09 07:39:29 +00:00
// Track the user agents. Unfortunately, everyone pretends to be Mozilla, so this'll be a little less efficient than I would like.
// TODO: Add a setting to disable this?
// TODO: Use a more efficient detector instead of smashing every possible combination in
2021-02-17 05:27:56 +00:00
// TODO: Make this testable
2020-03-08 04:15:27 +00:00
var agent int
2020-02-26 10:34:38 +00:00
if ! c . Config . DisableAnalytics {
ua := strings . TrimSpace ( strings . Replace ( strings . TrimPrefix ( req . UserAgent ( ) , "Mozilla/5.0 " ) , " Safari/537.36" , "" , - 1 ) ) // Noise, no one's going to be running this and it would require some sort of agent ranking system to determine which identifier should be prioritised over another
2018-02-03 05:47:14 +00:00
if ua == "" {
2019-10-29 07:13:04 +00:00
co . AgentViewCounter . Bump ( { { . AllAgentMap . blank } } )
2021-03-18 05:10:20 +00:00
r . unknownUA ( req )
2018-12-31 09:03:49 +00:00
} else {
2018-02-03 05:47:14 +00:00
// WIP UA Parser
2020-05-26 21:08:13 +00:00
//var ii = uaBufPool.Get()
var buf [ ] byte
//if ii != nil {
// buf = ii.([]byte)
//}
2018-02-03 05:47:14 +00:00
var items [ ] string
2020-02-28 05:47:43 +00:00
var os int
2020-02-28 23:11:07 +00:00
for _ , it := range uutils . StringToBytes ( ua ) {
2021-02-17 05:27:56 +00:00
if ( it > 64 && it < 91 ) || ( it > 96 && it < 123 ) || ( it > 47 && it < 58 ) || it == '_' {
2020-03-27 06:05:35 +00:00
// TODO: Store an index and slice that instead?
2020-05-26 21:08:13 +00:00
buf = append ( buf , it )
2021-02-17 05:27:56 +00:00
} else if it == ' ' || it == '(' || it == ')' || it == '-' || it == ';' || it == ':' || it == '.' || it == '+' || it == '~' || it == '@' /*|| (it == ':' && bytes.Equal(buf,[]byte("http")))*/ || it == ',' || it == '/' {
//log.Print("buf: ",string(buf))
//log.Print("it: ",string(it))
2020-05-26 21:08:13 +00:00
if len ( buf ) != 0 {
if len ( buf ) > 2 {
2018-12-31 09:03:49 +00:00
// Use an unsafe zero copy conversion here just to use the switch, it's not safe for this string to escape from here, as it will get mutated, so do a regular string conversion in append
2020-05-26 21:08:13 +00:00
switch ( uutils . BytesToString ( buf ) ) {
2018-12-31 09:03:49 +00:00
case "Windows" :
2020-02-28 05:47:43 +00:00
os = { { . AllOSMap . windows } }
2018-12-31 09:03:49 +00:00
case "Linux" :
2020-02-28 05:47:43 +00:00
os = { { . AllOSMap . linux } }
2018-12-31 09:03:49 +00:00
case "Mac" :
2020-02-28 05:47:43 +00:00
os = { { . AllOSMap . mac } }
2018-12-31 09:03:49 +00:00
case "iPhone" :
2020-02-28 05:47:43 +00:00
os = { { . AllOSMap . iphone } }
2018-12-31 09:03:49 +00:00
case "Android" :
2020-02-28 05:47:43 +00:00
os = { { . AllOSMap . android } }
2021-02-17 05:27:56 +00:00
case "like" , "compatible" , "NT" , "X" , "com" , "KHTML" :
2019-02-10 09:32:05 +00:00
// Skip these words
2018-12-31 09:03:49 +00:00
default :
2021-02-17 05:27:56 +00:00
//log.Print("append buf")
2020-05-26 21:08:13 +00:00
items = append ( items , string ( buf ) )
2018-12-31 09:03:49 +00:00
}
}
2021-02-17 05:27:56 +00:00
//log.Print("reset buf")
2020-05-26 21:08:13 +00:00
buf = buf [ : 0 ]
2018-02-04 08:15:20 +00:00
}
} else {
// TODO: Test this
items = items [ : 0 ]
2021-03-25 23:06:35 +00:00
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 ) )
}
2018-02-04 08:15:20 +00:00
break
2018-02-03 05:47:14 +00:00
}
}
2020-05-26 21:08:13 +00:00
//uaBufPool.Put(buf)
2018-02-03 05:47:14 +00:00
// Iterate over this in reverse as the real UA tends to be on the right side
for i := len ( items ) - 1 ; i >= 0 ; i -- {
2020-03-08 04:15:27 +00:00
//fAgent, ok := markToAgent[items[i]]
fAgent , ok := markToID [ items [ i ] ]
2018-02-03 05:47:14 +00:00
if ok {
agent = fAgent
2020-03-08 04:15:27 +00:00
if agent != { { . AllAgentMap . safari } } {
2018-02-03 05:47:14 +00:00
break
}
}
}
2019-04-19 07:16:09 +00:00
if c . Dev . SuperDebug {
2021-03-23 09:57:50 +00:00
r . reqLogger . Print ( "parsed agent: " , agent , "\nos: " , os )
r . reqLogger . Printf ( "items: %+v\n" , items )
2020-05-26 21:08:13 +00:00
/ * for _ , it := range items {
2021-03-23 09:57:50 +00:00
r . reqLogger . Printf ( "it: %+v\n" , string ( it ) )
2020-05-26 21:08:13 +00:00
} * /
2018-02-03 05:47:14 +00:00
}
// Special handling
switch ( agent ) {
2020-03-08 04:15:27 +00:00
case { { . AllAgentMap . chrome } } :
2020-02-28 05:47:43 +00:00
if os == { { . AllOSMap . android } } {
2020-03-08 04:15:27 +00:00
agent = { { . AllAgentMap . androidchrome } }
2018-02-04 08:15:20 +00:00
}
2020-03-08 04:15:27 +00:00
case { { . AllAgentMap . safari } } :
2020-02-28 05:47:43 +00:00
if os == { { . AllOSMap . iphone } } {
2020-03-08 04:15:27 +00:00
agent = { { . AllAgentMap . mobilesafari } }
2018-02-04 08:15:20 +00:00
}
2020-03-08 04:15:27 +00:00
case { { . AllAgentMap . trident } } :
2018-02-04 08:15:20 +00:00
// Hack to support IE11, change this after we start logging versions
if strings . Contains ( ua , "rv:11" ) {
2020-03-08 04:15:27 +00:00
agent = { { . AllAgentMap . internetexplorer } }
2018-02-03 05:47:14 +00:00
}
2020-03-08 04:15:27 +00:00
case { { . AllAgentMap . zgrab } } :
2021-01-02 21:46:24 +00:00
w . WriteHeader ( 200 ) // 400
w . Write ( [ ] byte ( "" ) )
r . DumpRequest ( req , "Blocked Scanner" )
co . AgentViewCounter . Bump ( { { . AllAgentMap . zgrab } } )
return
2018-02-03 05:47:14 +00:00
}
2020-03-08 04:15:27 +00:00
if agent == 0 {
2020-05-26 21:08:13 +00:00
//co.AgentViewCounter.Bump({{.AllAgentMap.unknown}})
2021-03-18 05:10:20 +00:00
r . unknownUA ( req )
2020-05-26 21:08:13 +00:00
} // else {
2020-03-08 04:15:27 +00:00
//co.AgentViewCounter.Bump(agentMapEnum[agent])
co . AgentViewCounter . Bump ( agent )
2020-05-26 21:08:13 +00:00
//}
2020-02-28 05:47:43 +00:00
co . OSViewCounter . Bump ( os )
2018-01-09 07:39:29 +00:00
}
2018-02-05 10:29:13 +00:00
2018-03-08 03:59:47 +00:00
// TODO: Do we want to track missing language headers too? Maybe as it's own type, e.g. "noheader"?
2020-03-08 04:15:27 +00:00
// TODO: Default to anything other than en, if anything else is present, to avoid over-representing it for multi-linguals?
2018-03-08 03:59:47 +00:00
lang := req . Header . Get ( "Accept-Language" )
if lang != "" {
2020-03-27 06:05:35 +00:00
// TODO: Reduce allocs here
2020-02-26 10:34:38 +00:00
lLang := strings . Split ( strings . TrimSpace ( lang ) , "-" )
2019-02-28 07:28:17 +00:00
tLang := strings . Split ( strings . Split ( lLang [ 0 ] , ";" ) [ 0 ] , "," )
2019-04-19 07:16:09 +00:00
c . DebugDetail ( "tLang:" , tLang )
2019-02-28 07:28:17 +00:00
var llLang string
for _ , seg := range tLang {
if seg == "*" {
continue
}
llLang = seg
2019-03-03 02:28:17 +00:00
break
2019-02-28 07:28:17 +00:00
}
2019-04-19 07:16:09 +00:00
c . DebugDetail ( "llLang:" , llLang )
2020-03-08 04:15:27 +00:00
if ! co . LangViewCounter . Bump ( llLang ) {
2020-02-25 04:25:50 +00:00
r . DumpRequest ( req , "Invalid ISO Code" )
2019-02-23 06:29:19 +00:00
}
2018-03-17 08:16:43 +00:00
} else {
2020-03-08 04:15:27 +00:00
co . LangViewCounter . Bump2 ( 0 )
2018-03-08 03:59:47 +00:00
}
2019-08-14 10:39:04 +00:00
if ! c . Config . RefNoTrack {
2020-04-09 09:30:51 +00:00
ae := req . Header . Get ( "Accept-Encoding" )
likelyBot := ae == "gzip" || ae == ""
if ! likelyBot {
ref := req . Header . Get ( "Referer" ) // Check the 'referrer' header too? :P
2021-03-02 08:47:36 +00:00
// TODO: Extend the effects of DNT elsewhere?
if ref != "" && req . Header . Get ( "DNT" ) != "1" {
2020-04-09 09:30:51 +00:00
// ? Optimise this a little?
ref = strings . TrimPrefix ( strings . TrimPrefix ( ref , "http://" ) , "https://" )
ref = strings . Split ( ref , "/" ) [ 0 ]
portless := strings . Split ( ref , ":" ) [ 0 ]
// TODO: Handle c.Site.Host in uppercase too?
if portless != "localhost" && portless != "127.0.0.1" && portless != c . Site . Host {
2020-04-13 03:28:52 +00:00
r . DumpRequest ( req , "Ref Route" )
2020-04-09 09:30:51 +00:00
co . ReferrerTracker . Bump ( ref )
}
2019-08-14 10:39:04 +00:00
}
2018-02-05 10:29:13 +00:00
}
}
2020-02-26 10:34:38 +00:00
}
2017-07-17 10:23:42 +00:00
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
// Deal with the session stuff, etc.
2020-03-18 09:21:34 +00:00
ucpy , ok := c . PreRoute ( w , req )
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
if ! ok {
return
}
2020-03-18 09:21:34 +00:00
user := & ucpy
2019-04-02 07:43:11 +00:00
user . LastAgent = agent
2019-04-19 07:16:09 +00:00
if c . Dev . SuperDebug {
2021-03-23 09:57:50 +00:00
r . reqLogger . Print (
2018-04-03 04:34:07 +00:00
"after PreRoute\n" +
"routeMapEnum: " , routeMapEnum )
2017-07-17 10:23:42 +00:00
}
2020-03-27 06:05:35 +00:00
//log.Println("req: ", req)
2018-08-11 15:53:42 +00:00
2018-08-21 08:00:35 +00:00
// Disable Gzip when SSL is disabled for security reasons?
2020-05-26 21:08:13 +00:00
if prefix != "/ws" {
ae := req . Header . Get ( "Accept-Encoding" )
/ * if strings . Contains ( ae , "br" ) {
h := w . Header ( )
h . Set ( "Content-Encoding" , "br" )
var ii = brPool . Get ( )
var igzw * brotli . Writer
if ii == nil {
igzw = brotli . NewWriter ( w )
} else {
igzw = ii . ( * brotli . Writer )
igzw . Reset ( w )
}
gzw := c . BrResponseWriter { Writer : igzw , ResponseWriter : w }
defer func ( ) {
//h := w.Header()
if h . Get ( "Content-Encoding" ) == "br" && h . Get ( "X-I" ) == "" {
//log.Print("push br close")
igzw := gzw . Writer . ( * brotli . Writer )
igzw . Close ( )
brPool . Put ( igzw )
}
} ( )
w = gzw
} else * / if strings . Contains ( ae , "gzip" ) {
h := w . Header ( )
h . Set ( "Content-Encoding" , "gzip" )
var ii = gzipPool . Get ( )
var igzw * gzip . Writer
if ii == nil {
igzw = gzip . NewWriter ( w )
} else {
igzw = ii . ( * gzip . Writer )
igzw . Reset ( w )
2018-08-22 07:10:29 +00:00
}
2020-05-26 21:08:13 +00:00
gzw := c . GzipResponseWriter { Writer : igzw , ResponseWriter : w }
defer func ( ) {
//h := w.Header()
if h . Get ( "Content-Encoding" ) == "gzip" && h . Get ( "X-I" ) == "" {
//log.Print("push gzip close")
igzw := gzw . Writer . ( * gzip . Writer )
igzw . Close ( )
gzipPool . Put ( igzw )
}
} ( )
w = gzw
}
2018-08-21 08:00:35 +00:00
}
2018-11-12 09:23:36 +00:00
2020-05-26 21:08:13 +00:00
skip , ferr = c . H_router_pre_route_hook ( hTbl , w , req , user , prefix )
2020-03-27 06:05:35 +00:00
if skip || ferr != nil {
r . handleError ( ferr , w , req , user )
return
}
2020-03-22 23:14:08 +00:00
var extraData string
if req . URL . Path [ len ( req . URL . Path ) - 1 ] != '/' {
extraData = req . URL . Path [ strings . LastIndexByte ( req . URL . Path , '/' ) + 1 : ]
req . URL . Path = req . URL . Path [ : strings . LastIndexByte ( req . URL . Path , '/' ) + 1 ]
}
2019-04-06 01:08:49 +00:00
ferr = r . routeSwitch ( w , req , user , prefix , extraData )
2018-11-12 09:23:36 +00:00
if ferr != nil {
2018-11-13 06:51:34 +00:00
r . handleError ( ferr , w , req , user )
2020-03-27 06:05:35 +00:00
return
2018-11-12 09:23:36 +00:00
}
2020-02-26 10:34:38 +00:00
/ * if ! c . Config . DisableAnalytics {
co . RouteViewCounter . Bump ( id )
} * /
2019-04-06 01:08:49 +00:00
hTbl . VhookNoRet ( "router_end" , w , req , user , prefix , extraData )
2019-04-19 07:16:09 +00:00
//c.StoppedServer("Profile end")
2018-08-11 15:53:42 +00:00
}
2017-07-17 10:23:42 +00:00
2020-03-18 09:21:34 +00:00
func ( r * GenRouter ) routeSwitch ( w http . ResponseWriter , req * http . Request , user * c . User , prefix , extraData string ) /*(id int, orerr */ c . RouteError /*)*/ {
2019-04-19 07:16:09 +00:00
var err c . RouteError
2020-02-29 01:10:39 +00:00
cn := uutils . Nanotime ( )
2017-04-13 09:26:40 +00:00
switch ( prefix ) { ` + out + `
2017-12-24 07:38:46 +00:00
/ * case "/sitemaps" : // TODO: Count these views
req . URL . Path += extraData
2018-08-22 02:29:11 +00:00
err = sitemapSwitch ( w , req ) * /
2020-03-13 06:37:18 +00:00
// ! Temporary fix for certain bots
case "/static" :
w . Header ( ) . Set ( "Connection" , "close" )
http . Redirect ( w , req , "/s/" + extraData , http . StatusTemporaryRedirect )
2018-08-22 02:13:17 +00:00
case "/uploads" :
if extraData == "" {
2020-03-07 05:28:25 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.UploadedFile" } } , cn )
2019-04-19 07:16:09 +00:00
return c . NotFound ( w , req , nil )
2018-08-22 02:13:17 +00:00
}
2021-02-17 05:27:56 +00:00
w = r . responseWriter ( w )
2018-08-22 02:13:17 +00:00
req . URL . Path += extraData
// TODO: Find a way to propagate errors up from this?
2018-11-13 06:51:34 +00:00
r . UploadHandler ( w , req ) // TODO: Count these views
2020-03-07 05:28:25 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.UploadedFile" } } , cn )
2018-11-12 09:23:36 +00:00
return nil
2017-04-13 09:26:40 +00:00
case "" :
2017-08-06 15:22:18 +00:00
// Stop the favicons, robots.txt file, etc. resolving to the topics list
2017-09-10 16:57:22 +00:00
// TODO: Add support for favicons and robots.txt files
2017-12-19 03:53:13 +00:00
switch ( extraData ) {
2017-08-06 15:22:18 +00:00
case "robots.txt" :
2020-03-07 05:28:25 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.RobotsTxt" } } , cn )
2018-11-12 09:23:36 +00:00
return routes . RobotsTxt ( w , req )
2019-02-28 07:28:17 +00:00
case "favicon.ico" :
2021-02-17 05:27:56 +00:00
w = r . responseWriter ( w )
2019-08-14 10:39:04 +00:00
req . URL . Path = "/s/favicon.ico"
2020-03-04 23:56:45 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.Favicon" } } , cn )
2021-03-23 09:57:50 +00:00
routes . StaticFile ( w , req )
2019-02-28 07:28:17 +00:00
return nil
2019-05-08 05:08:38 +00:00
case "opensearch.xml" :
2020-03-07 05:28:25 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.OpenSearchXml" } } , cn )
2019-05-08 05:08:38 +00:00
return routes . OpenSearchXml ( w , req )
2017-12-24 07:38:46 +00:00
/ * case "sitemap.xml" :
2020-03-07 05:28:25 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.SitemapXml" } } , cn )
2018-11-12 09:23:36 +00:00
return routes . SitemapXml ( w , req ) * /
2017-08-06 15:22:18 +00:00
}
2020-02-26 10:34:38 +00:00
co . RouteViewCounter . Bump ( { { index . AllRouteMap "routes.Error" } } )
2019-04-19 07:16:09 +00:00
return c . NotFound ( w , req , nil )
2017-10-30 09:57:08 +00:00
default :
2018-11-13 06:51:34 +00:00
// A fallback for dynamic routes, e.g. ones declared by plugins
r . RLock ( )
2020-02-26 10:34:38 +00:00
h , ok := r . extraRoutes [ req . URL . Path ]
2018-11-13 06:51:34 +00:00
r . RUnlock ( )
2020-03-27 12:00:29 +00:00
req . URL . Path += extraData
2017-10-30 09:57:08 +00:00
if ok {
2020-02-26 10:34:38 +00:00
// TODO: Be more specific about *which* dynamic route it is
co . RouteViewCounter . Bump ( { { index . AllRouteMap "routes.DynamicRoute" } } )
return h ( w , req , user )
2017-10-30 09:57:08 +00:00
}
2020-03-27 12:00:29 +00:00
co . RouteViewCounter . Bump3 ( { { index . AllRouteMap "routes.BadRoute" } } , cn )
2018-01-26 05:53:34 +00:00
2021-03-24 08:08:37 +00:00
if ! c . Config . DisableSuspLog {
2020-02-25 04:25:50 +00:00
lp := strings . ToLower ( req . URL . Path )
2021-03-23 09:57:50 +00:00
if strings . Contains ( lp , "w" ) {
if strings . Contains ( lp , "wp" ) || strings . Contains ( lp , "wordpress" ) || strings . Contains ( lp , "wget" ) || strings . Contains ( lp , "wp-" ) {
r . SuspiciousRequest ( req , "Bad Route" )
return c . MicroNotFound ( w , req )
}
}
if strings . Contains ( lp , "admin" ) || strings . Contains ( lp , "sql" ) || strings . Contains ( lp , "manage" ) || strings . Contains ( lp , "//" ) || strings . Contains ( lp , "\\\\" ) || strings . Contains ( lp , "config" ) || strings . Contains ( lp , "setup" ) || strings . Contains ( lp , "install" ) || strings . Contains ( lp , "update" ) || strings . Contains ( lp , "php" ) || strings . Contains ( lp , "pl" ) || strings . Contains ( lp , "include" ) || strings . Contains ( lp , "vendor" ) || strings . Contains ( lp , "bin" ) || strings . Contains ( lp , "system" ) || strings . Contains ( lp , "eval" ) || strings . Contains ( lp , "config" ) {
2018-11-13 06:51:34 +00:00
r . SuspiciousRequest ( req , "Bad Route" )
2020-03-27 12:00:29 +00:00
return c . MicroNotFound ( w , req )
2018-01-27 07:30:44 +00:00
}
2021-03-24 08:08:37 +00:00
}
2020-04-13 03:28:52 +00:00
2021-03-25 23:06:35 +00:00
if ! c . Config . DisableBadRouteLog {
r . DumpRequest ( req , "Bad Route" )
}
2020-04-13 03:28:52 +00:00
ae := req . Header . Get ( "Accept-Encoding" )
likelyBot := ae == "gzip" || ae == ""
if likelyBot {
return c . MicroNotFound ( w , req )
}
2019-04-19 07:16:09 +00:00
return c . NotFound ( w , req , nil )
2017-04-12 13:39:03 +00:00
}
2018-11-12 09:23:36 +00:00
return err
2017-04-12 10:10:36 +00:00
}
`
2019-10-20 01:42:19 +00:00
tmpl := template . Must ( template . New ( "router" ) . Parse ( fileData ) )
2017-12-19 03:53:13 +00:00
var b bytes . Buffer
2019-10-20 01:42:19 +00:00
if err := tmpl . Execute ( & b , tmplVars ) ; err != nil {
2017-12-19 03:53:13 +00:00
log . Fatal ( err )
}
2021-02-27 06:52:51 +00:00
writeFile ( "./gen_router.go" , b . String ( ) )
2017-08-13 11:22:34 +00:00
log . Println ( "Successfully generated the router" )
2017-04-12 10:10:36 +00:00
}
2020-02-25 04:25:50 +00:00
func writeFile ( name , content string ) {
2021-03-23 09:57:50 +00:00
f , e := os . Create ( name )
if e != nil {
log . Fatal ( e )
2017-04-12 10:10:36 +00:00
}
2021-03-23 09:57:50 +00:00
_ , e = f . WriteString ( content )
if e != nil {
log . Fatal ( e )
2017-04-12 10:10:36 +00:00
}
2021-03-23 09:57:50 +00:00
if e = f . Sync ( ) ; e != nil {
log . Fatal ( e )
2020-02-25 04:25:50 +00:00
}
2021-03-23 09:57:50 +00:00
if e = f . Close ( ) ; e != nil {
log . Fatal ( e )
2020-02-26 10:34:38 +00:00
}
2017-04-12 10:10:36 +00:00
}