github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"os/signal"
     9  	"runtime"
    10  	"strings"
    11  	"syscall"
    12  
    13  	grpcServer "github.com/laof/lite-speed-test/api/rpc/liteserver"
    14  	C "github.com/laof/lite-speed-test/constant"
    15  	"github.com/laof/lite-speed-test/core"
    16  	"github.com/laof/lite-speed-test/utils"
    17  	webServer "github.com/laof/lite-speed-test/web"
    18  )
    19  
    20  var (
    21  	port    = flag.Int("p", 8090, "set port")
    22  	test    = flag.String("test", "", "test from command line with subscription link or file")
    23  	conf    = flag.String("config", "", "command line options")
    24  	ping    = flag.Int("ping", 2, "retry times to ping link on startup")
    25  	grpc    = flag.Bool("grpc", false, "start grpc server")
    26  	version = flag.Bool("v", false, "show current version of clash")
    27  )
    28  
    29  func main() {
    30  	flag.Parse()
    31  	if *version {
    32  		fmt.Printf("LiteSpeedTest  %s %s %s with %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
    33  		return
    34  	}
    35  	link := ""
    36  	for _, arg := range os.Args {
    37  		if strings.HasPrefix(arg, "-") {
    38  			continue
    39  		}
    40  		if _, err := utils.CheckLink(arg); err == nil {
    41  			link = arg
    42  			break
    43  		}
    44  	}
    45  	if *test != "" {
    46  		if err := webServer.TestFromCMD(*test, conf); err != nil {
    47  			log.Fatal(err)
    48  		}
    49  		return
    50  	}
    51  	// start grpc server
    52  	if *grpc {
    53  		if err := grpcServer.StartServer(uint16(*port)); err != nil {
    54  			log.Fatalln(err)
    55  		}
    56  		return
    57  	}
    58  	if link == "" {
    59  		if len(os.Args) < 2 {
    60  			*port = 10888
    61  		}
    62  		if err := webServer.ServeFile(*port); err != nil {
    63  			log.Fatalln(err)
    64  		}
    65  		return
    66  	}
    67  	c := core.Config{
    68  		LocalHost: "0.0.0.0",
    69  		LocalPort: *port,
    70  		Link:      link,
    71  		Ping:      *ping,
    72  	}
    73  	p, err := core.StartInstance(c)
    74  	if err != nil {
    75  		log.Fatalln(err)
    76  	}
    77  	sigs := make(chan os.Signal, 1)
    78  	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
    79  	defer signal.Stop(sigs)
    80  	go func() {
    81  		<-sigs
    82  		p.Close()
    83  	}()
    84  	p.Run()
    85  }