Fix a race for socket in HTTP-01

This commit is contained in:
xenolf 2016-01-11 22:04:04 +01:00
parent 4a71f77815
commit 33216d7563

View file

@ -12,12 +12,15 @@ type httpChallenge struct {
validate validateFunc validate validateFunc
iface string iface string
port string port string
done chan bool
} }
func (s *httpChallenge) Solve(chlng challenge, domain string) error { func (s *httpChallenge) Solve(chlng challenge, domain string) error {
logf("[INFO][%s] acme: Trying to solve HTTP-01", domain) logf("[INFO][%s] acme: Trying to solve HTTP-01", domain)
s.done = make(chan bool)
// Generate the Key Authorization for the challenge // Generate the Key Authorization for the challenge
keyAuth, err := getKeyAuthorization(chlng.Token, &s.jws.privKey.PublicKey) keyAuth, err := getKeyAuthorization(chlng.Token, &s.jws.privKey.PublicKey)
if err != nil { if err != nil {
@ -39,10 +42,19 @@ func (s *httpChallenge) Solve(chlng challenge, domain string) error {
if err != nil { if err != nil {
return fmt.Errorf("Could not start HTTP server for challenge -> %v", err) return fmt.Errorf("Could not start HTTP server for challenge -> %v", err)
} }
defer listener.Close()
path := "/.well-known/acme-challenge/" + chlng.Token path := "/.well-known/acme-challenge/" + chlng.Token
go s.serve(listener, path, keyAuth, domain)
err = s.validate(s.jws, domain, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
listener.Close()
<-s.done
return err
}
func (s *httpChallenge) serve(listener net.Listener, path, keyAuth, domain string) {
// The handler validates the HOST header and request type. // The handler validates the HOST header and request type.
// For validation it then writes the token the server returned with the challenge // For validation it then writes the token the server returned with the challenge
mux := http.NewServeMux() mux := http.NewServeMux()
@ -57,7 +69,6 @@ func (s *httpChallenge) Solve(chlng challenge, domain string) error {
} }
}) })
go http.Serve(listener, mux) http.Serve(listener, mux)
s.done <- true
return s.validate(s.jws, domain, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
} }