1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 15:24:42 -05:00
sub2sing-box/cmd/convert.go

145 lines
3.4 KiB
Go
Raw Permalink Normal View History

2024-03-11 05:31:29 -04:00
package cmd
import (
"encoding/json"
2024-03-11 05:31:29 -04:00
"fmt"
2024-03-20 08:54:23 -04:00
"os"
"path/filepath"
2024-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/common"
"github.com/nitezs/sub2sing-box/model"
2024-03-22 04:10:15 -04:00
"github.com/spf13/cobra"
2024-03-11 05:31:29 -04:00
)
var (
subscriptions []string
proxies []string
template string
output string
delete string
rename map[string]string
group bool
groupType string
sortKey string
sortType string
config string
)
2024-03-11 11:39:58 -04:00
func init() {
2024-11-15 12:16:22 -05:00
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", make([]string, 0), "subscription URLs")
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", make([]string, 0), "proxies share links")
2024-10-21 03:56:12 -04:00
convertCmd.Flags().StringVarP(&template, "template", "t", "", "template file path or URL")
2024-03-11 11:39:58 -04:00
convertCmd.Flags().StringVarP(&output, "output", "o", "", "output file path")
2024-10-21 03:56:12 -04:00
convertCmd.Flags().StringVarP(&delete, "delete", "d", "", "delete proxies with regex")
2024-11-15 12:16:22 -05:00
convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", make(map[string]string), "rename proxies with regex")
2024-10-21 03:56:12 -04:00
convertCmd.Flags().BoolVarP(&group, "group", "g", false, "grouping proxies by country")
convertCmd.Flags().StringVarP(&groupType, "group-type", "G", "", "group type, selector or urltest")
convertCmd.Flags().StringVarP(&sortKey, "sort", "S", "", "sort key, tag or num")
convertCmd.Flags().StringVarP(&sortType, "sort-type", "T", "", "sort type, asc or desc")
2024-10-21 03:56:12 -04:00
convertCmd.Flags().StringVarP(&config, "config", "c", "", "configuration file path")
2024-03-11 11:39:58 -04:00
RootCmd.AddCommand(convertCmd)
}
2024-03-11 09:00:13 -04:00
2024-03-11 05:31:29 -04:00
var convertCmd = &cobra.Command{
Use: "convert",
2024-03-11 07:56:50 -04:00
Long: "Convert common proxy to sing-box proxy",
Short: "Convert common proxy to sing-box proxy",
Run: convertRun,
}
func convertRun(cmd *cobra.Command, args []string) {
loadConfig()
result, err := common.Convert(
subscriptions,
proxies,
template,
delete,
rename,
group,
groupType,
sortKey,
sortType,
)
if err != nil {
fmt.Println("Conversion error:", err)
return
}
if output != "" {
err = os.WriteFile(output, []byte(result), 0666)
2024-03-11 09:00:13 -04:00
if err != nil {
fmt.Println("Error writing to file:", err)
2024-03-11 09:00:13 -04:00
return
}
fmt.Println("Config has been saved in:", output)
} else {
fmt.Println(result)
}
}
func loadConfig() {
if config == "" {
if wd, err := os.Getwd(); err == nil {
config = filepath.Join(wd, "sub2sing-box.json")
if _, err := os.Stat(config); os.IsNotExist(err) {
2024-03-11 05:31:29 -04:00
return
}
2024-03-11 09:00:13 -04:00
} else {
fmt.Println("Error getting working directory:", err)
return
2024-03-11 05:31:29 -04:00
}
}
bytes, err := os.ReadFile(config)
if err != nil {
fmt.Println("Error reading config file:", err)
return
}
var cfg model.ConvertRequest
if err := json.Unmarshal(bytes, &cfg); err != nil {
fmt.Println("Error parsing config JSON:", err)
return
}
mergeConfig(cfg)
}
func mergeConfig(cfg model.ConvertRequest) {
subscriptions = append(subscriptions, cfg.Subscriptions...)
proxies = append(proxies, cfg.Proxies...)
if template == "" {
template = cfg.Template
}
if delete == "" {
delete = cfg.Delete
}
for k, v := range cfg.Rename {
rename[k] = v
}
if groupType == "" {
if cfg.GroupType != "" {
groupType = cfg.GroupType
} else {
groupType = "selector"
}
}
if sortKey == "" {
if cfg.SortKey != "" {
sortKey = cfg.SortKey
} else {
sortKey = "tag"
}
}
if sortType == "" {
if cfg.SortType != "" {
sortType = cfg.SortType
} else {
sortType = "asc"
}
}
if output == "" {
output = cfg.Output
}
2024-03-11 05:31:29 -04:00
}