2020-10-12 20:34:52 +00:00
|
|
|
// +build ignore
|
2020-09-21 21:23:51 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-13 18:22:15 +00:00
|
|
|
"crypto/tls"
|
2020-09-21 21:23:51 +00:00
|
|
|
"log"
|
2020-10-13 18:22:15 +00:00
|
|
|
"time"
|
2020-09-21 23:17:10 +00:00
|
|
|
|
2020-09-28 22:19:59 +00:00
|
|
|
"git.sr.ht/~adnano/gmi"
|
2020-09-21 21:23:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-10-12 20:34:52 +00:00
|
|
|
var server gmi.Server
|
|
|
|
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-13 18:22:15 +00: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 20:34:52 +00:00
|
|
|
|
|
|
|
var mux gmi.ServeMux
|
2020-09-28 00:20:59 +00:00
|
|
|
mux.Handle("/", gmi.FileServer(gmi.Dir("/var/www")))
|
2020-09-21 21:23:51 +00:00
|
|
|
|
2020-10-12 20:34:52 +00:00
|
|
|
server.Handle("localhost", &mux)
|
|
|
|
if err := server.ListenAndServe(); err != nil {
|
2020-10-12 04:13:24 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-21 21:23:51 +00:00
|
|
|
}
|