2019-08-09 21:05:47 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package httpcaddyfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"net/http"
|
2019-08-21 19:46:35 +03:00
|
|
|
"reflect"
|
2019-08-09 21:05:47 +03:00
|
|
|
|
2019-12-10 23:36:46 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-08-22 21:26:48 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-08-09 21:05:47 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
|
|
|
)
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
func init() {
|
|
|
|
RegisterDirective("bind", parseBind)
|
|
|
|
RegisterDirective("root", parseRoot)
|
|
|
|
RegisterDirective("tls", parseTLS)
|
|
|
|
RegisterHandlerDirective("redir", parseRedir)
|
2019-09-16 20:04:18 +03:00
|
|
|
RegisterHandlerDirective("respond", parseRespond)
|
2020-01-10 00:00:32 +03:00
|
|
|
RegisterHandlerDirective("route", parseRoute)
|
2020-01-17 03:08:52 +03:00
|
|
|
RegisterHandlerDirective("handle", parseHandle)
|
2019-08-21 19:46:35 +03:00
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
func parseBind(h Helper) ([]ConfigValue, error) {
|
|
|
|
var lnHosts []string
|
|
|
|
for h.Next() {
|
|
|
|
lnHosts = append(lnHosts, h.RemainingArgs()...)
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
return h.NewBindAddresses(lnHosts), nil
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
func parseRoot(h Helper) ([]ConfigValue, error) {
|
|
|
|
if !h.Next() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
matcherSet, ok, err := h.MatcherToken()
|
2019-08-09 21:05:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
if !ok {
|
|
|
|
// no matcher token; oops
|
|
|
|
h.Dispenser.Prev()
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
if !h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
root := h.Val()
|
|
|
|
if h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
varsHandler := caddyhttp.VarsMiddleware{"root": root}
|
|
|
|
route := caddyhttp.Route{
|
|
|
|
HandlersRaw: []json.RawMessage{
|
|
|
|
caddyconfig.JSONModuleObject(varsHandler, "handler", "vars", nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if matcherSet != nil {
|
2019-12-10 23:36:46 +03:00
|
|
|
route.MatcherSetsRaw = []caddy.ModuleMap{matcherSet}
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2020-01-17 03:08:52 +03:00
|
|
|
return []ConfigValue{{Class: "route", Value: route}}, nil
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
func parseTLS(h Helper) ([]ConfigValue, error) {
|
|
|
|
var configVals []ConfigValue
|
|
|
|
|
2019-12-14 02:32:27 +03:00
|
|
|
var cp *caddytls.ConnectionPolicy
|
2019-08-09 21:05:47 +03:00
|
|
|
var fileLoader caddytls.FileLoader
|
|
|
|
var folderLoader caddytls.FolderLoader
|
2019-08-21 19:46:35 +03:00
|
|
|
var mgr caddytls.ACMEManagerMaker
|
|
|
|
var off bool
|
|
|
|
|
2019-09-30 18:11:30 +03:00
|
|
|
// fill in global defaults, if configured
|
|
|
|
if email := h.Option("email"); email != nil {
|
|
|
|
mgr.Email = email.(string)
|
|
|
|
}
|
|
|
|
if acmeCA := h.Option("acme_ca"); acmeCA != nil {
|
|
|
|
mgr.CA = acmeCA.(string)
|
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
for h.Next() {
|
|
|
|
// file certificate loader
|
|
|
|
firstLine := h.RemainingArgs()
|
|
|
|
switch len(firstLine) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if firstLine[0] == "off" {
|
|
|
|
off = true
|
|
|
|
} else {
|
|
|
|
mgr.Email = firstLine[0]
|
|
|
|
}
|
|
|
|
case 2:
|
2019-08-09 21:05:47 +03:00
|
|
|
fileLoader = append(fileLoader, caddytls.CertKeyFilePair{
|
|
|
|
Certificate: firstLine[0],
|
|
|
|
Key: firstLine[1],
|
2019-08-21 19:46:35 +03:00
|
|
|
// TODO: add tags, for enterprise module's certificate selection
|
2019-08-09 21:05:47 +03:00
|
|
|
})
|
2019-08-21 19:46:35 +03:00
|
|
|
default:
|
|
|
|
return nil, h.ArgErr()
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
var hasBlock bool
|
2019-09-11 04:21:52 +03:00
|
|
|
for h.NextBlock(0) {
|
2019-08-21 19:46:35 +03:00
|
|
|
hasBlock = true
|
2019-08-09 21:05:47 +03:00
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
switch h.Val() {
|
|
|
|
// connection policy
|
2019-08-09 21:05:47 +03:00
|
|
|
case "protocols":
|
2019-08-21 19:46:35 +03:00
|
|
|
args := h.RemainingArgs()
|
2019-08-09 21:05:47 +03:00
|
|
|
if len(args) == 0 {
|
2019-08-21 19:46:35 +03:00
|
|
|
return nil, h.SyntaxErr("one or two protocols")
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
if _, ok := caddytls.SupportedProtocols[args[0]]; !ok {
|
2019-08-21 19:46:35 +03:00
|
|
|
return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[0])
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-12-14 02:32:27 +03:00
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
cp.ProtocolMin = args[0]
|
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
|
|
|
if _, ok := caddytls.SupportedProtocols[args[1]]; !ok {
|
2019-08-21 19:46:35 +03:00
|
|
|
return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[1])
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-12-14 02:32:27 +03:00
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
cp.ProtocolMax = args[1]
|
|
|
|
}
|
|
|
|
case "ciphers":
|
2019-08-21 19:46:35 +03:00
|
|
|
for h.NextArg() {
|
|
|
|
if _, ok := caddytls.SupportedCipherSuites[h.Val()]; !ok {
|
|
|
|
return nil, h.Errf("Wrong cipher suite name or cipher suite not supported: '%s'", h.Val())
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-12-14 02:32:27 +03:00
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
cp.CipherSuites = append(cp.CipherSuites, h.Val())
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
case "curves":
|
2019-08-21 19:46:35 +03:00
|
|
|
for h.NextArg() {
|
|
|
|
if _, ok := caddytls.SupportedCurves[h.Val()]; !ok {
|
|
|
|
return nil, h.Errf("Wrong curve name or curve not supported: '%s'", h.Val())
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-12-14 02:32:27 +03:00
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
cp.Curves = append(cp.Curves, h.Val())
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
case "alpn":
|
2019-08-21 19:46:35 +03:00
|
|
|
args := h.RemainingArgs()
|
2019-08-09 21:05:47 +03:00
|
|
|
if len(args) == 0 {
|
2019-08-21 19:46:35 +03:00
|
|
|
return nil, h.ArgErr()
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-12-14 02:32:27 +03:00
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-09 21:05:47 +03:00
|
|
|
cp.ALPN = args
|
2019-08-21 19:46:35 +03:00
|
|
|
|
|
|
|
// certificate folder loader
|
|
|
|
case "load":
|
|
|
|
folderLoader = append(folderLoader, h.RemainingArgs()...)
|
|
|
|
|
|
|
|
// automation policy
|
|
|
|
case "ca":
|
|
|
|
arg := h.RemainingArgs()
|
|
|
|
if len(arg) != 1 {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
mgr.CA = arg[0]
|
|
|
|
|
2019-09-30 18:11:30 +03:00
|
|
|
default:
|
|
|
|
return nil, h.Errf("unknown subdirective: %s", h.Val())
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
|
|
|
|
// a naked tls directive is not allowed
|
|
|
|
if len(firstLine) == 0 && !hasBlock {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// certificate loaders
|
|
|
|
if len(fileLoader) > 0 {
|
|
|
|
configVals = append(configVals, ConfigValue{
|
|
|
|
Class: "tls.certificate_loader",
|
|
|
|
Value: fileLoader,
|
|
|
|
})
|
2019-12-14 02:32:27 +03:00
|
|
|
// ensure server uses HTTPS by setting non-nil conn policy
|
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
2019-08-21 19:46:35 +03:00
|
|
|
}
|
|
|
|
if len(folderLoader) > 0 {
|
|
|
|
configVals = append(configVals, ConfigValue{
|
|
|
|
Class: "tls.certificate_loader",
|
|
|
|
Value: folderLoader,
|
|
|
|
})
|
2019-12-14 02:32:27 +03:00
|
|
|
// ensure server uses HTTPS by setting non-nil conn policy
|
|
|
|
if cp == nil {
|
|
|
|
cp = new(caddytls.ConnectionPolicy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// connection policy
|
|
|
|
if cp != nil {
|
|
|
|
configVals = append(configVals, ConfigValue{
|
|
|
|
Class: "tls.connection_policy",
|
|
|
|
Value: cp,
|
|
|
|
})
|
2019-08-21 19:46:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// automation policy
|
|
|
|
if off {
|
|
|
|
configVals = append(configVals, ConfigValue{
|
|
|
|
Class: "tls.off",
|
|
|
|
Value: true,
|
|
|
|
})
|
|
|
|
} else if !reflect.DeepEqual(mgr, caddytls.ACMEManagerMaker{}) {
|
|
|
|
configVals = append(configVals, ConfigValue{
|
|
|
|
Class: "tls.automation_manager",
|
|
|
|
Value: mgr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return configVals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
|
|
if !h.Next() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
to := h.Val()
|
|
|
|
|
|
|
|
var code string
|
|
|
|
if h.NextArg() {
|
|
|
|
code = h.Val()
|
|
|
|
}
|
|
|
|
if code == "permanent" {
|
|
|
|
code = "301"
|
|
|
|
}
|
|
|
|
if code == "temporary" || code == "" {
|
|
|
|
code = "307"
|
|
|
|
}
|
|
|
|
var body string
|
|
|
|
if code == "meta" {
|
|
|
|
// Script tag comes first since that will better imitate a redirect in the browser's
|
|
|
|
// history, but the meta tag is a fallback for most non-JS clients.
|
|
|
|
const metaRedir = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Redirecting...</title>
|
|
|
|
<script>window.location.replace("%s");</script>
|
|
|
|
<meta http-equiv="refresh" content="0; URL='%s'">
|
|
|
|
</head>
|
|
|
|
<body>Redirecting to <a href="%s">%s</a>...</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
safeTo := html.EscapeString(to)
|
|
|
|
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
return caddyhttp.StaticResponse{
|
|
|
|
StatusCode: caddyhttp.WeakString(code),
|
|
|
|
Headers: http.Header{"Location": []string{to}},
|
|
|
|
Body: body,
|
|
|
|
}, nil
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
2019-09-16 20:04:18 +03:00
|
|
|
|
|
|
|
func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
|
|
sr := new(caddyhttp.StaticResponse)
|
|
|
|
err := sr.UnmarshalCaddyfile(h.Dispenser)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sr, nil
|
|
|
|
}
|
2020-01-10 00:00:32 +03:00
|
|
|
|
|
|
|
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
|
|
sr := new(caddyhttp.Subroute)
|
|
|
|
|
|
|
|
for h.Next() {
|
|
|
|
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
|
|
|
dir := h.Val()
|
|
|
|
|
|
|
|
dirFunc, ok := registeredDirectives[dir]
|
|
|
|
if !ok {
|
|
|
|
return nil, h.Errf("unrecognized directive: %s", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
subHelper := h
|
|
|
|
subHelper.Dispenser = h.NewFromNextTokens()
|
|
|
|
|
|
|
|
results, err := dirFunc(subHelper)
|
|
|
|
if err != nil {
|
|
|
|
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
|
|
|
handler, ok := result.Value.(caddyhttp.Route)
|
|
|
|
if !ok {
|
|
|
|
return nil, h.Errf("%s directive returned something other than an HTTP route: %#v (only handler directives can be used in routes)", dir, result.Value)
|
|
|
|
}
|
|
|
|
sr.Routes = append(sr.Routes, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr, nil
|
|
|
|
}
|
2020-01-17 03:08:52 +03:00
|
|
|
|
|
|
|
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
|
|
var allResults []ConfigValue
|
|
|
|
|
|
|
|
for h.Next() {
|
|
|
|
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
|
|
|
dir := h.Val()
|
|
|
|
|
|
|
|
dirFunc, ok := registeredDirectives[dir]
|
|
|
|
if !ok {
|
|
|
|
return nil, h.Errf("unrecognized directive: %s", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
subHelper := h
|
|
|
|
subHelper.Dispenser = h.NewFromNextTokens()
|
|
|
|
|
|
|
|
results, err := dirFunc(subHelper)
|
|
|
|
if err != nil {
|
|
|
|
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
|
|
|
result.directive = dir
|
|
|
|
allResults = append(allResults, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSubroute(allResults, h.groupCounter)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|