-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rules.go
215 lines (199 loc) · 5.26 KB
/
Rules.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"github.com/mkideal/cli"
)
type rulesT struct {
cli.Helper
ConfigName string `cli:"C,config" usage:"Specify the config to use" dft:"config.json"`
Delete bool `cli:"d,delete" usage:"Delete iptable rules for given config. Disabe blocking IPs for given config"`
Create bool `cli:"c,create" usage:"Create iptable rules for given config. Enable blocking IPs for given config"`
Update bool `cli:"u,update" usage:"Update iptables rules for given config"`
Yes bool `cli:"y,yes" usage:"Don't confirm deletion" dft:"false"`
Verbose int `cli:"v,verbose" usage:"Specify how much logs should be displayed" dft:"0"`
}
func (argv *rulesT) Validate(ctx *cli.Context) error {
if !checkBoolArrUnique(argv.Create, argv.Delete, argv.Update) {
return errors.New("you need to enter a command")
}
return nil
}
func checkBoolArrUnique(bools ...bool) bool {
hasTrue := false
trueCount := 0
for _, b := range bools {
if b {
hasTrue = true
trueCount++
}
}
return hasTrue && trueCount == 1
}
var rulesCMD = &cli.Command{
Name: "rules",
Aliases: []string{"rule", "rul"},
Desc: "change iptable rules created by triplink",
Argv: func() interface{} { return new(rulesT) },
Fn: func(ctx *cli.Context) error {
argv := ctx.Argv().(*rulesT)
verboseLevel = argv.Verbose
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Couldn't retrieve homeDir!")
return nil
}
if os.Getuid() != 0 {
LogError("Only root can do this!")
return nil
}
confPath := getConfPath(homeDir)
confFile := getConfFile(confPath, argv.ConfigName)
_, err = os.Stat(confFile)
if err == nil {
realConf := readConfig(confFile)
//Warn/confirm if auto rules are disabled
if !realConf.AutocreateIptables && !argv.Yes && (argv.Create || argv.Update) {
cont, i := confirmInput("Auto iptable rules are disabled for this configuration. Do you want to continue anyway? [y/n] > ", bufio.NewReader(os.Stdin))
if i != 1 || !cont {
return nil
}
}
success := false
bln := getBlocklistName(confFile)
if argv.Delete {
deleteBlocklistIptableRules(bln)
success = true
} else if argv.Create {
success = createIPtableRules(bln, realConf)
} else if argv.Update {
deleteBlocklistIptableRules(bln)
success = createIPtableRules(bln, realConf)
}
if success {
fmt.Println("Success")
}
} else {
fmt.Println("Config not found. Nothing to do.")
}
return nil
},
}
func createIPtableRules(blocklistName string, config *Config) bool {
if !isIpsetInstalled(true) {
return false
}
setupIPset(blocklistName)
errorCreatingtriplinkChain := checkChain("triplink")
if errorCreatingtriplinkChain {
LogError("Couldn't create triplinkchain! Blocking might be unavailable")
return false
}
//check/create blocklistname-chain
errorCreatingblnChain := checkChain(blocklistName)
if errorCreatingblnChain {
LogError("Couldn't create blocklist-chain! Blocking might be unavailable")
return false
}
blocklistTCPUDPname := blocklistName + "_tcp_udp"
//check/create blocklistname_-chain
errorCreatingblnportChain := checkChain(blocklistTCPUDPname)
if errorCreatingblnportChain {
LogError("Couldn't create blocklist_tcp_udp-chain! Blocking might be unavailable")
return false
}
commands := []iptableCommand{
//INPUT -> triplink
iptableCommand{
"A",
"INPUT -j triplink",
},
//triplink -> bloclist_config if not udp
iptableCommand{
"I",
"triplink ! -p udp -j " + blocklistName,
},
//DROP if not tcp
iptableCommand{
"I",
blocklistName + " ! -p tcp -m set --match-set " + blocklistName + " src -j DROP",
},
//triplink -> bloclist_config if not udp
iptableCommand{
"I",
"triplink -j " + blocklistTCPUDPname,
},
//RETURN back to triplink
iptableCommand{
"A",
blocklistName + " -j RETURN",
},
//DROP TCP PORTS
iptableCommand{
"I",
blocklistTCPUDPname + " -p tcp -m set --match-set " + blocklistName + " src -m multiport --dports " + config.PortsToBlock + " -j DROP",
},
iptableCommand{
"I",
blocklistTCPUDPname + " -p udp -m set --match-set " + blocklistName + " src -m multiport --dports " + config.PortsToBlock + " -j DROP",
},
iptableCommand{
"A",
blocklistTCPUDPname + " -j RETURN",
},
iptableCommand{
"A",
"triplink -j RETURN",
},
}
for _, cmd := range commands {
if !runIptablesAction(cmd) {
return false
}
}
return true
}
func deleteBlocklistIptableRules(blocklistName string) bool {
blocklistTCPUDPname := blocklistName + "_tcp_udp"
commandso := []iptableCommand{
//remove triplink -> bloclist_config
iptableCommand{
"D",
"triplink ! -p udp -j " + blocklistName,
},
//remove triplink -> bloclist_config_tcp_udp
iptableCommand{
"D",
"triplink -j " + blocklistTCPUDPname,
},
//Flush blocklist_config
iptableCommand{
"F",
blocklistName,
},
//Flush blocklist_config_tcp_udp
iptableCommand{
"F",
blocklistTCPUDPname,
},
//Flush blocklist_config
iptableCommand{
"X",
blocklistName,
},
//Flush blocklist_config_tcp_udp
iptableCommand{
"X",
blocklistTCPUDPname,
},
}
for _, cmd := range commandso {
if !runIptablesAction(cmd, true) {
continue
}
}
runCommand(nil, "iptables -X "+blocklistName)
return true
}