1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-09-16 08:36:30 +02:00

fix ipv6 addr parsing and using

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov
2019-07-17 10:38:50 +03:00
parent d4fefc4b76
commit e688ab0a45
8 changed files with 104 additions and 68 deletions

View File

@@ -30,7 +30,7 @@ func isPrivateIP(ipAddr string) bool {
// Extract returns a real ip
func Extract(addr string) (string, error) {
// if addr specified then its returned
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]") {
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
return addr, nil
}
@@ -113,10 +113,13 @@ func IPs() []string {
continue
}
ip = ip.To4()
if ip == nil {
continue
}
// dont skip ipv6 addrs
/*
ip = ip.To4()
if ip == nil {
continue
}
*/
ipAddrs = append(ipAddrs, ip.String())
}

View File

@@ -11,39 +11,43 @@ import (
// Listen takes addr:portmin-portmax and binds to the first available port
// Example: Listen("localhost:5000-6000", fn)
func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
// host:port || host:min-max
parts := strings.Split(addr, ":")
//
if len(parts) < 2 {
if strings.Count(addr, ":") == 1 && strings.Count(addr, "-") == 0 {
return fn(addr)
}
// host:port || host:min-max
host, ports, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if host == "::" {
host = fmt.Sprintf("[%s]", host)
}
// try to extract port range
ports := strings.Split(parts[len(parts)-1], "-")
prange := strings.Split(ports, "-")
// single port
if len(ports) < 2 {
if len(prange) < 2 {
return fn(addr)
}
// we have a port range
// extract min port
min, err := strconv.Atoi(ports[0])
min, err := strconv.Atoi(prange[0])
if err != nil {
return nil, errors.New("unable to extract port range")
}
// extract max port
max, err := strconv.Atoi(ports[1])
max, err := strconv.Atoi(prange[1])
if err != nil {
return nil, errors.New("unable to extract port range")
}
// set host
host := parts[:len(parts)-1]
// range the ports
for port := min; port <= max; port++ {
// try bind to host:port