Embedded NATS Broker (#1110)
* if the address is produced by a default route don't hash it * embedded nats * fix url parsing * don't override help * add ready flag
This commit is contained in:
@@ -27,6 +27,29 @@ func isPrivateIP(ipAddr string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsLocal tells us whether an ip is local
|
||||
func IsLocal(addr string) bool {
|
||||
// extract the host
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err == nil {
|
||||
addr = host
|
||||
}
|
||||
|
||||
// check if its localhost
|
||||
if addr == "localhost" {
|
||||
return true
|
||||
}
|
||||
|
||||
// check against all local ips
|
||||
for _, ip := range IPs() {
|
||||
if addr == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Extract returns a real ip
|
||||
func Extract(addr string) (string, error) {
|
||||
// if addr specified then its returned
|
||||
|
@@ -5,6 +5,26 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsLocal(t *testing.T) {
|
||||
testData := []struct {
|
||||
addr string
|
||||
expect bool
|
||||
}{
|
||||
{"localhost", true},
|
||||
{"localhost:8080", true},
|
||||
{"127.0.0.1", true},
|
||||
{"127.0.0.1:1001", true},
|
||||
{"80.1.1.1", false},
|
||||
}
|
||||
|
||||
for _, d := range testData {
|
||||
res := IsLocal(d.addr)
|
||||
if res != d.expect {
|
||||
t.Fatalf("expected %t got %t", d.expect, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractor(t *testing.T) {
|
||||
testData := []struct {
|
||||
addr string
|
||||
|
Reference in New Issue
Block a user