2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-06-05 13:39:28 -04:00
|
|
|
"crypto/tls"
|
2020-06-05 14:13:48 -04:00
|
|
|
"errors"
|
2019-11-06 10:08:44 -05:00
|
|
|
"fmt"
|
2019-11-06 15:23:51 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"mime"
|
2019-11-06 10:08:44 -05:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2019-11-06 15:23:51 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-01-12 07:39:38 -05:00
|
|
|
"regexp"
|
2019-11-06 15:23:51 -05:00
|
|
|
"strings"
|
2019-11-06 11:38:41 -05:00
|
|
|
"time"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
2019-11-06 11:38:41 -05:00
|
|
|
func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) {
|
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
|
2019-11-06 11:50:44 -05:00
|
|
|
defer func() { logEntries <- log }()
|
2019-11-06 11:38:41 -05:00
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
// Read request
|
2020-06-05 14:13:48 -04:00
|
|
|
URL, err := readRequest(conn, log)
|
|
|
|
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-04 14:41:40 -04:00
|
|
|
// Check whether this URL is mapped to an SCGI app
|
|
|
|
for scgi_url, scgi_socket := range config.SCGIPaths {
|
|
|
|
fmt.Println(scgi_url, URL.Path)
|
|
|
|
matched, err := regexp.Match(scgi_url, []byte(URL.Path))
|
|
|
|
if matched && err == nil {
|
|
|
|
fmt.Println("Matched:", scgi_url, scgi_socket)
|
2020-06-06 05:33:00 -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 {
|
|
|
|
conn.Write([]byte("40 Temporaray failure!\r\n"))
|
|
|
|
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
|
|
|
|
if path == config.CertPath || path == config.KeyPath || path == config.LogPath {
|
|
|
|
conn.Write([]byte("51 Not found!\r\n"))
|
|
|
|
log.Status = 51
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
index_path := filepath.Join(path, "index.gmi")
|
|
|
|
index_info, err := os.Stat(index_path)
|
|
|
|
if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 {
|
|
|
|
serveFile(index_path, log, conn)
|
2020-06-04 14:35:14 -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
|
|
|
|
conn.Write([]byte(generateDirectoryListing(path)))
|
|
|
|
}
|
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-01-12 07:39:38 -05:00
|
|
|
inCGIPath, err := regexp.Match(config.CGIPath, []byte(path))
|
|
|
|
if inCGIPath && info.Mode().Perm() & 0111 == 0111 {
|
2020-06-06 05:33:00 -04:00
|
|
|
handleCGI(config, path, URL, log, conn)
|
2020-06-04 14:35:14 -04:00
|
|
|
return
|
|
|
|
}
|
2020-01-12 06:50:01 -05:00
|
|
|
|
2019-11-24 13:24:35 -05:00
|
|
|
// Otherwise, serve the file contents
|
2020-06-04 14:35:14 -04:00
|
|
|
serveFile(path, log, conn)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:13:48 -04:00
|
|
|
func readRequest(conn net.Conn, log LogEntry) (*url.URL, error) {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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, "/~") {
|
|
|
|
bits := strings.Split(path, "/")
|
|
|
|
username := bits[1][1:]
|
|
|
|
new_prefix := filepath.Join(config.DocBase, config.HomeDocBase, username)
|
|
|
|
path = strings.Replace(path, bits[1], new_prefix, 1)
|
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
|
|
|
|
|
|
|
func generateDirectoryListing(path string) string {
|
|
|
|
var listing string
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
listing = "# Directory listing\n\n"
|
|
|
|
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-05-19 16:03:44 -04:00
|
|
|
listing += fmt.Sprintf("=> %s %s\n", url.PathEscape(file.Name()), file.Name())
|
2019-11-06 15:23:51 -05:00
|
|
|
}
|
|
|
|
return listing
|
|
|
|
}
|
2020-06-04 14:35:14 -04:00
|
|
|
|
|
|
|
func serveFile(path string, log LogEntry, conn net.Conn) {
|
|
|
|
// Get MIME type of files
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
var mimeType string
|
|
|
|
if ext == ".gmi" {
|
|
|
|
mimeType = "text/gemini"
|
|
|
|
} else {
|
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
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-04 14:35:14 -04:00
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
conn.Write([]byte("50 Error!\r\n"))
|
|
|
|
log.Status = 50
|
|
|
|
}
|
|
|
|
conn.Write([]byte(fmt.Sprintf("20 %s\r\n", mimeType)))
|
|
|
|
log.Status = 20
|
|
|
|
conn.Write(contents)
|
|
|
|
}
|
|
|
|
|