2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-05-21 16:50:33 -04:00
|
|
|
"strconv"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var conf_file string
|
|
|
|
|
|
|
|
// Parse args and read config
|
|
|
|
flag.StringVar(&conf_file, "c", "", "Path to config file")
|
|
|
|
flag.Parse()
|
|
|
|
if conf_file == "" {
|
|
|
|
_, err := os.Stat("/etc/molly.conf")
|
2020-07-01 13:56:43 -04:00
|
|
|
if err == nil {
|
2019-11-06 10:08:44 -05:00
|
|
|
conf_file = "/etc/molly.conf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config, err := getConfig(conf_file)
|
|
|
|
if err != nil {
|
2020-06-08 14:02:29 -04:00
|
|
|
log.Fatal(err)
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
// Open log files
|
|
|
|
errorLogFile, err := os.OpenFile(config.ErrorLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2019-11-06 11:38:41 -05:00
|
|
|
if err != nil {
|
2020-06-08 14:02:29 -04:00
|
|
|
log.Fatal(err)
|
2019-11-06 11:38:41 -05:00
|
|
|
}
|
2020-06-28 12:34:50 -04:00
|
|
|
defer errorLogFile.Close()
|
2020-07-01 13:57:39 -04:00
|
|
|
errorLog := log.New(errorLogFile, "", log.Ldate | log.Ltime)
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
accessLogFile, err := os.OpenFile(config.AccessLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
2020-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("Error opening access log file: " + err.Error())
|
2020-06-28 12:34:50 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer accessLogFile.Close()
|
2019-11-06 11:38:41 -05:00
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
// Read TLS files, create TLS config
|
|
|
|
cert, err := tls.LoadX509KeyPair(config.CertPath, config.KeyPath)
|
|
|
|
if err != nil {
|
2020-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("Error loading TLS keypair: " + err.Error())
|
2019-11-06 10:08:44 -05:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
tlscfg := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2020-06-10 15:31:13 -04:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ClientAuth: tls.RequestClientCert,
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create TLS listener
|
2020-06-10 15:31:13 -04:00
|
|
|
listener, err := tls.Listen("tcp", ":"+strconv.Itoa(config.Port), tlscfg)
|
2019-11-06 10:08:44 -05:00
|
|
|
if err != nil {
|
2020-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("Error creating TLS listener: " + err.Error())
|
2020-06-08 14:02:29 -04:00
|
|
|
log.Fatal(err)
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
2020-06-28 12:34:50 -04:00
|
|
|
// Start log handling routines
|
|
|
|
accessLogEntries := make(chan LogEntry, 10)
|
|
|
|
go func() {
|
|
|
|
for {
|
2020-06-30 13:13:02 -04:00
|
|
|
entry := <-accessLogEntries
|
2020-06-28 12:34:50 -04:00
|
|
|
writeLogEntry(accessLogFile, entry)
|
|
|
|
}
|
|
|
|
}()
|
2019-11-06 11:38:41 -05:00
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
// Infinite serve loop
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2020-07-01 13:57:39 -04:00
|
|
|
errorLog.Println("Error accepting connection: " + err.Error())
|
2019-11-06 10:08:44 -05:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-07-01 13:57:39 -04:00
|
|
|
go handleGeminiRequest(conn, config, accessLogEntries, errorLog)
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|