molly-brown/main.go

85 lines
1.7 KiB
Go
Raw Normal View History

2019-11-06 15:08:44 +00:00
package main
import (
"crypto/tls"
"flag"
"log"
"os"
"strconv"
2020-06-28 16:34:50 +00:00
"time"
2019-11-06 15:08:44 +00: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 17:56:43 +00:00
if err == nil {
2019-11-06 15:08:44 +00:00
conf_file = "/etc/molly.conf"
}
}
config, err := getConfig(conf_file)
if err != nil {
log.Fatal(err)
2019-11-06 15:08:44 +00:00
}
2020-06-28 16:34:50 +00:00
// Open log files
errorLogFile, err := os.OpenFile(config.ErrorLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2019-11-06 16:38:41 +00:00
if err != nil {
log.Fatal(err)
2019-11-06 16:38:41 +00:00
}
2020-06-28 16:34:50 +00:00
defer errorLogFile.Close()
accessLogFile, err := os.OpenFile(config.AccessLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer accessLogFile.Close()
2019-11-06 16:38:41 +00:00
2019-11-06 15:08:44 +00:00
// Read TLS files, create TLS config
cert, err := tls.LoadX509KeyPair(config.CertPath, config.KeyPath)
if err != nil {
log.Fatal(err)
}
tlscfg := &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequestClientCert,
2019-11-06 15:08:44 +00:00
}
// Create TLS listener
listener, err := tls.Listen("tcp", ":"+strconv.Itoa(config.Port), tlscfg)
2019-11-06 15:08:44 +00:00
if err != nil {
log.Fatal(err)
2019-11-06 15:08:44 +00:00
}
defer listener.Close()
2020-06-28 16:34:50 +00:00
// Start log handling routines
accessLogEntries := make(chan LogEntry, 10)
go func() {
for {
2020-06-30 17:13:02 +00:00
entry := <-accessLogEntries
2020-06-28 16:34:50 +00:00
writeLogEntry(accessLogFile, entry)
}
}()
errorLogEntries := make(chan string, 10)
go func() {
2019-11-06 16:38:41 +00:00
for {
2020-06-30 17:13:02 +00:00
message := <-errorLogEntries
errorLogFile.WriteString(time.Now().Format(time.RFC3339) + " " + message + "\n")
2019-11-06 16:38:41 +00:00
}
}()
2019-11-06 15:08:44 +00:00
// Infinite serve loop
for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
2020-06-28 16:34:50 +00:00
go handleGeminiRequest(conn, config, accessLogEntries, errorLogEntries)
2019-11-06 15:08:44 +00:00
}
}