| skipped 4 lines |
5 | 5 | | "io" |
6 | 6 | | "os" |
7 | 7 | | "os/signal" |
| 8 | + | "path/filepath" |
8 | 9 | | runtimeDebug "runtime/debug" |
| 10 | + | "sort" |
| 11 | + | "strings" |
9 | 12 | | "syscall" |
10 | 13 | | |
11 | 14 | | "github.com/sagernet/sing-box" |
| 15 | + | "github.com/sagernet/sing-box/common/badjsonmerge" |
12 | 16 | | "github.com/sagernet/sing-box/log" |
13 | 17 | | "github.com/sagernet/sing-box/option" |
14 | 18 | | E "github.com/sagernet/sing/common/exceptions" |
| skipped 16 lines |
31 | 35 | | mainCommand.AddCommand(commandRun) |
32 | 36 | | } |
33 | 37 | | |
34 | | - | func readConfig() (option.Options, error) { |
| 38 | + | type OptionsEntry struct { |
| 39 | + | content []byte |
| 40 | + | path string |
| 41 | + | options option.Options |
| 42 | + | } |
| 43 | + | |
| 44 | + | func readConfigAt(path string) (*OptionsEntry, error) { |
35 | 45 | | var ( |
36 | 46 | | configContent []byte |
37 | 47 | | err error |
38 | 48 | | ) |
39 | | - | if configPath == "stdin" { |
| 49 | + | if path == "stdin" { |
40 | 50 | | configContent, err = io.ReadAll(os.Stdin) |
41 | 51 | | } else { |
42 | | - | configContent, err = os.ReadFile(configPath) |
| 52 | + | configContent, err = os.ReadFile(path) |
43 | 53 | | } |
44 | 54 | | if err != nil { |
45 | | - | return option.Options{}, E.Cause(err, "read config") |
| 55 | + | return nil, E.Cause(err, "read config at ", path) |
46 | 56 | | } |
47 | 57 | | var options option.Options |
48 | 58 | | err = options.UnmarshalJSON(configContent) |
49 | 59 | | if err != nil { |
50 | | - | return option.Options{}, E.Cause(err, "decode config") |
| 60 | + | return nil, E.Cause(err, "decode config at ", path) |
51 | 61 | | } |
52 | | - | return options, nil |
| 62 | + | return &OptionsEntry{ |
| 63 | + | content: configContent, |
| 64 | + | path: path, |
| 65 | + | options: options, |
| 66 | + | }, nil |
| 67 | + | } |
| 68 | + | |
| 69 | + | func readConfig() ([]*OptionsEntry, error) { |
| 70 | + | var optionsList []*OptionsEntry |
| 71 | + | for _, path := range configPaths { |
| 72 | + | optionsEntry, err := readConfigAt(path) |
| 73 | + | if err != nil { |
| 74 | + | return nil, err |
| 75 | + | } |
| 76 | + | optionsList = append(optionsList, optionsEntry) |
| 77 | + | } |
| 78 | + | for _, directory := range configDirectories { |
| 79 | + | entries, err := os.ReadDir(directory) |
| 80 | + | if err != nil { |
| 81 | + | return nil, E.Cause(err, "read config directory at ", directory) |
| 82 | + | } |
| 83 | + | for _, entry := range entries { |
| 84 | + | if !strings.HasSuffix(entry.Name(), ".json") || entry.IsDir() { |
| 85 | + | continue |
| 86 | + | } |
| 87 | + | optionsEntry, err := readConfigAt(filepath.Join(directory, entry.Name())) |
| 88 | + | if err != nil { |
| 89 | + | return nil, err |
| 90 | + | } |
| 91 | + | optionsList = append(optionsList, optionsEntry) |
| 92 | + | } |
| 93 | + | } |
| 94 | + | sort.Slice(optionsList, func(i, j int) bool { |
| 95 | + | return optionsList[i].path < optionsList[j].path |
| 96 | + | }) |
| 97 | + | return optionsList, nil |
| 98 | + | } |
| 99 | + | |
| 100 | + | func readConfigAndMerge() (option.Options, error) { |
| 101 | + | optionsList, err := readConfig() |
| 102 | + | if err != nil { |
| 103 | + | return option.Options{}, err |
| 104 | + | } |
| 105 | + | var mergedOptions option.Options |
| 106 | + | for _, options := range optionsList { |
| 107 | + | mergedOptions, err = badjsonmerge.MergeOptions(options.options, mergedOptions) |
| 108 | + | if err != nil { |
| 109 | + | return option.Options{}, E.Cause(err, "merge config at ", options.path) |
| 110 | + | } |
| 111 | + | } |
| 112 | + | return mergedOptions, nil |
53 | 113 | | } |
54 | 114 | | |
55 | 115 | | func create() (*box.Box, context.CancelFunc, error) { |
56 | | - | options, err := readConfig() |
| 116 | + | options, err := readConfigAndMerge() |
57 | 117 | | if err != nil { |
58 | 118 | | return nil, nil, err |
59 | 119 | | } |
| skipped 63 lines |