1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-25 08:50:54 -05:00
sub2sing-box/parser/vmess.go

139 lines
2.9 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
2024-03-20 08:54:23 -04:00
model2 "sub2sing-box/model"
"sub2sing-box/util"
2024-03-10 15:13:42 -04:00
)
2024-03-20 08:54:23 -04:00
func ParseVmess(proxy string) (model2.Proxy, error) {
2024-03-10 15:13:42 -04:00
if !strings.HasPrefix(proxy, "vmess://") {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("invalid vmess url")
2024-03-10 15:13:42 -04:00
}
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
2024-03-10 15:13:42 -04:00
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
2024-03-10 15:13:42 -04:00
}
2024-03-20 08:54:23 -04:00
var vmess model2.VmessJson
2024-03-10 15:13:42 -04:00
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, 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 08:54:23 -04:00
return model2.Proxy{}, 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 08:54:23 -04:00
return model2.Proxy{}, 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 08:54:23 -04:00
result := model2.Proxy{
2024-03-10 15:13:42 -04:00
Type: "vmess",
2024-03-11 09:00:13 -04:00
Tag: name,
2024-03-20 08:54:23 -04:00
VMess: model2.VMess{
2024-03-10 15:13:42 -04:00
Server: vmess.Add,
ServerPort: uint16(port),
UUID: vmess.Id,
AlterId: aid,
Security: vmess.Scy,
},
}
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 08:54:23 -04:00
tls := model2.OutboundTLSOptions{
2024-03-10 15:13:42 -04:00
Enabled: true,
2024-03-20 08:54:23 -04:00
UTLS: &model2.OutboundUTLSOptions{
2024-03-10 15:13:42 -04:00
Fingerprint: vmess.Fp,
},
2024-03-11 12:13:59 -04:00
ALPN: alpn,
2024-03-10 15:13:42 -04:00
}
result.VMess.TLS = &tls
}
if vmess.Net == "ws" {
if vmess.Path == "" {
vmess.Path = "/"
}
if vmess.Host == "" {
vmess.Host = vmess.Add
}
2024-03-20 08:54:23 -04:00
ws := model2.V2RayWebsocketOptions{
2024-03-10 15:13:42 -04:00
Path: vmess.Path,
Headers: map[string]string{
"Host": vmess.Host,
},
}
2024-03-20 08:54:23 -04:00
transport := model2.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "ws",
WebsocketOptions: ws,
}
result.VMess.Transport = &transport
}
if vmess.Net == "quic" {
2024-03-20 08:54:23 -04:00
quic := model2.V2RayQUICOptions{}
transport := model2.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "quic",
QUICOptions: quic,
}
result.VMess.Transport = &transport
}
if vmess.Net == "grpc" {
2024-03-20 08:54:23 -04:00
grpc := model2.V2RayGRPCOptions{
2024-03-10 15:13:42 -04:00
ServiceName: vmess.Path,
PermitWithoutStream: true,
}
2024-03-20 08:54:23 -04:00
transport := model2.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "grpc",
GRPCOptions: grpc,
}
result.VMess.Transport = &transport
}
if vmess.Net == "h2" {
2024-03-20 08:54:23 -04:00
httpOps := model2.V2RayHTTPOptions{
2024-03-10 15:13:42 -04:00
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
2024-03-20 08:54:23 -04:00
transport := model2.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "http",
HTTPOptions: httpOps,
}
result.VMess.Transport = &transport
}
return result, nil
}