Files
mirror-3x-ui/internal/sub/loon.go
T
Hamed 4a680cb859 fix(sub): export Loon REALITY with the Chrome 147 profile (#7248)
* fix(sub): export Loon REALITY with the Chrome 147 profile

* fix(sub): detect Loon on the default subscription URL

* docs(sub): translate the Loon REALITY import section into fa, ru and zh

The Loon section added to the English REALITY page had no counterpart in
the other three locales, so readers of those pages never learned that the
default subscription URL now serves Loon's native format, or that
format=loon and format=raw select it by hand.

---------

Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-11-30 22:54:25 +03:30

91 lines
2.5 KiB
Go

package sub
import (
"fmt"
"net/url"
"strconv"
"strings"
"unicode"
"github.com/google/uuid"
)
// loonSubscription keeps existing URI entries and renders eligible REALITY TCP nodes in Loon's native format.
func loonSubscription(links []string) string {
var out strings.Builder
out.WriteString("[Proxy]\n")
usedNames := make(map[string]bool)
for _, linksForClient := range links {
for line := range strings.SplitSeq(linksForClient, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
name, node, ok := loonRealityNode(line)
if ok {
base := name
for suffix := 2; usedNames[name]; suffix++ {
name = base + " (" + strconv.Itoa(suffix) + ")"
}
usedNames[name] = true
line = name + " = " + node
}
out.WriteString(line)
out.WriteByte('\n')
}
}
return out.String()
}
func loonRealityNode(raw string) (name, node string, ok bool) {
u, err := url.Parse(raw)
if err != nil || u.Scheme != "vless" || u.User == nil {
return "", "", false
}
q := u.Query()
switch q.Get("type") {
case "", "raw", "tcp":
default:
return "", "", false
}
if q.Get("security") != "reality" || q.Get("support-x25519mlkem768") != "true" ||
q.Get("pqv") != "" || (q.Get("encryption") != "" && q.Get("encryption") != "none") ||
(q.Get("headerType") != "" && q.Get("headerType") != "none") {
return "", "", false
}
host, key, serverName, shortID, flow := u.Hostname(), q.Get("pbk"), q.Get("sni"), q.Get("sid"), q.Get("flow")
port, portErr := strconv.ParseUint(u.Port(), 10, 16)
id, idErr := uuid.Parse(u.User.Username())
if portErr != nil || port == 0 || idErr != nil || host == "" || key == "" ||
(flow != "" && flow != "xtls-rprx-vision" && flow != "xtls-rprx-vision-udp443") {
return "", "", false
}
for _, value := range []string{host, key, serverName, shortID} {
if strings.ContainsAny(value, ",=\"\\[]#") || strings.ContainsFunc(value, func(r rune) bool {
return unicode.IsControl(r) || unicode.IsSpace(r)
}) {
return "", "", false
}
}
name = strings.TrimSpace(strings.Map(func(r rune) rune {
if unicode.IsControl(r) || strings.ContainsRune("=,[]#\"", r) {
return ' '
}
return r
}, u.Fragment))
if name == "" {
name = host
}
node = fmt.Sprintf("VLESS,%s,%d,%q,transport=tcp,over-tls=true,public-key=%q,tls-profile=chrome147", host, port, id.String(), key)
if shortID != "" {
node += ",short-id=" + shortID
}
if serverName != "" {
node += ",sni=" + serverName
}
if flow != "" {
node += ",flow=" + flow
}
return name, node, true
}