Castor is a gemini-protocol server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
2.0 KiB

package main
import (
"bufio"
"crypto/tls"
"flag"
"mime"
"net"
"net/url"
"git.yetaga.in/alazyreader/castor/certificates"
"git.yetaga.in/alazyreader/castor/handlers"
"git.yetaga.in/alazyreader/castor/logger"
)
func handleConnection(log logger.Logger, conn net.Conn, h handlers.Handler) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
if ok := scanner.Scan(); !ok {
log.Debug(scanner.Err())
}
u, err := url.Parse(scanner.Text())
if err != nil {
log.Debug(err)
}
w := handlers.NewResponse(conn, log)
r := handlers.NewRequest(u)
handlers.RecoveryHandler(log, h).Handle(w, r)
}
func main() {
hostname := flag.String("hostname", "localhost", "comma-separated list of hostnames to generate certificates for")
regenCerts := flag.Bool("regenerate-certs", false, "set to true to regenerate cert files")
serveFrom := flag.String("path", "./root/", "filepath to serve files from; defaults to ./root/")
flag.Parse()
log := logger.NewLogger(true)
mime.AddExtensionType(".gemini", "text/gemini")
mime.AddExtensionType(".gmi", "text/gemini")
err := certificates.TestCertificateExists("./certs/cert.pem", "./certs/key.pem")
var cer tls.Certificate
if err != nil || *regenCerts {
log.Info("Generating new certificate...")
key, cert, err := certificates.GenerateKeyPair(*hostname)
if err != nil {
log.Info("error generating certificates", err)
return
}
err = certificates.WriteCertsToFile("./certs/cert.pem", "./certs/key.pem", cert, key)
if err != nil {
log.Info("error saving certificates", err)
return
}
}
cer, err = tls.LoadX509KeyPair("./certs/cert.pem", "./certs/key.pem")
if err != nil {
log.Info("error loading certificates", err)
return
}
tlsc := tls.Config{
Certificates: []tls.Certificate{cer},
MinVersion: tls.VersionTLS12,
}
listener, err := tls.Listen("tcp", ":1965", &tlsc)
if err != nil {
log.Info(err)
return
}
for {
conn, err := listener.Accept()
if err != nil {
log.Debug(err.Error())
continue
}
go handleConnection(log, conn, handlers.NewFileHandler(*serveFrom))
}
}