2020-04-19 12:42:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-05-08 15:52:17 +00:00
|
|
|
"log"
|
2020-04-19 12:42:56 +00:00
|
|
|
)
|
|
|
|
|
2020-05-12 12:48:21 +00:00
|
|
|
const (
|
|
|
|
FileRemapSeparatorStr = " -> "
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileRemap struct {
|
2020-05-12 13:04:16 +00:00
|
|
|
Regex *regexp.Regexp
|
|
|
|
Template string
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 12:10:31 +00:00
|
|
|
/* Pre-compile gophermap file string regex */
|
|
|
|
func compileGophermapCheckRegex() *regexp.Regexp {
|
|
|
|
return regexp.MustCompile(`^(|.+/|.+\.)gophermap$`)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-compile cgi-bin path string regex */
|
|
|
|
func compileCgiBinCheckRegex() *regexp.Regexp {
|
|
|
|
return regexp.MustCompile(`^cgi-bin(|/.*)$`)
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Compile a user supplied new line separated list of regex statements */
|
|
|
|
func compileUserRestrictedRegex(restrictions string) []*regexp.Regexp {
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Return slice */
|
2020-05-12 12:48:21 +00:00
|
|
|
restrictedRegex := make([]*regexp.Regexp, 0)
|
2020-04-20 14:18:35 +00:00
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Split the user supplied regex statements by new line */
|
|
|
|
for _, expr := range strings.Split(restrictions, "\n") {
|
|
|
|
/* Empty expression, skip */
|
2020-05-05 22:46:50 +00:00
|
|
|
if len(expr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-19 12:42:56 +00:00
|
|
|
|
2020-05-12 12:48:21 +00:00
|
|
|
/* Try compile regex */
|
2020-05-05 22:46:50 +00:00
|
|
|
regex, err := regexp.Compile(expr)
|
|
|
|
if err != nil {
|
2020-05-08 15:52:17 +00:00
|
|
|
log.Fatalf("Failed compiling user supplied regex: %s\n", expr)
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
2020-05-12 12:48:21 +00:00
|
|
|
|
|
|
|
/* Append restricted */
|
|
|
|
restrictedRegex = append(restrictedRegex, regex)
|
|
|
|
Config.SysLog.Info("", "Compiled restricted: %s\n", expr)
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 12:48:21 +00:00
|
|
|
return restrictedRegex
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 12:48:21 +00:00
|
|
|
/* Compile a user supplied new line separated list of file remap regex statements */
|
|
|
|
func compileUserRemapRegex(remaps string) []*FileRemap {
|
|
|
|
/* Return slice */
|
|
|
|
fileRemaps := make([]*FileRemap, 0)
|
|
|
|
|
|
|
|
/* Split the user supplied regex statements by new line */
|
|
|
|
for _, expr := range strings.Split(remaps, "\n") {
|
|
|
|
/* Empty expression, skip */
|
|
|
|
if len(expr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split into alias and remap string (MUST BE LENGTH 2) */
|
|
|
|
split := strings.Split(expr, FileRemapSeparatorStr)
|
|
|
|
if len(split) != 2 {
|
|
|
|
continue
|
2020-04-20 14:18:35 +00:00
|
|
|
}
|
2020-05-05 22:46:50 +00:00
|
|
|
|
2020-05-12 12:48:21 +00:00
|
|
|
/* Try compile regex */
|
2020-05-12 13:04:16 +00:00
|
|
|
regex, err := regexp.Compile("(?m)"+strings.TrimPrefix(split[0], "/")+"$")
|
2020-05-12 12:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed compiling user supplied regex: %s\n", expr)
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
2020-05-12 12:48:21 +00:00
|
|
|
|
|
|
|
/* Append file remapper */
|
|
|
|
fileRemaps = append(fileRemaps, &FileRemap{ regex, strings.TrimPrefix(split[1], "/") })
|
|
|
|
Config.SysLog.Info("", "Compiled remap: %s\n", expr)
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
2020-05-12 12:48:21 +00:00
|
|
|
|
|
|
|
return fileRemaps
|
2020-05-05 22:46:50 +00:00
|
|
|
}
|
2020-05-14 12:10:31 +00:00
|
|
|
|
|
|
|
/* Check if file path is gophermap */
|
2020-05-14 16:54:06 +00:00
|
|
|
func isGophermap(path string) bool {
|
|
|
|
return Config.RgxGophermap.MatchString(path)
|
2020-05-14 12:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if file path within cgi-bin */
|
2020-05-14 16:54:06 +00:00
|
|
|
func withinCgiBin(path string) bool {
|
|
|
|
return Config.RgxCgiBin.MatchString(path)
|
2020-05-14 12:10:31 +00:00
|
|
|
}
|