2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-10 15:31:13 -04:00
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2020-06-27 12:52:29 -04:00
|
|
|
"sort"
|
2020-06-10 15:31:13 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan LogEntry, errorLogEntries chan string) {
|
2019-11-06 10:08:44 -05:00
|
|
|
defer conn.Close()
|
2020-06-05 13:39:28 -04:00
|
|
|
var tlsConn (*tls.Conn) = conn.(*tls.Conn)
|
2019-11-06 11:38:41 -05:00
|
|
|
var log LogEntry
|
|
|
|
log.Time = time.Now()
|
|
|
|
log.RemoteAddr = conn.RemoteAddr()
|
|
|
|
log.RequestURL = "-"
|
|
|
|
log.Status = 0
|
2020-06-28 12:34:50 -04:00
|
|
|
defer func() { accessLogEntries <- log }()
|
2019-11-06 11:38:41 -05:00
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
// Read request
|
2020-06-28 12:34:50 -04:00
|
|
|
URL, err := readRequest(conn, &log, errorLogEntries)
|
2020-06-05 14:13:48 -04:00
|
|
|
if err != nil {
|
2019-11-06 10:08:44 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:39:28 -04:00
|
|
|
clientCerts := tlsConn.ConnectionState().PeerCertificates
|
|
|
|
// Check validity
|
|
|
|
// This will fail if any of multiple certs are invalid
|
|
|
|
// Maybe we should just require one valid?
|
|
|
|
now := time.Now()
|
|
|
|
for _, cert := range clientCerts {
|
|
|
|
if now.Before(cert.NotBefore) {
|
|
|
|
conn.Write([]byte("64 Client certificate not yet valid!\r\n"))
|
|
|
|
log.Status = 64
|
|
|
|
return
|
|
|
|
} else if now.After(cert.NotAfter) {
|
|
|
|
conn.Write([]byte("65 Client certificate has expired!\r\n"))
|
|
|
|
log.Status = 65
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 11:46:44 -05:00
|
|
|
// Reject non-gemini schemes
|
|
|
|
if URL.Scheme != "gemini" {
|
|
|
|
conn.Write([]byte("53 No proxying to non-Gemini content!\r\n"))
|
|
|
|
log.Status = 53
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:39:25 -05:00
|
|
|
// Reject requests for content from other servers
|
2019-11-24 05:29:38 -05:00
|
|
|
requestHostname := strings.Split(URL.Host, ":")[0] // Shave off port
|
|
|
|
if requestHostname != config.Hostname {
|
2019-11-21 15:39:25 -05:00
|
|
|
conn.Write([]byte("53 No proxying to other hosts!\r\n"))
|
|
|
|
log.Status = 53
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-06 15:23:51 -05:00
|
|
|
// Fail if there are dots in the path
|
|
|
|
if strings.Contains(URL.Path, "..") {
|
|
|
|
conn.Write([]byte("50 Your directory traversal technique has been defeated!\r\n"))
|
|
|
|
log.Status = 50
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:36:10 -04:00
|
|
|
// Check for redirects
|
2020-06-08 13:59:16 -04:00
|
|
|
for src, dst := range config.TempRedirects {
|
2020-06-06 07:36:10 -04:00
|
|
|
if URL.Path == src {
|
|
|
|
URL.Path = dst
|
|
|
|
conn.Write([]byte("30 " + URL.String() + "\r\n"))
|
|
|
|
log.Status = 30
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 13:59:16 -04:00
|
|
|
for src, dst := range config.PermRedirects {
|
|
|
|
if URL.Path == src {
|
|
|
|
URL.Path = dst
|
|
|
|
conn.Write([]byte("31 " + URL.String() + "\r\n"))
|
|
|
|
log.Status = 31
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 07:36:10 -04:00
|
|
|
|
2020-06-28 07:47:30 -04:00
|
|
|
// Check whether this URL is in a certificate zone
|
|
|
|
authorised := true
|
2020-06-28 08:47:36 -04:00
|
|
|
for zone, allowedFingerprints := range config.CertificateZones {
|
2020-06-28 07:47:30 -04:00
|
|
|
matched, err := regexp.Match(zone, []byte(URL.Path))
|
|
|
|
if !matched || err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
authorised = false
|
2020-06-28 08:47:36 -04:00
|
|
|
for _, clientCert := range clientCerts {
|
|
|
|
for _, allowedFingerprint := range allowedFingerprints {
|
|
|
|
if getCertFingerprint(clientCert) == allowedFingerprint {
|
|
|
|
authorised = true
|
|
|
|
break
|
|
|
|
}
|
2020-06-28 07:47:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !authorised {
|
|
|
|
if len(clientCerts) > 0 {
|
|
|
|
conn.Write([]byte("61 Provided certificate not authorised for this resource\r\n"))
|
|
|
|
log.Status = 61
|
|
|
|
} else {
|
|
|
|
conn.Write([]byte("60 A pre-authorised certificate is required to access this resource\r\n"))
|
|
|
|
log.Status = 60
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-04 14:41:40 -04:00
|
|
|
// Check whether this URL is mapped to an SCGI app
|
|
|
|
for scgi_url, scgi_socket := range config.SCGIPaths {
|
2020-06-10 15:31:13 -04:00
|
|
|
matched, err := regexp.Match(scgi_url, []byte(URL.Path))
|
2020-06-04 14:41:40 -04:00
|
|
|
if matched && err == nil {
|
2020-06-06 06:08:34 -04:00
|
|
|
handleSCGI(scgi_socket, config, URL, &log, conn)
|
2020-06-04 14:41:40 -04:00
|
|
|
return
|
|
|
|
}
|
2019-11-06 15:23:51 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 14:35:14 -04:00
|
|
|
// Resolve URI path to actual filesystem path
|
|
|
|
path, info, err := resolvePath(URL.Path, config)
|
|
|
|
|
|
|
|
// Fail if file does not exist or perms aren't right
|
2019-11-06 15:23:51 -05:00
|
|
|
if os.IsNotExist(err) || os.IsPermission(err) {
|
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
2020-06-01 15:27:15 -04:00
|
|
|
} else if err != nil {
|
2020-06-17 09:22:21 -04:00
|
|
|
conn.Write([]byte("40 Temporary failure!\r\n"))
|
2020-06-01 15:27:15 -04:00
|
|
|
log.Status = 40
|
|
|
|
return
|
2020-06-04 14:35:14 -04:00
|
|
|
} else if uint64(info.Mode().Perm())&0444 != 0444 {
|
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
2019-11-06 15:23:51 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:24:19 -04:00
|
|
|
// Paranoid security measure:
|
|
|
|
// Fail if the URL has mapped to our TLS files or the log
|
2020-06-28 12:34:50 -04:00
|
|
|
if path == config.CertPath || path == config.KeyPath || path == config.AccessLog || path == config.ErrorLog {
|
2020-06-04 17:24:19 -04:00
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:40:13 -04:00
|
|
|
// Don't serve Molly files
|
2020-06-10 15:31:13 -04:00
|
|
|
if !info.IsDir() && filepath.Base(path) == ".molly" {
|
2020-06-10 14:40:13 -04:00
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read Molly files
|
2020-06-29 11:17:43 -04:00
|
|
|
if config.ReadMollyFiles {
|
|
|
|
parseMollyFiles(path, info, &config, errorLogEntries)
|
|
|
|
}
|
2020-06-10 14:40:13 -04:00
|
|
|
|
2020-06-04 14:35:14 -04:00
|
|
|
// Handle directories
|
2019-11-06 15:23:51 -05:00
|
|
|
if info.IsDir() {
|
|
|
|
// Redirect to add trailing slash if missing
|
|
|
|
// (otherwise relative links don't work properly)
|
|
|
|
if !strings.HasSuffix(URL.Path, "/") {
|
|
|
|
conn.Write([]byte(fmt.Sprintf("31 %s\r\n", URL.String()+"/")))
|
|
|
|
log.Status = 31
|
|
|
|
return
|
|
|
|
}
|
2020-06-04 15:32:20 -04:00
|
|
|
// Check for index.gmi if path is a directory
|
2020-06-10 15:31:13 -04:00
|
|
|
index_path := filepath.Join(path, "index."+config.GeminiExt)
|
2020-06-04 15:32:20 -04:00
|
|
|
index_info, err := os.Stat(index_path)
|
|
|
|
if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 {
|
2020-06-28 12:34:50 -04:00
|
|
|
serveFile(index_path, &log, conn, config, errorLogEntries)
|
2020-06-10 15:31:13 -04:00
|
|
|
// Serve a generated listing
|
2020-06-04 15:32:20 -04:00
|
|
|
} else {
|
|
|
|
conn.Write([]byte("20 text/gemini\r\n"))
|
|
|
|
log.Status = 20
|
2020-06-27 12:52:29 -04:00
|
|
|
conn.Write([]byte(generateDirectoryListing(URL, path, config)))
|
2020-06-04 15:32:20 -04:00
|
|
|
}
|
2019-11-06 15:23:51 -05:00
|
|
|
return
|
2020-01-12 07:39:38 -05:00
|
|
|
}
|
2020-06-04 14:35:14 -04:00
|
|
|
|
2019-11-24 13:24:35 -05:00
|
|
|
// If this file is executable, get dynamic content
|
2020-06-10 15:31:13 -04:00
|
|
|
if info.Mode().Perm()&0111 == 0111 {
|
|
|
|
for _, cgiPath := range config.CGIPaths {
|
2020-06-08 15:46:39 -04:00
|
|
|
inCGIPath, err := regexp.Match(cgiPath, []byte(path))
|
2020-06-10 15:31:13 -04:00
|
|
|
if err == nil && inCGIPath {
|
2020-06-28 12:34:50 -04:00
|
|
|
handleCGI(config, path, URL, &log, errorLogEntries, conn)
|
2020-06-08 15:46:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 14:35:14 -04:00
|
|
|
}
|
2020-01-12 06:50:01 -05:00
|
|
|
|
2019-11-24 13:24:35 -05:00
|
|
|
// Otherwise, serve the file contents
|
2020-06-28 12:34:50 -04:00
|
|
|
serveFile(path, &log, conn, config, errorLogEntries)
|
2020-06-04 14:35:14 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
func readRequest(conn net.Conn, log *LogEntry, errorLog chan string) (*url.URL, error) {
|
2020-06-05 14:13:48 -04:00
|
|
|
reader := bufio.NewReaderSize(conn, 1024)
|
|
|
|
request, overflow, err := reader.ReadLine()
|
|
|
|
if overflow {
|
|
|
|
conn.Write([]byte("59 Request too long!\r\n"))
|
|
|
|
log.Status = 59
|
|
|
|
return nil, errors.New("Request too long")
|
|
|
|
} else if err != nil {
|
2020-06-28 12:34:50 -04:00
|
|
|
errorLog <- "Error reading request: " + err.Error()
|
2020-06-05 14:13:48 -04:00
|
|
|
conn.Write([]byte("40 Unknown error reading request!\r\n"))
|
|
|
|
log.Status = 40
|
|
|
|
return nil, errors.New("Error reading request")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse request as URL
|
|
|
|
URL, err := url.Parse(string(request))
|
|
|
|
if err != nil {
|
2020-06-28 12:34:50 -04:00
|
|
|
errorLog <- "Error parsing request URL " + string(request) + ": " + err.Error()
|
2020-06-05 14:13:48 -04:00
|
|
|
conn.Write([]byte("59 Error parsing URL!\r\n"))
|
|
|
|
log.Status = 59
|
|
|
|
return nil, errors.New("Bad URL in request")
|
|
|
|
}
|
|
|
|
log.RequestURL = URL.String()
|
|
|
|
|
|
|
|
// Set implicit scheme
|
|
|
|
if URL.Scheme == "" {
|
|
|
|
URL.Scheme = "gemini"
|
|
|
|
}
|
|
|
|
|
|
|
|
return URL, nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 14:35:14 -04:00
|
|
|
func resolvePath(path string, config Config) (string, os.FileInfo, error) {
|
|
|
|
// Handle tildes
|
|
|
|
if strings.HasPrefix(path, "/~") {
|
2020-06-10 15:31:13 -04:00
|
|
|
bits := strings.Split(path, "/")
|
2020-06-04 14:35:14 -04:00
|
|
|
username := bits[1][1:]
|
|
|
|
new_prefix := filepath.Join(config.DocBase, config.HomeDocBase, username)
|
|
|
|
path = strings.Replace(path, bits[1], new_prefix, 1)
|
2020-06-10 13:43:49 -04:00
|
|
|
path = filepath.Clean(path)
|
2019-11-06 15:23:51 -05:00
|
|
|
} else {
|
2020-06-04 14:35:14 -04:00
|
|
|
path = filepath.Join(config.DocBase, path)
|
|
|
|
}
|
|
|
|
// Make sure this file exists and is readable
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return path, info, nil
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
2019-11-06 15:23:51 -05:00
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
func parseMollyFiles(path string, info os.FileInfo, config *Config, errorLogEntries chan string) {
|
2020-06-10 14:40:13 -04:00
|
|
|
// Build list of directories to check
|
|
|
|
dirs := make([]string, 16)
|
2020-06-10 15:31:13 -04:00
|
|
|
if !info.IsDir() {
|
2020-06-10 14:40:13 -04:00
|
|
|
path = filepath.Dir(path)
|
|
|
|
}
|
|
|
|
dirs = append(dirs, path)
|
|
|
|
for {
|
2020-06-10 15:20:01 -04:00
|
|
|
if path == filepath.Clean(config.DocBase) {
|
2020-06-10 14:40:13 -04:00
|
|
|
break
|
|
|
|
}
|
2020-06-10 15:20:01 -04:00
|
|
|
subpath := filepath.Dir(path)
|
|
|
|
dirs = append(dirs, subpath)
|
2020-06-10 14:40:13 -04:00
|
|
|
path = subpath
|
|
|
|
}
|
2020-06-27 17:44:15 -04:00
|
|
|
// Initialise MollyFile using main Config
|
2020-06-10 14:40:13 -04:00
|
|
|
var mollyFile MollyFile
|
2020-06-27 17:44:15 -04:00
|
|
|
mollyFile.GeminiExt = config.GeminiExt
|
|
|
|
mollyFile.DefaultLang = config.DefaultLang
|
|
|
|
mollyFile.DirectorySort = config.DirectorySort
|
|
|
|
mollyFile.DirectoryReverse = config.DirectoryReverse
|
|
|
|
mollyFile.DirectoryTitles = config.DirectoryTitles
|
|
|
|
// Parse files
|
2020-06-10 15:31:13 -04:00
|
|
|
for i := len(dirs) - 1; i >= 0; i-- {
|
2020-06-10 14:40:13 -04:00
|
|
|
dir := dirs[i]
|
|
|
|
mollyPath := filepath.Join(dir, ".molly")
|
|
|
|
_, err := os.Stat(mollyPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = toml.DecodeFile(mollyPath, &mollyFile)
|
|
|
|
if err != nil {
|
2020-06-28 12:34:50 -04:00
|
|
|
errorLogEntries <- "Error parsing .molly file " + mollyPath + ": " + err.Error()
|
2020-06-10 14:40:13 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-06-27 17:44:15 -04:00
|
|
|
// 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
|
2020-06-30 11:01:19 -04:00
|
|
|
for pathRegex, newType := range mollyFile.MimeOverrides {
|
|
|
|
config.MimeOverrides[pathRegex] = newType
|
|
|
|
}
|
2020-06-10 14:40:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:52:29 -04:00
|
|
|
func generateDirectoryListing(URL *url.URL, path string, config Config) string {
|
2019-11-06 15:23:51 -05:00
|
|
|
var listing string
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
listing = "# Directory listing\n\n"
|
2020-06-11 16:43:13 -04:00
|
|
|
// Override with .mollyhead file
|
|
|
|
header_path := filepath.Join(path, ".mollyhead")
|
|
|
|
_, err = os.Stat(header_path)
|
|
|
|
if err == nil {
|
|
|
|
header, err := ioutil.ReadFile(header_path)
|
|
|
|
if err == nil {
|
|
|
|
listing = string(header)
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 14:01:03 -04:00
|
|
|
// Do "up" link first
|
|
|
|
if URL.Path != "/" {
|
|
|
|
if strings.HasSuffix(URL.Path, "/") {
|
|
|
|
URL.Path = URL.Path[:len(URL.Path)-1]
|
|
|
|
}
|
|
|
|
up := filepath.Dir(URL.Path)
|
|
|
|
listing += fmt.Sprintf("=> %s %s\n", up, "..")
|
|
|
|
}
|
2020-06-27 12:52:29 -04:00
|
|
|
// Sort files
|
|
|
|
sort.SliceStable(files, func(i, j int) bool {
|
|
|
|
if config.DirectoryReverse {
|
|
|
|
i, j = j, i
|
|
|
|
}
|
|
|
|
if config.DirectorySort == "Name" {
|
|
|
|
return files[i].Name() < files[j].Name()
|
|
|
|
} else if config.DirectorySort == "Size" {
|
|
|
|
return files[i].Size() < files[j].Size()
|
|
|
|
} else if config.DirectorySort == "Time" {
|
|
|
|
return files[i].ModTime().Before(files[j].ModTime())
|
|
|
|
}
|
|
|
|
return false // Should not happen
|
|
|
|
})
|
|
|
|
// Format lines
|
2019-11-06 15:23:51 -05:00
|
|
|
for _, file := range files {
|
2019-11-22 14:35:51 -05:00
|
|
|
// Skip dotfiles
|
|
|
|
if strings.HasPrefix(file.Name(), ".") {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-21 16:03:39 -05:00
|
|
|
// Only list world readable files
|
|
|
|
if uint64(file.Mode().Perm())&0444 != 0444 {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-27 16:57:03 -04:00
|
|
|
listing += fmt.Sprintf("=> %s %s\n", url.PathEscape(file.Name()), generatePrettyFileLabel(file, path, config))
|
2019-11-06 15:23:51 -05:00
|
|
|
}
|
|
|
|
return listing
|
|
|
|
}
|
2020-06-04 14:35:14 -04:00
|
|
|
|
2020-06-27 16:57:03 -04:00
|
|
|
func generatePrettyFileLabel(info os.FileInfo, path string, config Config) string {
|
2020-06-06 15:28:12 -04:00
|
|
|
var size string
|
|
|
|
if info.IsDir() {
|
|
|
|
size = " "
|
|
|
|
} else if info.Size() < 1024 {
|
|
|
|
size = fmt.Sprintf("%4d B", info.Size())
|
|
|
|
} else if info.Size() < (1024 << 10) {
|
2020-06-10 15:31:13 -04:00
|
|
|
size = fmt.Sprintf("%4d KiB", info.Size()>>10)
|
|
|
|
} else if info.Size() < 1024<<20 {
|
|
|
|
size = fmt.Sprintf("%4d MiB", info.Size()>>20)
|
|
|
|
} else if info.Size() < 1024<<30 {
|
|
|
|
size = fmt.Sprintf("%4d GiB", info.Size()>>30)
|
|
|
|
} else if info.Size() < 1024<<40 {
|
|
|
|
size = fmt.Sprintf("%4d TiB", info.Size()>>40)
|
2020-06-06 15:28:12 -04:00
|
|
|
} else {
|
|
|
|
size = "GIGANTIC"
|
|
|
|
}
|
|
|
|
|
2020-06-27 16:57:03 -04:00
|
|
|
name := info.Name()
|
|
|
|
if config.DirectoryTitles && filepath.Ext(name) == "."+config.GeminiExt {
|
|
|
|
name = readHeading(path, info)
|
|
|
|
}
|
|
|
|
if len(name) > 40 {
|
2020-06-06 15:28:12 -04:00
|
|
|
name = info.Name()[:36] + "..."
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%-40s %s %v", name, size, info.ModTime().Format("Jan _2 2006"))
|
|
|
|
}
|
|
|
|
|
2020-06-27 16:57:03 -04:00
|
|
|
func readHeading(path string, info os.FileInfo) string {
|
|
|
|
filePath := filepath.Join(path, info.Name())
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return info.Name()
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "# ") {
|
|
|
|
return strings.TrimSpace(line[1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info.Name()
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
func serveFile(path string, log *LogEntry, conn net.Conn, config Config, errorLog chan string) {
|
2020-06-04 14:35:14 -04:00
|
|
|
// Get MIME type of files
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
var mimeType string
|
2020-06-10 15:31:13 -04:00
|
|
|
if ext == "."+config.GeminiExt {
|
2020-06-04 14:35:14 -04:00
|
|
|
mimeType = "text/gemini"
|
|
|
|
} else {
|
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
2020-06-27 18:12:32 -04:00
|
|
|
// Override extension-based MIME type
|
|
|
|
for pathRegex, newType := range config.MimeOverrides {
|
|
|
|
overridden, err := regexp.Match(pathRegex, []byte(path))
|
|
|
|
if err == nil && overridden {
|
|
|
|
mimeType = newType
|
|
|
|
}
|
2020-06-10 15:22:15 -04:00
|
|
|
}
|
2020-06-04 17:12:09 -04:00
|
|
|
// Set a generic MIME type if the extension wasn't recognised
|
|
|
|
if mimeType == "" {
|
|
|
|
mimeType = "application/octet-stream"
|
|
|
|
}
|
2020-06-27 18:12:32 -04:00
|
|
|
// Add lang parameter
|
|
|
|
if mimeType == "text/gemini" && config.DefaultLang != "" {
|
|
|
|
mimeType += "; lang=" + config.DefaultLang
|
|
|
|
}
|
2020-06-04 17:12:09 -04:00
|
|
|
|
2020-06-04 14:35:14 -04:00
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
conn.Write([]byte("50 Error!\r\n"))
|
|
|
|
log.Status = 50
|
2020-06-13 03:08:04 -04:00
|
|
|
return
|
2020-06-04 14:35:14 -04:00
|
|
|
}
|
|
|
|
conn.Write([]byte(fmt.Sprintf("20 %s\r\n", mimeType)))
|
|
|
|
log.Status = 20
|
|
|
|
conn.Write(contents)
|
|
|
|
}
|