1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 11:54:41 -05:00
sub2clash/parser/vmess.go

121 lines
2.4 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
"encoding/json"
"net/url"
2023-09-12 06:40:24 -04:00
"strconv"
"strings"
"github.com/nitezs/sub2clash/constant"
"github.com/nitezs/sub2clash/model"
2023-09-12 06:40:24 -04:00
)
func ParseVmess(proxy string) (model.Proxy, error) {
2024-04-23 02:39:16 -04:00
if !strings.HasPrefix(proxy, constant.VMessPrefix) {
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
proxy = strings.TrimPrefix(proxy, constant.VMessPrefix)
base64, err := DecodeBase64(proxy)
2023-09-12 06:40:24 -04:00
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
var vmess model.VmessJson
2023-09-12 06:40:24 -04:00
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
var port int
switch vmess.Port.(type) {
case string:
2024-04-23 02:39:16 -04:00
port, err = ParsePort(vmess.Port.(string))
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
}
case float64:
port = int(vmess.Port.(float64))
}
2024-04-23 02:39:16 -04:00
aid := 0
switch vmess.Aid.(type) {
case string:
aid, err = strconv.Atoi(vmess.Aid.(string))
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
}
case float64:
aid = int(vmess.Aid.(float64))
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
2023-09-12 06:40:24 -04:00
if vmess.Scy == "" {
vmess.Scy = "auto"
}
2024-04-23 02:39:16 -04:00
name, err := url.QueryUnescape(vmess.Ps)
if err != nil {
name = vmess.Ps
}
2024-04-23 02:39:16 -04:00
2023-09-12 06:40:24 -04:00
result := model.Proxy{
2024-04-23 02:39:16 -04:00
Name: name,
Type: "vmess",
Server: vmess.Add,
Port: port,
UUID: vmess.Id,
AlterID: aid,
Cipher: vmess.Scy,
}
if vmess.Tls == "tls" {
var alpn []string
if strings.Contains(vmess.Alpn, ",") {
alpn = strings.Split(vmess.Alpn, ",")
} else {
alpn = nil
}
result.TLS = true
result.Fingerprint = vmess.Fp
result.Alpn = alpn
result.Servername = vmess.Sni
2023-09-12 12:46:17 -04:00
}
2024-04-23 02:39:16 -04:00
2023-09-12 12:46:17 -04:00
if vmess.Net == "ws" {
2024-04-23 02:39:16 -04:00
if vmess.Path == "" {
vmess.Path = "/"
}
if vmess.Host == "" {
vmess.Host = vmess.Add
}
result.Network = "ws"
result.WSOpts = model.WSOptions{
2023-09-12 06:40:24 -04:00
Path: vmess.Path,
Headers: map[string]string{
"Host": vmess.Host,
2023-09-12 06:40:24 -04:00
},
2023-09-12 12:46:17 -04:00
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
if vmess.Net == "grpc" {
result.GrpcOpts = model.GrpcOptions{
GrpcServiceName: vmess.Path,
}
result.Network = "grpc"
}
if vmess.Net == "h2" {
result.HTTP2Opts = model.HTTP2Options{
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
result.Network = "h2"
}
2023-09-12 06:40:24 -04:00
return result, nil
}