2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-10 15:31:13 -04:00
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-07-01 13:57:39 -04:00
|
|
|
"log"
|
2020-06-10 15:31:13 -04:00
|
|
|
"mime"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2020-06-30 13:11:49 -04:00
|
|
|
"strconv"
|
2020-06-10 15:31:13 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
2020-07-01 13:57:39 -04:00
|
|
|
func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan LogEntry, errorLog *log.Logger) {
|
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-07-01 13:57:39 -04:00
|
|
|
URL, err := readRequest(conn, &log, errorLog)
|
2020-06-05 14:13:48 -04:00
|
|
|
if err != nil {
|
2019-11-06 10:08:44 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:25:37 -04:00
|
|
|
// Enforce client certificate validity
|
2020-06-05 13:39:28 -04:00
|
|
|
clientCerts := tlsConn.ConnectionState().PeerCertificates
|
2020-06-30 16:25:37 -04:00
|
|
|
enforceCertificateValidity(clientCerts, conn, &log)
|
|
|
|
if log.Status != 0 {
|
|
|
|
return
|
2020-06-05 13:39:28 -04:00
|
|
|
}
|
|
|
|
|
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
|
2020-06-30 13:11:49 -04:00
|
|
|
if URL.Hostname() != config.Hostname || (URL.Port() != "" && URL.Port() != strconv.Itoa(config.Port)) {
|
2020-06-30 11:27:53 -04:00
|
|
|
conn.Write([]byte("53 No proxying to other hosts or ports!\r\n"))
|
2019-11-21 15:39:25 -05:00
|
|
|
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-30 13:31:27 -04:00
|
|
|
// Resolve URI path to actual filesystem path
|
|
|
|
path := resolvePath(URL.Path, config)
|
|
|
|
|
2020-06-30 16:25:37 -04:00
|
|
|
// Paranoid security measures:
|
|
|
|
// Fail ASAP if the URL has mapped to a sensitive file
|
2020-07-01 10:05:09 -04:00
|
|
|
if path == config.CertPath || path == config.KeyPath || path == config.AccessLog || path == config.ErrorLog || filepath.Base(path) == ".molly" {
|
2020-06-30 16:25:37 -04:00
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:31:27 -04:00
|
|
|
// Read Molly files
|
|
|
|
if config.ReadMollyFiles {
|
2020-07-01 13:57:39 -04:00
|
|
|
parseMollyFiles(path, &config, errorLog)
|
2020-06-30 13:31:27 -04:00
|
|
|
}
|
2020-06-30 16:25:37 -04:00
|
|
|
|
2020-07-01 08:16:27 -04:00
|
|
|
// Check whether this URL is in a certificate zone
|
|
|
|
handleCertificateZones(URL, clientCerts, config, conn, &log)
|
2020-06-30 16:25:37 -04:00
|
|
|
if log.Status != 0 {
|
|
|
|
return
|
2020-06-08 13:59:16 -04:00
|
|
|
}
|
2020-06-06 07:36:10 -04:00
|
|
|
|
2020-07-01 08:16:27 -04:00
|
|
|
// Check for redirects
|
2020-07-01 14:15:52 -04:00
|
|
|
handleRedirects(URL, config, conn, &log, errorLog)
|
2020-06-30 16:25:37 -04:00
|
|
|
if log.Status != 0 {
|
2020-06-28 07:47:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:05:39 -04:00
|
|
|
// Check whether this URL is mapped to an SCGI app
|
2020-07-01 10:40:51 -04:00
|
|
|
for scgiPath, scgiSocket := range config.SCGIPaths {
|
|
|
|
if strings.HasPrefix(URL.Path, scgiPath) {
|
2020-07-01 14:15:52 -04:00
|
|
|
handleSCGI(URL, scgiPath, scgiSocket, config, &log, errorLog, conn)
|
2020-07-01 10:05:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:31:27 -04:00
|
|
|
// Check whether this URL is in a configured CGI path
|
|
|
|
for _, cgiPath := range config.CGIPaths {
|
2020-07-01 08:10:20 -04:00
|
|
|
if strings.HasPrefix(path, cgiPath) {
|
2020-07-01 13:57:39 -04:00
|
|
|
handleCGI(config, path, cgiPath, URL, &log, errorLog, conn)
|
2020-06-30 16:25:37 -04:00
|
|
|
if log.Status != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 13:31:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 14:35:14 -04:00
|
|
|
// Fail if file does not exist or perms aren't right
|
2020-06-30 13:31:27 -04:00
|
|
|
info, err := os.Stat(path)
|
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-07-01 14:15:52 -04:00
|
|
|
errorLog.Println("Error getting info for file " + path + ": " + err.Error())
|
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-30 16:25:37 -04:00
|
|
|
// Finally, serve the file or directory
|
2019-11-06 15:23:51 -05:00
|
|
|
if info.IsDir() {
|
2020-07-01 13:57:39 -04:00
|
|
|
serveDirectory(URL, path, &log, conn, config, errorLog)
|
2020-06-30 16:25:37 -04:00
|
|
|
} else {
|
2020-07-01 13:57:39 -04:00
|
|
|
serveFile(path, &log, conn, config, errorLog)
|
2020-01-12 07:39:38 -05:00
|
|
|
}
|
2020-06-04 14:35:14 -04:00
|
|
|
}
|
|
|
|
|
2020-07-01 13:57:39 -04:00
|
|
|
func readRequest(conn net.Conn, log *LogEntry, errorLog *log.Logger) (*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-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("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-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("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-30 13:31:27 -04:00
|
|
|
func resolvePath(path string, config Config) string {
|
2020-06-04 14:35:14 -04:00
|
|
|
// 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)
|
|
|
|
}
|
2020-06-30 13:31:27 -04:00
|
|
|
return path
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
2019-11-06 15:23:51 -05:00
|
|
|
|
2020-07-01 14:15:52 -04:00
|
|
|
func handleRedirects(URL *url.URL, config Config, conn net.Conn, log *LogEntry, errorLog *log.Logger) {
|
|
|
|
handleRedirectsInner(URL, config.TempRedirects, 30, conn, log, errorLog)
|
|
|
|
handleRedirectsInner(URL, config.PermRedirects, 31, conn, log, errorLog)
|
2020-07-01 05:13:38 -04:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:15:52 -04:00
|
|
|
func handleRedirectsInner(URL *url.URL, redirects map[string]string, status int, conn net.Conn, log *LogEntry, errorLog *log.Logger) {
|
2020-07-01 05:13:38 -04:00
|
|
|
strStatus := strconv.Itoa(status)
|
|
|
|
for src, dst := range redirects {
|
|
|
|
compiled, err := regexp.Compile(src)
|
|
|
|
if err != nil {
|
2020-07-01 14:15:52 -04:00
|
|
|
errorLog.Println("Error compiling redirect regexp " + src + ": " + err.Error())
|
2020-07-01 10:19:38 -04:00
|
|
|
continue
|
2020-06-08 14:01:03 -04:00
|
|
|
}
|
2020-07-01 05:13:38 -04:00
|
|
|
if compiled.MatchString(URL.Path) {
|
|
|
|
URL.Path = compiled.ReplaceAllString(URL.Path, dst)
|
|
|
|
conn.Write([]byte(strStatus + " " + URL.String() + "\r\n"))
|
|
|
|
log.Status = status
|
2020-06-30 16:25:37 -04:00
|
|
|
return
|
2019-11-21 16:03:39 -05:00
|
|
|
}
|
2020-06-06 15:28:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:57:39 -04:00
|
|
|
func serveDirectory(URL *url.URL, path string, log *LogEntry, conn net.Conn, config Config, errorLog *log.Logger) {
|
2020-06-30 16:25:37 -04:00
|
|
|
// 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-27 16:57:03 -04:00
|
|
|
}
|
2020-06-30 16:25:37 -04:00
|
|
|
// Check for index.gmi if path is a directory
|
|
|
|
index_path := filepath.Join(path, "index."+config.GeminiExt)
|
|
|
|
index_info, err := os.Stat(index_path)
|
|
|
|
if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 {
|
2020-07-01 13:57:39 -04:00
|
|
|
serveFile(index_path, log, conn, config, errorLog)
|
2020-06-30 16:25:37 -04:00
|
|
|
// Serve a generated listing
|
|
|
|
} else {
|
2020-07-01 16:58:07 -04:00
|
|
|
listing, err := generateDirectoryListing(URL, path, config)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Error generating listing for directory " + path + ": " + err.Error())
|
|
|
|
conn.Write([]byte("40 Server error!\r\n"))
|
|
|
|
log.Status = 40
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 16:25:37 -04:00
|
|
|
conn.Write([]byte("20 text/gemini\r\n"))
|
|
|
|
log.Status = 20
|
2020-07-01 16:58:07 -04:00
|
|
|
conn.Write([]byte(listing))
|
2020-06-27 16:57:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:57:39 -04:00
|
|
|
func serveFile(path string, log *LogEntry, conn net.Conn, config Config, errorLog *log.Logger) {
|
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 {
|
2020-07-01 14:15:52 -04:00
|
|
|
errorLog.Println("Error reading file " + path + ": " + err.Error())
|
2020-06-04 14:35:14 -04:00
|
|
|
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)
|
|
|
|
}
|