go-gemini/examples/server.go

80 lines
2.0 KiB
Go
Raw Normal View History

2020-10-12 14:34:52 -06:00
// +build ignore
2020-09-21 15:23:51 -06:00
package main
import (
2020-10-28 12:59:45 -06:00
"crypto"
"crypto/tls"
2020-10-13 15:33:14 -06:00
"crypto/x509"
"encoding/pem"
2020-10-28 12:59:45 -06:00
"io"
2020-09-21 15:23:51 -06:00
"log"
2020-10-13 15:33:14 -06:00
"os"
"time"
2020-10-28 12:59:45 -06:00
"git.sr.ht/~adnano/go-gemini"
2020-09-21 15:23:51 -06:00
)
func main() {
2020-10-28 12:59:45 -06:00
var server gemini.Server
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
2020-10-12 14:34:52 -06:00
log.Fatal(err)
}
2020-10-28 12:59:45 -06:00
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
fmt.Println("Generating certificate for", hostname)
cert, err := gemini.CreateCertificate(gemini.CertificateOptions{
DNSNames: []string{hostname},
Duration: time.Minute, // for testing purposes
})
if err == nil {
// Write the new certificate to disk
err = writeCertificate("/var/lib/gemini/certs/"+hostname, cert)
}
2020-10-28 12:59:45 -06:00
return cert, err
}
2020-10-12 14:34:52 -06:00
2020-10-28 12:59:45 -06:00
var mux gemini.ServeMux
mux.Handle("/", gemini.FileServer(gemini.Dir("/var/www")))
2020-09-21 15:23:51 -06:00
2020-10-21 14:28:50 -06:00
server.Register("localhost", &mux)
2020-10-12 14:34:52 -06:00
if err := server.ListenAndServe(); err != nil {
2020-10-11 22:13:24 -06:00
log.Fatal(err)
}
2020-09-21 15:23:51 -06:00
}
2020-10-13 14:50:59 -06:00
2020-10-13 15:33:14 -06:00
// writeCertificate writes the provided certificate and private key
2020-10-13 14:50:59 -06:00
// to path.crt and path.key respectively.
2020-10-13 15:33:14 -06:00
func writeCertificate(path string, cert tls.Certificate) error {
2020-10-13 14:50:59 -06:00
// Write the certificate
crtPath := path + ".crt"
crtOut, err := os.OpenFile(crtPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
2020-10-28 12:59:45 -06:00
if err := marshalX509Certificate(crtOut, cert.Leaf.Raw); err != nil {
2020-10-13 14:50:59 -06:00
return err
}
// Write the private key
keyPath := path + ".key"
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
2020-10-28 12:59:45 -06:00
return marshalPrivateKey(keyOut, cert.PrivateKey)
2020-10-13 14:50:59 -06:00
}
2020-10-13 15:33:14 -06:00
2020-10-28 12:59:45 -06:00
// marshalX509Certificate writes a PEM-encoded version of the given certificate.
func marshalX509Certificate(w io.Writer, cert []byte) error {
return pem.Encode(w, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
2020-10-13 15:33:14 -06:00
}
2020-10-28 12:59:45 -06:00
// marshalPrivateKey writes a PEM-encoded version of the given private key.
func marshalPrivateKey(w io.Writer, priv crypto.PrivateKey) error {
2020-10-13 15:33:14 -06:00
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
2020-10-28 12:59:45 -06:00
return err
2020-10-13 15:33:14 -06:00
}
2020-10-28 12:59:45 -06:00
return pem.Encode(w, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
2020-10-13 15:33:14 -06:00
}