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

69 lines
1.6 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
"encoding/json"
"errors"
"fmt"
"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{}, fmt.Errorf("invalid vmess Url")
2023-09-12 06:40:24 -04:00
}
// 解码
base64, err := DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
if err != nil {
2023-09-13 01:47:22 -04:00
return model.Proxy{}, errors.New("无效的 vmess Url")
2023-09-12 06:40:24 -04:00
}
// 解析
var vmess model.Vmess
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
2023-09-13 01:47:22 -04:00
return model.Proxy{}, errors.New("无效的 vmess Url")
2023-09-12 06:40:24 -04:00
}
// 处理端口
port, err := strconv.Atoi(strings.TrimSpace(vmess.Port))
if err != nil {
2023-09-13 01:47:22 -04:00
return model.Proxy{}, errors.New("无效的 vmess Url")
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
}
// 返回结果
result := model.Proxy{
Name: vmess.Ps,
Type: "vmess",
Server: vmess.Add,
Port: port,
UUID: vmess.Id,
AlterID: vmess.Aid,
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.WSOptsStruct{
2023-09-12 06:40:24 -04:00
Path: vmess.Path,
Headers: model.HeaderStruct{
Host: vmess.Host,
},
2023-09-12 12:46:17 -04:00
}
2023-09-12 06:40:24 -04:00
}
return result, nil
}