mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-07-05 09:52:33 +08:00
重构部分代码
fix: vless ws 配置缺少 path 字段
This commit is contained in:
@ -9,7 +9,8 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
model2 "sub2sing-box/model"
|
||||
C "sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
"sub2sing-box/parser"
|
||||
"sub2sing-box/util"
|
||||
)
|
||||
@ -28,7 +29,7 @@ func Convert(
|
||||
result := ""
|
||||
var err error
|
||||
|
||||
proxyList, err := ConvertSubscriptionsToSProxy(subscriptions)
|
||||
outbounds, err := ConvertSubscriptionsToSProxy(subscriptions)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -37,18 +38,18 @@ func Convert(
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
proxyList = append(proxyList, p)
|
||||
outbounds = append(outbounds, p)
|
||||
}
|
||||
|
||||
if delete != "" {
|
||||
proxyList, err = DeleteProxy(proxyList, delete)
|
||||
outbounds, err = DeleteProxy(outbounds, delete)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range rename {
|
||||
proxyList, err = RenameProxy(proxyList, k, v)
|
||||
outbounds, err = RenameProxy(outbounds, k, v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -56,14 +57,14 @@ func Convert(
|
||||
|
||||
keep := make(map[int]bool)
|
||||
set := make(map[string]struct {
|
||||
Proxy model2.Proxy
|
||||
Proxy model.Outbound
|
||||
Count int
|
||||
})
|
||||
for i, p := range proxyList {
|
||||
for i, p := range outbounds {
|
||||
if _, exists := set[p.Tag]; !exists {
|
||||
keep[i] = true
|
||||
set[p.Tag] = struct {
|
||||
Proxy model2.Proxy
|
||||
Proxy model.Outbound
|
||||
Count int
|
||||
}{p, 0}
|
||||
} else {
|
||||
@ -71,32 +72,16 @@ func Convert(
|
||||
p2, _ := json.Marshal(set[p.Tag])
|
||||
if string(p1) != string(p2) {
|
||||
set[p.Tag] = struct {
|
||||
Proxy model2.Proxy
|
||||
Proxy model.Outbound
|
||||
Count int
|
||||
}{p, set[p.Tag].Count + 1}
|
||||
keep[i] = true
|
||||
proxyList[i].Tag = fmt.Sprintf("%s %d", p.Tag, set[p.Tag].Count)
|
||||
outbounds[i].Tag = fmt.Sprintf("%s %d", p.Tag, set[p.Tag].Count)
|
||||
} else {
|
||||
keep[i] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
var newProxyList []model2.Proxy
|
||||
for i, p := range proxyList {
|
||||
if keep[i] {
|
||||
newProxyList = append(newProxyList, p)
|
||||
}
|
||||
}
|
||||
proxyList = newProxyList
|
||||
var outbounds []model2.Outbound
|
||||
ps, err := json.Marshal(&proxyList)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = json.Unmarshal(ps, &outbounds)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if group {
|
||||
outbounds = AddCountryGroup(outbounds, groupType, sortKey, sortType)
|
||||
}
|
||||
@ -116,52 +101,65 @@ func Convert(
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func AddCountryGroup(proxies []model2.Outbound, groupType string, sortKey string, sortType string) []model2.Outbound {
|
||||
newGroup := make(map[string]model2.Outbound)
|
||||
func AddCountryGroup(proxies []model.Outbound, groupType string, sortKey string, sortType string) []model.Outbound {
|
||||
newGroup := make(map[string]model.Outbound)
|
||||
for _, p := range proxies {
|
||||
if p.Type != "selector" && p.Type != "urltest" {
|
||||
country := model2.GetContryName(p.Tag)
|
||||
if p.Type != C.TypeSelector && p.Type != C.TypeURLTest {
|
||||
country := model.GetContryName(p.Tag)
|
||||
if group, ok := newGroup[country]; ok {
|
||||
group.Outbounds = append(group.Outbounds, p.Tag)
|
||||
group.SetOutbounds(append(group.GetOutbounds(), p.Tag))
|
||||
newGroup[country] = group
|
||||
} else {
|
||||
newGroup[country] = model2.Outbound{
|
||||
Tag: country,
|
||||
Type: groupType,
|
||||
Outbounds: []string{p.Tag},
|
||||
InterruptExistConnections: true,
|
||||
if groupType == C.TypeSelector {
|
||||
newGroup[country] = model.Outbound{
|
||||
Tag: country,
|
||||
Type: groupType,
|
||||
SelectorOptions: model.SelectorOutboundOptions{
|
||||
Outbounds: []string{p.Tag},
|
||||
InterruptExistConnections: true,
|
||||
},
|
||||
}
|
||||
} else if groupType == C.TypeURLTest {
|
||||
newGroup[country] = model.Outbound{
|
||||
Tag: country,
|
||||
Type: groupType,
|
||||
URLTestOptions: model.URLTestOutboundOptions{
|
||||
Outbounds: []string{p.Tag},
|
||||
InterruptExistConnections: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var groups []model2.Outbound
|
||||
var groups []model.Outbound
|
||||
for _, p := range newGroup {
|
||||
groups = append(groups, p)
|
||||
}
|
||||
if sortType == "asc" {
|
||||
switch sortKey {
|
||||
case "tag":
|
||||
sort.Sort(model2.SortByTag(groups))
|
||||
sort.Sort(model.SortByTag(groups))
|
||||
case "num":
|
||||
sort.Sort(model2.SortByNumber(groups))
|
||||
sort.Sort(model.SortByNumber(groups))
|
||||
default:
|
||||
sort.Sort(model2.SortByTag(groups))
|
||||
sort.Sort(model.SortByTag(groups))
|
||||
}
|
||||
} else {
|
||||
switch sortKey {
|
||||
case "tag":
|
||||
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
|
||||
sort.Sort(sort.Reverse(model.SortByTag(groups)))
|
||||
case "num":
|
||||
sort.Sort(sort.Reverse(model2.SortByNumber(groups)))
|
||||
sort.Sort(sort.Reverse(model.SortByNumber(groups)))
|
||||
default:
|
||||
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
|
||||
sort.Sort(sort.Reverse(model.SortByTag(groups)))
|
||||
}
|
||||
}
|
||||
return append(proxies, groups...)
|
||||
}
|
||||
|
||||
func MergeTemplate(outbounds []model2.Outbound, template string) (string, error) {
|
||||
var config model2.Config
|
||||
func MergeTemplate(outbounds []model.Outbound, template string) (string, error) {
|
||||
var config model.Config
|
||||
var err error
|
||||
if strings.HasPrefix(template, "http") {
|
||||
data, err := util.Fetch(template, 3)
|
||||
@ -179,15 +177,15 @@ func MergeTemplate(outbounds []model2.Outbound, template string) (string, error)
|
||||
}
|
||||
}
|
||||
config, err = ReadTemplate(template)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
proxyTags := make([]string, 0)
|
||||
groupTags := make([]string, 0)
|
||||
groups := make(map[string]model2.Outbound)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
groups := make(map[string]model.Outbound)
|
||||
for _, p := range outbounds {
|
||||
if model2.IsCountryGroup(p.Tag) {
|
||||
if model.IsCountryGroup(p.Tag) {
|
||||
groupTags = append(groupTags, p.Tag)
|
||||
reg := regexp.MustCompile("[A-Za-z]{2}")
|
||||
country := reg.FindString(p.Tag)
|
||||
@ -198,22 +196,24 @@ func MergeTemplate(outbounds []model2.Outbound, template string) (string, error)
|
||||
}
|
||||
reg := regexp.MustCompile("<[A-Za-z]{2}>")
|
||||
for i, outbound := range config.Outbounds {
|
||||
var parsedOutbound []string = make([]string, 0)
|
||||
for _, o := range outbound.Outbounds {
|
||||
if o == "<all-proxy-tags>" {
|
||||
parsedOutbound = append(parsedOutbound, proxyTags...)
|
||||
} else if o == "<all-country-tags>" {
|
||||
parsedOutbound = append(parsedOutbound, groupTags...)
|
||||
} else if reg.MatchString(o) {
|
||||
country := strings.ToUpper(strings.Trim(reg.FindString(o), "<>"))
|
||||
if group, ok := groups[country]; ok {
|
||||
parsedOutbound = append(parsedOutbound, group.Outbounds...)
|
||||
if outbound.Type == C.TypeSelector || outbound.Type == C.TypeURLTest {
|
||||
var parsedOutbound []string = make([]string, 0)
|
||||
for _, o := range outbound.GetOutbounds() {
|
||||
if o == "<all-proxy-tags>" {
|
||||
parsedOutbound = append(parsedOutbound, proxyTags...)
|
||||
} else if o == "<all-country-tags>" {
|
||||
parsedOutbound = append(parsedOutbound, groupTags...)
|
||||
} else if reg.MatchString(o) {
|
||||
country := strings.ToUpper(strings.Trim(reg.FindString(o), "<>"))
|
||||
if group, ok := groups[country]; ok {
|
||||
parsedOutbound = append(parsedOutbound, group.GetOutbounds()...)
|
||||
}
|
||||
} else {
|
||||
parsedOutbound = append(parsedOutbound, o)
|
||||
}
|
||||
} else {
|
||||
parsedOutbound = append(parsedOutbound, o)
|
||||
}
|
||||
config.Outbounds[i].SetOutbounds(parsedOutbound)
|
||||
}
|
||||
config.Outbounds[i].Outbounds = parsedOutbound
|
||||
}
|
||||
config.Outbounds = append(config.Outbounds, outbounds...)
|
||||
data, err := json.Marshal(config)
|
||||
@ -223,17 +223,17 @@ func MergeTemplate(outbounds []model2.Outbound, template string) (string, error)
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func ConvertCProxyToSProxy(proxy string) (model2.Proxy, error) {
|
||||
func ConvertCProxyToSProxy(proxy string) (model.Outbound, error) {
|
||||
for prefix, parseFunc := range parser.ParserMap {
|
||||
if strings.HasPrefix(proxy, prefix) {
|
||||
proxy, err := parseFunc(proxy)
|
||||
if err != nil {
|
||||
return model2.Proxy{}, err
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
return proxy, nil
|
||||
}
|
||||
}
|
||||
return model2.Proxy{}, errors.New("Unknown proxy format")
|
||||
return model.Outbound{}, errors.New("Unknown proxy format")
|
||||
}
|
||||
|
||||
func ConvertCProxyToJson(proxy string) (string, error) {
|
||||
@ -248,8 +248,8 @@ func ConvertCProxyToJson(proxy string) (string, error) {
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func ConvertSubscriptionsToSProxy(urls []string) ([]model2.Proxy, error) {
|
||||
proxyList := make([]model2.Proxy, 0)
|
||||
func ConvertSubscriptionsToSProxy(urls []string) ([]model.Outbound, error) {
|
||||
proxyList := make([]model.Outbound, 0)
|
||||
for _, url := range urls {
|
||||
data, err := util.Fetch(url, 3)
|
||||
if err != nil {
|
||||
@ -287,25 +287,25 @@ func ConvertSubscriptionsToJson(urls []string) (string, error) {
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func ReadTemplate(path string) (model2.Config, error) {
|
||||
func ReadTemplate(path string) (model.Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return model2.Config{}, err
|
||||
return model.Config{}, err
|
||||
}
|
||||
var res model2.Config
|
||||
var res model.Config
|
||||
err = json.Unmarshal(data, &res)
|
||||
if err != nil {
|
||||
return model2.Config{}, err
|
||||
return model.Config{}, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func DeleteProxy(proxies []model2.Proxy, regex string) ([]model2.Proxy, error) {
|
||||
func DeleteProxy(proxies []model.Outbound, regex string) ([]model.Outbound, error) {
|
||||
reg, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var newProxies []model2.Proxy
|
||||
var newProxies []model.Outbound
|
||||
for _, p := range proxies {
|
||||
if !reg.MatchString(p.Tag) {
|
||||
newProxies = append(newProxies, p)
|
||||
@ -314,7 +314,7 @@ func DeleteProxy(proxies []model2.Proxy, regex string) ([]model2.Proxy, error) {
|
||||
return newProxies, nil
|
||||
}
|
||||
|
||||
func RenameProxy(proxies []model2.Proxy, regex string, replaceText string) ([]model2.Proxy, error) {
|
||||
func RenameProxy(proxies []model.Outbound, regex string, replaceText string) ([]model.Outbound, error) {
|
||||
reg, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user