mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-06-16 10:03:18 +08:00
整理代码
This commit is contained in:
24
parser/error.go
Normal file
24
parser/error.go
Normal file
@ -0,0 +1,24 @@
|
||||
package parser
|
||||
|
||||
type ParseError struct {
|
||||
Type ParseErrorType
|
||||
Message string
|
||||
Raw string
|
||||
}
|
||||
|
||||
type ParseErrorType string
|
||||
|
||||
const (
|
||||
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
|
||||
ErrInvalidStruct ParseErrorType = "invalid struct"
|
||||
ErrInvalidPort ParseErrorType = "invalid port number"
|
||||
ErrCannotParseParams ParseErrorType = "cannot parse query parameters"
|
||||
ErrInvalidBase64 ParseErrorType = "invalid base64"
|
||||
)
|
||||
|
||||
func (e *ParseError) Error() string {
|
||||
if e.Message != "" {
|
||||
return string(e.Type) + ": " + e.Message + " \"" + e.Raw + "\""
|
||||
}
|
||||
return string(e.Type)
|
||||
}
|
@ -1,78 +1,76 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
//hysteria://host:port?protocol=udp&auth=123456&peer=sni.domain&insecure=1&upmbps=100&downmbps=100&alpn=hysteria&obfs=xplus&obfsParam=123456#remarks
|
||||
//
|
||||
//- host: hostname or IP address of the server to connect to (required)
|
||||
//- port: port of the server to connect to (required)
|
||||
//- protocol: protocol to use ("udp", "wechat-video", "faketcp") (optional, default: "udp")
|
||||
//- auth: authentication payload (string) (optional)
|
||||
//- peer: SNI for TLS (optional)
|
||||
//- insecure: ignore certificate errors (optional)
|
||||
//- upmbps: upstream bandwidth in Mbps (required)
|
||||
//- downmbps: downstream bandwidth in Mbps (required)
|
||||
//- alpn: QUIC ALPN (optional)
|
||||
//- obfs: Obfuscation mode (optional, empty or "xplus")
|
||||
//- obfsParam: Obfuscation password (optional)
|
||||
//- remarks: remarks (optional)
|
||||
|
||||
func ParseHysteria(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "hysteria://") {
|
||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
||||
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria://"), "?", 2)
|
||||
serverInfo := strings.SplitN(parts[0], ":", 2)
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.HysteriaPrefix)
|
||||
urlParts := strings.SplitN(proxy, "?", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '?' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[0], ":", 2)
|
||||
if len(serverInfo) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
params, err := url.ParseQuery(parts[1])
|
||||
server, portStr := serverInfo[0], serverInfo[1]
|
||||
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
host := serverInfo[0]
|
||||
port, err := strconv.Atoi(serverInfo[1])
|
||||
|
||||
params, err := url.ParseQuery(urlParts[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
||||
}
|
||||
protocol := params.Get("protocol")
|
||||
auth := params.Get("auth")
|
||||
// peer := params.Get("peer")
|
||||
insecure := params.Get("insecure")
|
||||
upmbps := params.Get("upmbps")
|
||||
downmbps := params.Get("downmbps")
|
||||
obfs := params.Get("obfs")
|
||||
// obfsParam := params.Get("obfsParam")
|
||||
var alpn []string
|
||||
if params.Get("alpn") != "" {
|
||||
alpn = strings.Split(params.Get("alpn"), ",")
|
||||
} else {
|
||||
alpn = nil
|
||||
}
|
||||
remarks := ""
|
||||
if strings.Contains(parts[1], "#") {
|
||||
r := strings.Split(parts[1], "#")
|
||||
remarks = r[len(r)-1]
|
||||
} else {
|
||||
remarks = serverInfo[0] + ":" + serverInfo[1]
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
protocol, auth, insecure, upmbps, downmbps, obfs, alpnStr := params.Get("protocol"), params.Get("auth"), params.Get("insecure"), params.Get("upmbps"), params.Get("downmbps"), params.Get("obfs"), params.Get("alpn")
|
||||
insecureBool, err := strconv.ParseBool(insecure)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria Url")
|
||||
insecureBool = false
|
||||
}
|
||||
result := model.Outbound{
|
||||
|
||||
var alpn []string
|
||||
alpnStr = strings.TrimSpace(alpnStr)
|
||||
if alpnStr != "" {
|
||||
alpn = strings.Split(alpnStr, ",")
|
||||
}
|
||||
|
||||
remarks := server + ":" + portStr
|
||||
if params.Get("remarks") != "" {
|
||||
remarks = params.Get("remarks")
|
||||
}
|
||||
|
||||
return model.Outbound{
|
||||
Type: "hysteria",
|
||||
Tag: remarks,
|
||||
HysteriaOptions: model.HysteriaOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: host,
|
||||
ServerPort: uint16(port),
|
||||
Server: server,
|
||||
ServerPort: port,
|
||||
},
|
||||
Up: upmbps,
|
||||
Down: downmbps,
|
||||
@ -87,6 +85,5 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return result, nil
|
||||
}, nil
|
||||
}
|
||||
|
@ -1,39 +1,73 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
|
||||
|
||||
func ParseHysteria2(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
if !strings.HasPrefix(proxy, constant.Hysteria2Prefix1) &&
|
||||
!strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
|
||||
serverInfo := strings.SplitN(parts[1], "/?", 2)
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix1)
|
||||
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix2)
|
||||
urlParts := strings.SplitN(proxy, "@", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '@' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
password := urlParts[0]
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[1], "/?", 2)
|
||||
if len(serverInfo) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing params in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
paramStr := serverInfo[1]
|
||||
|
||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||
var server string
|
||||
var portStr string
|
||||
if len(serverAndPort) == 1 {
|
||||
serverAndPort = append(serverAndPort, "443")
|
||||
} else if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
portStr = "443"
|
||||
} else if len(serverAndPort) == 2 {
|
||||
server, portStr = serverAndPort[0], serverAndPort[1]
|
||||
} else {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
params, err := url.ParseQuery(serverInfo[1])
|
||||
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
port, err := strconv.Atoi(serverAndPort[1])
|
||||
|
||||
params, err := url.ParseQuery(paramStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
remarks := params.Get("name")
|
||||
server := serverAndPort[0]
|
||||
password := parts[0]
|
||||
network := params.Get("network")
|
||||
|
||||
remarks, network, obfs, obfsPassword, pinSHA256, insecure, sni := params.Get("name"), params.Get("network"), params.Get("obfs"), params.Get("obfs-password"), params.Get("pinSHA256"), params.Get("insecure"), params.Get("sni")
|
||||
enableTLS := pinSHA256 != ""
|
||||
insecureBool := insecure == "1"
|
||||
|
||||
result := model.Outbound{
|
||||
Type: "hysteria2",
|
||||
Tag: remarks,
|
||||
@ -44,14 +78,14 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
|
||||
},
|
||||
Password: password,
|
||||
Obfs: &model.Hysteria2Obfs{
|
||||
Type: params.Get("obfs"),
|
||||
Password: params.Get("obfs-password"),
|
||||
Type: obfs,
|
||||
Password: obfsPassword,
|
||||
},
|
||||
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
|
||||
TLS: &model.OutboundTLSOptions{Enabled: params.Get("pinSHA256") != "",
|
||||
Insecure: params.Get("insecure") == "1",
|
||||
ServerName: params.Get("sni"),
|
||||
Certificate: []string{params.Get("pinSHA256")}},
|
||||
TLS: &model.OutboundTLSOptions{Enabled: enableTLS,
|
||||
Insecure: insecureBool,
|
||||
ServerName: sni,
|
||||
Certificate: []string{pinSHA256}},
|
||||
},
|
||||
Network: network,
|
||||
},
|
||||
|
@ -1,15 +1,16 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
var ParserMap map[string]func(string) (model.Outbound, error) = map[string]func(string) (model.Outbound, error){
|
||||
"ss://": ParseShadowsocks,
|
||||
"vmess://": ParseVmess,
|
||||
"trojan://": ParseTrojan,
|
||||
"vless://": ParseVless,
|
||||
"hysteria://": ParseHysteria,
|
||||
"hy2://": ParseHysteria2,
|
||||
"hysteria2://": ParseHysteria2,
|
||||
constant.ShadowsocksPrefix: ParseShadowsocks,
|
||||
constant.VMessPrefix: ParseVmess,
|
||||
constant.TrojanPrefix: ParseTrojan,
|
||||
constant.VLESSPrefix: ParseVless,
|
||||
constant.HysteriaPrefix: ParseHysteria,
|
||||
constant.Hysteria2Prefix1: ParseHysteria2,
|
||||
constant.Hysteria2Prefix2: ParseHysteria2,
|
||||
}
|
||||
|
23
parser/port.go
Normal file
23
parser/port.go
Normal file
@ -0,0 +1,23 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ParsePort(portStr string) (uint16, error) {
|
||||
port, err := strconv.Atoi(portStr)
|
||||
|
||||
if err != nil {
|
||||
return 0, &ParseError{
|
||||
Type: ErrInvalidPort,
|
||||
Message: portStr,
|
||||
}
|
||||
}
|
||||
if port < 1 || port > 65535 {
|
||||
return 0, &ParseError{
|
||||
Type: ErrInvalidPort,
|
||||
Message: portStr,
|
||||
}
|
||||
}
|
||||
return uint16(port), nil
|
||||
}
|
@ -1,62 +1,87 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
"sub2sing-box/util"
|
||||
)
|
||||
|
||||
func ParseShadowsocks(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "ss://") {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
}
|
||||
if !strings.Contains(parts[0], ":") {
|
||||
decoded, err := util.DecodeBase64(parts[0])
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.ShadowsocksPrefix)
|
||||
urlParts := strings.SplitN(proxy, "@", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '@' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
parts[0] = decoded
|
||||
}
|
||||
credentials := strings.SplitN(parts[0], ":", 2)
|
||||
|
||||
var serverAndPort []string
|
||||
if !strings.Contains(urlParts[0], ":") {
|
||||
decoded, err := util.DecodeBase64(urlParts[0])
|
||||
if err != nil {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "invalid base64 encoded",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
urlParts[0] = decoded
|
||||
}
|
||||
credentials := strings.SplitN(urlParts[0], ":", 2)
|
||||
if len(credentials) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
method, password := credentials[0], credentials[1]
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||
serverAndPort = strings.SplitN(serverInfo[0], ":", 2)
|
||||
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||
if len(serverInfo) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
remarks := ""
|
||||
|
||||
var remarks string
|
||||
if len(serverInfo) == 2 {
|
||||
unescape, err := url.QueryUnescape(serverInfo[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "cannot unescape remarks",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
remarks = strings.TrimSpace(unescape)
|
||||
} else {
|
||||
remarks = strings.TrimSpace(serverAndPort[0])
|
||||
remarks = strings.TrimSpace(server + ":" + portStr)
|
||||
}
|
||||
method := credentials[0]
|
||||
password := credentials[1]
|
||||
server := strings.TrimSpace(serverAndPort[0])
|
||||
|
||||
result := model.Outbound{
|
||||
Type: "shadowsocks",
|
||||
Tag: remarks,
|
||||
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: server,
|
||||
ServerPort: uint16(port),
|
||||
ServerPort: port,
|
||||
},
|
||||
Method: method,
|
||||
Password: password,
|
||||
|
105
parser/trojan.go
105
parser/trojan.go
@ -1,118 +1,155 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
func ParseTrojan(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "trojan://") {
|
||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
||||
if !strings.HasPrefix(proxy, constant.TrojanPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.TrojanPrefix)
|
||||
urlParts := strings.SplitN(proxy, "@", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '@' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
password := strings.TrimSpace(urlParts[0])
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
||||
if len(serverAndPortAndParams) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '?' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
|
||||
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||
|
||||
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid trojan Url")
|
||||
}
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
|
||||
remarks := ""
|
||||
if len(serverInfo) == 2 {
|
||||
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
|
||||
} else {
|
||||
remarks = serverAndPort[0]
|
||||
}
|
||||
server := strings.TrimSpace(serverAndPort[0])
|
||||
password := strings.TrimSpace(parts[0])
|
||||
|
||||
network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName := params.Get("type"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("pbk"), params.Get("sid"), params.Get("fp"), params.Get("path"), params.Get("host"), params.Get("serviceName")
|
||||
|
||||
var alpn []string
|
||||
if strings.Contains(alpnStr, ",") {
|
||||
alpn = strings.Split(alpnStr, ",")
|
||||
} else {
|
||||
alpn = nil
|
||||
}
|
||||
|
||||
enableUTLS := fp != ""
|
||||
|
||||
result := model.Outbound{
|
||||
Type: "trojan",
|
||||
Tag: remarks,
|
||||
TrojanOptions: model.TrojanOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: server,
|
||||
ServerPort: uint16(port),
|
||||
ServerPort: port,
|
||||
},
|
||||
Password: password,
|
||||
Network: params.Get("type"),
|
||||
Network: network,
|
||||
},
|
||||
}
|
||||
if params.Get("security") == "xtls" || params.Get("security") == "tls" {
|
||||
var alpn []string
|
||||
if strings.Contains(params.Get("alpn"), ",") {
|
||||
alpn = strings.Split(params.Get("alpn"), ",")
|
||||
} else {
|
||||
alpn = nil
|
||||
}
|
||||
|
||||
if security == "xtls" || security == "tls" {
|
||||
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||
TLS: &model.OutboundTLSOptions{
|
||||
Enabled: true,
|
||||
ALPN: alpn,
|
||||
ServerName: params.Get("sni"),
|
||||
ServerName: sni,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("security") == "reality" {
|
||||
result.TrojanOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||
TLS: &model.OutboundTLSOptions{
|
||||
Enabled: true,
|
||||
ServerName: params.Get("sni"),
|
||||
ServerName: sni,
|
||||
Reality: &model.OutboundRealityOptions{
|
||||
Enabled: true,
|
||||
PublicKey: params.Get("pbk"),
|
||||
ShortID: params.Get("sid"),
|
||||
PublicKey: pbk,
|
||||
ShortID: sid,
|
||||
},
|
||||
UTLS: &model.OutboundUTLSOptions{
|
||||
Enabled: params.Get("fp") != "",
|
||||
Fingerprint: params.Get("fp"),
|
||||
Enabled: enableUTLS,
|
||||
Fingerprint: fp,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "ws" {
|
||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "ws",
|
||||
WebsocketOptions: model.V2RayWebsocketOptions{
|
||||
Path: params.Get("path"),
|
||||
Path: path,
|
||||
Headers: map[string]string{
|
||||
"Host": params.Get("host"),
|
||||
"Host": host,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "http" {
|
||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "http",
|
||||
HTTPOptions: model.V2RayHTTPOptions{
|
||||
Host: []string{params.Get("host")},
|
||||
Host: []string{host},
|
||||
Path: params.Get("path"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "quic" {
|
||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "quic",
|
||||
QUICOptions: model.V2RayQUICOptions{},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "grpc" {
|
||||
result.TrojanOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "grpc",
|
||||
GRPCOptions: model.V2RayGRPCOptions{
|
||||
ServiceName: params.Get("serviceName"),
|
||||
ServiceName: serviceName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
133
parser/vless.go
133
parser/vless.go
@ -1,35 +1,59 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
func ParseVless(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "vless://") {
|
||||
return model.Outbound{}, errors.New("invalid vless Url")
|
||||
if !strings.HasPrefix(proxy, constant.VLESSPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid vless Url")
|
||||
|
||||
urlParts := strings.SplitN(strings.TrimPrefix(proxy, constant.VLESSPrefix), "@", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '@' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
|
||||
if len(serverAndPortAndParams) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '?' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
|
||||
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
|
||||
params, err := url.ParseQuery(serverAndPortAndParams[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid vless Url")
|
||||
}
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
remarks := ""
|
||||
if len(serverInfo) == 2 {
|
||||
if strings.Contains(serverInfo[1], "|") {
|
||||
@ -37,92 +61,107 @@ func ParseVless(proxy string) (model.Outbound, error) {
|
||||
} else {
|
||||
remarks, err = url.QueryUnescape(serverInfo[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
remarks, err = url.QueryUnescape(serverAndPort[0])
|
||||
remarks, err = url.QueryUnescape(server)
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
}
|
||||
server := strings.TrimSpace(serverAndPort[0])
|
||||
uuid := strings.TrimSpace(parts[0])
|
||||
|
||||
uuid := strings.TrimSpace(urlParts[0])
|
||||
flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName := params.Get("flow"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("allowInsecure"), params.Get("fp"), params.Get("pbk"), params.Get("sid"), params.Get("path"), params.Get("host"), params.Get("serviceName")
|
||||
|
||||
enableUTLS := fp != ""
|
||||
insecureBool := insecure == "1"
|
||||
var alpn []string
|
||||
if strings.Contains(alpnStr, ",") {
|
||||
alpn = strings.Split(alpnStr, ",")
|
||||
} else {
|
||||
alpn = nil
|
||||
}
|
||||
|
||||
result := model.Outbound{
|
||||
Type: "vless",
|
||||
Tag: remarks,
|
||||
VLESSOptions: model.VLESSOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: server,
|
||||
ServerPort: uint16(port),
|
||||
ServerPort: port,
|
||||
},
|
||||
UUID: uuid,
|
||||
Flow: params.Get("flow"),
|
||||
Flow: flow,
|
||||
},
|
||||
}
|
||||
if params.Get("security") == "tls" {
|
||||
var alpn []string
|
||||
if strings.Contains(params.Get("alpn"), ",") {
|
||||
alpn = strings.Split(params.Get("alpn"), ",")
|
||||
} else {
|
||||
alpn = nil
|
||||
}
|
||||
|
||||
if security == "tls" {
|
||||
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
||||
TLS: &model.OutboundTLSOptions{
|
||||
Enabled: true,
|
||||
ALPN: alpn,
|
||||
ServerName: params.Get("sni"),
|
||||
Insecure: params.Get("allowInsecure") == "1",
|
||||
ServerName: sni,
|
||||
Insecure: insecureBool,
|
||||
},
|
||||
}
|
||||
if params.Get("fp") != "" {
|
||||
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
|
||||
Enabled: true,
|
||||
Fingerprint: params.Get("fp"),
|
||||
}
|
||||
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
|
||||
Enabled: enableUTLS,
|
||||
Fingerprint: fp,
|
||||
}
|
||||
}
|
||||
if params.Get("security") == "reality" {
|
||||
|
||||
if security == "reality" {
|
||||
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality = &model.OutboundRealityOptions{
|
||||
Enabled: true,
|
||||
PublicKey: params.Get("pbk"),
|
||||
ShortID: params.Get("sid"),
|
||||
PublicKey: pbk,
|
||||
ShortID: sid,
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "ws" {
|
||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "ws",
|
||||
WebsocketOptions: model.V2RayWebsocketOptions{
|
||||
Path: params.Get("path"),
|
||||
Path: path,
|
||||
},
|
||||
}
|
||||
if params.Get("host") != "" {
|
||||
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = params.Get("host")
|
||||
}
|
||||
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = host
|
||||
}
|
||||
|
||||
if params.Get("type") == "quic" {
|
||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "quic",
|
||||
QUICOptions: model.V2RayQUICOptions{},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "grpc" {
|
||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "grpc",
|
||||
GRPCOptions: model.V2RayGRPCOptions{
|
||||
ServiceName: params.Get("serviceName"),
|
||||
ServiceName: serviceName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if params.Get("type") == "http" {
|
||||
host, err := url.QueryUnescape(params.Get("host"))
|
||||
hosts, err := url.QueryUnescape(host)
|
||||
if err != nil {
|
||||
return model.Outbound{}, err
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
|
||||
Type: "http",
|
||||
HTTPOptions: model.V2RayHTTPOptions{
|
||||
Host: strings.Split(host, ","),
|
||||
Host: strings.Split(hosts, ","),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -2,47 +2,53 @@ package parser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
"sub2sing-box/util"
|
||||
)
|
||||
|
||||
func ParseVmess(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "vmess://") {
|
||||
return model.Outbound{}, errors.New("invalid vmess url")
|
||||
if !strings.HasPrefix(proxy, constant.VMessPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.VMessPrefix)
|
||||
base64, err := util.DecodeBase64(proxy)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
|
||||
}
|
||||
|
||||
var vmess model.VmessJson
|
||||
err = json.Unmarshal([]byte(base64), &vmess)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||
}
|
||||
port := 0
|
||||
|
||||
var port uint16
|
||||
switch vmess.Port.(type) {
|
||||
case string:
|
||||
port, err = strconv.Atoi(vmess.Port.(string))
|
||||
port, err = ParsePort(vmess.Port.(string))
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
case float64:
|
||||
port = int(vmess.Port.(float64))
|
||||
port = uint16(vmess.Port.(float64))
|
||||
}
|
||||
|
||||
aid := 0
|
||||
switch vmess.Aid.(type) {
|
||||
case string:
|
||||
aid, err = strconv.Atoi(vmess.Aid.(string))
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||
}
|
||||
case float64:
|
||||
aid = int(vmess.Aid.(float64))
|
||||
}
|
||||
|
||||
if vmess.Scy == "" {
|
||||
vmess.Scy = "auto"
|
||||
}
|
||||
@ -58,7 +64,7 @@ func ParseVmess(proxy string) (model.Outbound, error) {
|
||||
VMessOptions: model.VMessOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: vmess.Add,
|
||||
ServerPort: uint16(port),
|
||||
ServerPort: port,
|
||||
},
|
||||
UUID: vmess.Id,
|
||||
AlterId: aid,
|
||||
|
Reference in New Issue
Block a user