mirror of
https://github.com/nitezs/sub2clash.git
synced 2024-12-25 01:04:41 -05:00
8d06ab3175
fix: 修复当订阅链接有多个 clash 配置时丢失节点的问题 update: 增加检测更新 modify: 修改数据库路径 modify: 修改短链生成逻辑 modify: 统一输出信息
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package parser
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sub2clash/model"
|
|
)
|
|
|
|
func ParseVmess(proxy string) (model.Proxy, error) {
|
|
// 判断是否以 vmess:// 开头
|
|
if !strings.HasPrefix(proxy, "vmess://") {
|
|
return model.Proxy{}, fmt.Errorf("invalid vmess Url")
|
|
}
|
|
// 解码
|
|
base64, err := DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
|
|
if err != nil {
|
|
return model.Proxy{}, errors.New("无效的 vmess Url")
|
|
}
|
|
// 解析
|
|
var vmess model.Vmess
|
|
err = json.Unmarshal([]byte(base64), &vmess)
|
|
if err != nil {
|
|
return model.Proxy{}, errors.New("无效的 vmess Url")
|
|
}
|
|
// 处理端口
|
|
port, err := strconv.Atoi(strings.TrimSpace(vmess.Port))
|
|
if err != nil {
|
|
return model.Proxy{}, errors.New("无效的 vmess Url")
|
|
}
|
|
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,
|
|
}
|
|
if vmess.Net == "ws" {
|
|
result.WSOpts = model.WSOptsStruct{
|
|
Path: vmess.Path,
|
|
Headers: model.HeaderStruct{
|
|
Host: vmess.Host,
|
|
},
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|