-
Notifications
You must be signed in to change notification settings - Fork 11
/
firewall.go
228 lines (213 loc) · 7.39 KB
/
firewall.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
package main
/*
import (
"fmt"
"strconv"
"strings"
"github.com/codegangsta/cli"
"github.com/toorop/govh"
"github.com/toorop/govh/ip"
)
// getFwCmds return commands for firewall subsection
func getFwCmds(client *govh.OVHClient) (fwCmds []cli.Command) {
ipr, err := ip.New(client)
if err != nil {
return
}
fwCmds = []cli.Command{
{
Name: "list",
Usage: "List IPs, of a given block, that are under firewall.",
Description: "ovh fw list IPBLOCK" + NLTAB + "Example: ovh fw list 91.121.228.135/32 ",
Action: func(c *cli.Context) {
if len(c.Args()) == 0 {
dieBadArgs()
}
ips, err := ipr.FwListIPOfBlock(ip.IPBlock{c.Args().First(), ""})
handleErrFromOvh(err)
for _, ip := range ips {
fmt.Println(ip)
}
dieOk()
},
},
{
Name: "add",
Usage: "Add an IP of IPBLOCK on firewall.",
Description: "ovh fw add IPBLOCK IP" + NLTAB + "Example: ovh fw add 92.222.14.249/32 92.222.14.249",
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
err := ipr.FwAddIP(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1))
handleErrFromOvh(err)
dieDone()
},
},
{
Name: "remove",
Usage: "Remove an IP of IPBLOCK from firewall.",
Description: "ovh fw remove IPBLOCK IP" + NLTAB + "Example: ovh fw remove 92.222.14.249/32 92.222.14.249",
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
err := ipr.FwRemoveIP(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1))
handleErrFromOvh(err)
dieDone()
},
},
{
Name: "getProperties",
Usage: "Get properties of an IP on the firewall.",
Description: "ovh fw getProperties IPBLOCK IP " + NLTAB + "Example: ovh fw getProperties 92.222.14.249/32 92.222.14.249",
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
p, err := ipr.FwGetIPProperties(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1))
handleErrFromOvh(err)
dieOk(fmt.Sprintf("Ip: %s%sEnabled: %t%sState: %s", p.Ip, NL, p.Enabled, NL, p.State))
},
},
{
Name: "update",
Usage: "Update an IP on the firewall.",
Description: "ovh fw update IPBLOCK IP [--flag...]" + NLTAB + "Example: ovh fw update 92.222.14.249/32 92.222.14.249 --enable true",
Flags: []cli.Flag{
cli.StringFlag{"enabled", "", "Set enabled state of the IP (true|false).", ""},
},
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
fEnabled := c.Bool("enabled")
err := ipr.FwUpdateIP(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1), fEnabled)
handleErrFromOvh(err)
dieDone()
},
},
{
Name: "listRules",
Usage: "Return a list ao rule sequences.",
Description: "ovh fw listRule IPBLOCK IP [--state]" + NLTAB + "Example: ovh fw listRule 92.222.14.249/32 92.222.14.249 --state ok",
Flags: []cli.Flag{
cli.StringFlag{"state", "", "Filter on state (creationPending|ok|removalPending).", ""},
},
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
var sequences []int
if c.IsSet("state") {
fState := c.String("state")
sequences, err = ipr.FwListRules(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1), fState)
} else {
sequences, err = ipr.FwListRules(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1))
}
handleErrFromOvh(err)
for _, seq := range sequences {
fmt.Println(seq)
}
dieOk()
},
},
{
Name: "addRule",
Usage: "Add a new rule on an IP.",
Description: "ovh fw addRule [--flag...] IPBLOCK IP " + NLTAB + "Example: ovh fw addRule --action deny --protocol tcp --toPort 22 --sequence 0 92.222.14.249/32 92.222.14.249",
Flags: []cli.Flag{
cli.StringFlag{"action", "", "Action on this rule (deny|permit). Required.", ""},
cli.StringFlag{"sequence", "", "Sequence number of your rule. Required.", ""},
cli.StringFlag{"protocol", "", "Network protocol (ah|esp|gre|icmp|ipv4|tcp|udp). Requiered.", ""},
cli.StringFlag{"fromPort", "", "Source port for your rule. Only with TCP/UDP protocol", ""},
cli.StringFlag{"fromIp", "", "Source ip for your rule. Any if not set.", ""},
cli.StringFlag{"toPort", "", "Destination port for your rule. Only with TCP/UDP protocol.", ""},
cli.StringFlag{"tcpFragments", "", "Can only be used with TCP protocol (true|false)", ""},
cli.StringFlag{"tcpOption", "", "Can only be used with TCP protocol (established|syn)", ""},
},
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 2)
rule := ip.FwRule2Add{}
// action
if !c.IsSet("action") {
dieBadArgs()
}
action := strings.ToLower(c.String("action"))
if !inSliceStr(action, []string{"deny", "permit"}) {
dieBadArgs()
}
rule.Action = action
// sequence
if !c.IsSet("sequence") {
dieBadArgs()
}
sequence := c.Int("sequence")
rule.Sequence = sequence
// protocol
if !c.IsSet("protocol") {
dieBadArgs()
}
protocol := strings.ToLower(c.String("protocol"))
if !inSliceStr(protocol, []string{"ah", "esp", "gre", "icmp", "ipv4", "tcp", "udp"}) {
dieBadArgs()
}
rule.Protocol = protocol
// fromPort
if c.IsSet("fromPort") {
rule.FromPort = c.Int("fromPort")
}
// fromIP
if c.IsSet("fromIp") {
rule.FromIp = c.String("fromIp")
}
// toPort
if c.IsSet("toPort") {
rule.ToPort = c.Int("toPort")
}
// fwTcpOption
fwTCPOption := ip.FwTcpOption{}
flagFwTCPOption := false
// tcpOptionFragment
if c.IsSet("tcpFragments") {
fwTCPOption.Fragments = c.Bool("tcpFragments")
flagFwTCPOption = true
}
// tcpOption
if c.IsSet("tcpOption") {
TCPOption := c.String("tcpOption")
if !inSliceStr(TCPOption, []string{"established", "syn"}) {
dieBadArgs()
}
fwTCPOption.Option = TCPOption
flagFwTCPOption = true
}
if flagFwTCPOption {
rule.TcpOption = &fwTCPOption
}
handleErrFromOvh(ipr.FwAddRule(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1), rule))
dieDone()
},
},
{
Name: "removeRule",
Usage: "Remove a firwall rule.",
Description: "ovh fw removeRule IPBLOCK IP SEQUENCE" + NLTAB + "Example: ovh fw removeRule 92.222.14.249/32 92.222.14.249 0",
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 3)
sequence, err := strconv.ParseInt(c.Args().Get(2), 10, 16)
if err != nil {
dieError(err)
}
handleErrFromOvh(ipr.FwRemoveRule(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1), int(sequence)))
dieDone()
},
},
{
Name: "getRuleProperties",
Usage: "Get properties of firewall rule.",
Description: "ovh fw getRuleProperties IPBLOCK IP SEQUENCE " + NLTAB + "Example: ovh fw getRuleProperties 92.222.14.249/32 92.222.14.249 0",
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 3)
sequence, err := strconv.ParseInt(c.Args().Get(2), 10, 16)
if err != nil {
dieError(err)
}
p, err := ipr.FwGetRuleProperties(ip.IPBlock{c.Args().First(), ""}, c.Args().Get(1), int(sequence))
handleErrFromOvh(err)
dieOk(fmt.Sprintf("Sequence: %d%sCreated: %s%sProtocol: %s%sFromIp: %s%sFromPort: %s%sToIP: %s%sToPort: %s%sAction: %s%sRule:%s%sState: %s%sTcpOption: %s%sFragments: %t", p.Sequence, NL, p.CreationDate, NL, p.Protocol, NL, p.FromIp, NL, p.FromPort, NL, p.ToIp, NL, p.ToPort, NL, p.Action, NL, p.Rule, NL, p.State, NL, p.TcpOption, NL, p.Fragments))
},
},
}
return
}*/