From d087a3d64f2a60357a2fdfb486de4acbb2d32766 Mon Sep 17 00:00:00 2001 From: vnxme <46669194+vnxme@users.noreply.github.com> Date: Sun, 21 Jul 2024 00:04:55 +0300 Subject: [PATCH] Rename ip handler into remote_ip (#218) --- README.md | 4 ++-- layer4/matchers.go | 30 +++++++++++++++--------------- layer4/matchers_test.go | 14 +++++++------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 150e164..d7b2495 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Current matchers: - **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. - **layer4.matchers.postgres** - matches connections that look like Postgres connections. -- **layer4.matchers.ip** - matches connections based on remote IP (or CIDR range). +- **layer4.matchers.remote_ip** - matches connections based on remote IP (or CIDR range). - **layer4.matchers.local_ip** - matches connections based on local IP (or CIDR range). - **layer4.matchers.proxy_protocol** - matches connections that start with [HAPROXY proxy protocol](https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt). - **layer4.matchers.socks4** - matches connections that look like [SOCKSv4](https://www.openssh.com/txt/socks4.protocol). @@ -367,7 +367,7 @@ While only allowing connections from a specific network and requiring a username "match": [ { "socks5": {}, - "ip": {"ranges": ["10.0.0.0/24"]} + "remote_ip": {"ranges": ["10.0.0.0/24"]} } ], "handle": [ diff --git a/layer4/matchers.go b/layer4/matchers.go index 14b6006..6c049e8 100644 --- a/layer4/matchers.go +++ b/layer4/matchers.go @@ -26,7 +26,7 @@ import ( ) func init() { - caddy.RegisterModule(MatchIP{}) + caddy.RegisterModule(MatchRemoteIP{}) caddy.RegisterModule(MatchLocalIP{}) caddy.RegisterModule(MatchNot{}) } @@ -109,22 +109,22 @@ func (mss *MatcherSets) FromInterface(matcherSets interface{}) error { return nil } -// MatchIP matches requests by remote IP (or CIDR range). -type MatchIP struct { +// MatchRemoteIP matches requests by remote IP (or CIDR range). +type MatchRemoteIP struct { Ranges []string `json:"ranges,omitempty"` cidrs []netip.Prefix } // CaddyModule returns the Caddy module information. -func (MatchIP) CaddyModule() caddy.ModuleInfo { +func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo { return caddy.ModuleInfo{ - ID: "layer4.matchers.ip", - New: func() caddy.Module { return new(MatchIP) }, + ID: "layer4.matchers.remote_ip", + New: func() caddy.Module { return new(MatchRemoteIP) }, } } // Provision parses m's IP ranges, either from IP or CIDR expressions. -func (m *MatchIP) Provision(_ caddy.Context) (err error) { +func (m *MatchRemoteIP) Provision(_ caddy.Context) (err error) { m.cidrs, err = ParseNetworks(m.Ranges) if err != nil { return err @@ -133,10 +133,10 @@ func (m *MatchIP) Provision(_ caddy.Context) (err error) { } // Match returns true if the connection is from one of the designated IP ranges. -func (m MatchIP) Match(cx *Connection) (bool, error) { - clientIP, err := m.getClientIP(cx) +func (m MatchRemoteIP) Match(cx *Connection) (bool, error) { + clientIP, err := m.getRemoteIP(cx) if err != nil { - return false, fmt.Errorf("getting client IP: %v", err) + return false, fmt.Errorf("getting remote IP: %v", err) } for _, ipRange := range m.cidrs { if ipRange.Contains(clientIP) { @@ -146,7 +146,7 @@ func (m MatchIP) Match(cx *Connection) (bool, error) { return false, nil } -func (m MatchIP) getClientIP(cx *Connection) (netip.Addr, error) { +func (m MatchRemoteIP) getRemoteIP(cx *Connection) (netip.Addr, error) { remote := cx.Conn.RemoteAddr().String() ipStr, _, err := net.SplitHostPort(remote) @@ -156,7 +156,7 @@ func (m MatchIP) getClientIP(cx *Connection) (netip.Addr, error) { ip, err := netip.ParseAddr(ipStr) if err != nil { - return netip.Addr{}, fmt.Errorf("invalid client IP address: %s", ipStr) + return netip.Addr{}, fmt.Errorf("invalid remote IP address: %s", ipStr) } return ip, nil } @@ -296,9 +296,9 @@ func (m MatchNot) Match(r *Connection) (bool, error) { // Interface guards var ( - _ caddy.Module = (*MatchIP)(nil) - _ ConnMatcher = (*MatchIP)(nil) - _ caddy.Provisioner = (*MatchIP)(nil) + _ caddy.Module = (*MatchRemoteIP)(nil) + _ ConnMatcher = (*MatchRemoteIP)(nil) + _ caddy.Provisioner = (*MatchRemoteIP)(nil) _ caddy.Module = (*MatchLocalIP)(nil) _ ConnMatcher = (*MatchLocalIP)(nil) _ caddy.Provisioner = (*MatchLocalIP)(nil) diff --git a/layer4/matchers_test.go b/layer4/matchers_test.go index 0f447fc..3d61cb3 100644 --- a/layer4/matchers_test.go +++ b/layer4/matchers_test.go @@ -74,7 +74,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"127.0.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"127.0.0.1"}}), }, }, }, @@ -92,7 +92,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"127.0.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"127.0.0.1"}}), }, }, }, @@ -110,7 +110,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"172.16.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}), }, { provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}), @@ -131,7 +131,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"172.16.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}), }, { provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}), @@ -152,7 +152,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"172.16.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}), }, { provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}), @@ -173,7 +173,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"172.16.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}), provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}), }, }, @@ -192,7 +192,7 @@ func TestNotMatcher(t *testing.T) { matcher: MatchNot{ MatcherSets: []MatcherSet{ { - provision(&MatchIP{Ranges: []string{"172.16.0.1"}}), + provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}), provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}), }, },