Run gofmt on everything for the first time ever!

This commit is contained in:
Solderpunk 2020-06-10 21:31:13 +02:00
parent 3e80488f92
commit b0b18971f4
5 changed files with 79 additions and 80 deletions

View File

@ -5,24 +5,24 @@ import (
) )
type Config struct { type Config struct {
Port int Port int
Hostname string Hostname string
CertPath string CertPath string
KeyPath string KeyPath string
DocBase string DocBase string
HomeDocBase string HomeDocBase string
GeminiExt string GeminiExt string
DefaultLang string DefaultLang string
LogPath string LogPath string
TempRedirects map[string]string TempRedirects map[string]string
PermRedirects map[string]string PermRedirects map[string]string
CGIPaths []string CGIPaths []string
SCGIPaths map[string]string SCGIPaths map[string]string
} }
type MollyFile struct { type MollyFile struct {
GeminiExt string GeminiExt string
DefaultLang string DefaultLang string
} }
func getConfig(filename string) (Config, error) { func getConfig(filename string) (Config, error) {

View File

@ -1,18 +1,18 @@
package main package main
import ( import (
"bufio" "bufio"
"context" "context"
"crypto/sha256" "crypto/sha256"
"crypto/tls" "crypto/tls"
"encoding/hex" "encoding/hex"
"io" "io"
"net" "net"
"net/url" "net/url"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
func handleCGI(config Config, path string, URL *url.URL, log *LogEntry, conn net.Conn) { func handleCGI(config Config, path string, URL *url.URL, log *LogEntry, conn net.Conn) {
@ -22,9 +22,9 @@ func handleCGI(config Config, path string, URL *url.URL, log *LogEntry, conn net
// Set environment variables // Set environment variables
vars := prepareCGIVariables(config, URL, conn, path) vars := prepareCGIVariables(config, URL, conn, path)
cmd.Env = []string{ } cmd.Env = []string{}
for key, value := range vars { for key, value := range vars {
cmd.Env = append(cmd.Env, key + "=" + value) cmd.Env = append(cmd.Env, key+"="+value)
} }
response, err := cmd.Output() response, err := cmd.Output()
@ -65,13 +65,13 @@ func handleSCGI(socket_path string, config Config, URL *url.URL, log *LogEntry,
// Send variables // Send variables
vars := prepareSCGIVariables(config, URL, conn) vars := prepareSCGIVariables(config, URL, conn)
length := 0 length := 0
for key, value := range(vars) { for key, value := range vars {
length += len(key) length += len(key)
length += len(value) length += len(value)
length += 2 length += 2
} }
socket.Write([]byte(strconv.Itoa(length) + ":")) socket.Write([]byte(strconv.Itoa(length) + ":"))
for key, value := range(vars) { for key, value := range vars {
socket.Write([]byte(key + "\x00")) socket.Write([]byte(key + "\x00"))
socket.Write([]byte(value + "\x00")) socket.Write([]byte(value + "\x00"))
} }
@ -139,7 +139,7 @@ func prepareGatewayVariables(config Config, URL *url.URL, conn net.Conn) map[str
// Add TLS variables // Add TLS variables
var tlsConn (*tls.Conn) = conn.(*tls.Conn) var tlsConn (*tls.Conn) = conn.(*tls.Conn)
connState := tlsConn.ConnectionState() connState := tlsConn.ConnectionState()
// vars["TLS_CIPHER"] = CipherSuiteName(connState.CipherSuite) // vars["TLS_CIPHER"] = CipherSuiteName(connState.CipherSuite)
// Add client cert variables // Add client cert variables
clientCerts := connState.PeerCertificates clientCerts := connState.PeerCertificates

View File

@ -1,21 +1,21 @@
package main package main
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "github.com/BurntSushi/toml"
"log" "io/ioutil"
"mime" "log"
"net" "mime"
"net/url" "net"
"os" "net/url"
"path/filepath" "os"
"regexp" "path/filepath"
"strings" "regexp"
"time" "strings"
"github.com/BurntSushi/toml" "time"
) )
func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) { func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) {
@ -93,7 +93,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
// Check whether this URL is mapped to an SCGI app // Check whether this URL is mapped to an SCGI app
for scgi_url, scgi_socket := range config.SCGIPaths { for scgi_url, scgi_socket := range config.SCGIPaths {
matched, err := regexp.Match(scgi_url, []byte(URL.Path)) matched, err := regexp.Match(scgi_url, []byte(URL.Path))
if matched && err == nil { if matched && err == nil {
handleSCGI(scgi_socket, config, URL, &log, conn) handleSCGI(scgi_socket, config, URL, &log, conn)
return return
@ -127,7 +127,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
} }
// Don't serve Molly files // Don't serve Molly files
if ! info.IsDir() && filepath.Base(path) == ".molly" { if !info.IsDir() && filepath.Base(path) == ".molly" {
conn.Write([]byte("51 Not found!\r\n")) conn.Write([]byte("51 Not found!\r\n"))
log.Status = 51 log.Status = 51
return return
@ -146,11 +146,11 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
return return
} }
// Check for index.gmi if path is a directory // Check for index.gmi if path is a directory
index_path := filepath.Join(path, "index." + config.GeminiExt) index_path := filepath.Join(path, "index."+config.GeminiExt)
index_info, err := os.Stat(index_path) index_info, err := os.Stat(index_path)
if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 { if err == nil && uint64(index_info.Mode().Perm())&0444 == 0444 {
serveFile(index_path, &log, conn, config) serveFile(index_path, &log, conn, config)
// Serve a generated listing // Serve a generated listing
} else { } else {
conn.Write([]byte("20 text/gemini\r\n")) conn.Write([]byte("20 text/gemini\r\n"))
log.Status = 20 log.Status = 20
@ -160,10 +160,10 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
} }
// If this file is executable, get dynamic content // If this file is executable, get dynamic content
if info.Mode().Perm() & 0111 == 0111 { if info.Mode().Perm()&0111 == 0111 {
for _, cgiPath := range(config.CGIPaths) { for _, cgiPath := range config.CGIPaths {
inCGIPath, err := regexp.Match(cgiPath, []byte(path)) inCGIPath, err := regexp.Match(cgiPath, []byte(path))
if err == nil && inCGIPath { if err == nil && inCGIPath {
handleCGI(config, path, URL, &log, conn) handleCGI(config, path, URL, &log, conn)
return return
} }
@ -176,7 +176,7 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
} }
func readRequest(conn net.Conn, log *LogEntry) (*url.URL, error) { func readRequest(conn net.Conn, log *LogEntry) (*url.URL, error) {
reader := bufio.NewReaderSize(conn, 1024) reader := bufio.NewReaderSize(conn, 1024)
request, overflow, err := reader.ReadLine() request, overflow, err := reader.ReadLine()
if overflow { if overflow {
@ -209,7 +209,7 @@ func readRequest(conn net.Conn, log *LogEntry) (*url.URL, error) {
func resolvePath(path string, config Config) (string, os.FileInfo, error) { func resolvePath(path string, config Config) (string, os.FileInfo, error) {
// Handle tildes // Handle tildes
if strings.HasPrefix(path, "/~") { if strings.HasPrefix(path, "/~") {
bits := strings.Split(path, "/") bits := strings.Split(path, "/")
username := bits[1][1:] username := bits[1][1:]
new_prefix := filepath.Join(config.DocBase, config.HomeDocBase, username) new_prefix := filepath.Join(config.DocBase, config.HomeDocBase, username)
path = strings.Replace(path, bits[1], new_prefix, 1) path = strings.Replace(path, bits[1], new_prefix, 1)
@ -228,7 +228,7 @@ func resolvePath(path string, config Config) (string, os.FileInfo, error) {
func parseMollyFiles(path string, info os.FileInfo, config *Config) { func parseMollyFiles(path string, info os.FileInfo, config *Config) {
// Build list of directories to check // Build list of directories to check
dirs := make([]string, 16) dirs := make([]string, 16)
if ! info.IsDir() { if !info.IsDir() {
path = filepath.Dir(path) path = filepath.Dir(path)
} }
dirs = append(dirs, path) dirs = append(dirs, path)
@ -242,7 +242,7 @@ func parseMollyFiles(path string, info os.FileInfo, config *Config) {
} }
// Parse files // Parse files
var mollyFile MollyFile var mollyFile MollyFile
for i := len(dirs)-1; i >= 0; i-- { for i := len(dirs) - 1; i >= 0; i-- {
dir := dirs[i] dir := dirs[i]
mollyPath := filepath.Join(dir, ".molly") mollyPath := filepath.Join(dir, ".molly")
_, err := os.Stat(mollyPath) _, err := os.Stat(mollyPath)
@ -299,13 +299,13 @@ func generatePrettyFileLabel(info os.FileInfo) string {
} else if info.Size() < 1024 { } else if info.Size() < 1024 {
size = fmt.Sprintf("%4d B", info.Size()) size = fmt.Sprintf("%4d B", info.Size())
} else if info.Size() < (1024 << 10) { } else if info.Size() < (1024 << 10) {
size = fmt.Sprintf("%4d KiB", info.Size() >> 10) size = fmt.Sprintf("%4d KiB", info.Size()>>10)
} else if info.Size() < 1024 << 20 { } else if info.Size() < 1024<<20 {
size = fmt.Sprintf("%4d MiB", info.Size() >> 20) size = fmt.Sprintf("%4d MiB", info.Size()>>20)
} else if info.Size() < 1024 << 30 { } else if info.Size() < 1024<<30 {
size = fmt.Sprintf("%4d GiB", info.Size() >> 30) size = fmt.Sprintf("%4d GiB", info.Size()>>30)
} else if info.Size() < 1024 << 40 { } else if info.Size() < 1024<<40 {
size = fmt.Sprintf("%4d TiB", info.Size() >> 40) size = fmt.Sprintf("%4d TiB", info.Size()>>40)
} else { } else {
size = "GIGANTIC" size = "GIGANTIC"
} }
@ -326,7 +326,7 @@ func serveFile(path string, log *LogEntry, conn net.Conn, config Config) {
// Get MIME type of files // Get MIME type of files
ext := filepath.Ext(path) ext := filepath.Ext(path)
var mimeType string var mimeType string
if ext == "." + config.GeminiExt { if ext == "."+config.GeminiExt {
mimeType = "text/gemini" mimeType = "text/gemini"
} else { } else {
mimeType = mime.TypeByExtension(ext) mimeType = mime.TypeByExtension(ext)
@ -349,4 +349,3 @@ func serveFile(path string, log *LogEntry, conn net.Conn, config Config) {
log.Status = 20 log.Status = 20
conn.Write(contents) conn.Write(contents)
} }

View File

@ -1,17 +1,17 @@
package main package main
import ( import (
"net" "net"
"os" "os"
"strconv" "strconv"
"time" "time"
) )
type LogEntry struct { type LogEntry struct {
Time time.Time Time time.Time
RemoteAddr net.Addr RemoteAddr net.Addr
RequestURL string RequestURL string
Status int Status int
} }
func writeLogEntry(fp *os.File, entry LogEntry) { func writeLogEntry(fp *os.File, entry LogEntry) {

10
main.go
View File

@ -39,12 +39,12 @@ func main() {
} }
tlscfg := &tls.Config{ tlscfg := &tls.Config{
Certificates: []tls.Certificate{cert}, Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequestClientCert, ClientAuth: tls.RequestClientCert,
} }
// Create TLS listener // Create TLS listener
listener, err := tls.Listen("tcp", ":" + strconv.Itoa(config.Port), tlscfg) listener, err := tls.Listen("tcp", ":"+strconv.Itoa(config.Port), tlscfg)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -52,9 +52,9 @@ func main() {
// Start log handling routine // Start log handling routine
logEntries := make(chan LogEntry, 10) logEntries := make(chan LogEntry, 10)
go func () { go func() {
for { for {
entry := <- logEntries entry := <-logEntries
writeLogEntry(logfile, entry) writeLogEntry(logfile, entry)
} }
}() }()