github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/natdisco/main.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 main 19 20 import ( 21 "context" 22 "flag" 23 "fmt" 24 "os" 25 "strings" 26 "time" 27 28 "github.com/mysteriumnetwork/node/nat" 29 30 "github.com/mysteriumnetwork/node/nat/behavior" 31 ) 32 33 const addressListSeparator = "," 34 35 var ( 36 addressList = flag.String("servers", "stun.mysterium.network:3478,stun.stunprotocol.org:3478,stun.sip.us:3478", "comma-separated list of STUN servers") 37 reqTimeout = flag.Duration("req-timeout", 1*time.Second, "timeout to wait for each STUN server response") 38 totalTimeout = flag.Duration("total-timeout", 10*time.Second, "overall operation deadline") 39 raw = flag.Bool("raw", false, "print raw NAT_TYPE_* value") 40 ) 41 42 func run() int { 43 flag.Parse() 44 45 var addresses []string 46 for _, address := range strings.Split(*addressList, addressListSeparator) { 47 address = strings.TrimSpace(address) 48 if address != "" { 49 addresses = append(addresses, address) 50 } 51 } 52 53 ctx, cl := context.WithTimeout(context.Background(), *totalTimeout) 54 defer cl() 55 res, err := behavior.RacingDiscoverNATBehavior(ctx, addresses, *reqTimeout) 56 if err != nil { 57 fmt.Fprintln(os.Stderr, "error:", err) 58 return 1 59 } 60 if *raw { 61 fmt.Println(res) 62 } else { 63 fmt.Println("NAT Type:", nat.HumanReadableTypes[res]) 64 } 65 return 0 66 } 67 68 func main() { 69 os.Exit(run()) 70 }