123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- package acme
- import (
- "bytes"
- "context"
- "crypto"
- "crypto/rand"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "math/big"
- "net/http"
- "strconv"
- "strings"
- "time"
- )
- type retryTimer struct {
-
-
- backoffFn func(n int, r *http.Request, res *http.Response) time.Duration
-
- n int
- }
- func (t *retryTimer) inc() {
- t.n++
- }
- func (t *retryTimer) backoff(ctx context.Context, r *http.Request, res *http.Response) error {
- d := t.backoffFn(t.n, r, res)
- if d <= 0 {
- return fmt.Errorf("acme: no more retries for %s; tried %d time(s)", r.URL, t.n)
- }
- wakeup := time.NewTimer(d)
- defer wakeup.Stop()
- select {
- case <-ctx.Done():
- return ctx.Err()
- case <-wakeup.C:
- return nil
- }
- }
- func (c *Client) retryTimer() *retryTimer {
- f := c.RetryBackoff
- if f == nil {
- f = defaultBackoff
- }
- return &retryTimer{backoffFn: f}
- }
- func defaultBackoff(n int, r *http.Request, res *http.Response) time.Duration {
- const max = 10 * time.Second
- var jitter time.Duration
- if x, err := rand.Int(rand.Reader, big.NewInt(1000)); err == nil {
-
-
-
-
- jitter = (1 + time.Duration(x.Int64())) * time.Millisecond
- }
- if v, ok := res.Header["Retry-After"]; ok {
- return retryAfter(v[0]) + jitter
- }
- if n < 1 {
- n = 1
- }
- if n > 30 {
- n = 30
- }
- d := time.Duration(1<<uint(n-1))*time.Second + jitter
- if d > max {
- return max
- }
- return d
- }
- func retryAfter(v string) time.Duration {
- if i, err := strconv.Atoi(v); err == nil {
- return time.Duration(i) * time.Second
- }
- t, err := http.ParseTime(v)
- if err != nil {
- return 0
- }
- return t.Sub(timeNow())
- }
- type resOkay func(*http.Response) bool
- func wantStatus(codes ...int) resOkay {
- return func(res *http.Response) bool {
- for _, code := range codes {
- if code == res.StatusCode {
- return true
- }
- }
- return false
- }
- }
- func (c *Client) get(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
- retry := c.retryTimer()
- for {
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return nil, err
- }
- res, err := c.doNoRetry(ctx, req)
- switch {
- case err != nil:
- return nil, err
- case ok(res):
- return res, nil
- case isRetriable(res.StatusCode):
- retry.inc()
- resErr := responseError(res)
- res.Body.Close()
-
-
- if retry.backoff(ctx, req, res) != nil {
- return nil, resErr
- }
- default:
- defer res.Body.Close()
- return nil, responseError(res)
- }
- }
- }
- func (c *Client) postAsGet(ctx context.Context, url string, ok resOkay) (*http.Response, error) {
- return c.post(ctx, nil, url, noPayload, ok)
- }
- func (c *Client) post(ctx context.Context, key crypto.Signer, url string, body interface{}, ok resOkay) (*http.Response, error) {
- retry := c.retryTimer()
- for {
- res, req, err := c.postNoRetry(ctx, key, url, body)
- if err != nil {
- return nil, err
- }
- if ok(res) {
- return res, nil
- }
- resErr := responseError(res)
- res.Body.Close()
- switch {
-
-
- case isBadNonce(resErr):
-
- c.clearNonces()
- case !isRetriable(res.StatusCode):
- return nil, resErr
- }
- retry.inc()
-
-
- if err := retry.backoff(ctx, req, res); err != nil {
- return nil, resErr
- }
- }
- }
- func (c *Client) postNoRetry(ctx context.Context, key crypto.Signer, url string, body interface{}) (*http.Response, *http.Request, error) {
- kid := noKeyID
- if key == nil {
- key = c.Key
- kid = c.accountKID(ctx)
- }
- nonce, err := c.popNonce(ctx, url)
- if err != nil {
- return nil, nil, err
- }
- b, err := jwsEncodeJSON(body, key, kid, nonce, url)
- if err != nil {
- return nil, nil, err
- }
- req, err := http.NewRequest("POST", url, bytes.NewReader(b))
- if err != nil {
- return nil, nil, err
- }
- req.Header.Set("Content-Type", "application/jose+json")
- res, err := c.doNoRetry(ctx, req)
- if err != nil {
- return nil, nil, err
- }
- c.addNonce(res.Header)
- return res, req, nil
- }
- func (c *Client) doNoRetry(ctx context.Context, req *http.Request) (*http.Response, error) {
- req.Header.Set("User-Agent", c.userAgent())
- res, err := c.httpClient().Do(req.WithContext(ctx))
- if err != nil {
- select {
- case <-ctx.Done():
-
-
-
-
-
- return nil, ctx.Err()
- default:
- return nil, err
- }
- }
- return res, nil
- }
- func (c *Client) httpClient() *http.Client {
- if c.HTTPClient != nil {
- return c.HTTPClient
- }
- return http.DefaultClient
- }
- var packageVersion string
- func (c *Client) userAgent() string {
- ua := "golang.org/x/crypto/acme"
- if packageVersion != "" {
- ua += "@" + packageVersion
- }
- if c.UserAgent != "" {
- ua = c.UserAgent + " " + ua
- }
- return ua
- }
- func isBadNonce(err error) bool {
-
-
-
-
- ae, ok := err.(*Error)
- return ok && strings.HasSuffix(strings.ToLower(ae.ProblemType), ":badnonce")
- }
- func isRetriable(code int) bool {
- return code <= 399 || code >= 500 || code == http.StatusTooManyRequests
- }
- func responseError(resp *http.Response) error {
-
-
- b, _ := ioutil.ReadAll(resp.Body)
- e := &wireError{Status: resp.StatusCode}
- if err := json.Unmarshal(b, e); err != nil {
-
-
-
- e.Detail = string(b)
- if e.Detail == "" {
- e.Detail = resp.Status
- }
- }
- return e.error(resp.Header)
- }
|