github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/examples/ping.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"time"
     9  
    10  	"github.com/xxf098/lite-proxy/download"
    11  	"github.com/xxf098/lite-proxy/web"
    12  )
    13  
    14  func main() {
    15  	async := flag.Bool("async", false, "use async test")
    16  	link := flag.String("link", "", "link to test")
    17  	mode := flag.String("mode", "pingonly", "speed test mode")
    18  	flag.Parse()
    19  	// link := "vmess://aHR0cHM6Ly9naXRodWIuY29tL3h4ZjA5OC9MaXRlU3BlZWRUZXN0"
    20  	if len(*link) < 1 {
    21  		log.Fatal("link required")
    22  	}
    23  	opts := web.ProfileTestOptions{
    24  		GroupName:     "Default",
    25  		SpeedTestMode: *mode,        //  pingonly speedonly all
    26  		PingMethod:    "googleping", // googleping
    27  		SortMethod:    "rspeed",     // speed rspeed ping rping
    28  		Concurrency:   2,
    29  		TestMode:      2, // 2: ALLTEST 3: RETEST
    30  		Subscription:  *link,
    31  		Language:      "en", // en cn
    32  		FontSize:      24,
    33  		Theme:         "rainbow",
    34  		Timeout:       10 * time.Second,
    35  		OutputMode:    0, // 0: base64 1:file path 2: no pic 3: json 4: txt
    36  	}
    37  	ctx := context.Background()
    38  	var err error
    39  	if *async {
    40  		err = pingAsync(ctx, opts)
    41  	} else {
    42  		err = pingSync(ctx, opts)
    43  	}
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  }
    48  
    49  func pingSync(ctx context.Context, opts web.ProfileTestOptions) error {
    50  	nodes, err := web.TestContext(ctx, opts, &web.EmptyMessageWriter{})
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	for _, node := range nodes {
    56  		// tested node info here
    57  		if node.IsOk {
    58  			fmt.Println("id:", node.Id, node.Remarks, "ping:", node.Ping)
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func pingAsync(ctx context.Context, opts web.ProfileTestOptions) error {
    65  	nodeChan, links, err := web.TestAsyncContext(ctx, opts)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	count := len(links)
    70  	for i := 0; i < count; i++ {
    71  		node := <-nodeChan
    72  		if node.IsOk {
    73  			fmt.Println("id:", node.Id, node.Remarks, "ping:", node.Ping, "avg", download.ByteCountIECTrim(node.AvgSpeed), "max", download.ByteCountIECTrim(node.MaxSpeed), "link:", links[node.Id])
    74  		}
    75  	}
    76  	close(nodeChan)
    77  	return nil
    78  }