2018-05-23 14:39:12 +08:00
|
|
|
package ports
|
2018-01-17 21:49:37 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-02-09 15:19:35 +08:00
|
|
|
"strconv"
|
2018-01-17 21:49:37 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-01-22 11:48:31 +08:00
|
|
|
MinPort = 1
|
2018-01-17 21:49:37 +08:00
|
|
|
MaxPort = 65535
|
|
|
|
MaxPortReservedDuration = time.Duration(24) * time.Hour
|
|
|
|
CleanReservedPortsInterval = time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrPortAlreadyUsed = errors.New("port already used")
|
|
|
|
ErrPortNotAllowed = errors.New("port not allowed")
|
|
|
|
ErrPortUnAvailable = errors.New("port unavailable")
|
|
|
|
ErrNoAvailablePort = errors.New("no available port")
|
|
|
|
)
|
|
|
|
|
|
|
|
type PortCtx struct {
|
|
|
|
ProxyName string
|
|
|
|
Port int
|
|
|
|
Closed bool
|
|
|
|
UpdateTime time.Time
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type Manager struct {
|
2018-01-17 21:49:37 +08:00
|
|
|
reservedPorts map[string]*PortCtx
|
|
|
|
usedPorts map[int]*PortCtx
|
|
|
|
freePorts map[int]struct{}
|
|
|
|
|
|
|
|
bindAddr string
|
|
|
|
netType string
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewManager(netType string, bindAddr string, allowPorts map[int]struct{}) *Manager {
|
|
|
|
pm := &Manager{
|
2018-01-17 21:49:37 +08:00
|
|
|
reservedPorts: make(map[string]*PortCtx),
|
|
|
|
usedPorts: make(map[int]*PortCtx),
|
|
|
|
freePorts: make(map[int]struct{}),
|
|
|
|
bindAddr: bindAddr,
|
|
|
|
netType: netType,
|
|
|
|
}
|
|
|
|
if len(allowPorts) > 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
for port := range allowPorts {
|
2018-01-17 21:49:37 +08:00
|
|
|
pm.freePorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := MinPort; i <= MaxPort; i++ {
|
|
|
|
pm.freePorts[i] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go pm.cleanReservedPortsWorker()
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (pm *Manager) Acquire(name string, port int) (realPort int, err error) {
|
2018-01-17 21:49:37 +08:00
|
|
|
portCtx := &PortCtx{
|
|
|
|
ProxyName: name,
|
|
|
|
Closed: false,
|
|
|
|
UpdateTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
portCtx.Port = realPort
|
|
|
|
}
|
|
|
|
pm.mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// check reserved ports first
|
|
|
|
if port == 0 {
|
|
|
|
if ctx, ok := pm.reservedPorts[name]; ok {
|
|
|
|
if pm.isPortAvailable(ctx.Port) {
|
|
|
|
realPort = ctx.Port
|
|
|
|
pm.usedPorts[realPort] = portCtx
|
|
|
|
pm.reservedPorts[name] = portCtx
|
|
|
|
delete(pm.freePorts, realPort)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if port == 0 {
|
|
|
|
// get random port
|
|
|
|
count := 0
|
|
|
|
maxTryTimes := 5
|
2020-05-24 17:48:37 +08:00
|
|
|
for k := range pm.freePorts {
|
2018-01-17 21:49:37 +08:00
|
|
|
count++
|
|
|
|
if count > maxTryTimes {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if pm.isPortAvailable(k) {
|
|
|
|
realPort = k
|
|
|
|
pm.usedPorts[realPort] = portCtx
|
|
|
|
pm.reservedPorts[name] = portCtx
|
|
|
|
delete(pm.freePorts, realPort)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if realPort == 0 {
|
|
|
|
err = ErrNoAvailablePort
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// specified port
|
|
|
|
if _, ok = pm.freePorts[port]; ok {
|
|
|
|
if pm.isPortAvailable(port) {
|
|
|
|
realPort = port
|
|
|
|
pm.usedPorts[realPort] = portCtx
|
|
|
|
pm.reservedPorts[name] = portCtx
|
|
|
|
delete(pm.freePorts, realPort)
|
|
|
|
} else {
|
|
|
|
err = ErrPortUnAvailable
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok = pm.usedPorts[port]; ok {
|
|
|
|
err = ErrPortAlreadyUsed
|
|
|
|
} else {
|
|
|
|
err = ErrPortNotAllowed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (pm *Manager) isPortAvailable(port int) bool {
|
2018-01-17 21:49:37 +08:00
|
|
|
if pm.netType == "udp" {
|
2022-02-09 15:19:35 +08:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(pm.bindAddr, strconv.Itoa(port)))
|
2018-01-17 21:49:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
l, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
l.Close()
|
|
|
|
return true
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
|
2022-02-09 15:19:35 +08:00
|
|
|
l, err := net.Listen(pm.netType, net.JoinHostPort(pm.bindAddr, strconv.Itoa(port)))
|
2020-05-24 17:48:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
l.Close()
|
|
|
|
return true
|
2018-01-17 21:49:37 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (pm *Manager) Release(port int) {
|
2018-01-17 21:49:37 +08:00
|
|
|
pm.mu.Lock()
|
|
|
|
defer pm.mu.Unlock()
|
|
|
|
if ctx, ok := pm.usedPorts[port]; ok {
|
|
|
|
pm.freePorts[port] = struct{}{}
|
|
|
|
delete(pm.usedPorts, port)
|
|
|
|
ctx.Closed = true
|
|
|
|
ctx.UpdateTime = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release reserved port if it isn't used in last 24 hours.
|
2020-05-24 17:48:37 +08:00
|
|
|
func (pm *Manager) cleanReservedPortsWorker() {
|
2018-01-17 21:49:37 +08:00
|
|
|
for {
|
|
|
|
time.Sleep(CleanReservedPortsInterval)
|
|
|
|
pm.mu.Lock()
|
|
|
|
for name, ctx := range pm.reservedPorts {
|
|
|
|
if ctx.Closed && time.Since(ctx.UpdateTime) > MaxPortReservedDuration {
|
|
|
|
delete(pm.reservedPorts, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pm.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|