mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-14 18:20:42 -07:00
Check port when removing self referenced routes
* Cleanup how self referenced routes were determined
This commit is contained in:
@@ -83,7 +83,11 @@ func main() {
|
||||
}
|
||||
|
||||
// Remove any host/ip that points to itself in Route
|
||||
opts.Routes = server.RemoveSelfReference(opts.Routes)
|
||||
newroutes, err := server.RemoveSelfReference(opts.ClusterPort, opts.Routes)
|
||||
if err != nil {
|
||||
server.PrintAndDie(err.Error())
|
||||
}
|
||||
opts.Routes = newroutes
|
||||
|
||||
// Create the server with appropriate options.
|
||||
s := server.New(&opts)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -206,54 +207,56 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options {
|
||||
return &opts
|
||||
}
|
||||
|
||||
func RemoveSelfReference(routes []*url.URL) []*url.URL {
|
||||
func RemoveSelfReference(clusterPort int, routes []*url.URL) ([]*url.URL, error) {
|
||||
var cleanRoutes []*url.URL
|
||||
cport := strconv.Itoa(clusterPort)
|
||||
|
||||
selfIPs := getInterfaceIPs()
|
||||
for i := 0; i < len(routes); i++ {
|
||||
for _, routeIP := range getUrlIp(routes[i]) {
|
||||
if !isIpInList(selfIPs, routeIP) {
|
||||
cleanRoutes = append(cleanRoutes, routes[i])
|
||||
} else {
|
||||
Log("Self referencing IP found: ", routes[i])
|
||||
}
|
||||
for _, r := range routes {
|
||||
host, port, err := net.SplitHostPort(r.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cport == port && isIpInList(selfIPs, getUrlIp(host)) {
|
||||
Log("Self referencing IP found: ", r)
|
||||
continue
|
||||
}
|
||||
cleanRoutes = append(cleanRoutes, r)
|
||||
}
|
||||
|
||||
return cleanRoutes
|
||||
return cleanRoutes, nil
|
||||
}
|
||||
|
||||
func isIpInList(list []net.IP, ip net.IP) bool {
|
||||
for _, selfIP := range list {
|
||||
if selfIP.Equal(ip) {
|
||||
return true
|
||||
func isIpInList(list1 []net.IP, list2 []net.IP) bool {
|
||||
for _, ip1 := range list1 {
|
||||
for _, ip2 := range list2 {
|
||||
if ip1.Equal(ip2) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getUrlIp(u *url.URL) []net.IP {
|
||||
var ipList []net.IP
|
||||
ipStr, _, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
Log("Error splitting host/port in route address: ", err)
|
||||
return ipList
|
||||
}
|
||||
func getUrlIp(ipStr string) []net.IP {
|
||||
ipList := []net.IP{}
|
||||
|
||||
ip := net.ParseIP(ipStr)
|
||||
if ip != nil {
|
||||
ipList = append(ipList, ip)
|
||||
} else {
|
||||
hostAddr, err := net.LookupHost(ipStr)
|
||||
if err != nil {
|
||||
Log("Error looking up host with route hostname: ", err)
|
||||
return ipList
|
||||
}
|
||||
for _, addr := range hostAddr {
|
||||
ip = net.ParseIP(addr)
|
||||
if ip != nil {
|
||||
ipList = append(ipList, ip)
|
||||
}
|
||||
return ipList
|
||||
}
|
||||
|
||||
hostAddr, err := net.LookupHost(ipStr)
|
||||
if err != nil {
|
||||
Log("Error looking up host with route hostname: ", err)
|
||||
return ipList
|
||||
}
|
||||
for _, addr := range hostAddr {
|
||||
ip = net.ParseIP(addr)
|
||||
if ip != nil {
|
||||
ipList = append(ipList, ip)
|
||||
}
|
||||
}
|
||||
return ipList
|
||||
|
||||
@@ -110,17 +110,32 @@ func TestRemoveSelfReference(t *testing.T) {
|
||||
url2, _ := url.Parse("nats-route://user:password@localhost:4223")
|
||||
url3, _ := url.Parse("nats-route://user:password@127.0.0.1:4223")
|
||||
|
||||
opts := &Options{
|
||||
Routes: []*url.URL{url1, url2, url3},
|
||||
routes := []*url.URL{url1, url2, url3}
|
||||
|
||||
newroutes, err := RemoveSelfReference(4223, routes)
|
||||
if err != nil {
|
||||
t.Fatalf("Error during RemoveSelfReference: %v", err)
|
||||
}
|
||||
|
||||
opts.Routes = RemoveSelfReference(opts.Routes)
|
||||
|
||||
if len(opts.Routes) != 1 {
|
||||
t.Fatalf("Self reference IP address exists in Routes ")
|
||||
if len(newroutes) != 1 {
|
||||
t.Fatalf("Wrong number of routes: %d", len(newroutes))
|
||||
}
|
||||
|
||||
if opts.Routes[0].String() != "nats-route://user:password@10.4.5.6:4223" {
|
||||
t.Fatalf("Self reference IP address %s in Routes", opts.Routes[0])
|
||||
if newroutes[0] != routes[0] {
|
||||
t.Fatalf("Self reference IP address %s in Routes", routes[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowRouteWithDifferentPort(t *testing.T) {
|
||||
url1, _ := url.Parse("nats-route://user:password@127.0.0.1:4224")
|
||||
routes := []*url.URL{url1}
|
||||
|
||||
newroutes, err := RemoveSelfReference(4223, routes)
|
||||
if err != nil {
|
||||
t.Fatalf("Error during RemoveSelfReference: %v", err)
|
||||
}
|
||||
|
||||
if len(newroutes) != 1 {
|
||||
t.Fatalf("Wrong number of routes: %d", len(newroutes))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user