Skip to content

Commit

Permalink
Rename ip handler into remote_ip (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnxme authored Jul 20, 2024
1 parent ca3e2f3 commit d087a3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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": [
Expand Down
30 changes: 15 additions & 15 deletions layer4/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func init() {
caddy.RegisterModule(MatchIP{})
caddy.RegisterModule(MatchRemoteIP{})
caddy.RegisterModule(MatchLocalIP{})
caddy.RegisterModule(MatchNot{})
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions layer4/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}),
},
},
},
Expand All @@ -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"}}),
},
},
},
Expand All @@ -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"}}),
Expand All @@ -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"}}),
Expand All @@ -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"}}),
Expand All @@ -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"}}),
},
},
Expand All @@ -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"}}),
},
},
Expand Down

0 comments on commit d087a3d

Please sign in to comment.