forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
178 lines (150 loc) · 3.63 KB
/
common.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package conf
import (
"encoding/json"
"strings"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
)
type StringList []string
func NewStringList(raw []string) *StringList {
list := StringList(raw)
return &list
}
func (v StringList) Len() int {
return len(v)
}
func (v *StringList) UnmarshalJSON(data []byte) error {
var strarray []string
if err := json.Unmarshal(data, &strarray); err == nil {
*v = *NewStringList(strarray)
return nil
}
var rawstr string
if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(rawstr, ",")
*v = *NewStringList(strlist)
return nil
}
return newError("unknown format of a string list: " + string(data))
}
type Address struct {
v2net.Address
}
func (v *Address) UnmarshalJSON(data []byte) error {
var rawStr string
if err := json.Unmarshal(data, &rawStr); err != nil {
return err
}
v.Address = v2net.ParseAddress(rawStr)
return nil
}
func (v *Address) Build() *v2net.IPOrDomain {
return v2net.NewIPOrDomain(v.Address)
}
type Network string
func (v Network) Build() v2net.Network {
return v2net.ParseNetwork(string(v))
}
type NetworkList []Network
func (v *NetworkList) UnmarshalJSON(data []byte) error {
var strarray []Network
if err := json.Unmarshal(data, &strarray); err == nil {
nl := NetworkList(strarray)
*v = nl
return nil
}
var rawstr Network
if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(string(rawstr), ",")
nl := make([]Network, len(strlist))
for idx, network := range strlist {
nl[idx] = Network(network)
}
*v = nl
return nil
}
return newError("unknown format of a string list: " + string(data))
}
func (v *NetworkList) Build() *v2net.NetworkList {
if v == nil {
return &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
}
}
list := new(v2net.NetworkList)
for _, network := range *v {
list.Network = append(list.Network, network.Build())
}
return list
}
func parseIntPort(data []byte) (v2net.Port, error) {
var intPort uint32
err := json.Unmarshal(data, &intPort)
if err != nil {
return v2net.Port(0), err
}
return v2net.PortFromInt(intPort)
}
func parseStringPort(data []byte) (v2net.Port, v2net.Port, error) {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
pair := strings.SplitN(s, "-", 2)
if len(pair) == 0 {
return v2net.Port(0), v2net.Port(0), newError("Config: Invalid port range: ", s)
}
if len(pair) == 1 {
port, err := v2net.PortFromString(pair[0])
return port, port, err
}
fromPort, err := v2net.PortFromString(pair[0])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
toPort, err := v2net.PortFromString(pair[1])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
return fromPort, toPort, nil
}
type PortRange struct {
From uint32
To uint32
}
func (v *PortRange) Build() *v2net.PortRange {
return &v2net.PortRange{
From: v.From,
To: v.To,
}
}
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
func (v *PortRange) UnmarshalJSON(data []byte) error {
port, err := parseIntPort(data)
if err == nil {
v.From = uint32(port)
v.To = uint32(port)
return nil
}
from, to, err := parseStringPort(data)
if err == nil {
v.From = uint32(from)
v.To = uint32(to)
if v.From > v.To {
return newError("invalid port range ", v.From, " -> ", v.To)
}
return nil
}
return newError("invalid port range: ", string(data))
}
type User struct {
EmailString string `json:"email"`
LevelByte byte `json:"level"`
}
func (v *User) Build() *protocol.User {
return &protocol.User{
Email: v.EmailString,
Level: uint32(v.LevelByte),
}
}