From 5dc33c8c084672f43917681fa648a9a92a504725 Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Sat, 5 Dec 2015 11:58:08 +0000 Subject: [PATCH] Simplify httpChallenge code. Solve is blocking, so no need to run initialization code in a separate goroutine. Removes the need for s.start. Once the listener is closed, all I/O resources have been returned. No need to wait for http.Serve to return. Removes the need for s.end. --- acme/http_challenge.go | 39 +++++---------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/acme/http_challenge.go b/acme/http_challenge.go index 979ec4b3..87359049 100644 --- a/acme/http_challenge.go +++ b/acme/http_challenge.go @@ -10,45 +10,18 @@ import ( type httpChallenge struct { jws *jws optPort string - start chan net.Listener - end chan error } func (s *httpChallenge) Solve(chlng challenge, domain string) error { logf("[INFO] acme: Trying to solve HTTP-01") - s.start = make(chan net.Listener) - s.end = make(chan error) - // Generate the Key Authorization for the challenge keyAuth, err := getKeyAuthorization(chlng.Token, &s.jws.privKey.PublicKey) if err != nil { return err } - go s.startHTTPServer(domain, chlng.Token, keyAuth) - var listener net.Listener - select { - case listener = <-s.start: - break - case err := <-s.end: - return fmt.Errorf("Could not start HTTP server for challenge -> %v", err) - } - - // Make sure we properly close the HTTP server before we return - defer func() { - listener.Close() - err = <-s.end - close(s.start) - close(s.end) - }() - - return validate(s.jws, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) -} - -func (s *httpChallenge) startHTTPServer(domain string, token string, keyAuth string) { - // Allow for CLI port override port := ":80" if s.optPort != "" { @@ -60,13 +33,12 @@ func (s *httpChallenge) startHTTPServer(domain string, token string, keyAuth str // if the domain:port bind failed, fall back to :port bind and try that instead. listener, err = net.Listen("tcp", port) if err != nil { - s.end <- err + return fmt.Errorf("Could not start HTTP server for challenge -> %v", err) } } - // Signal successfull start - s.start <- listener + defer listener.Close() - path := "/.well-known/acme-challenge/" + token + path := "/.well-known/acme-challenge/" + chlng.Token // The handler validates the HOST header and request type. // For validation it then writes the token the server returned with the challenge @@ -81,8 +53,7 @@ func (s *httpChallenge) startHTTPServer(domain string, token string, keyAuth str } }) - http.Serve(listener, nil) + go http.Serve(listener, nil) - // Signal that the server was shut down - s.end <- nil + return validate(s.jws, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth}) }