-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
291 lines (261 loc) · 7.72 KB
/
Copy pathrequest.go
File metadata and controls
291 lines (261 loc) · 7.72 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package vyos
import (
"encoding/json"
"fmt"
"net"
"net/url"
)
const (
ENDPOINT_CONFIGURE = "configure"
ENDPOINT_CONF_FILE = "config-file"
ENDPOINT_SHOW_CONFIGURATION = "retrieve"
ACTION_SET = "set"
ACTION_DELETE = "delete"
ACTTION_SHOW_CONFIGURATION = "showConfig"
VERSION_IPV4 = 4
VERSION_IPV6 = 6
)
type Action struct {
Op string `json:"op"`
Path []string `json:"path"`
}
// 一次执行一条动作
func (c *Client) executeAction(endpoint string, action Action) ([]byte, error) {
actionBytes, err := json.Marshal(action)
if err != nil {
return nil, err
}
return c.execute(endpoint, actionBytes)
}
// 一次执行多条动作
func (c *Client) executeBatchAction(endpoint string, actions []Action) ([]byte, error) {
actionBytes, err := json.Marshal(actions)
if err != nil {
return nil, err
}
return c.execute(endpoint, actionBytes)
}
func (c *Client) execute(endpoint string, actionBytes []byte) ([]byte, error) {
data := url.Values{}
data.Add("data", string(actionBytes))
data.Add("key", "")
contentType := "application/x-www-form-urlencoded"
return c.post(endpoint, nil, []byte(data.Encode()), contentType)
}
func (c *Client) decodeResp(resp []byte) error {
var respInfo apiResponse
if err := json.Unmarshal(resp, &respInfo); err != nil {
return err
}
if !respInfo.Success {
return fmt.Errorf("%s", respInfo.Error)
}
return nil
}
// SetAddress 设置网卡MTU和地址,address应由ip和掩码组成
func (c *Client) SetAddress(interfaceName, address string) error {
var actions []Action
// 网卡mtu必须设置
actions = append(actions, Action{
Op: ACTION_SET,
Path: []string{"interfaces", "ethernet", interfaceName, "mtu", "1450"},
})
// 地址不为空时再设置地址
if len(address) != 0 {
if _, _, err := net.ParseCIDR(address); err != nil {
return fmt.Errorf("invalid address, address ust be in CIDR format, e.g. 192.168.1.1/24")
}
actions = append(actions, Action{
Op: ACTION_SET,
Path: []string{"interfaces", "ethernet", interfaceName, "address", address},
})
}
resp, err := c.executeBatchAction(ENDPOINT_CONFIGURE, actions)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// DeleteAddress 删除网卡地址,如果不指定具体地址,则直接删除指定网卡上的所有地址
func (c *Client) DeleteAddress(interfaceName, address string) error {
action := Action{
Op: ACTION_DELETE,
Path: []string{"interfaces", "ethernet", interfaceName, "address"},
}
if len(address) != 0 {
if _, _, err := net.ParseCIDR(address); err != nil {
return fmt.Errorf("invalid address, address ust be in CIDR format, e.g. 192.168.1.1/24")
}
action.Path = append(action.Path, address)
}
resp, err := c.executeAction(ENDPOINT_CONFIGURE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// DeleteInterface 删除网卡
func (c *Client) DeleteInterface(interfaceName string) error {
action := Action{
Op: ACTION_DELETE,
Path: []string{"interfaces", "ethernet", interfaceName},
}
resp, err := c.executeAction(ENDPOINT_CONFIGURE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// SaveConfig 持久化保存配置,任何对配置的修改最后都应调这个方法
func (c *Client) SaveConfig() error {
action := Action{
Op: "save",
}
resp, err := c.executeAction(ENDPOINT_CONF_FILE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// AddSnat 创建snat规则
func (c *Client) AddSnat(ruleId int, srcIp, translationIp, interfaceName string) error {
srcIpInfo := net.ParseIP(srcIp)
if srcIpInfo == nil {
return fmt.Errorf("invalid srcIps %s", srcIp)
}
translationIpInfo := net.ParseIP(translationIp)
if translationIpInfo == nil {
return fmt.Errorf("invalid translationIp %s", translationIp)
}
// 判断两个ip的版本是否一致
if (srcIpInfo.To4() != nil && translationIpInfo.To4() == nil) || (srcIpInfo.To4() == nil && translationIpInfo.To4() != nil) {
return fmt.Errorf("ip类型不一致")
}
actions := []Action{
{
Op: ACTION_SET,
Path: []string{"nat", "source", "rule", fmt.Sprintf("%d", ruleId), "outbound-interface", interfaceName},
},
{
Op: ACTION_SET,
Path: []string{"nat", "source", "rule", fmt.Sprintf("%d", ruleId), "source", "address", srcIp},
},
{
Op: ACTION_SET,
Path: []string{"nat", "source", "rule", fmt.Sprintf("%d", ruleId), "translation", "address", translationIp},
},
}
resp, err := c.executeBatchAction(ENDPOINT_CONFIGURE, actions)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// DeleteSnat 删除snat
func (c *Client) DeleteSnat(ruleId int) error {
action := Action{
Op: ACTION_DELETE,
Path: []string{"nat", "source", "rule", fmt.Sprintf("%d", ruleId)},
}
resp, err := c.executeAction(ENDPOINT_CONFIGURE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// AddDnat 创建dnat规则
func (c *Client) AddDnat(ruleId int, dstIp, translationIp, interfaceName string) error {
dstIpInfo := net.ParseIP(dstIp)
if dstIpInfo == nil {
return fmt.Errorf("invalid dstIP %s", dstIp)
}
translationIpInfo := net.ParseIP(translationIp)
if translationIpInfo == nil {
return fmt.Errorf("invalid translationIp %s", translationIp)
}
// 判断两个ip的版本是否一致
if (dstIpInfo.To4() != nil && translationIpInfo.To4() == nil) || (dstIpInfo.To4() == nil && translationIpInfo.To4() != nil) {
return fmt.Errorf("ip类型不一致")
}
actions := []Action{
{
Op: ACTION_SET,
Path: []string{"nat", "destination", "rule", fmt.Sprintf("%d", ruleId), "inbound-interface", interfaceName},
},
{
Op: ACTION_SET,
Path: []string{"nat", "destination", "rule", fmt.Sprintf("%d", ruleId), "destination", "address", dstIp},
},
{
Op: ACTION_SET,
Path: []string{"nat", "destination", "rule", fmt.Sprintf("%d", ruleId), "translation", "address", translationIp},
},
}
resp, err := c.executeBatchAction(ENDPOINT_CONFIGURE, actions)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// DeleteDnat 删除dnat
func (c *Client) DeleteDnat(ruleId int) error {
action := Action{
Op: ACTION_DELETE,
Path: []string{"nat", "destination", "rule", fmt.Sprintf("%d", ruleId)},
}
resp, err := c.executeAction(ENDPOINT_CONFIGURE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// AddRoute 添加路由
func (c *Client) AddRoute(destNet, nextHop string, version int) error {
// 校验destNat,提取出cidr,如192.168.1.1/24提取出192.168.1.0/24
destNetIp, destCidr, err := net.ParseCIDR(destNet)
if err != nil {
return fmt.Errorf("invalid destNet %s", destNet)
}
nextHopIp := net.ParseIP(nextHop)
if nextHopIp == nil {
return fmt.Errorf("invalid nextHop %s", nextHop)
}
if version == VERSION_IPV4 && (destNetIp.To4() == nil || nextHopIp.To4() == nil) {
return fmt.Errorf("ip类型不一致")
}
if version == VERSION_IPV6 && (destNetIp.To4() != nil || nextHopIp.To4() != nil) {
return fmt.Errorf("ip类型不一致")
}
var routeType = "route"
if version == VERSION_IPV6 {
routeType = "route6"
}
action := Action{
Op: ACTION_SET,
Path: []string{"protocols", "static", routeType, destCidr.String(), "next-hop", nextHop},
}
resp, err := c.executeAction(ENDPOINT_CONFIGURE, action)
if err != nil {
return err
}
return c.decodeResp(resp)
}
// ShowConfiguration 获取所有配置
func (c *Client) ShowConfiguration() (any, error) {
action := Action{
Op: ACTTION_SHOW_CONFIGURATION,
Path: []string{},
}
resp, err := c.executeAction(ENDPOINT_SHOW_CONFIGURATION, action)
if err != nil {
return nil, err
}
var respInfo apiResponse
if err = json.Unmarshal(resp, &respInfo); err != nil {
return nil, err
}
if !respInfo.Success {
return nil, fmt.Errorf("%s", respInfo.Error)
}
return respInfo.Data, nil
}