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")
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-06 11:38:41 -05:00
|
|
|
// Open logfile
|
|
|
|
logfile, err := os.OpenFile(config.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
2020-06-08 14:02:29 -04:00
|
|
|
log.Fatal(err)
|
2019-11-06 11:38:41 -05:00
|
|
|
}
|
|
|
|
defer logfile.Close()
|
|
|
|
|
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 {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
tlscfg := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-06-05 13:39:28 -04:00
|
|
|
ClientAuth: tls.RequestClientCert,
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create TLS listener
|
2020-05-19 16:05:48 -04:00
|
|
|
listener, err := tls.Listen("tcp", ":" + strconv.Itoa(config.Port), tlscfg)
|
2019-11-06 10:08:44 -05:00
|
|
|
if err != nil {
|
2020-06-08 14:02:29 -04:00
|
|
|
log.Fatal(err)
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
2019-11-06 11:38:41 -05:00
|
|
|
// Start log handling routine
|
|
|
|
logEntries := make(chan LogEntry, 10)
|
|
|
|
go func () {
|
|
|
|
for {
|
|
|
|
entry := <- logEntries
|
|
|
|
writeLogEntry(logfile, entry)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-06 10:08:44 -05:00
|
|
|
// Infinite serve loop
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-06 11:38:41 -05:00
|
|
|
go handleGeminiRequest(conn, config, logEntries)
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|