|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/sagernet/sing-box/log" |
| 12 | + "github.com/sagernet/sing-box/option" |
| 13 | + E "github.com/sagernet/sing/common/exceptions" |
| 14 | + "github.com/sagernet/sing/common/json" |
| 15 | + "github.com/sagernet/sing/common/json/badjson" |
| 16 | + "github.com/sagernet/sing/common/rw" |
| 17 | + |
| 18 | + "github.com/spf13/cobra" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + ruleSetPaths []string |
| 23 | + ruleSetDirectories []string |
| 24 | +) |
| 25 | + |
| 26 | +var commandRuleSetMerge = &cobra.Command{ |
| 27 | + Use: "merge <output-path>", |
| 28 | + Short: "Merge rule-set source files", |
| 29 | + Run: func(cmd *cobra.Command, args []string) { |
| 30 | + err := mergeRuleSet(args[0]) |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + }, |
| 35 | + Args: cobra.ExactArgs(1), |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + commandRuleSetMerge.Flags().StringArrayVarP(&ruleSetPaths, "config", "c", nil, "set input rule-set file path") |
| 40 | + commandRuleSetMerge.Flags().StringArrayVarP(&ruleSetDirectories, "config-directory", "C", nil, "set input rule-set directory path") |
| 41 | + commandRuleSet.AddCommand(commandRuleSetMerge) |
| 42 | +} |
| 43 | + |
| 44 | +type RuleSetEntry struct { |
| 45 | + content []byte |
| 46 | + path string |
| 47 | + options option.PlainRuleSetCompat |
| 48 | +} |
| 49 | + |
| 50 | +func readRuleSetAt(path string) (*RuleSetEntry, error) { |
| 51 | + var ( |
| 52 | + configContent []byte |
| 53 | + err error |
| 54 | + ) |
| 55 | + if path == "stdin" { |
| 56 | + configContent, err = io.ReadAll(os.Stdin) |
| 57 | + } else { |
| 58 | + configContent, err = os.ReadFile(path) |
| 59 | + } |
| 60 | + if err != nil { |
| 61 | + return nil, E.Cause(err, "read config at ", path) |
| 62 | + } |
| 63 | + options, err := json.UnmarshalExtendedContext[option.PlainRuleSetCompat](globalCtx, configContent) |
| 64 | + if err != nil { |
| 65 | + return nil, E.Cause(err, "decode config at ", path) |
| 66 | + } |
| 67 | + return &RuleSetEntry{ |
| 68 | + content: configContent, |
| 69 | + path: path, |
| 70 | + options: options, |
| 71 | + }, nil |
| 72 | +} |
| 73 | + |
| 74 | +func readRuleSet() ([]*RuleSetEntry, error) { |
| 75 | + var optionsList []*RuleSetEntry |
| 76 | + for _, path := range ruleSetPaths { |
| 77 | + optionsEntry, err := readRuleSetAt(path) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + optionsList = append(optionsList, optionsEntry) |
| 82 | + } |
| 83 | + for _, directory := range ruleSetDirectories { |
| 84 | + entries, err := os.ReadDir(directory) |
| 85 | + if err != nil { |
| 86 | + return nil, E.Cause(err, "read rule-set directory at ", directory) |
| 87 | + } |
| 88 | + for _, entry := range entries { |
| 89 | + if !strings.HasSuffix(entry.Name(), ".json") || entry.IsDir() { |
| 90 | + continue |
| 91 | + } |
| 92 | + optionsEntry, err := readRuleSetAt(filepath.Join(directory, entry.Name())) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + optionsList = append(optionsList, optionsEntry) |
| 97 | + } |
| 98 | + } |
| 99 | + sort.Slice(optionsList, func(i, j int) bool { |
| 100 | + return optionsList[i].path < optionsList[j].path |
| 101 | + }) |
| 102 | + return optionsList, nil |
| 103 | +} |
| 104 | + |
| 105 | +func readRuleSetAndMerge() (option.PlainRuleSetCompat, error) { |
| 106 | + optionsList, err := readRuleSet() |
| 107 | + if err != nil { |
| 108 | + return option.PlainRuleSetCompat{}, err |
| 109 | + } |
| 110 | + if len(optionsList) == 1 { |
| 111 | + return optionsList[0].options, nil |
| 112 | + } |
| 113 | + var optionVersion uint8 |
| 114 | + for _, options := range optionsList { |
| 115 | + if optionVersion < options.options.Version { |
| 116 | + optionVersion = options.options.Version |
| 117 | + } |
| 118 | + } |
| 119 | + var mergedMessage json.RawMessage |
| 120 | + for _, options := range optionsList { |
| 121 | + mergedMessage, err = badjson.MergeJSON(globalCtx, options.options.RawMessage, mergedMessage, false) |
| 122 | + if err != nil { |
| 123 | + return option.PlainRuleSetCompat{}, E.Cause(err, "merge config at ", options.path) |
| 124 | + } |
| 125 | + } |
| 126 | + mergedOptions, err := json.UnmarshalExtendedContext[option.PlainRuleSetCompat](globalCtx, mergedMessage) |
| 127 | + if err != nil { |
| 128 | + return option.PlainRuleSetCompat{}, E.Cause(err, "unmarshal merged config") |
| 129 | + } |
| 130 | + mergedOptions.Version = optionVersion |
| 131 | + return mergedOptions, nil |
| 132 | +} |
| 133 | + |
| 134 | +func mergeRuleSet(outputPath string) error { |
| 135 | + mergedOptions, err := readRuleSetAndMerge() |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + buffer := new(bytes.Buffer) |
| 140 | + encoder := json.NewEncoder(buffer) |
| 141 | + encoder.SetIndent("", " ") |
| 142 | + err = encoder.Encode(mergedOptions) |
| 143 | + if err != nil { |
| 144 | + return E.Cause(err, "encode config") |
| 145 | + } |
| 146 | + if existsContent, err := os.ReadFile(outputPath); err != nil { |
| 147 | + if string(existsContent) == buffer.String() { |
| 148 | + return nil |
| 149 | + } |
| 150 | + } |
| 151 | + err = rw.MkdirParent(outputPath) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + err = os.WriteFile(outputPath, buffer.Bytes(), 0o644) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + outputPath, _ = filepath.Abs(outputPath) |
| 160 | + os.Stderr.WriteString(outputPath + "\n") |
| 161 | + return nil |
| 162 | +} |
0 commit comments