From 7475e7f9c25194fa6d874b6d351fcd3156aba7bd Mon Sep 17 00:00:00 2001 From: xenolf Date: Sun, 14 Feb 2016 16:56:14 +0100 Subject: [PATCH] Move the HTTP-01 and TLS-SNI-01 default solvers to a more unified layout. Made the solvers exported and added New... functions to them. --- acme/client.go | 4 ++-- acme/http_challenge_server.go | 22 ++++++++++++++++------ acme/tls_sni_challenge_server.go | 18 ++++++++++++++---- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/acme/client.go b/acme/client.go index 700aeab5..3851a69f 100644 --- a/acme/client.go +++ b/acme/client.go @@ -126,7 +126,7 @@ func (c *Client) SetHTTPAddress(iface string) error { } if chlng, ok := c.solvers[HTTP01]; ok { - chlng.(*httpChallenge).provider = &httpChallengeServer{iface: host, port: port} + chlng.(*httpChallenge).provider = NewHTTPProviderServer(host, port) } return nil @@ -142,7 +142,7 @@ func (c *Client) SetTLSAddress(iface string) error { } if chlng, ok := c.solvers[TLSSNI01]; ok { - chlng.(*tlsSNIChallenge).provider = &tlsSNIChallengeServer{iface: host, port: port} + chlng.(*tlsSNIChallenge).provider = NewTLSProviderServer(host, port) } return nil } diff --git a/acme/http_challenge_server.go b/acme/http_challenge_server.go index 33882236..42541380 100644 --- a/acme/http_challenge_server.go +++ b/acme/http_challenge_server.go @@ -7,16 +7,25 @@ import ( "strings" ) -// httpChallengeServer implements ChallengeProvider for `http-01` challenge -type httpChallengeServer struct { +// HTTPProviderServer implements ChallengeProvider for `http-01` challenge +// It may be instantiated without using the NewHTTPProviderServer function if +// you want only to use the default values. +type HTTPProviderServer struct { iface string port string done chan bool listener net.Listener } -// Present makes the token available at `HTTP01ChallengePath(token)` -func (s *httpChallengeServer) Present(domain, token, keyAuth string) error { +// NewHTTPProviderServer creates a new HTTPProviderServer on the selected interface and port. +// Setting iface and / or port to an empty string will make the server fall back to +// the "any" interface and port 80 respectively. +func NewHTTPProviderServer(iface, port string) *HTTPProviderServer { + return &HTTPProviderServer{iface: iface, port: port} +} + +// Present starts a web server and makes the token available at `HTTP01ChallengePath(token)` for web requests. +func (s *HTTPProviderServer) Present(domain, token, keyAuth string) error { if s.port == "" { s.port = "80" } @@ -32,7 +41,8 @@ func (s *httpChallengeServer) Present(domain, token, keyAuth string) error { return nil } -func (s *httpChallengeServer) CleanUp(domain, token, keyAuth string) error { +// CleanUp closes the HTTP server and removes the token from `HTTP01ChallengePath(token)` +func (s *HTTPProviderServer) CleanUp(domain, token, keyAuth string) error { if s.listener == nil { return nil } @@ -41,7 +51,7 @@ func (s *httpChallengeServer) CleanUp(domain, token, keyAuth string) error { return nil } -func (s *httpChallengeServer) serve(domain, token, keyAuth string) { +func (s *HTTPProviderServer) serve(domain, token, keyAuth string) { path := HTTP01ChallengePath(token) // The handler validates the HOST header and request type. diff --git a/acme/tls_sni_challenge_server.go b/acme/tls_sni_challenge_server.go index 13749632..faaf16f6 100644 --- a/acme/tls_sni_challenge_server.go +++ b/acme/tls_sni_challenge_server.go @@ -7,16 +7,25 @@ import ( "net/http" ) -// tlsSNIChallengeServer implements ChallengeProvider for `TLS-SNI-01` challenge -type tlsSNIChallengeServer struct { +// TLSProviderServer implements ChallengeProvider for `TLS-SNI-01` challenge +// It may be instantiated without using the NewTLSProviderServer function if +// you want only to use the default values. +type TLSProviderServer struct { iface string port string done chan bool listener net.Listener } +// NewTLSProviderServer creates a new TLSProviderServer on the selected interface and port. +// Setting iface and / or port to an empty string will make the server fall back to +// the "any" interface and port 443 respectively. +func NewTLSProviderServer(iface, port string) *TLSProviderServer { + return &TLSProviderServer{iface: iface, port: port} +} + // Present makes the keyAuth available as a cert -func (s *tlsSNIChallengeServer) Present(domain, token, keyAuth string) error { +func (s *TLSProviderServer) Present(domain, token, keyAuth string) error { if s.port == "" { s.port = "443" } @@ -42,7 +51,8 @@ func (s *tlsSNIChallengeServer) Present(domain, token, keyAuth string) error { return nil } -func (s *tlsSNIChallengeServer) CleanUp(domain, token, keyAuth string) error { +// CleanUp closes the HTTP server. +func (s *TLSProviderServer) CleanUp(domain, token, keyAuth string) error { if s.listener == nil { return nil }