From b09335a5836496f5585ddc98a9c48be93a77617c Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 23 Nov 2020 15:15:14 -0500 Subject: [PATCH] Caddyfile support --- caddy.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/caddy.go b/caddy.go index dc6c24a..1d25acf 100644 --- a/caddy.go +++ b/caddy.go @@ -1,11 +1,8 @@ package caddy2proxyprotocol -import "github.com/caddyserver/caddy/v2" - -var ( - _ = caddy.Provisioner(&Wrapper{}) - _ = caddy.Module(&Wrapper{}) - _ = caddy.ListenerWrapper(&Wrapper{}) +import ( + "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) func init() { @@ -18,3 +15,47 @@ func (Wrapper) CaddyModule() caddy.ModuleInfo { New: func() caddy.Module { return new(Wrapper) }, } } + +// UnmarshalCaddyfile sets up the listener wrapper from Caddyfile tokens. Syntax: +// +// proxy_protocol { +// timeout +// allow +// } +// +func (w *Wrapper) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + // No same-line options are supported + if d.NextArg() { + return d.ArgErr() + } + + for d.NextBlock(0) { + switch d.Val() { + case "timeout": + if !d.NextArg() { + return d.ArgErr() + } + dur, err := caddy.ParseDuration(d.Val()) + if err != nil { + return d.Errf("parsing proxy_protocol timeout duration: %v", err) + } + w.Timeout = caddy.Duration(dur) + + case "allow": + w.Allow = append(w.Allow, d.RemainingArgs()...) + + default: + return d.ArgErr() + } + } + } + return nil +} + +var ( + _ caddy.Provisioner = (*Wrapper)(nil) + _ caddy.Module = (*Wrapper)(nil) + _ caddy.ListenerWrapper = (*Wrapper)(nil) + _ caddyfile.Unmarshaler = (*Wrapper)(nil) +)