Files
sing-box-extended-mirror/cmd/sing-box/cmd_tools.go
T

55 lines
1.3 KiB
Go
Raw Normal View History

2023-03-18 20:26:58 +08:00
package main
import (
2024-10-23 13:44:08 +08:00
"errors"
"os"
2023-03-20 23:01:54 +08:00
"github.com/sagernet/sing-box"
2023-03-18 20:26:58 +08:00
E "github.com/sagernet/sing/common/exceptions"
2023-03-18 21:02:29 +08:00
N "github.com/sagernet/sing/common/network"
2023-03-18 20:26:58 +08:00
"github.com/spf13/cobra"
)
2023-03-18 23:01:10 +08:00
var commandToolsFlagOutbound string
2023-03-18 20:26:58 +08:00
var commandTools = &cobra.Command{
Use: "tools",
2023-03-18 21:02:29 +08:00
Short: "Experimental tools",
2023-03-18 20:26:58 +08:00
}
func init() {
2023-03-18 23:01:10 +08:00
commandTools.PersistentFlags().StringVarP(&commandToolsFlagOutbound, "outbound", "o", "", "Use specified tag instead of default outbound")
2023-03-18 20:26:58 +08:00
mainCommand.AddCommand(commandTools)
}
func createPreStartedClient() (*box.Box, error) {
options, err := readConfigAndMerge()
if err != nil {
2024-10-23 13:44:08 +08:00
if !(errors.Is(err, os.ErrNotExist) && len(configDirectories) == 0 && len(configPaths) == 1) || configPaths[0] != "config.json" {
return nil, err
}
2023-03-18 20:26:58 +08:00
}
2024-11-02 00:39:02 +08:00
instance, err := box.New(box.Options{Context: globalCtx, Options: options})
2023-03-18 20:26:58 +08:00
if err != nil {
return nil, E.Cause(err, "create service")
}
err = instance.PreStart()
if err != nil {
return nil, E.Cause(err, "start service")
}
return instance, nil
}
2023-03-18 21:02:29 +08:00
2024-11-09 21:16:11 +08:00
func createDialer(instance *box.Box, outboundTag string) (N.Dialer, error) {
2023-03-18 21:02:29 +08:00
if outboundTag == "" {
2024-11-09 21:16:11 +08:00
return instance.Outbound().Default(), nil
2023-03-18 21:02:29 +08:00
} else {
2024-11-09 21:16:11 +08:00
outbound, loaded := instance.Outbound().Outbound(outboundTag)
2023-03-18 21:02:29 +08:00
if !loaded {
return nil, E.New("outbound not found: ", outboundTag)
}
return outbound, nil
}
}