2020-10-12 22:34:52 +02:00
|
|
|
// +build ignore
|
2020-09-21 23:23:51 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-13 20:22:15 +02:00
|
|
|
"crypto/tls"
|
2020-09-21 23:23:51 +02:00
|
|
|
"log"
|
2020-10-13 20:22:15 +02:00
|
|
|
"time"
|
2020-09-22 01:17:10 +02:00
|
|
|
|
2020-09-29 00:19:59 +02:00
|
|
|
"git.sr.ht/~adnano/gmi"
|
2020-09-21 23:23:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-10-12 22:34:52 +02:00
|
|
|
var server gmi.Server
|
|
|
|
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-13 20:22:15 +02:00
|
|
|
server.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate {
|
|
|
|
cert, err := store.Lookup(hostname)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case gmi.ErrInvalidCertificate:
|
|
|
|
log.Print("Old certificate expired, creating new one")
|
|
|
|
// Generate a new certificate if the old one is expired.
|
|
|
|
crt, key, err := gmi.NewRawCertificate(hostname, time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
// Failed to generate new certificate, abort
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Store and return the new certificate
|
|
|
|
err = gmi.WriteX509KeyPair("/var/lib/gemini/certs/"+hostname, crt, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
newCert, err := tls.X509KeyPair(crt, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
store.Add(hostname, newCert)
|
|
|
|
return &newCert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cert
|
|
|
|
}
|
2020-10-12 22:34:52 +02:00
|
|
|
|
|
|
|
var mux gmi.ServeMux
|
2020-09-28 02:20:59 +02:00
|
|
|
mux.Handle("/", gmi.FileServer(gmi.Dir("/var/www")))
|
2020-09-21 23:23:51 +02:00
|
|
|
|
2020-10-12 22:34:52 +02:00
|
|
|
server.Handle("localhost", &mux)
|
|
|
|
if err := server.ListenAndServe(); err != nil {
|
2020-10-12 06:13:24 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-21 23:23:51 +02:00
|
|
|
}
|