aboutsummaryrefslogtreecommitdiff
path: root/cmd/acme/solve_http.go
blob: 8e5cfaf55ae70ccd869b2034bbe02452ac2806e5 (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
package main

import (
	"io"
	"net/http"
	"path"

	"dim13.org/acme"
)

type httpChallenge struct {
	http.Server
	Webroot string
	started bool
}

func (c *httpChallenge) Solve(token, keyAuth string) error {
	if c.Webroot != "" {
		p := path.Join(c.Webroot, acme.WellKnown, token)
		fd, err := acme.CreatePubFile(p)
		if err != nil {
			return err
		}
		defer fd.Close()
		_, err = io.WriteString(fd, keyAuth)
		return err
	} else {
		if !c.started {
			go c.ListenAndServe()
			c.started = true
		}
		p := path.Join(acme.WellKnown, token)
		http.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
			io.WriteString(w, keyAuth)
		})
		return nil
	}
}