github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/portcheck/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/core/port"
    29  )
    30  
    31  const addressListSeparator = ","
    32  
    33  var (
    34  	serverList   = flag.String("server-list", "vm-0.com:4589", "comma-separated list of asymmetric UDP echo servers")
    35  	reqTimeout   = flag.Duration("req-timeout", 2*time.Second, "timeout to wait for UDP response")
    36  	checkedPort  = flag.Int("port", 12345, "checked port")
    37  	totalTimeout = flag.Duration("total-timeout", 5*time.Second, "overall operation deadline")
    38  )
    39  
    40  func run() int {
    41  	flag.Parse()
    42  
    43  	var addresses []string
    44  	for _, address := range strings.Split(*serverList, addressListSeparator) {
    45  		address = strings.TrimSpace(address)
    46  		if address != "" {
    47  			addresses = append(addresses, address)
    48  		}
    49  	}
    50  
    51  	ctx, cl := context.WithTimeout(context.Background(), *totalTimeout)
    52  	defer cl()
    53  	res, err := port.GloballyReachable(ctx, port.Port(*checkedPort), addresses, *reqTimeout)
    54  	if err != nil {
    55  		fmt.Fprintln(os.Stderr, "error:", err)
    56  		return 1
    57  	}
    58  	fmt.Println(res)
    59  	return 0
    60  }
    61  
    62  func main() {
    63  	os.Exit(run())
    64  }