2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-09-19 06:12:24 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2sing-box/constant"
|
|
|
|
"github.com/nitezs/sub2sing-box/model"
|
|
|
|
"github.com/nitezs/sub2sing-box/util"
|
2024-10-02 13:24:54 -04:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-03-10 15:13:42 -04:00
|
|
|
)
|
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
func ParseVmess(proxy string) (model.Outbound, error) {
|
2024-03-22 04:10:15 -04:00
|
|
|
if !strings.HasPrefix(proxy, constant.VMessPrefix) {
|
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
proxy = strings.TrimPrefix(proxy, constant.VMessPrefix)
|
|
|
|
base64, err := util.DecodeBase64(proxy)
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-08-05 03:01:05 -04:00
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
var vmess model.VmessJson
|
2024-03-10 15:13:42 -04:00
|
|
|
err = json.Unmarshal([]byte(base64), &vmess)
|
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
var port uint16
|
2024-03-10 15:13:42 -04:00
|
|
|
switch vmess.Port.(type) {
|
|
|
|
case string:
|
2024-03-22 04:10:15 -04:00
|
|
|
port, err = ParsePort(vmess.Port.(string))
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-04-23 02:41:14 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidPort,
|
|
|
|
Message: err.Error(),
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
case float64:
|
2024-03-22 04:10:15 -04:00
|
|
|
port = uint16(vmess.Port.(float64))
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
2024-03-10 15:13:42 -04:00
|
|
|
aid := 0
|
|
|
|
switch vmess.Aid.(type) {
|
|
|
|
case string:
|
|
|
|
aid, err = strconv.Atoi(vmess.Aid.(string))
|
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
aid = int(vmess.Aid.(float64))
|
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
2024-03-10 15:13:42 -04:00
|
|
|
if vmess.Scy == "" {
|
|
|
|
vmess.Scy = "auto"
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := url.QueryUnescape(vmess.Ps)
|
|
|
|
if err != nil {
|
|
|
|
name = vmess.Ps
|
|
|
|
}
|
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
result := model.Outbound{
|
2024-10-02 13:24:54 -04:00
|
|
|
Outbound: option.Outbound{
|
|
|
|
Type: "vmess",
|
|
|
|
Tag: name,
|
|
|
|
VMessOptions: option.VMessOutboundOptions{
|
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: vmess.Add,
|
|
|
|
ServerPort: port,
|
|
|
|
},
|
|
|
|
UUID: vmess.Id,
|
|
|
|
AlterId: aid,
|
|
|
|
Security: vmess.Scy,
|
2024-03-20 12:02:38 -04:00
|
|
|
},
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Tls == "tls" {
|
2024-03-11 12:13:59 -04:00
|
|
|
var alpn []string
|
|
|
|
if strings.Contains(vmess.Alpn, ",") {
|
|
|
|
alpn = strings.Split(vmess.Alpn, ",")
|
|
|
|
} else {
|
|
|
|
alpn = nil
|
|
|
|
}
|
2024-10-02 13:24:54 -04:00
|
|
|
result.VMessOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &option.OutboundTLSOptions{
|
2024-03-20 12:02:38 -04:00
|
|
|
Enabled: true,
|
2024-10-02 13:24:54 -04:00
|
|
|
UTLS: &option.OutboundUTLSOptions{
|
2024-03-20 12:02:38 -04:00
|
|
|
Fingerprint: vmess.Fp,
|
|
|
|
},
|
2024-03-20 12:59:47 -04:00
|
|
|
ALPN: alpn,
|
|
|
|
ServerName: vmess.Sni,
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
2024-03-20 12:59:47 -04:00
|
|
|
if vmess.Fp != "" {
|
2024-10-02 13:24:54 -04:00
|
|
|
result.VMessOptions.OutboundTLSOptionsContainer.TLS.UTLS = &option.OutboundUTLSOptions{
|
2024-03-20 12:59:47 -04:00
|
|
|
Enabled: true,
|
|
|
|
Fingerprint: vmess.Fp,
|
|
|
|
}
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "ws" {
|
|
|
|
if vmess.Path == "" {
|
|
|
|
vmess.Path = "/"
|
|
|
|
}
|
|
|
|
if vmess.Host == "" {
|
|
|
|
vmess.Host = vmess.Add
|
|
|
|
}
|
2024-10-02 13:24:54 -04:00
|
|
|
result.VMessOptions.Transport = &option.V2RayTransportOptions{
|
2024-03-20 12:02:38 -04:00
|
|
|
Type: "ws",
|
2024-10-02 13:24:54 -04:00
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
2024-03-20 12:02:38 -04:00
|
|
|
Path: vmess.Path,
|
2024-10-02 13:24:54 -04:00
|
|
|
Headers: map[string]option.Listable[string]{
|
|
|
|
"Host": {vmess.Host},
|
2024-03-20 12:02:38 -04:00
|
|
|
},
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "quic" {
|
2024-10-02 13:24:54 -04:00
|
|
|
quic := option.V2RayQUICOptions{}
|
|
|
|
result.VMessOptions.Transport = &option.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "quic",
|
|
|
|
QUICOptions: quic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "grpc" {
|
2024-10-02 13:24:54 -04:00
|
|
|
grpc := option.V2RayGRPCOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
ServiceName: vmess.Path,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}
|
2024-10-02 13:24:54 -04:00
|
|
|
result.VMessOptions.Transport = &option.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "grpc",
|
|
|
|
GRPCOptions: grpc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "h2" {
|
2024-10-02 13:24:54 -04:00
|
|
|
httpOps := option.V2RayHTTPOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Host: strings.Split(vmess.Host, ","),
|
|
|
|
Path: vmess.Path,
|
|
|
|
}
|
2024-10-02 13:24:54 -04:00
|
|
|
result.VMessOptions.Transport = &option.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "http",
|
|
|
|
HTTPOptions: httpOps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|