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

137 lines
3.2 KiB
Go
Raw 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() {
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", nil, "subscription URLs")
2024-10-21 03:56:12 -04:00
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", nil, "proxies share links")
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")
convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", nil, "rename proxies with regex")
convertCmd.Flags().BoolVarP(&group, "group", "g", false, "grouping proxies by country")
2024-03-19 09:02:53 -04:00
convertCmd.Flags().StringVarP(&groupType, "group-type", "G", "selector", "group type, selector or urltest")
convertCmd.Flags().StringVarP(&sortKey, "sort", "S", "tag", "sort key, tag or num")
convertCmd.Flags().StringVarP(&sortType, "sort-type", "T", "asc", "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 {
2024-09-19 06:12:24 -04:00
config = filepath.Join(wd, "github.com/nitezs/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) {
if len(subscriptions) == 0 {
subscriptions = cfg.Subscriptions
}
if len(proxies) == 0 {
proxies = cfg.Proxies
}
if template == "" {
template = cfg.Template
}
if delete == "" {
delete = cfg.Delete
}
if len(rename) == 0 {
rename = cfg.Rename
}
if groupType == "" {
groupType = cfg.GroupType
}
if sortKey == "" {
sortKey = cfg.SortKey
}
if sortType == "" {
sortType = cfg.SortType
}
if output == "" {
output = cfg.Output
}
2024-03-11 05:31:29 -04:00
}