Skip to content

Commit

Permalink
Merge pull request #4 from francislavoie/caddyfile-support
Browse files Browse the repository at this point in the history
Caddyfile support
  • Loading branch information
mastercactapus authored Jan 29, 2021
2 parents 3a92c81 + b09335a commit 8cd1772
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions caddy.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 <duration>
// allow <IPs...>
// }
//
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)
)

0 comments on commit 8cd1772

Please sign in to comment.