github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/nat/manual.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package nat 19 20 import ( 21 "github.com/rs/zerolog/log" 22 23 "github.com/mysteriumnetwork/node/config" 24 "github.com/mysteriumnetwork/node/core/port" 25 ) 26 27 type manualPort struct { 28 pool *port.Pool 29 } 30 31 // NewManualPortProvider creates new instance of the manual port provider. 32 func NewManualPortProvider() PortProvider { 33 udpPortRange, err := port.ParseRange(config.GetString(config.FlagUDPListenPorts)) 34 if err != nil { 35 log.Warn().Err(err).Msg("Failed to parse UDP listen port range, using default value") 36 37 udpPortRange, err = port.ParseRange("10000:60000") 38 if err != nil { 39 panic(err) // This must never happen. 40 } 41 } 42 43 return &manualPort{port.NewFixedRangePool(udpPortRange)} 44 } 45 46 func (mp *manualPort) PreparePorts() (ports []int, release func(), start StartPorts, err error) { 47 poolPorts, err := mp.pool.AcquireMultiple(requiredConnCount) 48 if err != nil { 49 return nil, nil, nil, err 50 } 51 52 for _, p := range poolPorts { 53 ports = append(ports, p.Num()) 54 } 55 56 if err := checkAllPorts(ports); err != nil { 57 log.Debug().Err(err).Msgf("Failed to check manual ports %d globally", ports) 58 return nil, nil, nil, err 59 } 60 61 return ports, nil, nil, nil 62 }