1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 21:44:41 -05:00

The convert command has a new flag: config

🐛 Fix Fetch func cannot return err
🐛 Fix cannot read templates correctly
This commit is contained in:
Nite07 2024-05-23 16:57:19 +08:00
parent 9789354a51
commit 8f6e39b634
7 changed files with 117 additions and 43 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ dist
template.json template.json
.idea .idea
test test
sub2sing-box.json
config.json

View File

@ -2,8 +2,8 @@ package handler
import ( import (
"encoding/json" "encoding/json"
"sub2sing-box/api/model"
"sub2sing-box/common" "sub2sing-box/common"
"sub2sing-box/model"
"sub2sing-box/util" "sub2sing-box/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"

View File

@ -1,35 +1,42 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath"
"sub2sing-box/common" "sub2sing-box/common"
"sub2sing-box/model"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var subscriptions []string var (
var proxies []string subscriptions []string
var template string proxies []string
var output string template string
var delete string output string
var rename map[string]string delete string
var group bool rename map[string]string
var groupType string group bool
var sortKey string groupType string
var sortType string sortKey string
sortType string
config string
)
func init() { func init() {
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", []string{}, "subscription urls") convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", nil, "subscription URLs")
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", []string{}, "common proxies") convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", nil, "common proxies")
convertCmd.Flags().StringVarP(&template, "template", "t", "", "template file path") convertCmd.Flags().StringVarP(&template, "template", "t", "", "template file path")
convertCmd.Flags().StringVarP(&output, "output", "o", "", "output file path") convertCmd.Flags().StringVarP(&output, "output", "o", "", "output file path")
convertCmd.Flags().StringVarP(&delete, "delete", "d", "", "delete proxy with regex") convertCmd.Flags().StringVarP(&delete, "delete", "d", "", "delete proxy with regex")
convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", map[string]string{}, "rename proxy with regex") convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", nil, "rename proxy with regex")
convertCmd.Flags().BoolVarP(&group, "group", "g", false, "group proxies by country") convertCmd.Flags().BoolVarP(&group, "group", "g", false, "group proxies by country")
convertCmd.Flags().StringVarP(&groupType, "group-type", "G", "selector", "group type, selector or urltest") 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(&sortKey, "sort", "S", "tag", "sort key, tag or num")
convertCmd.Flags().StringVarP(&sortType, "sort-type", "T", "asc", "sort type, asc or desc") convertCmd.Flags().StringVarP(&sortType, "sort-type", "T", "asc", "sort type, asc or desc")
convertCmd.Flags().StringVarP(&config, "config", "c", "", "config file path")
RootCmd.AddCommand(convertCmd) RootCmd.AddCommand(convertCmd)
} }
@ -37,32 +44,95 @@ var convertCmd = &cobra.Command{
Use: "convert", Use: "convert",
Long: "Convert common proxy to sing-box proxy", Long: "Convert common proxy to sing-box proxy",
Short: "Convert common proxy to sing-box proxy", Short: "Convert common proxy to sing-box proxy",
Run: func(cmd *cobra.Command, args []string) { Run: convertRun,
result := "" }
var err error
result, err = common.Convert( func convertRun(cmd *cobra.Command, args []string) {
subscriptions, loadConfig()
proxies, result, err := common.Convert(
template, subscriptions,
delete, proxies,
rename, template,
group, delete,
groupType, rename,
sortKey, group,
sortType, groupType,
) sortKey,
sortType,
)
if err != nil {
fmt.Println("Conversion error:", err)
return
}
if output != "" {
err = os.WriteFile(output, []byte(result), 0666)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println("Error writing to file:", err)
return return
} }
if output != "" { fmt.Println("Config has been saved in:", output)
err = os.WriteFile(output, []byte(result), 0666) } else {
if err != nil { fmt.Println(result)
fmt.Println(err) }
}
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) {
return return
} }
} else { } else {
fmt.Println(string(result)) fmt.Println("Error getting working directory:", err)
return
} }
}, }
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 !group {
group = cfg.Group
}
if groupType == "" {
groupType = cfg.GroupType
}
if sortKey == "" {
sortKey = cfg.SortKey
}
if sortType == "" {
sortType = cfg.SortType
}
if output == "" {
output = cfg.Output
}
} }

View File

@ -176,8 +176,9 @@ func MergeTemplate(outbounds []model.Outbound, template string) (string, error)
} }
} else { } else {
if !strings.Contains(template, string(filepath.Separator)) { if !strings.Contains(template, string(filepath.Separator)) {
if _, err := os.Stat(template); os.IsNotExist(err) { path := filepath.Join("templates", template)
template = filepath.Join("templates", template) if _, err := os.Stat(path); err == nil {
template = path
} }
} }
config, err = ReadTemplate(template) config, err = ReadTemplate(template)

View File

@ -10,4 +10,5 @@ type ConvertRequest struct {
GroupType string `form:"group-type" json:"group-type"` GroupType string `form:"group-type" json:"group-type"`
SortKey string `form:"sort" json:"sort"` SortKey string `form:"sort" json:"sort"`
SortType string `form:"sort-type" json:"sort-type"` SortType string `form:"sort-type" json:"sort-type"`
Output string `json:"output"`
} }

View File

@ -191,9 +191,7 @@
"sniff_override_destination": true, "sniff_override_destination": true,
"strict_route": true, "strict_route": true,
"type": "tun", "type": "tun",
"stack": "mixed", "stack": "mixed"
"inet4_route_exclude_address": ["100.64.0.0/10"],
"inet6_route_exclude_address": ["fd7a:115c:a1e0::/48"]
} }
], ],
"outbounds": [ "outbounds": [

View File

@ -8,13 +8,15 @@ import (
func Fetch(url string, maxRetryTimes int) (string, error) { func Fetch(url string, maxRetryTimes int) (string, error) {
retryTime := 0 retryTime := 0
var err error var err error
var resp *http.Response
for retryTime < maxRetryTimes { for retryTime < maxRetryTimes {
resp, err := http.Get(url) resp, err = http.Get(url)
if err != nil { if err != nil {
retryTime++ retryTime++
continue continue
} }
data, err := io.ReadAll(resp.Body) var data []byte
data, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
retryTime++ retryTime++
continue continue