-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathlisten.go
64 lines (52 loc) · 1.78 KB
/
listen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package utils
import (
"fmt"
"net"
"strings"
"github.com/antoniomika/multilistener"
)
const (
// AddressSeparator is the separtor when providing multiple addresses.
AddressSeparator = ","
// NetworkSeparator is the sepator between a network and address.
NetworkSeparator = "://"
)
// Listen uses the multilistener package to generate a net.Listener that uses multiple addresses.
func Listen(addresses string) (net.Listener, error) {
listeners := map[string][]string{}
addressList := strings.Split(addresses, AddressSeparator)
for _, address := range addressList {
addressSplit := strings.Split(address, NetworkSeparator)
if len(addressSplit) != 2 {
if _, ok := listeners["tcp"]; !ok {
listeners["tcp"] = []string{}
}
listeners["tcp"] = append(listeners["tcp"], address)
continue
}
if _, ok := listeners[addressSplit[0]]; !ok {
listeners[addressSplit[0]] = []string{}
}
listeners[addressSplit[0]] = append(listeners[addressSplit[0]], addressSplit[1])
}
return multilistener.Listen(listeners)
}
// ParseAddress parse a list of addresses into a host, port, err split.
func ParseAddress(addresses string) (string, string, error) {
addressList := strings.Split(addresses, AddressSeparator)
addressSplit := strings.Split(addressList[0], NetworkSeparator)
address := addressSplit[0]
if len(addressList) == 2 {
address = addressSplit[1]
}
return net.SplitHostPort(address)
}
// GenerateAddress generates an address string with ports set.
func GenerateAddress(addresses string, port uint32) string {
newAddressList := []string{}
addressList := strings.Split(addresses, AddressSeparator)
for _, address := range addressList {
newAddressList = append(newAddressList, fmt.Sprintf("%s:%d", address, port))
}
return strings.Join(newAddressList, AddressSeparator)
}