1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 22:14:41 -05:00
sub2clash/parser/vmess.go
nitezs ad7d2b98f6 fix: 修复当base64字符串长度不为4的倍数时,解码失败的问题
update: 提高根据ISO匹配国家名称的正确率
fix: 修复vmess的port和aid不规范导致无法解析的问题
modify: 一些没用的修改
2023-09-24 18:06:44 +08:00

86 lines
2.0 KiB
Go

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