mirror of
https://github.com/nitezs/sub2sing-box.git
synced 2024-12-23 21:44:41 -05:00
update: template可以接受网络文件
This commit is contained in:
parent
8b3c590364
commit
b05b7756a0
@ -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(),
|
||||
|
@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
25
internal/util/fetch.go
Normal file
25
internal/util/fetch.go
Normal 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
|
||||
}
|
@ -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())
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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) {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user