diff --git a/api/handler/convert.go b/api/handler/convert.go index d4b42be..0057815 100644 --- a/api/handler/convert.go +++ b/api/handler/convert.go @@ -3,8 +3,8 @@ package handler import ( "encoding/json" "sub2sing-box/api/model" - "sub2sing-box/internal" - "sub2sing-box/pkg/util" + iutil "sub2sing-box/internal/util" + putil "sub2sing-box/pkg/util" "github.com/gin-gonic/gin" ) @@ -17,7 +17,7 @@ func Convert(c *gin.Context) { }) return } - j, err := internal.DecodeBase64(c.Query("data")) + j, err := iutil.DecodeBase64(c.Query("data")) if err != nil { c.JSON(400, gin.H{ "error": "Invalid data", @@ -38,7 +38,7 @@ func Convert(c *gin.Context) { }) return } - result, err := util.Convert(data.Subscriptions, data.Proxies, data.Template, data.Delete, data.Rename) + result, err := putil.Convert(data.Subscriptions, data.Proxies, data.Template, data.Delete, data.Rename) if err != nil { c.JSON(400, gin.H{ "error": err.Error(), diff --git a/internal/base64.go b/internal/util/base64.go similarity index 96% rename from internal/base64.go rename to internal/util/base64.go index c12bfba..2d70b5e 100644 --- a/internal/base64.go +++ b/internal/util/base64.go @@ -1,4 +1,4 @@ -package internal +package util import ( "encoding/base64" diff --git a/internal/util/fetch.go b/internal/util/fetch.go new file mode 100644 index 0000000..c2cd15c --- /dev/null +++ b/internal/util/fetch.go @@ -0,0 +1,25 @@ +package util + +import ( + "io" + "net/http" +) + +func Fetch(url string, maxRetryTimes int) (string, error) { + retryTime := 0 + var err error + for retryTime < maxRetryTimes { + resp, err := http.Get(url) + if err != nil { + retryTime++ + continue + } + data, err := io.ReadAll(resp.Body) + if err != nil { + retryTime++ + continue + } + return string(data), err + } + return "", err +} diff --git a/pkg/parser/shadowsocks.go b/pkg/parser/shadowsocks.go index 398fc8f..7b28579 100644 --- a/pkg/parser/shadowsocks.go +++ b/pkg/parser/shadowsocks.go @@ -5,8 +5,8 @@ import ( "net/url" "strconv" "strings" - "sub2sing-box/internal" "sub2sing-box/internal/model" + "sub2sing-box/internal/util" ) func ParseShadowsocks(proxy string) (model.Proxy, error) { @@ -18,7 +18,7 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) { return model.Proxy{}, errors.New("invalid ss Url") } if !strings.Contains(parts[0], ":") { - decoded, err := internal.DecodeBase64(parts[0]) + decoded, err := util.DecodeBase64(parts[0]) if err != nil { return model.Proxy{}, errors.New("invalid ss Url" + err.Error()) } diff --git a/pkg/parser/vmess.go b/pkg/parser/vmess.go index 74b044b..a00ff00 100644 --- a/pkg/parser/vmess.go +++ b/pkg/parser/vmess.go @@ -6,15 +6,15 @@ import ( "net/url" "strconv" "strings" - "sub2sing-box/internal" "sub2sing-box/internal/model" + "sub2sing-box/internal/util" ) func ParseVmess(proxy string) (model.Proxy, error) { if !strings.HasPrefix(proxy, "vmess://") { return model.Proxy{}, errors.New("invalid vmess url") } - base64, err := internal.DecodeBase64(strings.TrimPrefix(proxy, "vmess://")) + base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://")) if err != nil { return model.Proxy{}, errors.New("invalid vmess url" + err.Error()) } diff --git a/pkg/util/convert.go b/pkg/util/convert.go index eb4d3bf..a973748 100644 --- a/pkg/util/convert.go +++ b/pkg/util/convert.go @@ -4,15 +4,13 @@ import ( "encoding/json" "errors" "fmt" - "io" - "net/http" "os" "path/filepath" "regexp" "strings" - . "sub2sing-box/internal" "sub2sing-box/internal/model" + "sub2sing-box/internal/util" "sub2sing-box/pkg/parser" ) @@ -98,12 +96,25 @@ func Convert(subscriptions []string, proxies []string, template string, delete s } func MergeTemplate(proxies []model.Proxy, template string) (string, error) { - if !strings.Contains(template, string(filepath.Separator)) { - if _, err := os.Stat(template); os.IsNotExist(err) { - template = filepath.Join("templates", template) + var config model.Config + var err error + if strings.HasPrefix(template, "http") { + data, err := util.Fetch(template, 3) + if err != nil { + return "", err } + err = json.Unmarshal([]byte(data), &config) + if err != nil { + return "", err + } + } else { + if !strings.Contains(template, string(filepath.Separator)) { + if _, err := os.Stat(template); os.IsNotExist(err) { + template = filepath.Join("templates", template) + } + } + config, err = ReadTemplate(template) } - config, err := ReadTemplate(template) proxyTags := make([]string, 0) if err != nil { return "", err @@ -165,33 +176,14 @@ func ConvertCProxyToJson(proxy string) (string, error) { return string(data), nil } -func FetchSubscription(url string, maxRetryTimes int) (string, error) { - retryTime := 0 - var err error - for retryTime < maxRetryTimes { - resp, err := http.Get(url) - if err != nil { - retryTime++ - continue - } - data, err := io.ReadAll(resp.Body) - if err != nil { - 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) + data, err := util.Fetch(url, 3) if err != nil { return nil, err } - proxy, err := DecodeBase64(data) + proxy, err := util.DecodeBase64(data) if err != nil { return nil, err }