Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ip handler into remote_ip #218

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading