2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-27 12:52:29 -04:00
|
|
|
"errors"
|
2020-07-01 17:05:49 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-01 10:18:49 -04:00
|
|
|
"path/filepath"
|
2019-11-06 10:08:44 -05:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-06-27 12:52:29 -04:00
|
|
|
Port int
|
|
|
|
Hostname string
|
|
|
|
CertPath string
|
|
|
|
KeyPath string
|
|
|
|
DocBase string
|
|
|
|
HomeDocBase string
|
|
|
|
GeminiExt string
|
|
|
|
DefaultLang string
|
2020-06-28 12:34:50 -04:00
|
|
|
AccessLog string
|
|
|
|
ErrorLog string
|
2020-06-29 11:17:43 -04:00
|
|
|
ReadMollyFiles bool
|
2020-06-27 12:52:29 -04:00
|
|
|
TempRedirects map[string]string
|
|
|
|
PermRedirects map[string]string
|
2020-06-27 18:12:32 -04:00
|
|
|
MimeOverrides map[string]string
|
2020-06-27 12:52:29 -04:00
|
|
|
CGIPaths []string
|
|
|
|
SCGIPaths map[string]string
|
2020-06-28 08:47:36 -04:00
|
|
|
CertificateZones map[string][]string
|
2020-06-27 12:52:29 -04:00
|
|
|
DirectorySort string
|
|
|
|
DirectoryReverse bool
|
2020-06-27 16:57:03 -04:00
|
|
|
DirectoryTitles bool
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:40:13 -04:00
|
|
|
type MollyFile struct {
|
2020-06-27 17:44:15 -04:00
|
|
|
GeminiExt string
|
2020-06-30 14:38:46 -04:00
|
|
|
TempRedirects map[string]string
|
|
|
|
PermRedirects map[string]string
|
2020-06-30 11:01:19 -04:00
|
|
|
MimeOverrides map[string]string
|
2020-06-30 14:39:26 -04:00
|
|
|
CertificateZones map[string][]string
|
2020-06-27 17:44:15 -04:00
|
|
|
DefaultLang string
|
|
|
|
DirectorySort string
|
|
|
|
DirectoryReverse bool
|
|
|
|
DirectoryTitles bool
|
2020-06-10 14:40:13 -04:00
|
|
|
}
|
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
func getConfig(filename string) (Config, error) {
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
// Defaults
|
2020-03-24 17:05:26 -04:00
|
|
|
config.Port = 1965
|
2019-11-06 10:08:44 -05:00
|
|
|
config.Hostname = "localhost"
|
|
|
|
config.CertPath = "cert.pem"
|
|
|
|
config.KeyPath = "key.pem"
|
|
|
|
config.DocBase = "/var/gemini/"
|
|
|
|
config.HomeDocBase = "users"
|
2020-06-08 15:47:33 -04:00
|
|
|
config.GeminiExt = "gmi"
|
2020-06-10 15:22:15 -04:00
|
|
|
config.DefaultLang = ""
|
2020-06-28 12:34:50 -04:00
|
|
|
config.AccessLog = "access.log"
|
|
|
|
config.ErrorLog = "error.log"
|
2020-06-08 13:59:16 -04:00
|
|
|
config.TempRedirects = make(map[string]string)
|
|
|
|
config.PermRedirects = make(map[string]string)
|
2020-06-08 15:46:39 -04:00
|
|
|
config.CGIPaths = make([]string, 0)
|
2020-06-04 14:41:40 -04:00
|
|
|
config.SCGIPaths = make(map[string]string)
|
2020-06-27 12:52:29 -04:00
|
|
|
config.DirectorySort = "Name"
|
2019-11-06 10:08:44 -05:00
|
|
|
|
|
|
|
// Return defaults if no filename given
|
|
|
|
if filename == "" {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to overwrite defaults from file
|
|
|
|
_, err := toml.DecodeFile(filename, &config)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
2020-06-27 12:52:29 -04:00
|
|
|
|
|
|
|
// Validate pseudo-enums
|
|
|
|
switch config.DirectorySort {
|
|
|
|
case "Name", "Size", "Time":
|
|
|
|
default:
|
|
|
|
return config, errors.New("Invalid DirectorySort value.")
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:18:49 -04:00
|
|
|
// Expand CGI paths
|
|
|
|
var cgiPaths []string
|
|
|
|
for _, cgiPath := range config.CGIPaths {
|
|
|
|
expandedPaths, err := filepath.Glob(cgiPath)
|
|
|
|
if err != nil {
|
|
|
|
return config, errors.New("Error expanding CGI path glob " + cgiPath + ": " + err.Error())
|
|
|
|
}
|
|
|
|
cgiPaths = append(cgiPaths, expandedPaths...)
|
|
|
|
}
|
|
|
|
config.CGIPaths = cgiPaths
|
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
2020-07-01 17:05:49 -04:00
|
|
|
|
|
|
|
func parseMollyFiles(path string, config *Config, errorLog *log.Logger) {
|
2020-07-06 13:08:03 -04:00
|
|
|
// Replace config variables which use pointers with new ones,
|
|
|
|
// so that changes made here aren't reflected everywhere.
|
|
|
|
newTempRedirects := make(map[string]string)
|
|
|
|
for key, value := range config.TempRedirects {
|
|
|
|
newTempRedirects[key] = value
|
|
|
|
}
|
|
|
|
config.TempRedirects = newTempRedirects
|
|
|
|
newPermRedirects := make(map[string]string)
|
|
|
|
for key, value := range config.PermRedirects {
|
|
|
|
newPermRedirects[key] = value
|
|
|
|
}
|
|
|
|
config.PermRedirects = newPermRedirects
|
|
|
|
newMimeOverrides := make(map[string]string)
|
|
|
|
for key, value := range config.MimeOverrides {
|
|
|
|
newMimeOverrides[key] = value
|
|
|
|
}
|
|
|
|
config.MimeOverrides = newMimeOverrides
|
|
|
|
newCertificateZones := make(map[string][]string)
|
|
|
|
for key, value := range config.CertificateZones {
|
|
|
|
newCertificateZones[key] = value
|
|
|
|
}
|
|
|
|
config.CertificateZones = newCertificateZones
|
|
|
|
// Initialise MollyFile using main Config
|
|
|
|
var mollyFile MollyFile
|
|
|
|
mollyFile.GeminiExt = config.GeminiExt
|
|
|
|
mollyFile.DefaultLang = config.DefaultLang
|
|
|
|
mollyFile.DirectorySort = config.DirectorySort
|
|
|
|
mollyFile.DirectoryReverse = config.DirectoryReverse
|
|
|
|
mollyFile.DirectoryTitles = config.DirectoryTitles
|
2020-07-01 17:05:49 -04:00
|
|
|
// Build list of directories to check
|
|
|
|
var dirs []string
|
|
|
|
dirs = append(dirs, path)
|
|
|
|
for {
|
|
|
|
if path == filepath.Clean(config.DocBase) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
subpath := filepath.Dir(path)
|
|
|
|
dirs = append(dirs, subpath)
|
|
|
|
path = subpath
|
|
|
|
}
|
|
|
|
// Parse files in reverse order
|
|
|
|
for i := len(dirs) - 1; i >= 0; i-- {
|
|
|
|
dir := dirs[i]
|
|
|
|
// Break out of the loop if a directory doesn't exist
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Construct path for a .molly file in this dir
|
|
|
|
mollyPath := filepath.Join(dir, ".molly")
|
|
|
|
_, err = os.Stat(mollyPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the file exists and we can read it, try to parse it
|
|
|
|
_, err = toml.DecodeFile(mollyPath, &mollyFile)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Error parsing .molly file " + mollyPath + ": " + err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Overwrite main Config using MollyFile
|
|
|
|
config.GeminiExt = mollyFile.GeminiExt
|
|
|
|
config.DefaultLang = mollyFile.DefaultLang
|
|
|
|
config.DirectorySort = mollyFile.DirectorySort
|
|
|
|
config.DirectoryReverse = mollyFile.DirectoryReverse
|
|
|
|
config.DirectoryTitles = mollyFile.DirectoryTitles
|
|
|
|
for key, value := range mollyFile.TempRedirects {
|
|
|
|
config.TempRedirects[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.PermRedirects {
|
|
|
|
config.PermRedirects[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.MimeOverrides {
|
|
|
|
config.MimeOverrides[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.CertificateZones {
|
|
|
|
config.CertificateZones[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|