mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-05-25 05:37:40 +03:00
Add MTProxy, MASQUE, VPN, Link parser. Update AmneziaWG. Remove Tunneling
This commit is contained in:
50
parser/raw/parser.go
Normal file
50
parser/raw/parser.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package raw
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-box/parser/link"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
func ParseRawSubscription(ctx context.Context, content string) ([]option.Outbound, error) {
|
||||
if base64Content, err := DecodeBase64URLSafe(content); err == nil {
|
||||
servers, _ := parseRawSubscription(base64Content)
|
||||
if len(servers) > 0 {
|
||||
return servers, err
|
||||
}
|
||||
}
|
||||
return parseRawSubscription(content)
|
||||
}
|
||||
|
||||
func parseRawSubscription(content string) ([]option.Outbound, error) {
|
||||
var servers []option.Outbound
|
||||
content = strings.ReplaceAll(content, "\r\n", "\n")
|
||||
linkList := strings.Split(content, "\n")
|
||||
for _, linkLine := range linkList {
|
||||
server, err := link.ParseSubscriptionLink(linkLine)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
if len(servers) == 0 {
|
||||
return nil, E.New("no servers found")
|
||||
}
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
func DecodeBase64URLSafe(content string) (string, error) {
|
||||
s := strings.ReplaceAll(content, " ", "-")
|
||||
s = strings.ReplaceAll(s, "/", "_")
|
||||
s = strings.ReplaceAll(s, "+", "-")
|
||||
s = strings.ReplaceAll(s, "=", "")
|
||||
result, err := base64.RawURLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return content, nil
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
Reference in New Issue
Block a user