Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package acmeserver |
| 16 | |
| 17 | import ( |
| 18 | "github.com/caddyserver/caddy/v2" |
| 19 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" |
| 20 | "github.com/caddyserver/caddy/v2/modules/caddypki" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | httpcaddyfile.RegisterDirective("acme_server", parseACMEServer) |
| 25 | } |
| 26 | |
| 27 | // parseACMEServer sets up an ACME server handler from Caddyfile tokens. |
| 28 | // |
| 29 | // acme_server [<matcher>] { |
| 30 | // ca <id> |
| 31 | // lifetime <duration> |
| 32 | // resolvers <addresses...> |
| 33 | // challenges <challenges...> |
| 34 | // allow_wildcard_names |
| 35 | // allow { |
| 36 | // domains <domains...> |
| 37 | // ip_ranges <addresses...> |
| 38 | // } |
| 39 | // deny { |
| 40 | // domains <domains...> |
| 41 | // ip_ranges <addresses...> |
| 42 | // } |
| 43 | // sign_with_root |
| 44 | // } |
| 45 | func parseACMEServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) { |
| 46 | h.Next() // consume directive name |
| 47 | matcherSet, err := h.ExtractMatcherSet() |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | h.Next() // consume the directive name again (matcher parsing resets) |
| 52 | |
| 53 | // no inline args allowed |
| 54 | if h.NextArg() { |
| 55 | return nil, h.ArgErr() |
| 56 | } |
| 57 | |
| 58 | var acmeServer Handler |
| 59 | var ca *caddypki.CA |
| 60 | |
| 61 | for h.NextBlock(0) { |
| 62 | switch h.Val() { |
| 63 | case "ca": |
| 64 | if !h.AllArgs(&acmeServer.CA) { |
| 65 | return nil, h.ArgErr() |
| 66 | } |
| 67 | if ca == nil { |
| 68 | ca = new(caddypki.CA) |
| 69 | } |
| 70 | ca.ID = acmeServer.CA |
| 71 | case "lifetime": |
| 72 | if !h.NextArg() { |
| 73 | return nil, h.ArgErr() |
| 74 | } |
| 75 | dur, err := caddy.ParseDuration(h.Val()) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | acmeServer.Lifetime = caddy.Duration(dur) |
| 80 | case "resolvers": |
| 81 | acmeServer.Resolvers = h.RemainingArgs() |
| 82 | if len(acmeServer.Resolvers) == 0 { |
| 83 | return nil, h.Errf("must specify at least one resolver address") |
| 84 | } |
| 85 | case "challenges": |
| 86 | acmeServer.Challenges = append(acmeServer.Challenges, stringToChallenges(h.RemainingArgs())...) |
| 87 | case "allow_wildcard_names": |
| 88 | if acmeServer.Policy == nil { |
| 89 | acmeServer.Policy = &Policy{} |
| 90 | } |
| 91 | acmeServer.Policy.AllowWildcardNames = true |
| 92 | case "allow": |
| 93 | r := &RuleSet{} |
| 94 | for nesting := h.Nesting(); h.NextBlock(nesting); { |
| 95 | if h.CountRemainingArgs() == 0 { |
| 96 | return nil, h.ArgErr() // TODO: |
| 97 | } |
| 98 | switch h.Val() { |
| 99 | case "domains": |
| 100 | r.Domains = append(r.Domains, h.RemainingArgs()...) |
| 101 | case "ip_ranges": |
| 102 | r.IPRanges = append(r.IPRanges, h.RemainingArgs()...) |
| 103 | default: |
| 104 | return nil, h.Errf("unrecognized 'allow' subdirective: %s", h.Val()) |
| 105 | } |
| 106 | } |
| 107 | if acmeServer.Policy == nil { |
| 108 | acmeServer.Policy = &Policy{} |
| 109 | } |
| 110 | acmeServer.Policy.Allow = r |
| 111 | case "deny": |
| 112 | r := &RuleSet{} |
| 113 | for nesting := h.Nesting(); h.NextBlock(nesting); { |
| 114 | if h.CountRemainingArgs() == 0 { |
| 115 | return nil, h.ArgErr() // TODO: |
| 116 | } |
| 117 | switch h.Val() { |
| 118 | case "domains": |
| 119 | r.Domains = append(r.Domains, h.RemainingArgs()...) |
| 120 | case "ip_ranges": |
| 121 | r.IPRanges = append(r.IPRanges, h.RemainingArgs()...) |
| 122 | default: |
| 123 | return nil, h.Errf("unrecognized 'deny' subdirective: %s", h.Val()) |
| 124 | } |
| 125 | } |
| 126 | if acmeServer.Policy == nil { |
| 127 | acmeServer.Policy = &Policy{} |
| 128 | } |
| 129 | acmeServer.Policy.Deny = r |
| 130 | case "sign_with_root": |
| 131 | if h.NextArg() { |
| 132 | return nil, h.ArgErr() |
| 133 | } |
| 134 | acmeServer.SignWithRoot = true |
| 135 | default: |
| 136 | return nil, h.Errf("unrecognized ACME server directive: %s", h.Val()) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | configVals := h.NewRoute(matcherSet, acmeServer) |
| 141 | |
| 142 | if ca == nil { |
| 143 | return configVals, nil |
| 144 | } |
| 145 | |
| 146 | return append(configVals, httpcaddyfile.ConfigValue{ |
| 147 | Class: "pki.ca", |
| 148 | Value: ca, |
| 149 | }), nil |
| 150 | } |