2019-05-31 01:52:10 +03:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-04-21 17:54:40 +03:00
|
|
|
"os"
|
2019-05-31 01:52:10 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-07-18 00:18:40 +03:00
|
|
|
// HostPort format addr and port suitable for dial
|
|
|
|
func HostPort(addr string, port interface{}) string {
|
|
|
|
host := addr
|
|
|
|
if strings.Count(addr, ":") > 0 {
|
|
|
|
host = fmt.Sprintf("[%s]", addr)
|
|
|
|
}
|
2019-08-14 16:32:28 +03:00
|
|
|
// when port is blank or 0, host is a queue name
|
|
|
|
if v, ok := port.(string); ok && v == "" {
|
|
|
|
return host
|
|
|
|
} else if v, ok := port.(int); ok && v == 0 && net.ParseIP(host) == nil {
|
|
|
|
return host
|
2019-08-06 11:15:54 +03:00
|
|
|
}
|
2019-08-14 16:32:28 +03:00
|
|
|
|
2019-07-18 00:18:40 +03:00
|
|
|
return fmt.Sprintf("%s:%v", host, port)
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:52:10 +03:00
|
|
|
// 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) {
|
|
|
|
|
2019-07-17 10:38:50 +03:00
|
|
|
if strings.Count(addr, ":") == 1 && strings.Count(addr, "-") == 0 {
|
2019-05-31 01:52:10 +03:00
|
|
|
return fn(addr)
|
|
|
|
}
|
|
|
|
|
2019-07-17 10:38:50 +03:00
|
|
|
// host:port || host:min-max
|
|
|
|
host, ports, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:52:10 +03:00
|
|
|
// try to extract port range
|
2019-07-17 10:38:50 +03:00
|
|
|
prange := strings.Split(ports, "-")
|
2019-05-31 01:52:10 +03:00
|
|
|
|
|
|
|
// single port
|
2019-07-17 10:38:50 +03:00
|
|
|
if len(prange) < 2 {
|
2019-05-31 01:52:10 +03:00
|
|
|
return fn(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have a port range
|
|
|
|
|
|
|
|
// extract min port
|
2019-07-17 10:38:50 +03:00
|
|
|
min, err := strconv.Atoi(prange[0])
|
2019-05-31 01:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to extract port range")
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract max port
|
2019-07-17 10:38:50 +03:00
|
|
|
max, err := strconv.Atoi(prange[1])
|
2019-05-31 01:52:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to extract port range")
|
|
|
|
}
|
|
|
|
|
|
|
|
// range the ports
|
|
|
|
for port := min; port <= max; port++ {
|
|
|
|
// try bind to host:port
|
2019-07-18 00:18:40 +03:00
|
|
|
ln, err := fn(HostPort(host, port))
|
2019-05-31 01:52:10 +03:00
|
|
|
if err == nil {
|
|
|
|
return ln, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// hit max port
|
|
|
|
if port == max {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// why are we here?
|
|
|
|
return nil, fmt.Errorf("unable to bind to %s", addr)
|
|
|
|
}
|
2020-04-21 17:54:40 +03:00
|
|
|
|
|
|
|
// Proxy returns the proxy and the address if it exits
|
|
|
|
func Proxy(service string, address []string) (string, []string, bool) {
|
|
|
|
var hasProxy bool
|
|
|
|
|
|
|
|
// get proxy
|
|
|
|
if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 {
|
|
|
|
// default name
|
|
|
|
if prx == "service" {
|
|
|
|
prx = "go.micro.proxy"
|
|
|
|
}
|
|
|
|
service = prx
|
|
|
|
hasProxy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// get proxy address
|
|
|
|
if prx := os.Getenv("MICRO_PROXY_ADDRESS"); len(prx) > 0 {
|
|
|
|
address = []string{prx}
|
|
|
|
hasProxy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if prx := os.Getenv("MICRO_NETWORK"); len(prx) > 0 {
|
|
|
|
// default name
|
|
|
|
if prx == "service" {
|
|
|
|
prx = "go.micro.network"
|
|
|
|
}
|
|
|
|
service = prx
|
|
|
|
hasProxy = true
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:44:34 +03:00
|
|
|
if prx := os.Getenv("MICRO_NETWORK_ADDRESS"); len(prx) > 0 {
|
2020-04-21 17:54:40 +03:00
|
|
|
address = []string{prx}
|
|
|
|
hasProxy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return service, address, hasProxy
|
|
|
|
}
|