From 3ce4a5acf69d047a0dd93c187a5e1a95a960da83 Mon Sep 17 00:00:00 2001 From: "kim (grufwub)" Date: Wed, 21 Oct 2020 23:54:45 +0100 Subject: [PATCH] add support for hidden files regex (don't show in directory scans) Signed-off-by: kim (grufwub) --- core/filesystem.go | 2 +- core/regex.go | 48 +++++++++++++++++++++++++++++++++++++++- core/server.go | 15 +++++++++++-- core/string_constants.go | 8 +++++++ gopher/format.go | 4 ++-- gopher/gophermap.go | 8 ++++++- gopher/itemtype.go | 12 +++++----- 7 files changed, 84 insertions(+), 13 deletions(-) diff --git a/core/filesystem.go b/core/filesystem.go index 7f527e4..e073cbf 100644 --- a/core/filesystem.go +++ b/core/filesystem.go @@ -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 } diff --git a/core/regex.go b/core/regex.go index b4c7c90..64ae3ec 100644 --- a/core/regex.go +++ b/core/regex.go @@ -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 { diff --git a/core/server.go b/core/server.go index 34ebb7a..5a9396d 100644 --- a/core/server.go +++ b/core/server.go @@ -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 diff --git a/core/string_constants.go b/core/string_constants.go index d84fd07..09ad60d 100644 --- a/core/string_constants.go +++ b/core/string_constants.go @@ -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" diff --git a/gopher/format.go b/gopher/format.go index d3c3bb3..cdcaf96 100644 --- a/gopher/format.go +++ b/gopher/format.go @@ -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 diff --git a/gopher/gophermap.go b/gopher/gophermap.go index 5b9b5a2..c4b4f6d 100644 --- a/gopher/gophermap.go +++ b/gopher/gophermap.go @@ -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) }) diff --git a/gopher/itemtype.go b/gopher/itemtype.go index 1e7750e..45663c1 100644 --- a/gopher/itemtype.go +++ b/gopher/itemtype.go @@ -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