@marcel91200 / caddy Mirror · read-only / commits / 2de0acc1
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

Initial implementation of global default SNI option (#3047)

* add global default sni * fixed grammar * httpcaddyfile: Reduce some duplicated code * Um, re-commit already-committed commit, I guess? (sigh) Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Mark Sargent committed Feb 27, 2020 at 12:01 UTC 2de0acc11fcaaed9e4b3561d9a2d1457f015e9e3
3 files changed +24 -20
caddyconfig/httpcaddyfile/httptype.go
+16 -5
@@ -169,9 +169,10 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
169
170 // now that each server is configured, make the HTTP app
171 httpApp := caddyhttp.App{
172 - HTTPPort: tryInt(options["http_port"], &warnings),
173 - HTTPSPort: tryInt(options["https_port"], &warnings),
174 - Servers: servers,
172 + HTTPPort: tryInt(options["http_port"], &warnings),
173 + HTTPSPort: tryInt(options["https_port"], &warnings),
174 + DefaultSNI: tryString(options["default_sni"], &warnings),
175 + Servers: servers,
176 }
177
178 // now for the TLS app! (TODO: refactor into own func)
@@ -364,6 +365,8 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
365 val, err = parseOptHTTPPort(disp)
366 case "https_port":
367 val, err = parseOptHTTPSPort(disp)
368 + case "default_sni":
369 + val, err = parseOptSingleString(disp)
370 case "order":
371 val, err = parseOptOrder(disp)
372 case "experimental_http3":
@@ -371,9 +374,9 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
374 case "storage":
375 val, err = parseOptStorage(disp)
376 case "acme_ca", "acme_dns", "acme_ca_root":
374 - val, err = parseOptACME(disp)
377 + val, err = parseOptSingleString(disp)
378 case "email":
376 - val, err = parseOptEmail(disp)
379 + val, err = parseOptSingleString(disp)
380 case "admin":
381 val, err = parseOptAdmin(disp)
382 case "debug":
@@ -951,6 +954,14 @@ func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
954 return intVal
955 }
956
957 +func tryString(val interface{}, warnings *[]caddyconfig.Warning) string {
958 + stringVal, ok := val.(string)
959 + if val != nil && !ok && warnings != nil {
960 + *warnings = append(*warnings, caddyconfig.Warning{Message: "not a string type"})
961 + }
962 + return stringVal
963 +}
964 +
965 // sliceContains returns true if needle is in haystack.
966 func sliceContains(haystack []string, needle string) bool {
967 for _, s := range haystack {
caddyconfig/httpcaddyfile/options.go
+1 -15
@@ -162,19 +162,7 @@ func parseOptStorage(d *caddyfile.Dispenser) (caddy.StorageConverter, error) {
162 return storage, nil
163 }
164
165 -func parseOptACME(d *caddyfile.Dispenser) (string, error) {
166 - d.Next() // consume parameter name
167 - if !d.Next() {
168 - return "", d.ArgErr()
169 - }
170 - val := d.Val()
171 - if d.Next() {
172 - return "", d.ArgErr()
173 - }
174 - return val, nil
175 -}
176 -
177 -func parseOptEmail(d *caddyfile.Dispenser) (string, error) {
165 +func parseOptSingleString(d *caddyfile.Dispenser) (string, error) {
166 d.Next() // consume parameter name
167 if !d.Next() {
168 return "", d.ArgErr()
@@ -190,11 +178,9 @@ func parseOptAdmin(d *caddyfile.Dispenser) (string, error) {
178 if d.Next() {
179 var listenAddress string
180 d.AllArgs(&listenAddress)
193 -
181 if listenAddress == "" {
182 listenAddress = caddy.DefaultAdminListen
183 }
197 -
184 return listenAddress, nil
185 }
186 return "", nil
modules/caddyhttp/caddyhttp.go
+7
@@ -29,6 +29,7 @@ import (
29
30 "github.com/caddyserver/caddy/v2"
31 "github.com/lucas-clemente/quic-go/http3"
32 + "github.com/mholt/certmagic"
33 "go.uber.org/zap"
34 )
35
@@ -111,6 +112,10 @@ type App struct {
112 // affect functionality.
113 Servers map[string]*Server `json:"servers,omitempty"`
114
115 + // DefaultSNI if set configures all certificate lookups to fallback to use
116 + // this SNI name if a more specific certificate could not be found
117 + DefaultSNI string `json:"default_sni,omitempty"`
118 +
119 servers []*http.Server
120 h3servers []*http3.Server
121 h3listeners []net.PacketConn
@@ -134,6 +139,8 @@ func (app *App) Provision(ctx caddy.Context) error {
139
140 repl := caddy.NewReplacer()
141
142 + certmagic.Default.DefaultServerName = app.DefaultSNI
143 +
144 // this provisions the matchers for each route,
145 // and prepares auto HTTP->HTTP redirects, and
146 // is required before we provision each server