fix(proxy): improve proxy removal logic in build functions
This commit is contained in:
parent
94416d59a6
commit
6d13060aeb
@ -22,13 +22,21 @@ var shadowsocksSupportMethod = []string{"aes-128-gcm", "aes-192-gcm", "aes-256-g
|
||||
|
||||
func BuildSurfboard(servers proxy.Adapter, siteName string, user UserInfo) []byte {
|
||||
var proxies, proxyGroup string
|
||||
var removed []string
|
||||
for _, node := range servers.Proxies {
|
||||
if uri := buildProxy(node, user.UUID); uri != "" {
|
||||
proxies += uri
|
||||
} else {
|
||||
removed = append(removed, node.Name)
|
||||
}
|
||||
}
|
||||
|
||||
for _, group := range servers.Group {
|
||||
|
||||
if len(removed) > 0 {
|
||||
group.Proxies = tool.RemoveStringElement(group.Proxies, removed...)
|
||||
}
|
||||
|
||||
if group.Type == proxy.GroupTypeSelect {
|
||||
proxyGroup += fmt.Sprintf("%s = select, %s", group.Name, strings.Join(group.Proxies, ", ")) + "\r\n"
|
||||
} else if group.Type == proxy.GroupTypeURLTest {
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
"net/url"
|
||||
"strings"
|
||||
"text/template"
|
||||
@ -40,6 +41,7 @@ func NewSurge(adapter proxy.Adapter) *Surge {
|
||||
|
||||
func (m *Surge) Build(uuid, siteName string, user UserInfo) []byte {
|
||||
var proxies, proxyGroup, rules string
|
||||
var removed []string
|
||||
|
||||
for _, p := range m.Adapter.Proxies {
|
||||
switch p.Protocol {
|
||||
@ -51,9 +53,14 @@ func (m *Surge) Build(uuid, siteName string, user UserInfo) []byte {
|
||||
proxies += buildHysteria2(p, uuid)
|
||||
case "vmess":
|
||||
proxies += buildVMess(p, uuid)
|
||||
default:
|
||||
removed = append(removed, p.Name)
|
||||
}
|
||||
}
|
||||
for _, group := range m.Adapter.Group {
|
||||
if len(removed) > 0 {
|
||||
group.Proxies = tool.RemoveStringElement(group.Proxies, removed...)
|
||||
}
|
||||
if group.Type == proxy.GroupTypeSelect {
|
||||
proxyGroup += fmt.Sprintf("%s = select, %s", group.Name, strings.Join(group.Proxies, ", ")) + "\r\n"
|
||||
} else if group.Type == proxy.GroupTypeURLTest {
|
||||
|
||||
@ -196,24 +196,12 @@ func RemoveEmptyString(arr []string) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// RemoveElement 移除指定元素
|
||||
func RemoveElement(arr []string, element ...string) []string {
|
||||
var result []string
|
||||
for _, str := range arr {
|
||||
if !tool.Contains(element, str) {
|
||||
logger.Infof("Remove Element: %s", str)
|
||||
result = append(result, str)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func RemoveEmptyGroup(arr []proxy.Group) []proxy.Group {
|
||||
var result []proxy.Group
|
||||
var removeNames []string
|
||||
for _, group := range arr {
|
||||
if group.Name == "手动选择" {
|
||||
group.Proxies = RemoveElement(group.Proxies, removeNames...)
|
||||
group.Proxies = tool.RemoveStringElement(group.Proxies, removeNames...)
|
||||
}
|
||||
if len(group.Proxies) > 0 {
|
||||
result = append(result, group)
|
||||
|
||||
@ -128,3 +128,15 @@ func Contains[T comparable](slice []T, target T) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveStringElement 移除指定元素
|
||||
func RemoveStringElement(arr []string, element ...string) []string {
|
||||
var result []string
|
||||
for _, str := range arr {
|
||||
if !Contains(element, str) {
|
||||
logger.Infof("Remove Element: %s", str)
|
||||
result = append(result, str)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user