1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 13:14:42 -05:00

update: template可以接受网络文件

This commit is contained in:
Nite07 2024-03-19 17:01:53 +08:00
parent 8b3c590364
commit b05b7756a0
6 changed files with 54 additions and 37 deletions

View File

@ -3,8 +3,8 @@ package handler
import ( import (
"encoding/json" "encoding/json"
"sub2sing-box/api/model" "sub2sing-box/api/model"
"sub2sing-box/internal" iutil "sub2sing-box/internal/util"
"sub2sing-box/pkg/util" putil "sub2sing-box/pkg/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -17,7 +17,7 @@ func Convert(c *gin.Context) {
}) })
return return
} }
j, err := internal.DecodeBase64(c.Query("data")) j, err := iutil.DecodeBase64(c.Query("data"))
if err != nil { if err != nil {
c.JSON(400, gin.H{ c.JSON(400, gin.H{
"error": "Invalid data", "error": "Invalid data",
@ -38,7 +38,7 @@ func Convert(c *gin.Context) {
}) })
return 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 { if err != nil {
c.JSON(400, gin.H{ c.JSON(400, gin.H{
"error": err.Error(), "error": err.Error(),

View File

@ -1,4 +1,4 @@
package internal package util
import ( import (
"encoding/base64" "encoding/base64"

25
internal/util/fetch.go Normal file
View File

@ -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
}

View File

@ -5,8 +5,8 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sub2sing-box/internal"
"sub2sing-box/internal/model" "sub2sing-box/internal/model"
"sub2sing-box/internal/util"
) )
func ParseShadowsocks(proxy string) (model.Proxy, error) { 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") return model.Proxy{}, errors.New("invalid ss Url")
} }
if !strings.Contains(parts[0], ":") { if !strings.Contains(parts[0], ":") {
decoded, err := internal.DecodeBase64(parts[0]) decoded, err := util.DecodeBase64(parts[0])
if err != nil { if err != nil {
return model.Proxy{}, errors.New("invalid ss Url" + err.Error()) return model.Proxy{}, errors.New("invalid ss Url" + err.Error())
} }

View File

@ -6,15 +6,15 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sub2sing-box/internal"
"sub2sing-box/internal/model" "sub2sing-box/internal/model"
"sub2sing-box/internal/util"
) )
func ParseVmess(proxy string) (model.Proxy, error) { func ParseVmess(proxy string) (model.Proxy, error) {
if !strings.HasPrefix(proxy, "vmess://") { if !strings.HasPrefix(proxy, "vmess://") {
return model.Proxy{}, errors.New("invalid vmess url") 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 { if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error()) return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
} }

View File

@ -4,15 +4,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
. "sub2sing-box/internal"
"sub2sing-box/internal/model" "sub2sing-box/internal/model"
"sub2sing-box/internal/util"
"sub2sing-box/pkg/parser" "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) { func MergeTemplate(proxies []model.Proxy, template string) (string, error) {
if !strings.Contains(template, string(filepath.Separator)) { var config model.Config
if _, err := os.Stat(template); os.IsNotExist(err) { var err error
template = filepath.Join("templates", template) 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) proxyTags := make([]string, 0)
if err != nil { if err != nil {
return "", err return "", err
@ -165,33 +176,14 @@ func ConvertCProxyToJson(proxy string) (string, error) {
return string(data), nil 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) { func ConvertSubscriptionsToSProxy(urls []string) ([]model.Proxy, error) {
proxyList := make([]model.Proxy, 0) proxyList := make([]model.Proxy, 0)
for _, url := range urls { for _, url := range urls {
data, err := FetchSubscription(url, 3) data, err := util.Fetch(url, 3)
if err != nil { if err != nil {
return nil, err return nil, err
} }
proxy, err := DecodeBase64(data) proxy, err := util.DecodeBase64(data)
if err != nil { if err != nil {
return nil, err return nil, err
} }