Skip to content

Commit

Permalink
Clock matcher (#225)
Browse files Browse the repository at this point in the history
* Clock matcher: initial commit

* Clock matcher: add time zone support

* Clock matcher: use UTC by default

* Clock matcher: add custom offset support

* Clock matcher: provide comments for fields
  • Loading branch information
vnxme authored Aug 2, 2024
1 parent 32fede4 commit 3524134
Show file tree
Hide file tree
Showing 6 changed files with 509 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This app works similarly to the `http` app. You define servers, and each server

Current matchers:

- **layer4.matchers.clock** - matches connections on the time they are wrapped/matched.
- **layer4.matchers.http** - matches connections that start with HTTP requests. In addition, any [`http.matchers` modules](https://caddyserver.com/docs/modules/) can be used for matching on HTTP-specific properties of requests, such as header or path. Note that only the first request of each connection can be used for matching.
- **layer4.matchers.tls** - matches connections that start with TLS handshakes. In addition, any [`tls.handshake_match` modules](https://caddyserver.com/docs/modules/) can be used for matching on TLS-specific properties of the ClientHello, such as ServerName (SNI).
- **layer4.matchers.ssh** - matches connections that look like SSH connections.
Expand Down
1 change: 1 addition & 0 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package caddyl4
import (
// plugging in the standard modules for the layer4 app
_ "github.com/mholt/caddy-l4/layer4"
_ "github.com/mholt/caddy-l4/modules/l4clock"
_ "github.com/mholt/caddy-l4/modules/l4echo"
_ "github.com/mholt/caddy-l4/modules/l4http"
_ "github.com/mholt/caddy-l4/modules/l4postgres"
Expand Down
220 changes: 220 additions & 0 deletions integration/caddyfile_adapt/gd_matcher_clock.caddytest
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
{
layer4 {
:8080 {
@night_m clock before 05:00:00
@morning clock 05:00:00 12:00:00
@afternoon clock 12:00:00 17:00:00
@evening clock 17:00:00 21:00:00
@night_e clock after 21:00:00
route @night_m @night_e {
proxy 00.upstream.local:8080
}
route @morning {
proxy 01.upstream.local:8080 02.upstream.local:8080
}
route @afternoon {
proxy 03.upstream.local:8080 04.upstream.local:8080 05.upstream.local:8080
}
route @evening {
proxy 06.upstream.local:8080 07.upstream.local:8080
}
}
:8888 {
@la_is_awake clock 08:00:00 20:00:00 America/Los_Angeles
route @la_is_awake {
proxy existing.machine.local:8888
}
@la_is_asleep not clock 08:00:00 20:00:00 America/Los_Angeles
route @la_is_asleep {
proxy non-existing.machine.local:8888
}
}
}
}
----------
{
"apps": {
"layer4": {
"servers": {
"srv0": {
"listen": [
":8080"
],
"routes": [
{
"match": [
{
"clock": {
"after": "00:00:00",
"before": "05:00:00"
}
},
{
"clock": {
"after": "21:00:00",
"before": "00:00:00"
}
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"00.upstream.local:8080"
]
}
]
}
]
},
{
"match": [
{
"clock": {
"after": "05:00:00",
"before": "12:00:00"
}
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"01.upstream.local:8080"
]
},
{
"dial": [
"02.upstream.local:8080"
]
}
]
}
]
},
{
"match": [
{
"clock": {
"after": "12:00:00",
"before": "17:00:00"
}
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"03.upstream.local:8080"
]
},
{
"dial": [
"04.upstream.local:8080"
]
},
{
"dial": [
"05.upstream.local:8080"
]
}
]
}
]
},
{
"match": [
{
"clock": {
"after": "17:00:00",
"before": "21:00:00"
}
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"06.upstream.local:8080"
]
},
{
"dial": [
"07.upstream.local:8080"
]
}
]
}
]
}
]
},
"srv1": {
"listen": [
":8888"
],
"routes": [
{
"match": [
{
"clock": {
"after": "08:00:00",
"before": "20:00:00",
"timezone": "America/Los_Angeles"
}
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"existing.machine.local:8888"
]
}
]
}
]
},
{
"match": [
{
"not": [
{
"clock": {
"after": "08:00:00",
"before": "20:00:00",
"timezone": "America/Los_Angeles"
}
}
]
}
],
"handle": [
{
"handler": "proxy",
"upstreams": [
{
"dial": [
"non-existing.machine.local:8888"
]
}
]
}
]
}
]
}
}
}
}
}
2 changes: 2 additions & 0 deletions layer4/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"net"
"sync"
"time"

"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"
Expand All @@ -33,6 +34,7 @@ func WrapConnection(underlying net.Conn, buf []byte, logger *zap.Logger) *Connec
repl := caddy.NewReplacer()
repl.Set("l4.conn.remote_addr", underlying.RemoteAddr())
repl.Set("l4.conn.local_addr", underlying.LocalAddr())
repl.Set("l4.conn.wrap_time", time.Now().UTC())

ctx := context.Background()
ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
Expand Down
Loading

0 comments on commit 3524134

Please sign in to comment.