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

91 lines
2.0 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
"encoding/json"
"errors"
"net/url"
2023-09-12 06:40:24 -04:00
"strconv"
"strings"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
2023-09-12 06:40:24 -04:00
)
func ParseVmess(proxy string) (model.Proxy, error) {
// 判断是否以 vmess:// 开头
if !strings.HasPrefix(proxy, "vmess://") {
return model.Proxy{}, errors.New("invalid vmess url")
2023-09-12 06:40:24 -04:00
}
// 解码
base64, err := DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
2023-09-12 06:40:24 -04:00
}
// 解析
var vmess model.VmessJson
2023-09-12 06:40:24 -04:00
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
2023-09-12 06:40:24 -04:00
}
// 解析端口
port := 0
switch vmess.Port.(type) {
case string:
port, err = strconv.Atoi(vmess.Port.(string))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
port = int(vmess.Port.(float64))
}
// 解析Aid
aid := 0
switch vmess.Aid.(type) {
case string:
aid, err = strconv.Atoi(vmess.Aid.(string))
if err != nil {
return model.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
aid = int(vmess.Aid.(float64))
2023-09-12 06:40:24 -04:00
}
// 设置默认值
2023-09-12 06:40:24 -04:00
if vmess.Scy == "" {
vmess.Scy = "auto"
}
if vmess.Net == "ws" && vmess.Path == "" {
vmess.Path = "/"
}
if vmess.Net == "ws" && vmess.Host == "" {
vmess.Host = vmess.Add
}
name, err := url.QueryUnescape(vmess.Ps)
if err != nil {
name = vmess.Ps
}
2023-09-12 06:40:24 -04:00
// 返回结果
result := model.Proxy{
Name: name,
2023-09-12 06:40:24 -04:00
Type: "vmess",
Server: vmess.Add,
Port: port,
UUID: vmess.Id,
AlterID: aid,
2023-09-12 06:40:24 -04:00
Cipher: vmess.Scy,
UDP: true,
TLS: vmess.Tls == "tls",
Fingerprint: vmess.Fp,
ClientFingerprint: "chrome",
SkipCertVerify: true,
Servername: vmess.Add,
Network: vmess.Net,
2023-09-12 12:46:17 -04:00
}
if vmess.Net == "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
}
return result, nil
}