forked from ahmetozer/iptables-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables.go
174 lines (155 loc) · 4.18 KB
/
iptables.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
package main
import (
"bufio"
"bytes"
"crypto/sha1"
"fmt"
"log"
"os"
"os/exec"
"strings"
)
type errorArray []error
//Iptables add and delete mode for Iptables commands to related domain
func (obj Domain) Iptables(first_resolution string, second_resolution string) error {
// Remove netfilter rules for old IPs
var errA errorArray
if first_resolution != "" {
// Replace this function with has table
err := obj.IptablesExecuter("delete", first_resolution)
if err != nil {
errA = append(errA, err)
}
}
if second_resolution != "" {
err := obj.IptablesExecuter("add", second_resolution)
if err != nil {
errA = append(errA, err)
}
} else {
return fmt.Errorf("the second_resolution is not to be empty")
}
if errA != nil {
return fmt.Errorf("%s", errA)
}
return nil
}
//IptablesExecuter
func (obj Domain) IptablesExecuter(mode string, address string) error {
var iptablesCommand string
if obj.Qtype == "ip" {
iptablesCommand = "iptables"
} else if obj.Qtype == "ip6" {
iptablesCommand = "ip6tables"
} else {
panic(fmt.Sprintf("Unknown iptables mode %s %s", obj.Name, obj.Qtype))
}
file, err := os.Open(iptablesFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNumber := 0
for scanner.Scan() {
lineNumber++
// If its not commend and contains domain name
if len(scanner.Text()) != 0 && (scanner.Text()[0] != 35 && DomainContains(scanner.Text(), obj.Name, obj.Qtype)) {
if debug {
fmt.Println("Line", lineNumber, " ", scanner.Text())
}
if mode == "add" {
// Detect Add or inject
rule1 := ""
words := strings.Fields(scanner.Text())
for _, arg := range words {
if arg == "-I" {
rule1 = "-I"
break
} else if arg == "-A" {
rule1 = "-A"
break
}
}
if rule1 == "" {
words = append([]string{"-A"}, words...)
}
// if debug {
// fmt.Printf("%s\n", words)
// }
obj.replaceHostToAddr(&words, address)
if debug {
fmt.Printf("%s %v\n", iptablesCommand, words)
}
currentRuleHash, newRule := ruleHash(iptablesCommand, words)
_, currentRuleExist := ddnsCurrentTable[currentRuleHash]
if !currentRuleExist {
err := iptables(iptablesCommand, words...)
if err != nil {
fmt.Printf("%s\n", err)
} else {
ddnsCurrentTable[currentRuleHash] = newRule
}
}
} else if mode == "delete" {
// Detect Add or inject
words := strings.Fields(scanner.Text())
// if debug {
// fmt.Printf("%s\n", words)
// }
if words[0] == "-I" || words[0] == "-A" {
words[0] = "-D"
} else {
words = append([]string{"-D"}, words...)
}
obj.replaceHostToAddr(&words, address)
if debug {
fmt.Printf("%s %v\n", iptablesCommand, words)
}
err := iptables(iptablesCommand, words...)
if err == nil {
// Remove rule from ddnsCurrentTable
currentRuleHash, _ := ruleHash(iptablesCommand, words)
_, currentRuleExist := ddnsCurrentTable[currentRuleHash]
if currentRuleExist {
delete(ddnsCurrentTable, currentRuleHash)
}
} else {
fmt.Printf("%s\n", err)
}
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
//fmt.Printf("\tIptables function %s type %s mode %s address %s\n", obj.Name, obj.Qtype, mode, address)
return nil
}
func (obj Domain) replaceHostToAddr(k *[]string, new string) {
for i := range *k {
if StartWithHost((*k)[i], obj.Name, obj.Qtype) {
(*k)[i] = strings.Replace((*k)[i], obj.Name, new, 1)
}
}
}
func iptables(iptablesCommand string, words ...string) error {
wait := []string{"-w", "100"}
cmd := exec.Command(iptablesCommand, append(words, wait...)...)
var cmdErr, cmdOut bytes.Buffer
cmd.Stderr = &cmdErr
cmd.Stdout = &cmdOut
err := cmd.Run()
if err != nil || cmdOut.String() != "" {
return fmt.Errorf("%s %s: %s %s %s", iptablesCommand, words, err, cmdErr.String(), cmdOut.String())
}
return nil
}
func ruleHash(iptablesCommand string, obj []string) (string, []string) {
temp_obj := make([]string, len(obj), cap(obj))
copy(temp_obj, obj)
temp_obj[0] = iptablesCommand
h := sha1.New()
h.Write([]byte(strings.Join(temp_obj, "")))
return fmt.Sprintf("%x", h.Sum(nil)), temp_obj
}