2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-27 12:52:29 -04:00
|
|
|
"errors"
|
2023-02-10 11:19:21 -05:00
|
|
|
"github.com/BurntSushi/toml"
|
2020-07-01 17:05:49 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-01 10:18:49 -04:00
|
|
|
"path/filepath"
|
2023-01-28 13:16:11 -05:00
|
|
|
"strings"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-02-10 11:19:21 -05:00
|
|
|
Port int
|
|
|
|
Hostname string
|
|
|
|
CertPath string
|
|
|
|
KeyPath string
|
|
|
|
DocBase string
|
|
|
|
HomeDocBase string
|
|
|
|
GeminiExt string
|
|
|
|
DefaultLang string
|
|
|
|
DefaultEncoding string
|
|
|
|
AccessLog string
|
|
|
|
ErrorLog string
|
|
|
|
ReadMollyFiles bool
|
|
|
|
TempRedirects map[string]string
|
|
|
|
PermRedirects map[string]string
|
|
|
|
MimeOverrides map[string]string
|
|
|
|
CGIPaths []string
|
|
|
|
SCGIPaths map[string]string
|
|
|
|
CertificateZones map[string][]string
|
2023-02-23 13:31:16 -05:00
|
|
|
AllowTLS12 bool
|
2023-02-10 11:19:21 -05:00
|
|
|
DirectorySort string
|
2023-02-05 08:35:29 -05:00
|
|
|
DirectorySubdirsFirst bool
|
2023-02-10 11:19:21 -05:00
|
|
|
DirectoryReverse bool
|
|
|
|
DirectoryTitles bool
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:40:13 -04:00
|
|
|
type MollyFile struct {
|
2023-02-10 11:19:21 -05:00
|
|
|
GeminiExt string
|
|
|
|
TempRedirects map[string]string
|
|
|
|
PermRedirects map[string]string
|
|
|
|
MimeOverrides map[string]string
|
|
|
|
CertificateZones map[string][]string
|
|
|
|
DefaultLang string
|
|
|
|
DefaultEncoding string
|
|
|
|
DirectorySort string
|
2023-02-05 08:35:29 -05:00
|
|
|
DirectorySubdirsFirst bool
|
2023-02-10 11:19:21 -05:00
|
|
|
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 = ""
|
2023-01-29 06:07:52 -05:00
|
|
|
config.DefaultEncoding = ""
|
2020-06-28 12:34:50 -04:00
|
|
|
config.AccessLog = "access.log"
|
2023-02-07 13:33:14 -05:00
|
|
|
config.ErrorLog = ""
|
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)
|
2023-02-23 13:31:16 -05:00
|
|
|
config.AllowTLS12 = true
|
2020-06-27 12:52:29 -04:00
|
|
|
config.DirectorySort = "Name"
|
2023-02-05 08:35:29 -05:00
|
|
|
config.DirectorySubdirsFirst = false
|
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
|
|
|
|
2023-01-28 13:22:31 -05:00
|
|
|
// Force hostname to lowercase
|
|
|
|
config.Hostname = strings.ToLower(config.Hostname)
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:15 -05:00
|
|
|
// Absolutise paths
|
|
|
|
config.DocBase, err = filepath.Abs(config.DocBase)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.CertPath, err = filepath.Abs(config.CertPath)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.KeyPath, err = filepath.Abs(config.KeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
if config.AccessLog != "" && config.AccessLog != "-" {
|
|
|
|
config.AccessLog, err = filepath.Abs(config.AccessLog)
|
2023-02-15 15:15:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
2023-02-15 15:10:22 -05:00
|
|
|
}
|
2023-02-23 12:49:15 -05:00
|
|
|
if config.ErrorLog != "" {
|
|
|
|
config.ErrorLog, err = filepath.Abs(config.ErrorLog)
|
2023-02-08 14:32:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 10:54:00 -05:00
|
|
|
// Absolutise CGI paths
|
|
|
|
for index, cgiPath := range config.CGIPaths {
|
2023-02-10 11:19:21 -05:00
|
|
|
if !filepath.IsAbs(cgiPath) {
|
2023-02-05 10:54:00 -05:00
|
|
|
config.CGIPaths[index] = filepath.Join(config.DocBase, cgiPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2023-02-08 14:32:17 -05:00
|
|
|
// Absolutise SCGI paths
|
|
|
|
for index, scgiPath := range config.SCGIPaths {
|
2023-02-23 12:49:15 -05:00
|
|
|
config.SCGIPaths[index], err = filepath.Abs( scgiPath)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
2023-02-08 14:32:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:16:11 -05:00
|
|
|
// Validate redirects
|
|
|
|
for _, value := range config.TempRedirects {
|
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
|
|
|
return config, errors.New("Invalid cross-protocol redirect to " + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, value := range config.PermRedirects {
|
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
|
|
|
return config, errors.New("Ignoring cross-protocol redirect to " + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
2020-07-01 17:05:49 -04:00
|
|
|
|
2023-02-19 09:04:34 -05:00
|
|
|
func parseMollyFiles(path string, config *Config) {
|
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
|
2023-01-29 06:07:52 -05:00
|
|
|
mollyFile.DefaultEncoding = config.DefaultEncoding
|
2020-07-06 13:08:03 -04:00
|
|
|
mollyFile.DirectorySort = config.DirectorySort
|
2023-02-05 08:35:29 -05:00
|
|
|
mollyFile.DirectorySubdirsFirst = config.DirectorySubdirsFirst
|
2020-07-06 13:08:03 -04:00
|
|
|
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 {
|
2023-02-19 09:04:34 -05:00
|
|
|
log.Println("Error parsing .molly file " + mollyPath + ": " + err.Error())
|
2020-07-01 17:05:49 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Overwrite main Config using MollyFile
|
|
|
|
config.GeminiExt = mollyFile.GeminiExt
|
|
|
|
config.DefaultLang = mollyFile.DefaultLang
|
2023-01-29 06:07:52 -05:00
|
|
|
config.DefaultEncoding = mollyFile.DefaultEncoding
|
2020-07-01 17:05:49 -04:00
|
|
|
config.DirectorySort = mollyFile.DirectorySort
|
2023-02-05 08:35:29 -05:00
|
|
|
config.DirectorySubdirsFirst = mollyFile.DirectorySubdirsFirst
|
2020-07-01 17:05:49 -04:00
|
|
|
config.DirectoryReverse = mollyFile.DirectoryReverse
|
|
|
|
config.DirectoryTitles = mollyFile.DirectoryTitles
|
|
|
|
for key, value := range mollyFile.TempRedirects {
|
2023-01-28 13:16:11 -05:00
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
2023-02-19 09:04:34 -05:00
|
|
|
log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath)
|
2023-01-28 13:16:11 -05:00
|
|
|
continue
|
|
|
|
}
|
2020-07-01 17:05:49 -04:00
|
|
|
config.TempRedirects[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.PermRedirects {
|
2023-01-28 13:16:11 -05:00
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
2023-02-19 09:04:34 -05:00
|
|
|
log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath)
|
2023-01-28 13:16:11 -05:00
|
|
|
continue
|
|
|
|
}
|
2020-07-01 17:05:49 -04:00
|
|
|
config.PermRedirects[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.MimeOverrides {
|
|
|
|
config.MimeOverrides[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range mollyFile.CertificateZones {
|
|
|
|
config.CertificateZones[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|