From 3dee6dcff3a55282e9c0e7bf7310d9c8720a4bda Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Mon, 9 Nov 2020 13:54:15 -0500 Subject: [PATCH] Add (*CertificateStore).Write function --- cert.go | 15 ++++++++++++--- server.go | 7 ++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cert.go b/cert.go index 23cdcd4..2650082 100644 --- a/cert.go +++ b/cert.go @@ -28,7 +28,7 @@ type CertificateStore struct { // Add adds a certificate for the given scope to the store. // It tries to parse the certificate if it is not already parsed. -func (c *CertificateStore) Add(scope string, cert tls.Certificate) error { +func (c *CertificateStore) Add(scope string, cert tls.Certificate) { if c.store == nil { c.store = map[string]tls.Certificate{} } @@ -39,15 +39,18 @@ func (c *CertificateStore) Add(scope string, cert tls.Certificate) error { cert.Leaf = parsed } } + c.store[scope] = cert +} + +// Write writes the provided certificate to the certificate directory. +func (c *CertificateStore) Write(scope string, cert tls.Certificate) error { if c.dir { - // Write certificates certPath := filepath.Join(c.path, scope+".crt") keyPath := filepath.Join(c.path, scope+".key") if err := WriteCertificate(cert, certPath, keyPath); err != nil { return err } } - c.store[scope] = cert return nil } @@ -82,6 +85,12 @@ func (c *CertificateStore) Load(path string) error { return nil } +// SetOutput sets the directory that new certificates will be written to. +func (c *CertificateStore) SetOutput(path string) { + c.dir = true + c.path = path +} + // CertificateOptions configures the creation of a certificate. type CertificateOptions struct { // Subject Alternate Name values. diff --git a/server.go b/server.go index 6cc98f3..52de33a 100644 --- a/server.go +++ b/server.go @@ -160,8 +160,9 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) { if s.CreateCertificate != nil { cert, err := s.CreateCertificate(hostname) if err == nil { - if err := s.Certificates.Add(hostname, cert); err != nil { - s.logf("gemini: Failed to add new certificate for %s: %s", hostname, err) + s.Certificates.Add(hostname, cert) + if err := s.Certificates.Write(hostname, cert); err != nil { + s.logf("gemini: Failed to write new certificate for %s: %s", hostname, err) } } return &cert, err @@ -262,7 +263,7 @@ type ResponseWriter struct { b *bufio.Writer bodyAllowed bool wroteHeader bool - mediatype string + mediatype string } func newResponseWriter(conn net.Conn) *ResponseWriter {