github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/nat/order.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 "strings" 22 23 "github.com/rs/zerolog/log" 24 25 "github.com/mysteriumnetwork/node/config" 26 ) 27 28 // NamedPortProvider contains information of the NAT traversal method. 29 type NamedPortProvider struct { 30 Method string 31 Provider PortProvider 32 } 33 34 // PortProvider describes an method for provideing ports for the service. 35 type PortProvider interface { 36 PreparePorts() (ports []int, release func(), start StartPorts, err error) 37 } 38 39 var traversalOptions map[string]func() PortProvider = map[string]func() PortProvider{ 40 "manual": NewManualPortProvider, 41 "upnp": NewUPnPPortProvider, 42 "holepunching": NewNATHolePunchingPortProvider, 43 } 44 45 // OrderedPortProviders returns a ordered list of the port providers. 46 func OrderedPortProviders() (list []NamedPortProvider) { 47 methods := strings.Split(config.GetString(config.FlagTraversal), ",") 48 49 for _, m := range methods { 50 if t, ok := traversalOptions[m]; ok { 51 list = append(list, NamedPortProvider{Method: m, Provider: t()}) 52 } else { 53 log.Warn().Msgf("Unsupported traversal method %s, ignoring it", m) 54 } 55 } 56 57 if len(list) == 0 { 58 log.Warn().Msg("Failed to parse ordered list of traversal methods, falling back to default values") 59 60 return []NamedPortProvider{ 61 {"manual", NewManualPortProvider()}, 62 {"upnp", NewUPnPPortProvider()}, 63 {"holepunching", NewNATHolePunchingPortProvider()}, 64 } 65 } 66 67 return list 68 }