mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-27 01:21:17 +00:00
update
This commit is contained in:
202
cmd/convert.go
Normal file
202
cmd/convert.go
Normal file
@@ -0,0 +1,202 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
. "sub2sing-box/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var convertCmd = &cobra.Command{
|
||||
Use: "convert",
|
||||
Long: "Convert common proxy format to json",
|
||||
Short: "Convert common proxy format to json",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
subscriptions, _ := cmd.Flags().GetStringSlice("subscription")
|
||||
proxies, _ := cmd.Flags().GetStringSlice("proxy")
|
||||
template, _ := cmd.Flags().GetString("template")
|
||||
if template == "" {
|
||||
result, err := ConvertSubscriptionsToJson(subscriptions)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(result)
|
||||
} else {
|
||||
config, err := ConvertWithTemplate(subscriptions, proxies, template)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
convertCmd.Flags().StringSliceP("subscription", "s", []string{}, "subscription urls")
|
||||
convertCmd.Flags().StringSliceP("proxy", "p", []string{}, "common proxies")
|
||||
convertCmd.Flags().StringP("template", "t", "", "path of template file")
|
||||
RootCmd.AddCommand(convertCmd)
|
||||
}
|
||||
|
||||
func Convert(urls []string, proxies []string) ([]model.Proxy, error) {
|
||||
proxyList := make([]model.Proxy, 0)
|
||||
newProxies, err := ConvertSubscriptionsToSProxy(urls)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proxyList = append(proxyList, newProxies...)
|
||||
for _, p := range proxies {
|
||||
proxy, err := ConvertCProxyToSProxy(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
}
|
||||
return proxyList, nil
|
||||
}
|
||||
|
||||
func ConvertWithTemplate(urls []string, proxies []string, template string) (model.Config, error) {
|
||||
proxyList := make([]model.Proxy, 0)
|
||||
newProxies, err := ConvertSubscriptionsToSProxy(urls)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
proxyList = append(proxyList, newProxies...)
|
||||
for _, p := range proxies {
|
||||
proxy, err := ConvertCProxyToSProxy(p)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
}
|
||||
config, err := ReadTemplate(template)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
ps, err := json.Marshal(proxyList)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
var newOutbounds []model.Outbound
|
||||
err = json.Unmarshal(ps, &newOutbounds)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
config.Outbounds = append(config.Outbounds, newOutbounds...)
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ConvertCProxyToSProxy(proxy string) (model.Proxy, error) {
|
||||
for prefix, parseFunc := range constant.ParserMap {
|
||||
if strings.HasPrefix(proxy, prefix) {
|
||||
proxy, err := parseFunc(proxy)
|
||||
if err != nil {
|
||||
return model.Proxy{}, err
|
||||
}
|
||||
return proxy, nil
|
||||
}
|
||||
}
|
||||
return model.Proxy{}, errors.New("Unknown proxy format")
|
||||
}
|
||||
|
||||
func ConvertCProxyToJson(proxy string) (string, error) {
|
||||
sProxy, err := ConvertCProxyToSProxy(proxy)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data, err := json.Marshal(&sProxy)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func FetchSubscription(url string, maxRetryTime int) (string, error) {
|
||||
retryTime := 0
|
||||
var err error
|
||||
for retryTime < maxRetryTime {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
retryTime++
|
||||
continue
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
retryTime++
|
||||
continue
|
||||
}
|
||||
return string(data), err
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
func ConvertSubscriptionsToSProxy(urls []string) ([]model.Proxy, error) {
|
||||
proxyList := make([]model.Proxy, 0)
|
||||
for _, url := range urls {
|
||||
data, err := FetchSubscription(url, 3)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
proxy, err := DecodeBase64(data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
proxies := strings.Split(proxy, "\n")
|
||||
for _, p := range proxies {
|
||||
for prefix, parseFunc := range constant.ParserMap {
|
||||
if strings.HasPrefix(p, prefix) {
|
||||
proxy, err := parseFunc(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return proxyList, nil
|
||||
}
|
||||
|
||||
func ConvertSubscriptionsToJson(urls []string) (string, error) {
|
||||
proxyList, err := ConvertSubscriptionsToSProxy(urls)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result, err := json.Marshal(proxyList)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func ReadTemplate(path string) (model.Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
var res model.Config
|
||||
err = json.Unmarshal(data, &res)
|
||||
if err != nil {
|
||||
return model.Config{}, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
8
cmd/root.go
Normal file
8
cmd/root.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package cmd
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var RootCmd = &cobra.Command{}
|
||||
|
||||
func init() {
|
||||
}
|
||||
87
cmd/url.go
87
cmd/url.go
@@ -1,87 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sub2sing-box/model"
|
||||
"sub2sing-box/parser"
|
||||
. "sub2sing-box/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func Url(cmd *cobra.Command, args []string) {
|
||||
proxyList := make([]model.Proxy, 0)
|
||||
if cmd.Flag("url").Changed {
|
||||
urls, _ := cmd.Flags().GetStringSlice("url")
|
||||
for _, url := range urls {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
proxy, err := DecodeBase64(string(data))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
proxies := strings.Split(proxy, "\n")
|
||||
for _, p := range proxies {
|
||||
if strings.HasPrefix(p, "ss://") {
|
||||
proxy, err := parser.ParseShadowsocks(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
} else if strings.HasPrefix(p, "vmess://") {
|
||||
proxy, err := parser.ParseVmess(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
} else if strings.HasPrefix(p, "trojan://") {
|
||||
proxy, err := parser.ParseTrojan(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
} else if strings.HasPrefix(p, "vless://") {
|
||||
proxy, err := parser.ParseVless(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
} else if strings.HasPrefix(p, "hysteria://") {
|
||||
proxy, err := parser.ParseHysteria(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
} else if strings.HasPrefix(p, "hy2://") || strings.HasPrefix(p, "hysteria2://") {
|
||||
proxy, err := parser.ParseHysteria2(p)
|
||||
if err != nil {
|
||||
fmt.Println(proxy)
|
||||
}
|
||||
proxyList = append(proxyList, proxy)
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := json.Marshal(proxyList)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
} else {
|
||||
fmt.Println(string(result))
|
||||
}
|
||||
} else {
|
||||
fmt.Println("No URLs provided")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user