add support for hidden files regex (don't show in directory scans)

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
master
kim (grufwub) 4 years ago
parent b311d2036c
commit 3ce4a5acf6

@ -195,7 +195,7 @@ func (fs *FileSystemObject) ScanDirectory(fd *os.File, p *Path, iterator func(os
fp := p.JoinPath(info.Name())
// Skip restricted files
if IsRestrictedPath(fp) || WithinCGIDir(fp) {
if IsRestrictedPath(fp) || IsHiddenPath(fp) || WithinCGIDir(fp) {
continue
}

@ -13,12 +13,18 @@ var (
// WithinCGIDir returns whether a path is within the server's specified CGI scripts directory
WithinCGIDir func(*Path) bool
// RestrictedPaths is the global slice of restricted paths
// restrictedPaths is the global slice of restricted paths
restrictedPaths []*regexp.Regexp
// IsRestrictedPath is the global function to check against restricted paths
IsRestrictedPath func(*Path) bool
// hiddenPaths is the global slice of hidden (from dir view) paths
hiddenPaths []*regexp.Regexp
// IsHiddenPath is the global function to check against hidden paths
IsHiddenPath func(*Path) bool
// requestRemaps is the global slice of remapped paths
requestRemaps []*RequestRemap
@ -73,6 +79,31 @@ func compileRestrictedPathsRegex(restrictions string) []*regexp.Regexp {
return regexes
}
// compileRestrictedPathsRegex turns a string of hidden paths into a slice of compiled regular expressions
func compileHiddenPathsRegex(hidden string) []*regexp.Regexp {
regexes := make([]*regexp.Regexp, 0)
// Split restrictions string by new lines
for _, expr := range strings.Split(hidden, "\n") {
// Skip empty expressions
if len(expr) == 0 {
continue
}
// Compile the regular expression
regex, err := regexp.Compile("(?m)" + expr + "$")
if err != nil {
SystemLog.Fatal(pathHidingRegexCompileFailStr, expr)
}
// Append compiled regex and log
regexes = append(regexes, regex)
SystemLog.Info(pathHidingRegexCompiledStr, expr)
}
return regexes
}
// compil RequestRemapRegex turns a string of remapped paths into a slice of compiled RequestRemap structures
func compileRequestRemapRegex(remaps string) []*RequestRemap {
requestRemaps := make([]*RequestRemap, 0)
@ -129,6 +160,21 @@ func isRestrictedPathDisabled(p *Path) bool {
return false
}
// isHiddenPathEnabled returns whether a Path's relative value should be hidden
func isHiddenPathEnabled(p *Path) bool {
for _, regex := range hiddenPaths {
if regex.MatchString(p.Selector()) {
return true
}
}
return false
}
// isHiddenPathDisabled always returns false, there are no hidden paths
func isHiddenPathDisabled(p *Path) bool {
return false
}
// remapRequestEnabled tries to remap a request, returning bool as to success
func remapRequestEnabled(request *Request) bool {
for _, remap := range requestRemaps {

@ -44,6 +44,7 @@ func ParseFlagsAndSetup(proto string, errorMessageFunc func(ErrorCode) string) {
cacheMax := flag.Float64(cacheFileMaxFlagStr, 1.0, cacheFileMaxDescStr)
cacheSize := flag.Uint(cacheSizeFlagStr, 100, cacheSizeDescStr)
restrictedPathsList := flag.String(restrictPathsFlagStr, "", restrictPathsDescStr)
hiddenPathsList := flag.String(hiddenPathsFlagStr, "", hiddenPathsDescStr)
remapRequestsList := flag.String(remapRequestsFlagStr, "", remapRequestsDescStr)
cgiDir := flag.String(cgiDirFlagStr, "", cgiDirDescStr)
flag.DurationVar(&maxCGIRunTime, maxCGITimeFlagStr, time.Duration(time.Second*3), maxCGITimeDescStr)
@ -114,7 +115,7 @@ func ParseFlagsAndSetup(proto string, errorMessageFunc func(ErrorCode) string) {
fileSizeMax = int64(1048576.0 * *cacheMax) // gets megabytes value in bytes
FileSystem = newFileSystemObject(int(*cacheSize))
// If no restricted files provided, set to the disabled function. Else, compile and enable
// If no restricted paths provided, set to the disabled function. Else, compile and enable
if *restrictedPathsList == "" {
SystemLog.Info(pathRestrictionsDisabledStr)
IsRestrictedPath = isRestrictedPathDisabled
@ -124,7 +125,17 @@ func ParseFlagsAndSetup(proto string, errorMessageFunc func(ErrorCode) string) {
IsRestrictedPath = isRestrictedPathEnabled
}
// If no remapped files provided, set to the disabled function. Else, compile and enable
// If no hidden paths provided, set to the disabled function. Else, compile and enable
if *hiddenPathsList == "" {
SystemLog.Info(pathHidingDisableStr)
IsHiddenPath = isHiddenPathDisabled
} else {
SystemLog.Info(pathHidingEnabledStr)
hiddenPaths = compileHiddenPathsRegex(*hiddenPathsList)
IsHiddenPath = isHiddenPathEnabled
}
// If no remapped paths provided, set to the disabled function. Else, compile and enable
if *remapRequestsList == "" {
SystemLog.Info(requestRemapDisabledStr)
RemapRequest = remapRequestDisabled

@ -53,6 +53,9 @@ const (
restrictPathsFlagStr = "restrict-paths"
restrictPathsDescStr = "Restrict paths as new-line separated list of regex statements (see documenation)"
hiddenPathsFlagStr = "hidden-paths"
hiddenPathsDescStr = "Hidden paths as new-line separated list of regex statements (see documentation)"
remapRequestsFlagStr = "remap-requests"
remapRequestsDescStr = "Remap requests as new-line separated list of remap statements (see documenation)"
@ -96,6 +99,11 @@ const (
pathRestrictRegexCompileFailStr = "Failed compiling restricted path regex: %s"
pathRestrictRegexCompiledStr = "Compiled restricted path regex: %s"
pathHidingEnabledStr = "Path hiding enabled"
pathHidingDisableStr = "Path hiding disabled"
pathHidingRegexCompileFailStr = "Failed compiling hidden path regex: %s"
pathHidingRegexCompiledStr = "Compiled hidden path regex: %s"
requestRemapEnabledStr = "Request remapping enabled"
requestRemapDisabledStr = "Request remapping disabled"
requestRemapRegexInvalidStr = "Invalid request remap regex: %s"

@ -55,7 +55,7 @@ func replacePlacementStrs(line string) string {
// Return recombined (and replaced! string)
return split[0] + "\t" +
split[1] + "\t" +
strings.Replace(split[2], "$hostname", core.Hostname, 1) + "\t" +
strings.Replace(split[2], "$host", core.Hostname, 1) + "\t" +
strings.Replace(split[3], "$port", core.FwdPort, 1)
}
@ -80,7 +80,7 @@ func appendFileListing(b []byte, file os.FileInfo, p *core.Path) []byte {
case file.Mode()&os.ModeDir != 0:
return append(b, buildLine(typeDirectory, file.Name(), p.Selector(), core.Hostname, core.FwdPort)...)
case file.Mode()&os.ModeType == 0:
t := getItemType(p.Relative())
t := getItemType(file.Name())
return append(b, buildLine(t, file.Name(), p.Selector(), core.Hostname, core.FwdPort)...)
default:
return b

@ -57,7 +57,7 @@ func readGophermap(fd *os.File, p *core.Path) ([]gophermapSection, core.Error) {
case typeHiddenFile:
// Add to hidden files map
hidden[p.JoinRelative(line[1:])] = true
hidden[line[1:]] = true
return true
case typeSubGophermap:
@ -165,6 +165,12 @@ func (s *DirectorySection) RenderAndWrite(client *core.Client) core.Error {
// Scan directory and build lines
err = core.FileSystem.ScanDirectory(fd, s.path, func(file os.FileInfo, p *core.Path) {
// Ignore hidden files!
_, ok := s.hidden[file.Name()]
if ok {
return
}
// Append new formatted file listing (if correct type)
dirContents = appendFileListing(dirContents, file, p)
})

@ -141,7 +141,7 @@ func getItemType(name string) ItemType {
splitLen := len(split)
switch splitLen {
case 0:
// Always return typeDefault, we can't tell
// We cannot tell the file type, return default
return typeDefault
default:
@ -180,11 +180,11 @@ func parseLineType(line string) ItemType {
// The only accepted types for length >= 1 and without a tab
if t == typeComment ||
t == typeTitle ||
// Not typeInfo, otherwise this
// messes with the ability for people
// to have lines with words beginning 'i'
//
// t == typeInfo ||
// Not typeInfo, otherwise this
// messes with the ability for people
// to have lines with words beginning 'i'
//
// t == typeInfo ||
t == typeHiddenFile ||
t == typeSubGophermap {
return t

Loading…
Cancel
Save