github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_tools.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/sagernet/sing-box"
     5  	E "github.com/sagernet/sing/common/exceptions"
     6  	N "github.com/sagernet/sing/common/network"
     7  
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var commandToolsFlagOutbound string
    12  
    13  var commandTools = &cobra.Command{
    14  	Use:   "tools",
    15  	Short: "Experimental tools",
    16  }
    17  
    18  func init() {
    19  	commandTools.PersistentFlags().StringVarP(&commandToolsFlagOutbound, "outbound", "o", "", "Use specified tag instead of default outbound")
    20  	mainCommand.AddCommand(commandTools)
    21  }
    22  
    23  func createPreStartedClient() (*box.Box, error) {
    24  	options, err := readConfigAndMerge()
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	instance, err := box.New(box.Options{Options: options})
    29  	if err != nil {
    30  		return nil, E.Cause(err, "create service")
    31  	}
    32  	err = instance.PreStart()
    33  	if err != nil {
    34  		return nil, E.Cause(err, "start service")
    35  	}
    36  	return instance, nil
    37  }
    38  
    39  func createDialer(instance *box.Box, network string, outboundTag string) (N.Dialer, error) {
    40  	if outboundTag == "" {
    41  		return instance.Router().DefaultOutbound(N.NetworkName(network))
    42  	} else {
    43  		outbound, loaded := instance.Router().Outbound(outboundTag)
    44  		if !loaded {
    45  			return nil, E.New("outbound not found: ", outboundTag)
    46  		}
    47  		return outbound, nil
    48  	}
    49  }