2020-10-12 20:34:52 +00:00
|
|
|
// +build ignore
|
2020-09-27 21:39:44 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-25 19:24:49 +00:00
|
|
|
"crypto/sha512"
|
2020-11-01 21:25:59 +00:00
|
|
|
"crypto/tls"
|
2020-09-27 21:39:44 +00:00
|
|
|
"crypto/x509"
|
2020-11-03 04:11:46 +00:00
|
|
|
"crypto/x509/pkix"
|
2020-09-27 21:39:44 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-11-01 21:25:59 +00:00
|
|
|
"time"
|
2020-09-27 21:39:44 +00:00
|
|
|
|
2020-10-28 03:35:22 +00:00
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
2021-01-15 02:23:13 +00:00
|
|
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
2020-09-27 21:39:44 +00:00
|
|
|
)
|
|
|
|
|
2020-11-25 19:24:49 +00:00
|
|
|
type User struct {
|
|
|
|
Name string
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-11-25 19:24:49 +00:00
|
|
|
// Map of certificate hashes to users
|
|
|
|
users = map[string]*User{}
|
2020-09-27 21:39:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-10-28 03:35:22 +00:00
|
|
|
var mux gemini.ServeMux
|
2020-11-25 19:24:49 +00:00
|
|
|
mux.HandleFunc("/", profile)
|
|
|
|
mux.HandleFunc("/username", changeUsername)
|
2020-10-21 21:47:34 +00:00
|
|
|
|
2020-10-28 03:35:22 +00:00
|
|
|
var server gemini.Server
|
2020-10-28 18:59:45 +00:00
|
|
|
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
|
2020-10-12 04:13:24 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-11-01 21:25:59 +00:00
|
|
|
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
2021-01-15 02:23:13 +00:00
|
|
|
return certificate.Create(certificate.CreateOptions{
|
2020-11-03 04:11:46 +00:00
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: hostname,
|
|
|
|
},
|
2020-11-01 21:25:59 +00:00
|
|
|
DNSNames: []string{hostname},
|
|
|
|
Duration: time.Hour,
|
|
|
|
})
|
|
|
|
}
|
2020-10-21 21:47:34 +00:00
|
|
|
server.Register("localhost", &mux)
|
2020-09-27 21:39:44 +00:00
|
|
|
|
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 19:24:49 +00:00
|
|
|
func fingerprint(cert *x509.Certificate) string {
|
|
|
|
b := sha512.Sum512(cert.Raw)
|
|
|
|
return string(b[:])
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 19:24:49 +00:00
|
|
|
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
2020-11-01 21:25:59 +00:00
|
|
|
if r.Certificate == nil {
|
2021-01-15 00:04:11 +00:00
|
|
|
w.Status(gemini.StatusCertificateRequired)
|
2020-10-21 21:47:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-25 19:24:49 +00:00
|
|
|
fingerprint := fingerprint(r.Certificate.Leaf)
|
|
|
|
user, ok := users[fingerprint]
|
2020-10-21 21:47:34 +00:00
|
|
|
if !ok {
|
2020-11-25 19:24:49 +00:00
|
|
|
user = &User{}
|
|
|
|
users[fingerprint] = user
|
2020-10-21 21:47:34 +00:00
|
|
|
}
|
2020-11-25 19:24:49 +00:00
|
|
|
fmt.Fprintln(w, "Username:", user.Name)
|
|
|
|
fmt.Fprintln(w, "=> /username Change username")
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 19:24:49 +00:00
|
|
|
func changeUsername(w *gemini.ResponseWriter, r *gemini.Request) {
|
2020-11-01 21:25:59 +00:00
|
|
|
if r.Certificate == nil {
|
2021-01-15 00:04:11 +00:00
|
|
|
w.Status(gemini.StatusCertificateRequired)
|
2020-10-21 21:47:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-27 21:39:44 +00:00
|
|
|
|
2020-12-18 05:25:06 +00:00
|
|
|
username, err := gemini.QueryUnescape(r.URL.RawQuery)
|
|
|
|
if err != nil || username == "" {
|
2021-01-15 00:04:11 +00:00
|
|
|
w.Header(gemini.StatusInput, "Username")
|
2020-10-21 21:47:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-18 02:10:53 +00:00
|
|
|
fingerprint := fingerprint(r.Certificate.Leaf)
|
|
|
|
user, ok := users[fingerprint]
|
|
|
|
if !ok {
|
|
|
|
user = &User{}
|
|
|
|
users[fingerprint] = user
|
|
|
|
}
|
|
|
|
user.Name = username
|
2021-01-15 00:04:11 +00:00
|
|
|
w.Header(gemini.StatusRedirect, "/")
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|