aboutsummaryrefslogtreecommitdiff
path: root/challange_http.go
blob: ee1708a9a962c43a553f40b4bc17da1dfd0a06a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
}