-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvmess.go
More file actions
121 lines (115 loc) · 2.89 KB
/
vmess.go
File metadata and controls
121 lines (115 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
)
type XrVmessServerConfig struct {
Vmess []VmessServerConfig `json:"vnext"`
}
type VmessServerConfig struct {
Address string `json:"address"`
Port int `json:"port"`
Users []VmessUser `json:"users"`
}
type VmessUser struct {
Id string `json:"id"`
//AlterId string `json:"alterId"` // for v2fly
Security string `json:"security,omitempty"`
Level int `json:"level,omitempty"`
}
func decodeVmessServerConfig(str string) bool {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
fmt.Println("error:", err)
return false
}
datastr := string(data[:])
errstr := createVmessServerConfig(datastr)
if errstr != "" {
fmt.Println(errstr)
return false
}
return true
}
func createVmessServerConfig(str string) (errstr string) {
var params map[string]any
err := json.Unmarshal([]byte(str), ¶ms)
if err != nil {
fmt.Println("Error unmarshaling JSON vmess:", err)
return
}
if len(params) > 0 {
conf := new(VmessServerConfig)
conf.Address = params["add"].(string)
//check ip
if !isIpValid(config.IpCheckServer, conf.Address, config.IpCheckKey,
config.IpCheckValue, config.IpCheckBlackList) {
return "VM Ip is invalid"
}
//
switch t := params["port"].(type) {
case float64:
conf.Port = int(t)
case string:
i, err := strconv.Atoi(t)
if err != nil {
return "VM Cannot convert vmess port"
} else {
conf.Port = i
}
default:
return "VM Invalid format of vmess port"
}
user := new(VmessUser)
user.Id = params["id"].(string)
scy, ok := params["scy"].(string)
if ok && len(scy) > 0 {
user.Security = scy
}
conf.Users = append(conf.Users, *user)
streamSettings := new(XrStreamSettings)
netType, ok := params["net"].(string)
if ok && len(netType) > 0 {
streamSettings.Network = netType
switch netType {
case "tcp":
streamSettings.Network = "tcp"
case "ws":
streamSettings.Network = "ws"
path, ok := params["path"].(string)
if ok && len(path) > 0 {
//check path
wspar := new(XrWsSettings)
wspar.Path = path
streamSettings.WsSettings = *wspar
}
}
}
tls, ok := params["tls"].(string)
if ok && len(tls) > 0 {
streamSettings.Security = "tls"
host, ok := params["host"].(string)
tlsSet := new(XrTlsSettings)
if ok && len(host) > 0 {
tlsSet.ServerName = host
}
fp, ok := params["fp"].(string)
if ok && len(fp) > 0 {
tlsSet.ServerName = fp
}
}
xrconf := new(XrayConf)
xrconf.Protocol = "vmess"
servers := new(XrVmessServerConfig)
servers.Vmess = append(servers.Vmess, *conf)
xrconf.Settings = servers
xrconf.StreamSet = *streamSettings
confToSave++
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //config.VmessTag + strconv.Itoa(len(xrVmConfigs)+1)
xrVmConfigs = append(xrVmConfigs, *xrconf)
vmessConfToSave = vmessConfToSave + 1
}
return ""
}