mirror of
https://github.com/fatedier/frp.git
synced 2025-07-20 04:39:26 +07:00
new feature: assign a random port if remote_port is 0 in type tcp and
udp
This commit is contained in:
@ -32,7 +32,7 @@ var (
|
||||
httpServerWriteTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
func RunDashboardServer(addr string, port int64) (err error) {
|
||||
func RunDashboardServer(addr string, port int) (err error) {
|
||||
// url router
|
||||
router := httprouter.New()
|
||||
|
||||
|
@ -36,8 +36,8 @@ type ServerInfoResp struct {
|
||||
GeneralResponse
|
||||
|
||||
Version string `json:"version"`
|
||||
VhostHttpPort int64 `json:"vhost_http_port"`
|
||||
VhostHttpsPort int64 `json:"vhost_https_port"`
|
||||
VhostHttpPort int `json:"vhost_http_port"`
|
||||
VhostHttpsPort int `json:"vhost_https_port"`
|
||||
AuthTimeout int64 `json:"auth_timeout"`
|
||||
SubdomainHost string `json:"subdomain_host"`
|
||||
MaxPoolCount int64 `json:"max_pool_count"`
|
||||
|
180
server/ports.go
Normal file
180
server/ports.go
Normal file
@ -0,0 +1,180 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
MinPort = 1025
|
||||
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
|
||||
}
|
||||
|
||||
type PortManager struct {
|
||||
reservedPorts map[string]*PortCtx
|
||||
usedPorts map[int]*PortCtx
|
||||
freePorts map[int]struct{}
|
||||
|
||||
bindAddr string
|
||||
netType string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewPortManager(netType string, bindAddr string, allowPorts map[int]struct{}) *PortManager {
|
||||
pm := &PortManager{
|
||||
reservedPorts: make(map[string]*PortCtx),
|
||||
usedPorts: make(map[int]*PortCtx),
|
||||
freePorts: make(map[int]struct{}),
|
||||
bindAddr: bindAddr,
|
||||
netType: netType,
|
||||
}
|
||||
if len(allowPorts) > 0 {
|
||||
for port, _ := range allowPorts {
|
||||
pm.freePorts[port] = struct{}{}
|
||||
}
|
||||
} else {
|
||||
for i := MinPort; i <= MaxPort; i++ {
|
||||
pm.freePorts[i] = struct{}{}
|
||||
}
|
||||
}
|
||||
go pm.cleanReservedPortsWorker()
|
||||
return pm
|
||||
}
|
||||
|
||||
func (pm *PortManager) Acquire(name string, port int) (realPort int, err error) {
|
||||
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
|
||||
for k, _ := range pm.freePorts {
|
||||
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
|
||||
}
|
||||
|
||||
func (pm *PortManager) isPortAvailable(port int) bool {
|
||||
if pm.netType == "udp" {
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pm.bindAddr, port))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
l, err := net.ListenUDP("udp", addr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
l.Close()
|
||||
return true
|
||||
} else {
|
||||
l, err := net.Listen(pm.netType, fmt.Sprintf("%s:%d", pm.bindAddr, port))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
l.Close()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *PortManager) Release(port int) {
|
||||
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.
|
||||
func (pm *PortManager) cleanReservedPortsWorker() {
|
||||
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()
|
||||
}
|
||||
}
|
@ -165,11 +165,24 @@ func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy, err error) {
|
||||
type TcpProxy struct {
|
||||
BaseProxy
|
||||
cfg *config.TcpProxyConf
|
||||
|
||||
realPort int
|
||||
}
|
||||
|
||||
func (pxy *TcpProxy) Run() (remoteAddr string, err error) {
|
||||
remoteAddr = fmt.Sprintf(":%d", pxy.cfg.RemotePort)
|
||||
listener, errRet := frpNet.ListenTcp(config.ServerCommonCfg.ProxyBindAddr, pxy.cfg.RemotePort)
|
||||
pxy.realPort, err = pxy.ctl.svr.tcpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
|
||||
}
|
||||
}()
|
||||
|
||||
remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
|
||||
pxy.cfg.RemotePort = pxy.realPort
|
||||
listener, errRet := frpNet.ListenTcp(config.ServerCommonCfg.ProxyBindAddr, pxy.realPort)
|
||||
if errRet != nil {
|
||||
err = errRet
|
||||
return
|
||||
@ -188,6 +201,7 @@ func (pxy *TcpProxy) GetConf() config.ProxyConf {
|
||||
|
||||
func (pxy *TcpProxy) Close() {
|
||||
pxy.BaseProxy.Close()
|
||||
pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
|
||||
}
|
||||
|
||||
type HttpProxy struct {
|
||||
@ -412,6 +426,8 @@ type UdpProxy struct {
|
||||
BaseProxy
|
||||
cfg *config.UdpProxyConf
|
||||
|
||||
realPort int
|
||||
|
||||
// udpConn is the listener of udp packages
|
||||
udpConn *net.UDPConn
|
||||
|
||||
@ -432,8 +448,19 @@ type UdpProxy struct {
|
||||
}
|
||||
|
||||
func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
|
||||
remoteAddr = fmt.Sprintf(":%d", pxy.cfg.RemotePort)
|
||||
addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", config.ServerCommonCfg.ProxyBindAddr, pxy.cfg.RemotePort))
|
||||
pxy.realPort, err = pxy.ctl.svr.udpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
|
||||
}
|
||||
}()
|
||||
|
||||
remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
|
||||
pxy.cfg.RemotePort = pxy.realPort
|
||||
addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", config.ServerCommonCfg.ProxyBindAddr, pxy.realPort))
|
||||
if errRet != nil {
|
||||
err = errRet
|
||||
return
|
||||
@ -581,6 +608,7 @@ func (pxy *UdpProxy) Close() {
|
||||
close(pxy.readCh)
|
||||
close(pxy.sendCh)
|
||||
}
|
||||
pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
|
||||
}
|
||||
|
||||
// HandleUserTcpConnection is used for incoming tcp user connections.
|
||||
|
@ -60,17 +60,25 @@ type Service struct {
|
||||
// Manage all visitor listeners.
|
||||
visitorManager *VisitorManager
|
||||
|
||||
// Manage all tcp ports.
|
||||
tcpPortManager *PortManager
|
||||
|
||||
// Manage all udp ports.
|
||||
udpPortManager *PortManager
|
||||
|
||||
// Controller for nat hole connections.
|
||||
natHoleController *NatHoleController
|
||||
}
|
||||
|
||||
func NewService() (svr *Service, err error) {
|
||||
cfg := config.ServerCommonCfg
|
||||
svr = &Service{
|
||||
ctlManager: NewControlManager(),
|
||||
pxyManager: NewProxyManager(),
|
||||
visitorManager: NewVisitorManager(),
|
||||
tcpPortManager: NewPortManager("tcp", cfg.ProxyBindAddr, cfg.PrivilegeAllowPorts),
|
||||
udpPortManager: NewPortManager("udp", cfg.ProxyBindAddr, cfg.PrivilegeAllowPorts),
|
||||
}
|
||||
cfg := config.ServerCommonCfg
|
||||
|
||||
// Init assets.
|
||||
err = assets.Load(cfg.AssetsDir)
|
||||
|
Reference in New Issue
Block a user