feat(loon): add Loon configuration template and enhance proxy handling

This commit is contained in:
Chang lue Tsen 2025-07-23 05:29:36 -04:00 committed by Leif Draven
parent f4cd567492
commit f3a8f94b4b
4 changed files with 100 additions and 2 deletions

View File

@ -236,6 +236,7 @@ func (l *SubscribeLogic) buildClientConfig(req *types.SubscribeRequest, userSub
})
case "loon":
resp = proxyManager.BuildLoon(userSub.UUID)
l.setLoonHeaders()
case "surfboard":
subsURL := l.getSubscribeURL(userSub.Token, "surfboard")
resp = proxyManager.BuildSurfboard(l.svc.Config.Site.SiteName, surfboard.UserInfo{
@ -286,6 +287,11 @@ func (l *SubscribeLogic) setSurgeHeaders() {
l.ctx.Header("Content-Type", "application/octet-stream; charset=UTF-8")
}
func (l *SubscribeLogic) setLoonHeaders() {
l.ctx.Header("content-disposition", fmt.Sprintf("attachment;filename*=UTF-8''%s.conf", url.QueryEscape(l.svc.Config.Site.SiteName)))
l.ctx.Header("Content-Type", "application/octet-stream; charset=UTF-8")
}
func (l *SubscribeLogic) getSubscribeURL(token, flag string) string {
if l.svc.Config.Subscribe.PanDomain {
return fmt.Sprintf("https://%s", l.ctx.Request.Host)

View File

@ -1,27 +1,61 @@
package loon
import (
"bytes"
"embed"
"strings"
"text/template"
"github.com/perfect-panel/server/pkg/adapter/proxy"
"github.com/perfect-panel/server/pkg/logger"
)
//go:embed *.tpl
var configFiles embed.FS
func BuildLoon(servers []proxy.Proxy, uuid string) []byte {
uri := ""
nodes := make([]string, 0)
for _, s := range servers {
switch s.Protocol {
case "vmess":
nodes = append(nodes, s.Name)
uri += buildVMess(s, uuid)
case "shadowsocks":
nodes = append(nodes, s.Name)
uri += buildShadowsocks(s, uuid)
case "trojan":
nodes = append(nodes, s.Name)
uri += buildTrojan(s, uuid)
case "vless":
nodes = append(nodes, s.Name)
uri += buildVless(s, uuid)
case "hysteria2":
nodes = append(nodes, s.Name)
uri += buildHysteria2(s, uuid)
default:
continue
}
}
file, err := configFiles.ReadFile("default.tpl")
if err != nil {
logger.Errorf("read default surfboard config error: %v", err.Error())
return nil
}
// replace template
tpl, err := template.New("default").Parse(string(file))
if err != nil {
logger.Errorf("read default surfboard config error: %v", err.Error())
return nil
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, map[string]interface{}{
"Proxies": uri,
"Nodes": strings.Join(nodes, ","),
}); err != nil {
logger.Errorf("Execute Loon template error: %v", err.Error())
return nil
}
return []byte(uri)
return buf.Bytes()
}

View File

@ -0,0 +1,58 @@
[General]
ipv6-vif = auto
ip-mode = dual
skip-proxy = 192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,127.0.0.0/8,localhost,*.local
bypass-tun = 192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,127.0.0.0/8,localhost,*.local
dns-server = system,119.29.29.29,223.5.5.5
hijack-dns = 8.8.8.8:53,8.8.4.4:53,1.1.1.1:53,1.0.0.1:53
allow-wifi-access = true
wifi-access-http-port = 6888
wifi-access-socks5-port = 6889
proxy-test-url = http://bing.com/generate_204
internet-test-url = http://wifi.vivo.com.cn/generate_204
test-timeout = 5
interface-mode = auto
[Proxy]
{{.Proxies}}
[Proxy Group]
🚀 Proxy = select,🌏 Auto,{{.Nodes}}
🌏 Auto = fallback,{{.Nodes}},interval = 600,max-timeout = 3000
🍎 Apple = select,🚀 Proxy,🎯 Direct,{{.Nodes}}
🔍 Google = select,🚀 Proxy,{{.Nodes}}
🪟 Microsoft = select,🚀 Proxy,🎯 Direct,{{.Nodes}}
📠 X = select,🚀 Proxy,{{.Nodes}}
🤖 AI = select,🚀 Proxy,🎯 Direct,{{.Nodes}}
📟 Telegram = select,🚀 Proxy,{{.Nodes}}
📺 YouTube = select,🚀 Proxy,{{.Nodes}}
🇨🇳 China = select,🎯 Direct,🚀 Proxy,{{.Nodes}}
🐠 Final = select,🚀 Proxy,🎯 Direct,{{.Nodes}}
🎯 Direct = select,DIRECT
[Remote Rule]
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Apple/Apple.list, policy=🍎 Apple, tag=Apple, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Apple/Apple_Domain.list, policy=🍎 Apple, tag=Apple_Domain, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Google/Google.list, policy=🔍 Google, tag=Google, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Microsoft/Microsoft.list, policy=🪟 Microsoft, tag=Microsoft, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Twitter/Twitter.list, policy=📠 X, tag=X, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/OpenAI/OpenAI.list, policy=🤖 AI, tag=OpenAI, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Telegram/Telegram.list, policy=📟 Telegram, tag=Telegram, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/YouTube/YouTube.list, policy=📺 YouTube, tag=YouTube, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/YouTubeMusic/YouTubeMusic.list, policy=📺 YouTube, tag=YouTubeMusic, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Global/Global.list, policy=🚀 Proxy, tag=Global, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Global/Global_Domain.list, policy=🚀 Proxy, tag=Global_Domain, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/ChinaMax/ChinaMax.list, policy=🇨🇳 China, tag=ChinaMax, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/ChinaMax/ChinaMax_Domain.list, policy=🇨🇳 China, tag=ChinaMax_Domain, enabled=true
https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Loon/Lan/Lan.list, policy=🎯 Direct, tag=LAN, enabled=true
[Rule]
GEOIP,CN,🇨🇳 China
FINAL,🐠 Final
[Rewrite]
# Redirect Google Service
^https?:\/\/(www.)?g\.cn 302 https://www.google.com
^https?:\/\/(www.)?google\.cn 302 https://www.google.com
# Redirect Githubusercontent
^https://.*\.githubusercontent\.com\/ header-replace Accept-Language en-us

View File

@ -67,7 +67,7 @@ func BuildSurfboard(servers proxy.Adapter, siteName string, user UserInfo) []byt
total := traffic.AutoConvert(user.TotalTraffic, false)
unusedTraffic := traffic.AutoConvert(user.TotalTraffic-user.Upload-user.Download, false)
// query Host
if err := tpl.Execute(&buf, map[string]interface{}{
if err = tpl.Execute(&buf, map[string]interface{}{
"Proxies": proxies,
"ProxyGroup": proxyGroup,
"SubscribeURL": user.SubscribeURL,