Skip to content

Commit 25a82ab

Browse files
authored
Exclude IP addresses from proxyless dialing (#1503)
* exclude IP addresses from proxyless dialing
1 parent 675d112 commit 25a82ab

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

dialer/proxyless.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (d *proxylessDialer) onFailure(host string, err error) {
142142
// status checks the status of the dialer for the given address.
143143
// Returns SUCCEEDED, FAILED, or UNKNOWN.
144144
func (d *proxylessDialer) status(address string) int {
145+
if isIPAddress(address) {
146+
// If the address is an IP address, we can't use the proxyless dialer.
147+
return FAILED
148+
}
145149
_, host, err := normalizeAddrHost(address)
146150
if err != nil {
147151
return FAILED
@@ -203,6 +207,16 @@ func normalizeAddrHost(address string) (string, string, error) {
203207
return addr, host, nil
204208
}
205209

210+
func isIPAddress(ip string) bool {
211+
// First split the IP address by host and port
212+
host, _, err := net.SplitHostPort(ip)
213+
if err != nil { // If there's no port, we assume it's just an IP address
214+
host = ip
215+
}
216+
parsedIP := net.ParseIP(host)
217+
return parsedIP != nil
218+
}
219+
206220
// configBytes returns the configuration bytes for the smart dialer
207221
// It uses the embedded file system to read the configuration file
208222
func configBytes() ([]byte, error) {

dialer/proxyless_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,29 @@ func TestDialContext_DialStreamError(t *testing.T) {
148148
assert.Nil(t, conn)
149149
assert.Error(t, err)
150150
}
151+
func TestIsIPAddress(t *testing.T) {
152+
tests := []struct {
153+
input string
154+
expected bool
155+
}{
156+
{"127.0.0.1", true},
157+
{"192.168.1.1", true},
158+
{"192.168.1.1:90", true},
159+
{"::1", true},
160+
{"2001:db8::1", true},
161+
{"example.com", false},
162+
{"localhost", false},
163+
{"", false},
164+
{"256.256.256.256", false}, // invalid IP
165+
{"1234:5678:9abc:defg::1", false}, // invalid IPv6
166+
{"127.0.0.1:8080", true}, // port included
167+
{"[2001:db8::1]:8080", true}, // IPv6 with port
168+
}
169+
170+
for _, tt := range tests {
171+
t.Run(tt.input, func(t *testing.T) {
172+
result := isIPAddress(tt.input)
173+
assert.Equal(t, tt.expected, result)
174+
})
175+
}
176+
}

0 commit comments

Comments
 (0)