-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
441 lines (424 loc) · 11.7 KB
/
command.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2022 kwanhur
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import "github.com/urfave/cli/v2"
var flags = []cli.Flag{
&cli.BoolFlag{
Name: "yes",
Aliases: []string{"force", "f", "y"},
Usage: "Are you agree to do it?[yes/no]",
Required: true,
},
}
var statFlags = []cli.Flag{
&cli.BoolFlag{
Name: "stats",
Aliases: []string{"s", "stat"},
Usage: "Show statistics information",
},
}
var vsFlags = []cli.Flag{
&cli.StringFlag{
Name: "vip",
Usage: "Specify vs IP address",
Required: true,
},
&cli.UintFlag{
Name: "vport",
Usage: "Specify vs port number, range [0-65535]",
Required: true,
},
&cli.StringFlag{
Name: "protocol",
Aliases: []string{"proto"},
Usage: "Specify vs protocol, option [TCP UDP SCTP]",
DefaultText: "TCP",
},
}
var vsFlags2 = []cli.Flag{
&cli.StringFlag{
Name: "vip",
Usage: "Specify vs IP address",
},
&cli.UintFlag{
Name: "vport",
Usage: "Specify vs port number, range [0-65535]",
},
}
var vsOptionFlags = []cli.Flag{
&cli.StringFlag{
Name: "scheduler",
Aliases: []string{"s", "sched"},
Usage: "Specify vs scheduling method, option [rr wrr lc wlc lblc lblcr dh sh sed nq]",
},
&cli.StringFlag{
Name: "persistent",
Aliases: []string{"pe", "per", "persis"},
Usage: "Specify vs persistent name",
},
&cli.UintFlag{
Name: "timeout",
Usage: "Specify persistent session timeout in seconds",
DefaultText: "300",
},
&cli.UintFlag{
Name: "netmask",
Aliases: []string{"M", "mask"},
Usage: "Specify which clients are grouped for persistent virtual service, default IPv4/32 IPv6/128",
},
}
var rsFlags = []cli.Flag{
&cli.StringFlag{
Name: "rip",
Usage: "Specify rs IP address",
Required: true,
},
&cli.UintFlag{
Name: "rport",
Usage: "Specify rs port number, range [0-65535]",
Required: true,
},
}
var rsFlags2 = append(rsFlags, []cli.Flag{
&cli.StringFlag{
Name: "forward",
Aliases: []string{"fwd"},
Usage: "Specify rs connection flag, option [local mask masq bypass dr tun fnat]",
DefaultText: "dr",
Required: true,
},
&cli.IntFlag{
Name: "weight",
Aliases: []string{"w"},
Usage: "Specify rs weight",
DefaultText: "0",
Required: true,
},
}...)
// BasicCommands return basic operations
func (o *Operator) BasicCommands() []*cli.Command {
return []*cli.Command{
{
Name: "zero",
Aliases: []string{"z"},
Usage: "Zero ipvs all the virtual service stats(byte packet and rate counters)",
Action: o.Zero(),
Flags: flags,
},
{
Name: "flush",
Aliases: []string{"f", "clear"},
Usage: "Flush out the virtual server table",
Action: o.FlushService(),
Flags: flags,
},
}
}
// ServiceCommands return service relate operations, like get set flush import export
func (o *Operator) ServiceCommands() []*cli.Command {
return []*cli.Command{
{
Name: "service",
Aliases: []string{"s", "svc", "vs"},
Usage: "Operates virtual service[vip:vport protocol] (TCP UDP STCP)/(IPv4 IPv6)",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l", "ls"},
Usage: "List ipvs virtual service",
Action: o.ListService(),
Flags: statFlags,
},
{
Name: "string",
Aliases: []string{"s", "str"},
Usage: "Present ipvs virtual service string",
Action: o.StringService(),
Flags: vsFlags,
},
{
Name: "check",
Aliases: []string{"c", "chk", "exist", "find"},
Usage: "Check ipvs virtual service exist or not",
Action: o.ExistService(),
Flags: vsFlags,
},
{
Name: "get",
Aliases: []string{"g", "fetch", "one"},
Usage: "Get ipvs virtual service",
Action: o.GetService(),
Flags: append(vsFlags, statFlags...),
},
{
Name: "add",
Aliases: []string{"a", "new", "n", "set"},
Usage: "Add ipvs virtual service",
Description: `Add a virtual service. A service address is uniquely defined by a triplet: IP address, port number, and protocol. Alternatively, a virtual service may be defined by a firewall-mark.`,
Action: o.AddService(),
Flags: append(vsFlags, vsOptionFlags...),
},
{
Name: "update",
Aliases: []string{"u", "up"},
Usage: "Update ipvs virtual service",
Action: o.UpdateService(),
Flags: append(vsFlags, vsOptionFlags...),
},
{
Name: "del",
Aliases: []string{"d", "delete"},
Usage: "Del ipvs virtual service",
Action: o.DelService(),
Flags: vsFlags,
},
{
Name: "zero",
Aliases: []string{"z"},
Usage: "Zero ipvs virtual server stats(byte packet and rate counters)",
Action: o.ZeroService(),
Flags: vsFlags,
},
{
Name: "flush",
Aliases: []string{"f", "clear"},
Usage: "Flush out the virtual server table",
Action: o.FlushService(),
Flags: flags,
},
},
},
}
}
// ServerCommands return server relate operations, like add update del flush
func (o *Operator) ServerCommands() []*cli.Command {
return []*cli.Command{
{
Name: "server",
Aliases: []string{"ser", "svr", "d", "dst", "dest", "destination", "rs", "realserver"},
Usage: "Operates real server[rip:rport] (IPv4/IPv6)",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l", "ls"},
Usage: "List ipvs real server",
Action: o.ListServer(),
Flags: append(vsFlags, statFlags...),
},
{
Name: "add",
Aliases: []string{"a", "new", "n", "set"},
Usage: "Add ipvs real server",
Action: o.AddServer(),
Flags: append(vsFlags, rsFlags2...),
},
{
Name: "update",
Aliases: []string{"u", "up"},
Usage: "Update ipvs real server",
Action: o.UpdateServer(),
Flags: append(vsFlags, rsFlags2...),
},
{
Name: "del",
Aliases: []string{"d", "delete"},
Usage: "Del ipvs real server",
Action: o.DelServer(),
Flags: append(vsFlags, rsFlags...),
},
{
Name: "flush",
Aliases: []string{"f", "clear", "purge"},
Usage: "Flush rs, all the real servers will be clear",
Action: o.FlushServer(),
Flags: vsFlags,
},
},
},
}
}
// AddressCommands return local address relate operations, like add del flush
func (o *Operator) AddressCommands() []*cli.Command {
return []*cli.Command{
{
Name: "address",
Aliases: []string{"a", "addr", "local-address", "la", "laddr"},
Usage: "Operates local address (IPv4/IPv6)",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l", "ls"},
Usage: "List ipvs local address",
Action: o.ListAddress(),
Flags: vsFlags,
},
{
Name: "add",
Aliases: []string{"a", "new", "n", "set"},
Usage: "Add ipvs local address",
Action: o.AddAddress(),
Flags: append(vsFlags, &cli.StringSliceFlag{
Name: "lip",
Usage: "Specify vs local address, multiple with separator comma",
Required: true,
}),
},
{
Name: "del",
Aliases: []string{"d", "delete"},
Usage: "Del ipvs local address",
Action: o.DelAddress(),
Flags: append(vsFlags, &cli.StringSliceFlag{
Name: "lip",
Usage: "Specify vs local address, multiple with separator comma",
Required: true,
}),
},
{
Name: "flush",
Aliases: []string{"f", "clear", "purge"},
Usage: "Flush all the local address",
Action: o.FlushAddress(),
Flags: vsFlags,
},
},
},
}
}
// DaemonCommands return daemon relate operations, like show add del
func (o *Operator) DaemonCommands() []*cli.Command {
return []*cli.Command{
{
Name: "daemon",
Aliases: []string{"dm"},
Usage: "Operates synchronization daemon",
Subcommands: []*cli.Command{
{
Name: "show",
Aliases: []string{"ls", "get"},
Usage: "Shows daemons of state, sync-id and mcast-interface",
Action: o.ShowDaemon(),
},
{
Name: "add",
Aliases: []string{"a", "new", "n", "set"},
Usage: "Add daemon, currently only supports IPv4 connections",
Action: o.AddDaemon(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "state",
Usage: "Specify daemon state, option [master backup]",
Required: true,
},
&cli.UintFlag{
Name: "sync-id",
Usage: "Specify daemon syncId",
Required: true,
},
&cli.StringFlag{
Name: "mcast-ifn",
Usage: "Specify daemon mcast-interface",
Required: true,
},
},
},
{
Name: "del",
Aliases: []string{"d", "del"},
Usage: "Del daemon",
Action: o.DelDaemon(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "state",
Usage: "Specify daemon state, option [master backup]",
Required: true,
},
},
},
},
},
}
}
// TimeoutCommands return timeout relate operations, like get set
func (o *Operator) TimeoutCommands() []*cli.Command {
var cmds = []*cli.Command{
{
Name: "timeout",
Aliases: []string{"t", "to", "out"},
Usage: "Operates timeout (tcp tcpfin udp)",
Subcommands: []*cli.Command{
{
Name: "show",
Aliases: []string{"ls", "get"},
Usage: "Shows timeout of tcp tcpfin udp",
Action: o.ShowTimeout(),
},
{
Name: "set",
Aliases: []string{"s"},
Usage: "Sets timeout of tcp tcpfin udp",
Description: `Change the timeout values used for IPVS connections. This command support 3 options, representing the timeout values (in seconds) for TCP sessions, TCP sessions after receiving a FIN packet, and UDP packets, respectively. A timeout value 0 means that the current timeout value of the corresponding entry is preserved.`,
Action: o.SetTimeout(),
Flags: []cli.Flag{
&cli.IntFlag{
Name: "tcp",
Aliases: []string{"t"},
Usage: "set tcp timeout(unit second), 0 means no change",
DefaultText: "900",
},
&cli.IntFlag{
Name: "tcpfin",
Aliases: []string{"f", "fin"},
Usage: "set tcpfin timeout(unit second), 0 means no change",
DefaultText: "120",
},
&cli.IntFlag{
Name: "udp",
Aliases: []string{"u"},
Usage: "set udp timeout(unit second), 0 means no change",
DefaultText: "300",
},
},
},
},
},
}
return cmds
}
// ConnectionCommands return connection relate operations, like show
func (o *Operator) ConnectionCommands() []*cli.Command {
var cmds = []*cli.Command{
{
Name: "connection",
Aliases: []string{"c", "conn"},
Usage: "Operates connection",
Subcommands: []*cli.Command{
{
Name: "show",
Aliases: []string{"ls", "get"},
Usage: "Shows ip_vs current connection",
Action: o.ShowConnection(),
Flags: append(vsFlags2, &cli.PathFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "specify ip_vs connection file path",
DefaultText: "/proc/net/ip_vs_conn",
}),
},
},
},
}
return cmds
}