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

150 lines
3.2 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
"encoding/json"
"net/url"
"strconv"
"strings"
2024-03-22 04:10:15 -04:00
"sub2sing-box/constant"
"sub2sing-box/model"
2024-03-20 08:54:23 -04:00
"sub2sing-box/util"
2024-03-10 15:13:42 -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-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -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-03-22 04:10:15 -04:00
return model.Outbound{}, err
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
}
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "vmess",
2024-03-11 09:00:13 -04:00
Tag: name,
VMessOptions: model.VMessOutboundOptions{
ServerOptions: model.ServerOptions{
Server: vmess.Add,
2024-03-22 04:10:15 -04:00
ServerPort: 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
}
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
}
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" {
quic := model.V2RayQUICOptions{}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "quic",
QUICOptions: quic,
}
}
if vmess.Net == "grpc" {
grpc := model.V2RayGRPCOptions{
2024-03-10 15:13:42 -04:00
ServiceName: vmess.Path,
PermitWithoutStream: true,
}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "grpc",
GRPCOptions: grpc,
}
}
if vmess.Net == "h2" {
httpOps := model.V2RayHTTPOptions{
2024-03-10 15:13:42 -04:00
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
result.VMessOptions.Transport = &model.V2RayTransportOptions{
2024-03-10 15:13:42 -04:00
Type: "http",
HTTPOptions: httpOps,
}
}
return result, nil
}