diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-08 22:14:22 +0100 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2016-01-08 22:14:22 +0100 |
commit | ce9bda74942fb130c5f85839aa93a90abe971132 (patch) | |
tree | 29dd45c8d68d6066d8fff7ff8c7e4abcc6f1b1e5 | |
parent | c97a3228822415b7955153bcca155845dc984815 (diff) |
Move solver into desire
-rw-r--r-- | challenge.go | 8 | ||||
-rw-r--r-- | cmd/acme/main.go | 15 | ||||
-rw-r--r-- | cmd/acme/solve_http.go | 30 | ||||
-rw-r--r-- | cmd/acme/solve_webroot.go | 22 | ||||
-rw-r--r-- | desire.go | 19 |
5 files changed, 55 insertions, 39 deletions
diff --git a/challenge.go b/challenge.go index dd6d875..3b008a3 100644 --- a/challenge.go +++ b/challenge.go @@ -1,13 +1,5 @@ package acme -// Solver decribes a solving interface -type Solver interface { - Solve(token, keyAuth string) error - Finish() error -} - -const WellKnown = `/.well-known/acme-challenge/` - var canSolve = make(map[ChallengeType]Solver) func RegisterSolver(c ChallengeType, s Solver) { diff --git a/cmd/acme/main.go b/cmd/acme/main.go index 4534542..1dbc496 100644 --- a/cmd/acme/main.go +++ b/cmd/acme/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "log" + "net/http" "sync" "dim13.org/acme" @@ -80,14 +81,14 @@ func main() { } } - if des.Webroot != "" { - ws := &webrootChallenge{Webroot: des.Webroot} - acme.RegisterSolver(acme.ChallengeHTTP, ws) - } else { - // TODO des.Register - hs := &httpChallenge{Addr: conf.Defaults.Listen} - acme.RegisterSolver(acme.ChallengeHTTP, hs) + // TODO des.Register + hs := &httpChallenge{ + Server: http.Server{ + Addr: conf.Defaults.Listen, + }, + Webroot: des.Webroot, } + acme.RegisterSolver(acme.ChallengeHTTP, hs) wg.Add(1) go func(k string, des *desire) { diff --git a/cmd/acme/solve_http.go b/cmd/acme/solve_http.go index 7d0572b..31f3974 100644 --- a/cmd/acme/solve_http.go +++ b/cmd/acme/solve_http.go @@ -3,18 +3,46 @@ package main import ( "log" "net/http" + "os" + "path" + + "dim13.org/acme" ) type httpChallenge struct { http.Server - Addr string + Webroot string + file string } func (c *httpChallenge) Solve(token, keyAuth string) error { log.Println("solver", c) + if c.Webroot != "" { + return c.solveWebroot(token, keyAuth) + } return nil } func (c *httpChallenge) Finish() error { + if c.Webroot != "" { + return c.finishWebroot() + } return nil } + +func (c *httpChallenge) solveWebroot(token, keyAuth string) error { + log.Println("Solve WebRoot") + c.file = path.Join(c.Webroot, acme.WellKnown, token) + fd, err := acme.CreatePubFile(c.file) + if err != nil { + return err + } + defer fd.Close() + _, err = fd.Write([]byte(keyAuth)) + return err +} + +func (c *httpChallenge) finishWebroot() error { + log.Println("Cleanup WebRoot") + return os.Remove(c.file) +} diff --git a/cmd/acme/solve_webroot.go b/cmd/acme/solve_webroot.go index 13b50e0..756babb 100644 --- a/cmd/acme/solve_webroot.go +++ b/cmd/acme/solve_webroot.go @@ -1,28 +1,6 @@ package main -import ( - "os" - "path" - - "dim13.org/acme" -) - type webrootChallenge struct { Webroot string file string } - -func (c *webrootChallenge) Solve(token, keyAuth string) error { - c.file = path.Join(c.Webroot, acme.WellKnown, token) - fd, err := acme.CreatePubFile(c.file) - if err != nil { - return err - } - defer fd.Close() - _, err = fd.Write([]byte(keyAuth)) - return err -} - -func (c *webrootChallenge) Finish() error { - return os.Remove(c.file) -} @@ -12,14 +12,31 @@ type Desire struct { altnames []string key *rsa.PrivateKey cert []*x509.Certificate + solver map[ChallengeType]Solver } +// Solver decribes a solving interface +type Solver interface { + Solve(token, keyAuth string) error + Finish() error +} + +const WellKnown = `/.well-known/acme-challenge/` + func NewDesire(altnames []string, size int) (*Desire, error) { key, err := rsa.GenerateKey(rand.Reader, size) if err != nil { return nil, err } - return &Desire{key: key, altnames: altnames}, nil + return &Desire{ + key: key, + altnames: altnames, + solver: make(map[ChallengeType]Solver), + }, nil +} + +func (d *Desire) RegisterSolver(c ChallengeType, s Solver) { + d.solver[c] = s } func (d *Desire) SaveKey(fname string) error { |