Skip to content

Commit

Permalink
do not perform sampling if access is made by directly specifying an I…
Browse files Browse the repository at this point in the history
…P address.
  • Loading branch information
shogo82148 committed Oct 5, 2023
1 parent 6543208 commit 606cb85
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions xray/sampling/centralized_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math/rand"
"net"
"net/http"
"net/netip"

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on ubuntu-latest

package net/netip is not in GOROOT (/opt/hostedtoolcache/go/1.17.13/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on ubuntu-latest

package net/netip is not in GOROOT (/opt/hostedtoolcache/go/1.16.15/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on windows-latest

package net/netip is not in GOROOT (C:\hostedtoolcache\windows\go\1.17.13\x64\src\net\netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on windows-latest

package net/netip is not in GOROOT (C:\hostedtoolcache\windows\go\1.16.15\x64\src\net\netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on macos-latest

package net/netip is not in GOROOT (/Users/runner/hostedtoolcache/go/1.17.13/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on macos-latest

package net/netip is not in GOROOT (/Users/runner/hostedtoolcache/go/1.16.15/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on ubuntu-latest

package net/netip is not in GOROOT (/opt/hostedtoolcache/go/1.17.13/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on ubuntu-latest

package net/netip is not in GOROOT (/opt/hostedtoolcache/go/1.16.15/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on windows-latest

package net/netip is not in GOROOT (C:\hostedtoolcache\windows\go\1.17.13\x64\src\net\netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on windows-latest

package net/netip is not in GOROOT (C:\hostedtoolcache\windows\go\1.16.15\x64\src\net\netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.17 Test on macos-latest

package net/netip is not in GOROOT (/Users/runner/hostedtoolcache/go/1.17.13/x64/src/net/netip)

Check failure on line 13 in xray/sampling/centralized_strategy.go

View workflow job for this annotation

GitHub Actions / Go 1.16 Test on macos-latest

package net/netip is not in GOROOT (/Users/runner/hostedtoolcache/go/1.16.15/x64/src/net/netip)
"sort"
"sync"
"time"
Expand Down Expand Up @@ -299,6 +300,12 @@ func (s *CentralizedStrategy) Close() {

// ShouldTrace implements Strategy.
func (s *CentralizedStrategy) ShouldTrace(req *Request) *Decision {
if isDirectIPAccess(req) {
return &Decision{
Sample: false,
}
}

s.startOnce.Do(s.start)
manifest := s.getManifest()
if manifest == nil {
Expand All @@ -317,6 +324,21 @@ func (s *CentralizedStrategy) ShouldTrace(req *Request) *Decision {
return s.fallback.ShouldTrace(req)
}

// Nowadays, access using virtual host functionality is mostly used,
// and there are few cases where access is made by directly specifying an IP address.
// Therefore, if access is made by directly specifying an IP address,
// we do not perform sampling.
func isDirectIPAccess(req *Request) bool {
hostport := req.Host
host, _, err := net.SplitHostPort(hostport)
if err != nil {
host = hostport
}

_, err = netip.ParseAddr(host)
return err == nil
}

func (s *CentralizedStrategy) getManifest() *centralizedManifest {
s.mu.RLock()
defer s.mu.RUnlock()
Expand Down
25 changes: 25 additions & 0 deletions xray/sampling/centralized_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,28 @@ func TestCentralizedStrategy_refreshQuota(t *testing.T) {
t.Errorf("unexpected ttl: want %d, got %d", 1000000000, quota.ttl.Unix())
}
}

func TestIsDirectIPAccess(t *testing.T) {
tests := []struct {
input string
want bool
}{
{"example.com", false},
{"example.com:80", false},
{"192.0.2.1", true},
{"192.0.2.1:80", true},
{"198.51.100.1", true},
{"198.51.100.1:80", true},
{"2001:db8::1", true},
{"[2001:db8::1]:80", true},
}

for _, tt := range tests {
req := &Request{
Host: tt.input,
}
if got := isDirectIPAccess(req); got != tt.want {
t.Errorf("unexpected result: want %t, got %t", tt.want, got)
}
}
}

0 comments on commit 606cb85

Please sign in to comment.