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

更改项目结构

This commit is contained in:
Nite07 2024-03-20 20:54:23 +08:00
parent 9e0068f4e1
commit eee361dcbf
27 changed files with 210 additions and 212 deletions

View File

@ -3,8 +3,8 @@ package handler
import (
"encoding/json"
"sub2sing-box/api/model"
iutil "sub2sing-box/internal/util"
putil "sub2sing-box/pkg/util"
"sub2sing-box/common"
putil "sub2sing-box/util"
"github.com/gin-gonic/gin"
)
@ -17,7 +17,7 @@ func Convert(c *gin.Context) {
})
return
}
j, err := iutil.DecodeBase64(c.Query("data"))
j, err := putil.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 := putil.Convert(
result, err := common.Convert(
data.Subscriptions,
data.Proxies,
data.Template,

View File

@ -2,10 +2,9 @@ package cmd
import (
"fmt"
"os"
. "sub2sing-box/pkg/util"
"github.com/spf13/cobra"
"os"
. "sub2sing-box/common"
)
var subscriptions []string

View File

@ -1,4 +1,4 @@
package util
package common
import (
"encoding/json"
@ -9,10 +9,9 @@ import (
"regexp"
"sort"
"strings"
"sub2sing-box/internal/model"
"sub2sing-box/internal/util"
"sub2sing-box/pkg/parser"
model2 "sub2sing-box/model"
"sub2sing-box/parser"
"sub2sing-box/util"
)
func Convert(
@ -57,14 +56,14 @@ func Convert(
keep := make(map[int]bool)
set := make(map[string]struct {
Proxy model.Proxy
Proxy model2.Proxy
Count int
})
for i, p := range proxyList {
if _, exists := set[p.Tag]; !exists {
keep[i] = true
set[p.Tag] = struct {
Proxy model.Proxy
Proxy model2.Proxy
Count int
}{p, 0}
} else {
@ -72,7 +71,7 @@ func Convert(
p2, _ := json.Marshal(set[p.Tag])
if string(p1) != string(p2) {
set[p.Tag] = struct {
Proxy model.Proxy
Proxy model2.Proxy
Count int
}{p, set[p.Tag].Count + 1}
keep[i] = true
@ -82,14 +81,14 @@ func Convert(
}
}
}
var newProxyList []model.Proxy
var newProxyList []model2.Proxy
for i, p := range proxyList {
if keep[i] {
newProxyList = append(newProxyList, p)
}
}
proxyList = newProxyList
var outbounds []model.Outbound
var outbounds []model2.Outbound
ps, err := json.Marshal(&proxyList)
if err != nil {
return "", err
@ -117,16 +116,16 @@ func Convert(
return string(result), nil
}
func AddCountryGroup(proxies []model.Outbound, groupType string, sortKey string, sortType string) []model.Outbound {
newGroup := make(map[string]model.Outbound)
func AddCountryGroup(proxies []model2.Outbound, groupType string, sortKey string, sortType string) []model2.Outbound {
newGroup := make(map[string]model2.Outbound)
for _, p := range proxies {
if p.Type != "selector" && p.Type != "urltest" {
country := model.GetContryName(p.Tag)
country := model2.GetContryName(p.Tag)
if group, ok := newGroup[country]; ok {
group.Outbounds = append(group.Outbounds, p.Tag)
newGroup[country] = group
} else {
newGroup[country] = model.Outbound{
newGroup[country] = model2.Outbound{
Tag: country,
Type: groupType,
Outbounds: []string{p.Tag},
@ -135,34 +134,34 @@ func AddCountryGroup(proxies []model.Outbound, groupType string, sortKey string,
}
}
}
var groups []model.Outbound
var groups []model2.Outbound
for _, p := range newGroup {
groups = append(groups, p)
}
if sortType == "asc" {
switch sortKey {
case "tag":
sort.Sort(model.SortByTag(groups))
sort.Sort(model2.SortByTag(groups))
case "num":
sort.Sort(model.SortByNumber(groups))
sort.Sort(model2.SortByNumber(groups))
default:
sort.Sort(model.SortByTag(groups))
sort.Sort(model2.SortByTag(groups))
}
} else {
switch sortKey {
case "tag":
sort.Sort(sort.Reverse(model.SortByTag(groups)))
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
case "num":
sort.Sort(sort.Reverse(model.SortByNumber(groups)))
sort.Sort(sort.Reverse(model2.SortByNumber(groups)))
default:
sort.Sort(sort.Reverse(model.SortByTag(groups)))
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
}
}
return append(proxies, groups...)
}
func MergeTemplate(outbounds []model.Outbound, template string) (string, error) {
var config model.Config
func MergeTemplate(outbounds []model2.Outbound, template string) (string, error) {
var config model2.Config
var err error
if strings.HasPrefix(template, "http") {
data, err := util.Fetch(template, 3)
@ -183,12 +182,12 @@ func MergeTemplate(outbounds []model.Outbound, template string) (string, error)
}
proxyTags := make([]string, 0)
groupTags := make([]string, 0)
groups := make(map[string]model.Outbound)
groups := make(map[string]model2.Outbound)
if err != nil {
return "", err
}
for _, p := range outbounds {
if model.IsCountryGroup(p.Tag) {
if model2.IsCountryGroup(p.Tag) {
groupTags = append(groupTags, p.Tag)
reg := regexp.MustCompile("[A-Za-z]{2}")
country := reg.FindString(p.Tag)
@ -224,17 +223,17 @@ func MergeTemplate(outbounds []model.Outbound, template string) (string, error)
return string(data), nil
}
func ConvertCProxyToSProxy(proxy string) (model.Proxy, error) {
func ConvertCProxyToSProxy(proxy string) (model2.Proxy, error) {
for prefix, parseFunc := range parser.ParserMap {
if strings.HasPrefix(proxy, prefix) {
proxy, err := parseFunc(proxy)
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
return proxy, nil
}
}
return model.Proxy{}, errors.New("Unknown proxy format")
return model2.Proxy{}, errors.New("Unknown proxy format")
}
func ConvertCProxyToJson(proxy string) (string, error) {
@ -249,8 +248,8 @@ func ConvertCProxyToJson(proxy string) (string, error) {
return string(data), nil
}
func ConvertSubscriptionsToSProxy(urls []string) ([]model.Proxy, error) {
proxyList := make([]model.Proxy, 0)
func ConvertSubscriptionsToSProxy(urls []string) ([]model2.Proxy, error) {
proxyList := make([]model2.Proxy, 0)
for _, url := range urls {
data, err := util.Fetch(url, 3)
if err != nil {
@ -288,25 +287,25 @@ func ConvertSubscriptionsToJson(urls []string) (string, error) {
return string(result), nil
}
func ReadTemplate(path string) (model.Config, error) {
func ReadTemplate(path string) (model2.Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return model.Config{}, err
return model2.Config{}, err
}
var res model.Config
var res model2.Config
err = json.Unmarshal(data, &res)
if err != nil {
return model.Config{}, err
return model2.Config{}, err
}
return res, nil
}
func DeleteProxy(proxies []model.Proxy, regex string) ([]model.Proxy, error) {
func DeleteProxy(proxies []model2.Proxy, regex string) ([]model2.Proxy, error) {
reg, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
var newProxies []model.Proxy
var newProxies []model2.Proxy
for _, p := range proxies {
if !reg.MatchString(p.Tag) {
newProxies = append(newProxies, p)
@ -315,7 +314,7 @@ func DeleteProxy(proxies []model.Proxy, regex string) ([]model.Proxy, error) {
return newProxies, nil
}
func RenameProxy(proxies []model.Proxy, regex string, replaceText string) ([]model.Proxy, error) {
func RenameProxy(proxies []model2.Proxy, regex string, replaceText string) ([]model2.Proxy, error) {
reg, err := regexp.Compile(regex)
if err != nil {
return nil, err

View File

@ -5,7 +5,7 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
model2 "sub2sing-box/model"
)
//hysteria://host:port?protocol=udp&auth=123456&peer=sni.domain&insecure=1&upmbps=100&downmbps=100&alpn=hysteria&obfs=xplus&obfsParam=123456#remarks
@ -23,23 +23,23 @@ import (
//- obfsParam: Obfuscation password (optional)
//- remarks: remarks (optional)
func ParseHysteria(proxy string) (model.Proxy, error) {
func ParseHysteria(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "hysteria://") {
return model.Proxy{}, errors.New("invalid hysteria Url")
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria://"), "?", 2)
serverInfo := strings.SplitN(parts[0], ":", 2)
if len(serverInfo) != 2 {
return model.Proxy{}, errors.New("invalid hysteria Url")
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
params, err := url.ParseQuery(parts[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria Url")
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
host := serverInfo[0]
port, err := strconv.Atoi(serverInfo[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria Url")
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
protocol := params.Get("protocol")
auth := params.Get("auth")
@ -64,12 +64,12 @@ func ParseHysteria(proxy string) (model.Proxy, error) {
}
insecureBool, err := strconv.ParseBool(insecure)
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria Url")
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
result := model.Proxy{
result := model2.Proxy{
Type: "hysteria",
Tag: remarks,
Hysteria: model.Hysteria{
Hysteria: model2.Hysteria{
Server: host,
ServerPort: uint16(port),
Up: upmbps,
@ -77,7 +77,7 @@ func ParseHysteria(proxy string) (model.Proxy, error) {
Auth: []byte(auth),
Obfs: obfs,
Network: protocol,
TLS: &model.OutboundTLSOptions{
TLS: &model2.OutboundTLSOptions{
Enabled: true,
Insecure: insecureBool,
ALPN: alpn,

View File

@ -5,14 +5,14 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
model2 "sub2sing-box/model"
)
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
func ParseHysteria2(proxy string) (model.Proxy, error) {
func ParseHysteria2(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
serverInfo := strings.SplitN(parts[1], "/?", 2)
@ -20,32 +20,32 @@ func ParseHysteria2(proxy string) (model.Proxy, error) {
if len(serverAndPort) == 1 {
serverAndPort = append(serverAndPort, "443")
} else if len(serverAndPort) != 2 {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
params, err := url.ParseQuery(serverInfo[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
port, err := strconv.Atoi(serverAndPort[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
remarks := params.Get("name")
server := serverAndPort[0]
password := parts[0]
network := params.Get("network")
result := model.Proxy{
result := model2.Proxy{
Type: "hysteria2",
Tag: remarks,
Hysteria2: model.Hysteria2{
Hysteria2: model2.Hysteria2{
Server: server,
ServerPort: uint16(port),
Password: password,
Obfs: &model.Hysteria2Obfs{
Obfs: &model2.Hysteria2Obfs{
Type: params.Get("obfs"),
Password: params.Get("obfs-password"),
},
TLS: &model.OutboundTLSOptions{
TLS: &model2.OutboundTLSOptions{
Enabled: params.Get("pinSHA256") != "",
Insecure: params.Get("insecure") == "1",
ServerName: params.Get("sni"),

View File

@ -1,7 +1,7 @@
package parser
import (
"sub2sing-box/internal/model"
"sub2sing-box/model"
)
var ParserMap map[string]func(string) (model.Proxy, error) = map[string]func(string) (model.Proxy, error){

View File

@ -5,43 +5,43 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
"sub2sing-box/internal/util"
model2 "sub2sing-box/model"
"sub2sing-box/util"
)
func ParseShadowsocks(proxy string) (model.Proxy, error) {
func ParseShadowsocks(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "ss://") {
return model.Proxy{}, errors.New("invalid ss Url")
return model2.Proxy{}, errors.New("invalid ss Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
if len(parts) != 2 {
return model.Proxy{}, errors.New("invalid ss Url")
return model2.Proxy{}, errors.New("invalid ss Url")
}
if !strings.Contains(parts[0], ":") {
decoded, err := util.DecodeBase64(parts[0])
if err != nil {
return model.Proxy{}, errors.New("invalid ss Url" + err.Error())
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
parts[0] = decoded
}
credentials := strings.SplitN(parts[0], ":", 2)
if len(credentials) != 2 {
return model.Proxy{}, errors.New("invalid ss Url")
return model2.Proxy{}, errors.New("invalid ss Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
if len(serverAndPort) != 2 {
return model.Proxy{}, errors.New("invalid ss Url")
return model2.Proxy{}, errors.New("invalid ss Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model.Proxy{}, errors.New("invalid ss Url" + err.Error())
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
remarks := ""
if len(serverInfo) == 2 {
unescape, err := url.QueryUnescape(serverInfo[1])
if err != nil {
return model.Proxy{}, errors.New("invalid ss Url" + err.Error())
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
remarks = strings.TrimSpace(unescape)
} else {
@ -50,10 +50,10 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) {
method := credentials[0]
password := credentials[1]
server := strings.TrimSpace(serverAndPort[0])
result := model.Proxy{
result := model2.Proxy{
Type: "shadowsocks",
Tag: remarks,
Shadowsocks: model.Shadowsocks{
Shadowsocks: model2.Shadowsocks{
Method: method,
Password: password,
Server: server,

View File

@ -5,30 +5,30 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
model2 "sub2sing-box/model"
)
func ParseTrojan(proxy string) (model.Proxy, error) {
func ParseTrojan(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "trojan://") {
return model.Proxy{}, errors.New("invalid trojan Url")
return model2.Proxy{}, errors.New("invalid trojan Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
if len(parts) != 2 {
return model.Proxy{}, errors.New("invalid trojan Url")
return model2.Proxy{}, errors.New("invalid trojan Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
if len(serverAndPort) != 2 {
return model.Proxy{}, errors.New("invalid trojan Url")
return model2.Proxy{}, errors.New("invalid trojan Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
remarks := ""
if len(serverInfo) == 2 {
@ -38,10 +38,10 @@ func ParseTrojan(proxy string) (model.Proxy, error) {
}
server := strings.TrimSpace(serverAndPort[0])
password := strings.TrimSpace(parts[0])
result := model.Proxy{
result := model2.Proxy{
Type: "trojan",
Tag: remarks,
Trojan: model.Trojan{
Trojan: model2.Trojan{
Server: server,
ServerPort: uint16(port),
Password: password,
@ -55,31 +55,31 @@ func ParseTrojan(proxy string) (model.Proxy, error) {
} else {
alpn = nil
}
result.Trojan.TLS = &model.OutboundTLSOptions{
result.Trojan.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: params.Get("sni"),
}
}
if params.Get("security") == "reality" {
result.Trojan.TLS = &model.OutboundTLSOptions{
result.Trojan.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ServerName: params.Get("sni"),
Reality: &model.OutboundRealityOptions{
Reality: &model2.OutboundRealityOptions{
Enabled: true,
PublicKey: params.Get("pbk"),
ShortID: params.Get("sid"),
},
UTLS: &model.OutboundUTLSOptions{
UTLS: &model2.OutboundUTLSOptions{
Enabled: params.Get("fp") != "",
Fingerprint: params.Get("fp"),
},
}
}
if params.Get("type") == "ws" {
result.Trojan.Transport = &model.V2RayTransportOptions{
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
WebsocketOptions: model2.V2RayWebsocketOptions{
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
@ -88,24 +88,24 @@ func ParseTrojan(proxy string) (model.Proxy, error) {
}
}
if params.Get("type") == "http" {
result.Trojan.Transport = &model.V2RayTransportOptions{
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
HTTPOptions: model2.V2RayHTTPOptions{
Host: []string{params.Get("host")},
Path: params.Get("path"),
},
}
}
if params.Get("type") == "quic" {
result.Trojan.Transport = &model.V2RayTransportOptions{
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
QUICOptions: model2.V2RayQUICOptions{},
}
}
if params.Get("type") == "grpc" {
result.Trojan.Transport = &model.V2RayTransportOptions{
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
GRPCOptions: model2.V2RayGRPCOptions{
ServiceName: params.Get("serviceName"),
},
}

View File

@ -5,30 +5,30 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
model2 "sub2sing-box/model"
)
func ParseVless(proxy string) (model.Proxy, error) {
func ParseVless(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "vless://") {
return model.Proxy{}, errors.New("invalid vless Url")
return model2.Proxy{}, errors.New("invalid vless Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
if len(parts) != 2 {
return model.Proxy{}, errors.New("invalid vless Url")
return model2.Proxy{}, errors.New("invalid vless Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
if len(serverAndPort) != 2 {
return model.Proxy{}, errors.New("invalid vless Url")
return model2.Proxy{}, errors.New("invalid vless Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
remarks := ""
if len(serverInfo) == 2 {
@ -37,21 +37,21 @@ func ParseVless(proxy string) (model.Proxy, error) {
} else {
remarks, err = url.QueryUnescape(serverInfo[1])
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
}
} else {
remarks, err = url.QueryUnescape(serverAndPort[0])
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
}
server := strings.TrimSpace(serverAndPort[0])
uuid := strings.TrimSpace(parts[0])
result := model.Proxy{
result := model2.Proxy{
Type: "vless",
Tag: remarks,
VLESS: model.VLESS{
VLESS: model2.VLESS{
Server: server,
ServerPort: uint16(port),
UUID: uuid,
@ -65,7 +65,7 @@ func ParseVless(proxy string) (model.Proxy, error) {
} else {
alpn = nil
}
result.VLESS.TLS = &model.OutboundTLSOptions{
result.VLESS.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
Insecure: params.Get("allowInsecure") == "1",
@ -78,14 +78,14 @@ func ParseVless(proxy string) (model.Proxy, error) {
} else {
alpn = nil
}
result.VLESS.TLS = &model.OutboundTLSOptions{
result.VLESS.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ServerName: params.Get("sni"),
UTLS: &model.OutboundUTLSOptions{
UTLS: &model2.OutboundUTLSOptions{
Enabled: params.Get("fp") != "",
Fingerprint: params.Get("fp"),
},
Reality: &model.OutboundRealityOptions{
Reality: &model2.OutboundRealityOptions{
Enabled: true,
PublicKey: params.Get("pbk"),
ShortID: params.Get("sid"),
@ -94,9 +94,9 @@ func ParseVless(proxy string) (model.Proxy, error) {
}
}
if params.Get("type") == "ws" {
result.VLESS.Transport = &model.V2RayTransportOptions{
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
WebsocketOptions: model2.V2RayWebsocketOptions{
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
@ -105,15 +105,15 @@ func ParseVless(proxy string) (model.Proxy, error) {
}
}
if params.Get("type") == "quic" {
result.VLESS.Transport = &model.V2RayTransportOptions{
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
QUICOptions: model2.V2RayQUICOptions{},
}
}
if params.Get("type") == "grpc" {
result.VLESS.Transport = &model.V2RayTransportOptions{
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
GRPCOptions: model2.V2RayGRPCOptions{
ServiceName: params.Get("serviceName"),
},
}
@ -121,11 +121,11 @@ func ParseVless(proxy string) (model.Proxy, error) {
if params.Get("type") == "http" {
host, err := url.QueryUnescape(params.Get("host"))
if err != nil {
return model.Proxy{}, err
return model2.Proxy{}, err
}
result.VLESS.Transport = &model.V2RayTransportOptions{
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
HTTPOptions: model2.V2RayHTTPOptions{
Host: strings.Split(host, ","),
},
}

View File

@ -6,29 +6,29 @@ import (
"net/url"
"strconv"
"strings"
"sub2sing-box/internal/model"
"sub2sing-box/internal/util"
model2 "sub2sing-box/model"
"sub2sing-box/util"
)
func ParseVmess(proxy string) (model.Proxy, error) {
func ParseVmess(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "vmess://") {
return model.Proxy{}, errors.New("invalid vmess url")
return model2.Proxy{}, errors.New("invalid vmess url")
}
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
var vmess model.VmessJson
var vmess model2.VmessJson
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
port := 0
switch vmess.Port.(type) {
case string:
port, err = strconv.Atoi(vmess.Port.(string))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
port = int(vmess.Port.(float64))
@ -38,7 +38,7 @@ func ParseVmess(proxy string) (model.Proxy, error) {
case string:
aid, err = strconv.Atoi(vmess.Aid.(string))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
aid = int(vmess.Aid.(float64))
@ -52,10 +52,10 @@ func ParseVmess(proxy string) (model.Proxy, error) {
name = vmess.Ps
}
result := model.Proxy{
result := model2.Proxy{
Type: "vmess",
Tag: name,
VMess: model.VMess{
VMess: model2.VMess{
Server: vmess.Add,
ServerPort: uint16(port),
UUID: vmess.Id,
@ -71,9 +71,9 @@ func ParseVmess(proxy string) (model.Proxy, error) {
} else {
alpn = nil
}
tls := model.OutboundTLSOptions{
tls := model2.OutboundTLSOptions{
Enabled: true,
UTLS: &model.OutboundUTLSOptions{
UTLS: &model2.OutboundUTLSOptions{
Fingerprint: vmess.Fp,
},
ALPN: alpn,
@ -88,13 +88,13 @@ func ParseVmess(proxy string) (model.Proxy, error) {
if vmess.Host == "" {
vmess.Host = vmess.Add
}
ws := model.V2RayWebsocketOptions{
ws := model2.V2RayWebsocketOptions{
Path: vmess.Path,
Headers: map[string]string{
"Host": vmess.Host,
},
}
transport := model.V2RayTransportOptions{
transport := model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: ws,
}
@ -102,8 +102,8 @@ func ParseVmess(proxy string) (model.Proxy, error) {
}
if vmess.Net == "quic" {
quic := model.V2RayQUICOptions{}
transport := model.V2RayTransportOptions{
quic := model2.V2RayQUICOptions{}
transport := model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: quic,
}
@ -111,11 +111,11 @@ func ParseVmess(proxy string) (model.Proxy, error) {
}
if vmess.Net == "grpc" {
grpc := model.V2RayGRPCOptions{
grpc := model2.V2RayGRPCOptions{
ServiceName: vmess.Path,
PermitWithoutStream: true,
}
transport := model.V2RayTransportOptions{
transport := model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: grpc,
}
@ -123,11 +123,11 @@ func ParseVmess(proxy string) (model.Proxy, error) {
}
if vmess.Net == "h2" {
httpOps := model.V2RayHTTPOptions{
httpOps := model2.V2RayHTTPOptions{
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
transport := model.V2RayTransportOptions{
transport := model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: httpOps,
}