diff options
| author | adnano <[email protected]> | 2020-09-27 23:49:41 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-27 23:49:41 -0400 |
| commit | ae4b458964700692dd2bc03ba3f269ea88269fda (patch) | |
| tree | 55eb3422c9b0d68d27d1a0794c5f5f0651fa5c72 /examples | |
| parent | Remove tilde from comment (diff) | |
| download | go-gemini-ae4b458964700692dd2bc03ba3f269ea88269fda.tar.xz go-gemini-ae4b458964700692dd2bc03ba3f269ea88269fda.zip | |
Generate certificates on demand
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/cert/cert.go | 8 | ||||
| -rw-r--r-- | examples/client/client.go | 24 |
2 files changed, 25 insertions, 7 deletions
diff --git a/examples/cert/cert.go b/examples/cert/cert.go index 23975c0..b9a284f 100644 --- a/examples/cert/cert.go +++ b/examples/cert/cert.go @@ -4,19 +4,21 @@ package main import ( "log" + "time" - "git.sr.ht/~adnano/go-gemini" + gmi "git.sr.ht/~adnano/go-gemini" ) func main() { host := "localhost" - crt, key, err := gemini.NewCertificate(host) + duration := 365 * 24 * time.Hour + crt, key, err := gmi.NewRawCertificate(host, duration) if err != nil { log.Fatal(err) } - if err := gemini.WriteCertificate(host, crt, key); err != nil { + if err := gmi.WriteCertificate(host, crt, key); err != nil { log.Fatal(err) } } diff --git a/examples/client/client.go b/examples/client/client.go index 22c96de..a64365f 100644 --- a/examples/client/client.go +++ b/examples/client/client.go @@ -4,21 +4,22 @@ package main import ( "bufio" + "crypto/tls" "crypto/x509" "fmt" "os" + "time" - "git.sr.ht/~adnano/go-gemini" + gmi "git.sr.ht/~adnano/go-gemini" ) var ( scanner = bufio.NewScanner(os.Stdin) - client *gmi.Client + client = &gmi.Client{} ) func init() { // Initialize the client - client = &gmi.Client{} client.KnownHosts.Load() // Load known hosts client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gmi.KnownHosts) error { err := knownHosts.Lookup(hostname, cert) @@ -45,6 +46,21 @@ func init() { } return err } + + client.CertificateStore = gmi.NewCertificateStore() + client.GetCertificate = func(hostname string, store gmi.CertificateStore) *tls.Certificate { + if cert, ok := store[hostname]; ok { + return cert + } + // Generate a certificate + duration := time.Hour + cert, err := gmi.NewCertificate(hostname, duration) + if err != nil { + return nil + } + store[hostname] = &cert + return &cert + } } // sendRequest sends a request to the given URL. @@ -67,7 +83,7 @@ func sendRequest(req *gmi.Request) error { case gmi.StatusClassRedirect: fmt.Println("Redirecting to", resp.Meta) // Make the request to the same host - red, err := gmi.NewRequestTo(req.Host, resp.Meta) + red, err := gmi.NewRequestTo(resp.Meta, req.Host) if err != nil { return err } |