From 5fd587332fcc304fa21da782c62409b860f269e6 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 23 Dec 2015 14:44:22 +0100 Subject: One shot http server --- challange_http.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'challange_http.go') diff --git a/challange_http.go b/challange_http.go index 2d10da4..30233bb 100644 --- a/challange_http.go +++ b/challange_http.go @@ -1,7 +1,43 @@ package acme +import ( + "io" + "net" + "net/http" +) + const wellKnown = `/.well-known/acme-challenge/` func init() { registerChallenge(ChallengeHTTP) } + +type httpChallenge struct { + Challenge + Addr string +} + +func (c *httpChallenge) ServeHTTP(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, c.KeyAuthorization) +} + +func (c *httpChallenge) Solve() error { + done := make(chan bool) + l, err := net.Listen("tcp", c.Addr) + if err != nil { + return err + } + defer l.Close() + s := &http.Server{ + Handler: c, + ConnState: func(_ net.Conn, st http.ConnState) { + if st == http.StateClosed { + done <- true + } + }, + } + s.SetKeepAlivesEnabled(false) + go s.Serve(l) + <-done + return nil +} -- cgit v1.2.3