2014-01-23 06:43:50 -08:00
|
|
|
package portallocator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-01-23 07:46:42 -08:00
|
|
|
"net"
|
2014-01-23 06:43:50 -08:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
type (
|
|
|
|
portMap map[int]bool
|
|
|
|
protocolMap map[string]portMap
|
|
|
|
ipMapping map[string]protocolMap
|
|
|
|
)
|
|
|
|
|
2014-01-23 06:43:50 -08:00
|
|
|
const (
|
|
|
|
BeginPortRange = 49153
|
|
|
|
EndPortRange = 65535
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-03-31 22:21:52 -07:00
|
|
|
ErrAllPortsAllocated = errors.New("all ports are allocated")
|
2014-01-23 06:43:50 -08:00
|
|
|
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
|
|
|
|
ErrUnknownProtocol = errors.New("unknown protocol")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-05-20 15:05:57 -07:00
|
|
|
mutex sync.Mutex
|
2014-01-23 06:43:50 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
defaultIP = net.ParseIP("0.0.0.0")
|
|
|
|
globalMap = ipMapping{}
|
|
|
|
)
|
2014-01-23 06:43:50 -08:00
|
|
|
|
2014-01-23 07:46:42 -08:00
|
|
|
func RequestPort(ip net.IP, proto string, port int) (int, error) {
|
2014-05-20 15:05:57 -07:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2014-01-23 06:43:50 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
if err := validateProto(proto); err != nil {
|
2014-01-23 06:43:50 -08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
ip = getDefault(ip)
|
|
|
|
|
|
|
|
mapping := getOrCreate(ip)
|
|
|
|
|
2014-01-30 14:52:59 -08:00
|
|
|
if port > 0 {
|
2014-05-20 15:05:57 -07:00
|
|
|
if !mapping[proto][port] {
|
|
|
|
mapping[proto][port] = true
|
|
|
|
return port, nil
|
|
|
|
} else {
|
|
|
|
return 0, ErrPortAlreadyAllocated
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port, err := findPort(ip, proto)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-01-23 07:46:42 -08:00
|
|
|
return 0, err
|
|
|
|
}
|
2014-05-20 15:05:57 -07:00
|
|
|
|
2014-01-23 06:43:50 -08:00
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 07:46:42 -08:00
|
|
|
func ReleasePort(ip net.IP, proto string, port int) error {
|
2014-05-20 15:05:57 -07:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2014-01-23 06:43:50 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
ip = getDefault(ip)
|
2014-01-23 06:43:50 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
mapping := getOrCreate(ip)
|
|
|
|
delete(mapping[proto], port)
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-01-23 06:43:50 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:17:28 -08:00
|
|
|
func ReleaseAll() error {
|
2014-05-20 15:05:57 -07:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
globalMap = ipMapping{}
|
2014-01-23 12:17:28 -08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
func getOrCreate(ip net.IP) protocolMap {
|
|
|
|
ipstr := ip.String()
|
2014-03-12 17:22:57 -04:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
if _, ok := globalMap[ipstr]; !ok {
|
|
|
|
globalMap[ipstr] = protocolMap{
|
|
|
|
"tcp": portMap{},
|
|
|
|
"udp": portMap{},
|
2014-03-12 17:22:57 -04:00
|
|
|
}
|
2014-01-23 12:17:28 -08:00
|
|
|
}
|
2014-05-20 15:05:57 -07:00
|
|
|
|
|
|
|
return globalMap[ipstr]
|
2014-01-23 12:17:28 -08:00
|
|
|
}
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
func findPort(ip net.IP, proto string) (int, error) {
|
|
|
|
port := BeginPortRange
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
mapping := getOrCreate(ip)
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
for mapping[proto][port] {
|
|
|
|
port++
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
if port > EndPortRange {
|
2014-03-31 22:21:52 -07:00
|
|
|
return 0, ErrAllPortsAllocated
|
|
|
|
}
|
2014-03-12 17:22:57 -04:00
|
|
|
}
|
2014-05-20 15:05:57 -07:00
|
|
|
|
|
|
|
mapping[proto][port] = true
|
|
|
|
|
2014-03-12 17:22:57 -04:00
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
func getDefault(ip net.IP) net.IP {
|
|
|
|
if ip == nil {
|
|
|
|
return defaultIP
|
2014-03-31 22:21:52 -07:00
|
|
|
}
|
2014-01-23 12:17:28 -08:00
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
return ip
|
2014-01-23 12:17:28 -08:00
|
|
|
}
|
|
|
|
|
2014-05-20 15:05:57 -07:00
|
|
|
func validateProto(proto string) error {
|
|
|
|
if proto != "tcp" && proto != "udp" {
|
2014-01-23 06:43:50 -08:00
|
|
|
return ErrUnknownProtocol
|
|
|
|
}
|
2014-05-20 15:05:57 -07:00
|
|
|
|
2014-01-23 06:43:50 -08:00
|
|
|
return nil
|
|
|
|
}
|