Extract private ip for registration when 0.0.0.0 is specified as bind address
This commit is contained in:
parent
e00a57bb12
commit
0dbe14ad22
@ -2,11 +2,24 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
"github.com/myodc/go-micro/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
privateBlocks []*net.IPNet
|
||||
)
|
||||
|
||||
func init() {
|
||||
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
|
||||
if _, block, err := net.ParseCIDR(b); err == nil {
|
||||
privateBlocks = append(privateBlocks, block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extractValue(v reflect.Type) *registry.Value {
|
||||
if v == nil {
|
||||
return nil
|
||||
@ -81,3 +94,55 @@ func extractSubValue(typ reflect.Type) *registry.Value {
|
||||
}
|
||||
return extractValue(reqType)
|
||||
}
|
||||
|
||||
func extractAddress(addr string) (string, error) {
|
||||
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]") {
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to get interface addresses! Err: %v", err)
|
||||
}
|
||||
|
||||
var ipAddr []byte
|
||||
|
||||
for _, rawAddr := range addrs {
|
||||
var ip net.IP
|
||||
switch addr := rawAddr.(type) {
|
||||
case *net.IPAddr:
|
||||
ip = addr.IP
|
||||
case *net.IPNet:
|
||||
ip = addr.IP
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if ip.To4() == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if !isPrivateIP(ip.String()) {
|
||||
continue
|
||||
}
|
||||
|
||||
ipAddr = ip
|
||||
break
|
||||
}
|
||||
|
||||
if ipAddr == nil {
|
||||
return "", fmt.Errorf("No private IP address found, and explicit IP not provided")
|
||||
}
|
||||
|
||||
return net.IP(ipAddr).String(), nil
|
||||
}
|
||||
|
||||
func isPrivateIP(ipAddr string) bool {
|
||||
ip := net.ParseIP(ipAddr)
|
||||
for _, priv := range privateBlocks {
|
||||
if priv.Contains(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -116,6 +116,7 @@ func (s *rpcServer) Register() error {
|
||||
config := s.Config()
|
||||
var host string
|
||||
var port int
|
||||
|
||||
parts := strings.Split(config.Address(), ":")
|
||||
if len(parts) > 1 {
|
||||
host = strings.Join(parts[:len(parts)-1], ":")
|
||||
@ -124,10 +125,15 @@ func (s *rpcServer) Register() error {
|
||||
host = parts[0]
|
||||
}
|
||||
|
||||
addr, err := extractAddress(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// register service
|
||||
node := ®istry.Node{
|
||||
Id: config.Id(),
|
||||
Address: host,
|
||||
Address: addr,
|
||||
Port: port,
|
||||
Metadata: config.Metadata(),
|
||||
}
|
||||
@ -181,9 +187,14 @@ func (s *rpcServer) Deregister() error {
|
||||
host = parts[0]
|
||||
}
|
||||
|
||||
addr, err := extractAddress(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
node := ®istry.Node{
|
||||
Id: config.Id(),
|
||||
Address: host,
|
||||
Address: addr,
|
||||
Port: port,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user