2020-04-17 15:39:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-19 12:42:56 +00:00
|
|
|
var SingleFileExtMap = map[string]ItemType{
|
2020-04-17 15:39:25 +00:00
|
|
|
".out": TypeBin,
|
|
|
|
".a": TypeBin,
|
|
|
|
".o": TypeBin,
|
|
|
|
".ko": TypeBin, /* ... Though tbh, kernel extensions?!!! */
|
|
|
|
".msi": TypeBin,
|
|
|
|
".exe": TypeBin,
|
|
|
|
|
|
|
|
".txt": TypeFile,
|
|
|
|
".md": TypeFile,
|
|
|
|
".json": TypeFile,
|
|
|
|
".xml": TypeFile,
|
|
|
|
".yaml": TypeFile,
|
|
|
|
".ocaml": TypeFile,
|
|
|
|
".s": TypeFile,
|
|
|
|
".c": TypeFile,
|
|
|
|
".py": TypeFile,
|
|
|
|
".h": TypeFile,
|
|
|
|
".go": TypeFile,
|
|
|
|
".fs": TypeFile,
|
|
|
|
|
|
|
|
".doc": TypeDoc,
|
|
|
|
".docx": TypeDoc,
|
|
|
|
|
|
|
|
".gif": TypeGif,
|
|
|
|
|
|
|
|
".jpg": TypeImage,
|
|
|
|
".jpeg": TypeImage,
|
|
|
|
".png": TypeImage,
|
|
|
|
|
|
|
|
".html": TypeHtml,
|
|
|
|
|
|
|
|
".ogg": TypeAudio,
|
|
|
|
".mp3": TypeAudio,
|
|
|
|
".wav": TypeAudio,
|
|
|
|
".mod": TypeAudio,
|
|
|
|
".it": TypeAudio,
|
|
|
|
".xm": TypeAudio,
|
|
|
|
".mid": TypeAudio,
|
|
|
|
".vgm": TypeAudio,
|
|
|
|
|
|
|
|
".mp4": TypeVideo,
|
|
|
|
".mkv": TypeVideo,
|
|
|
|
}
|
|
|
|
|
2020-04-19 12:42:56 +00:00
|
|
|
var DoubleFileExtMap = map[string]ItemType{
|
|
|
|
".tar.gz": TypeBin,
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
func buildLine(t ItemType, name, selector, host string, port int) []byte {
|
|
|
|
ret := string(t)
|
|
|
|
|
|
|
|
/* Add name, truncate name if too long */
|
|
|
|
if len(name) > *PageWidth {
|
|
|
|
ret += name[:*PageWidth-4]+"...\t"
|
|
|
|
} else {
|
|
|
|
ret += name+"\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add selector. If too long use err, skip if empty */
|
|
|
|
selectorLen := len(selector)
|
|
|
|
if selectorLen > MaxSelectorLen {
|
|
|
|
ret += SelectorErrorStr+"\t"
|
|
|
|
} else if selectorLen > 0 {
|
|
|
|
ret += selector+"\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add host, set to nullhost if empty */
|
|
|
|
if host == "" {
|
|
|
|
ret += NullHost+"\t"
|
2020-04-17 17:12:36 +00:00
|
|
|
} else {
|
|
|
|
ret += host+"\t"
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add port, set to nullport if 0 */
|
|
|
|
if port == 0 {
|
2020-04-17 17:51:30 +00:00
|
|
|
ret += NullPort+DOSLineEnd
|
2020-04-17 15:39:25 +00:00
|
|
|
} else {
|
2020-04-17 17:51:30 +00:00
|
|
|
ret += strconv.Itoa(port)+DOSLineEnd
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildInfoLine(content string) []byte {
|
|
|
|
return buildLine(TypeInfo, content, "", "", 0)
|
|
|
|
}
|
|
|
|
|
2020-04-19 14:21:54 +00:00
|
|
|
/* getItemType(name string) ItemType:
|
|
|
|
* Here we use an empty function pointer, and set the correct
|
|
|
|
* function to be used during the restricted files regex parsing.
|
|
|
|
* This negates need to check if RestrictedFilesRegex is nil every
|
|
|
|
* single call.
|
|
|
|
*/
|
|
|
|
var getItemType func(name string) ItemType
|
|
|
|
func _getItemType(name string) ItemType {
|
|
|
|
return _checkItemType(strings.ToLower(name))
|
|
|
|
}
|
|
|
|
func _getItemTypeMatchRestricted(name string) ItemType {
|
2020-04-19 12:42:56 +00:00
|
|
|
nameLower := strings.ToLower(name)
|
|
|
|
|
|
|
|
/* If regex compiled, check if matches user restricted files */
|
|
|
|
if RestrictedFilesRegex != nil &&
|
|
|
|
RestrictedFilesRegex.MatchString(nameLower) {
|
|
|
|
return TypeBanned
|
|
|
|
}
|
|
|
|
|
2020-04-19 14:21:54 +00:00
|
|
|
return _checkItemType(nameLower)
|
|
|
|
}
|
|
|
|
func _checkItemType(nameLower string) ItemType {
|
|
|
|
/* Name MUST be lower before passed to this function */
|
|
|
|
|
2020-04-19 12:42:56 +00:00
|
|
|
/* First we look at how many '.' in name string */
|
|
|
|
switch strings.Count(nameLower, ".") {
|
|
|
|
case 0:
|
|
|
|
/* Always return TypeDefault. We can never tell */
|
|
|
|
return TypeDefault
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Get index of ".", try look in SingleFileExtMap */
|
|
|
|
i := strings.IndexByte(nameLower, '.')
|
|
|
|
fileType, ok := SingleFileExtMap[nameLower[i:]]
|
|
|
|
if ok {
|
|
|
|
return fileType
|
|
|
|
} else {
|
|
|
|
return TypeDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Get index of penultimate ".", try look in DoubleFileExtMap */
|
|
|
|
i, j := len(nameLower)-1, 0
|
|
|
|
for i >= 0 {
|
|
|
|
if nameLower[i] == '.' {
|
|
|
|
if j == 1 {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
j += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i -= 1
|
|
|
|
}
|
|
|
|
fileType, ok := DoubleFileExtMap[nameLower[i:]]
|
|
|
|
if ok {
|
|
|
|
return fileType
|
|
|
|
} else {
|
|
|
|
return TypeDefault
|
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLineType(line string) ItemType {
|
|
|
|
lineLen := len(line)
|
|
|
|
|
|
|
|
if lineLen == 0 {
|
|
|
|
return TypeInfoNotStated
|
|
|
|
} else if lineLen == 1 {
|
|
|
|
/* The only accepted types for a length 1 line */
|
|
|
|
switch ItemType(line[0]) {
|
|
|
|
case TypeEnd:
|
|
|
|
return TypeEnd
|
|
|
|
case TypeEndBeginList:
|
|
|
|
return TypeEndBeginList
|
|
|
|
case TypeComment:
|
|
|
|
return TypeComment
|
|
|
|
case TypeInfo:
|
|
|
|
return TypeInfo
|
|
|
|
case TypeTitle:
|
|
|
|
return TypeTitle
|
|
|
|
default:
|
|
|
|
return TypeUnknown
|
|
|
|
}
|
|
|
|
} else if !strings.Contains(line, string(Tab)) {
|
|
|
|
/* The only accepted types for a line with no tabs */
|
|
|
|
switch ItemType(line[0]) {
|
|
|
|
case TypeComment:
|
|
|
|
return TypeComment
|
|
|
|
case TypeTitle:
|
|
|
|
return TypeTitle
|
|
|
|
case TypeInfo:
|
|
|
|
return TypeInfo
|
|
|
|
case TypeHiddenFile:
|
|
|
|
return TypeHiddenFile
|
|
|
|
case TypeSubGophermap:
|
|
|
|
return TypeSubGophermap
|
|
|
|
case TypeExec:
|
|
|
|
return TypeExec
|
|
|
|
default:
|
|
|
|
return TypeInfoNotStated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ItemType(line[0])
|
|
|
|
}
|
|
|
|
|