2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-20 12:02:38 -04:00
|
|
|
"sub2sing-box/model"
|
2024-03-20 08:54:23 -04:00
|
|
|
"sub2sing-box/util"
|
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-10 15:13:42 -04:00
|
|
|
if !strings.HasPrefix(proxy, "vmess://") {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid vmess url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-19 05:01:53 -04:00
|
|
|
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
2024-03-10 15:13:42 -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-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
port := 0
|
|
|
|
switch vmess.Port.(type) {
|
|
|
|
case string:
|
|
|
|
port, err = strconv.Atoi(vmess.Port.(string))
|
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
port = int(vmess.Port.(float64))
|
|
|
|
}
|
|
|
|
aid := 0
|
|
|
|
switch vmess.Aid.(type) {
|
|
|
|
case string:
|
|
|
|
aid, err = strconv.Atoi(vmess.Aid.(string))
|
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid vmess url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
aid = int(vmess.Aid.(float64))
|
|
|
|
}
|
|
|
|
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-03-10 15:13:42 -04:00
|
|
|
Type: "vmess",
|
2024-03-11 09:00:13 -04:00
|
|
|
Tag: name,
|
2024-03-20 12:02:38 -04:00
|
|
|
VMessOptions: model.VMessOutboundOptions{
|
|
|
|
ServerOptions: model.ServerOptions{
|
|
|
|
Server: vmess.Add,
|
|
|
|
ServerPort: uint16(port),
|
|
|
|
},
|
|
|
|
UUID: vmess.Id,
|
|
|
|
AlterId: aid,
|
|
|
|
Security: vmess.Scy,
|
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-03-20 12:02:38 -04:00
|
|
|
result.VMessOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &model.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
UTLS: &model.OutboundUTLSOptions{
|
|
|
|
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 != "" {
|
|
|
|
result.VMessOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
|
|
|
|
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-03-20 12:02:38 -04:00
|
|
|
result.VMessOptions.Transport = &model.V2RayTransportOptions{
|
|
|
|
Type: "ws",
|
|
|
|
WebsocketOptions: model.V2RayWebsocketOptions{
|
|
|
|
Path: vmess.Path,
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Host": vmess.Host,
|
|
|
|
},
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "quic" {
|
2024-03-20 12:02:38 -04:00
|
|
|
quic := model.V2RayQUICOptions{}
|
|
|
|
result.VMessOptions.Transport = &model.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "quic",
|
|
|
|
QUICOptions: quic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "grpc" {
|
2024-03-20 12:02:38 -04:00
|
|
|
grpc := model.V2RayGRPCOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
ServiceName: vmess.Path,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}
|
2024-03-20 12:02:38 -04:00
|
|
|
result.VMessOptions.Transport = &model.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "grpc",
|
|
|
|
GRPCOptions: grpc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmess.Net == "h2" {
|
2024-03-20 12:02:38 -04:00
|
|
|
httpOps := model.V2RayHTTPOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Host: strings.Split(vmess.Host, ","),
|
|
|
|
Path: vmess.Path,
|
|
|
|
}
|
2024-03-20 12:02:38 -04:00
|
|
|
result.VMessOptions.Transport = &model.V2RayTransportOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "http",
|
|
|
|
HTTPOptions: httpOps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|