-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
274 lines (245 loc) · 8.4 KB
/
main.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
package main
import (
"crypto/md5"
"encoding/hex"
"strconv"
"strings"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"os/exec"
"github.com/fsnotify/fsnotify"
"bufio"
"bytes"
)
type Config struct {
ProgramSettings struct {
LogFilePath string `yaml:"log_file_path"`
Logging struct {
Info bool `yaml:"info"`
Error bool `yaml:"error"`
Debug bool `yaml:"debug"`
} `yaml:"logging"`
} `yaml:"program_settings"`
GRETunnels []struct {
Name string `yaml:"name"`
LocalIP string `yaml:"local_ip"`
RemoteIP string `yaml:"remote_ip"`
TunnelIP string `yaml:"tunnel_ip"`
SubnetMask string `yaml:"subnet_mask"`
} `yaml:"gre_tunnels"`
StaticRoutes []struct {
Destination string `yaml:"destination"`
Gateway string `yaml:"gateway"`
} `yaml:"static_routes"`
ECMPRoutes []struct {
Route string `yaml:"route"`
Table string `yaml:"table"`
Nexthops []struct {
Dev string `yaml:"dev"`
Via string `yaml:"via"`
Weight int `yaml:"weight"`
} `yaml:"nexthops"`
} `yaml:"ecmp_routes"`
}
var (
currentHash string
logger *log.Logger
config Config
)
func initLogger(logFilePath string, infoEnabled, errorEnabled, debugEnabled bool) {
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
logger = log.New(logFile, "GRE-Manager: ", log.LstdFlags|log.Lshortfile)
logger.SetOutput(logWriter{log.New(os.Stdout, "", 0), log.New(logFile, "", 0), infoEnabled, errorEnabled, debugEnabled})
}
type logWriter struct {
stdoutLogger *log.Logger
fileLogger *log.Logger
infoEnabled bool
errorEnabled bool
debugEnabled bool
}
func (l logWriter) Write(p []byte) (n int, err error) {
message := string(p)
if l.infoEnabled || l.errorEnabled || l.debugEnabled {
l.stdoutLogger.Print(message) // Print to stdout
err = l.fileLogger.Output(2, message) // Also log to file
}
return len(p), err // Return the length of p and the error
}
func execCommand(command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
err := cmd.Run()
return out.String(), err
}
func tunnelExists(name string) bool {
output, _ := execCommand("ip", "tunnel", "show")
return strings.Contains(output, name)
}
func setupGRETunnels() error {
for _, tunnel := range config.GRETunnels {
if tunnelExists(tunnel.Name) {
_, err := execCommand("ip", "tunnel", "del", tunnel.Name)
if err != nil {
logger.Printf("Failed to delete tunnel %s: %v", tunnel.Name, err)
}
}
_, err := execCommand("ip", "tunnel", "add", tunnel.Name, "mode", "gre", "local", tunnel.LocalIP, "remote", tunnel.RemoteIP)
if err != nil {
return err
}
_, err = execCommand("ip", "addr", "add", tunnel.TunnelIP+"/"+tunnel.SubnetMask, "dev", tunnel.Name)
if err != nil {
return err
}
_, err = execCommand("ip", "link", "set", tunnel.Name, "up")
if err != nil {
return err
}
logger.Printf("Configured tunnel: %s", tunnel.Name)
}
return nil
}
func routeExists(destination, gateway string) bool {
output, _ := execCommand("ip", "route", "show")
return strings.Contains(output, destination) && strings.Contains(output, gateway)
}
func setupStaticRoutes() error {
for _, route := range config.StaticRoutes {
if !routeExists(route.Destination, route.Gateway) {
if _, err := execCommand("ip", "route", "add", route.Destination, "via", route.Gateway); err != nil {
logger.Printf("Failed to add static route %s via %s: %v", route.Destination, route.Gateway, err)
} else {
logger.Printf("Added static route: %s via %s", route.Destination, route.Gateway)
}
}
}
return nil
}
func ecmpRouteExists(route, table string) bool {
output, _ := execCommand("ip", "route", "show", "table", table)
scanner := bufio.NewScanner(strings.NewReader(output))
for scanner.Scan() {
if strings.Contains(scanner.Text(), route) {
return true
}
}
return false
}
func setupECMPRoutes() error {
for _, ecmp := range config.ECMPRoutes {
if !ecmpRouteExists(ecmp.Route, ecmp.Table) {
var nexthopArgs []string
for _, nh := range ecmp.Nexthops {
nexthopArgs = append(nexthopArgs, "nexthop", "dev", nh.Dev, "via", nh.Via, "weight", strconv.Itoa(nh.Weight))
}
args := append([]string{"route", "add", ecmp.Route, "proto", "static", "scope", "global", "table", ecmp.Table}, nexthopArgs...)
if _, err := execCommand("ip", args...); err != nil {
logger.Printf("Failed to add ECMP route %s: %v", ecmp.Route, err)
} else {
logger.Printf("Added ECMP route: %s", strings.Join(args, " "))
}
}
}
return nil
}
func getFileMD5(filePath string) (string, error) {
var md5String string
data, err := ioutil.ReadFile(filePath)
if err != nil {
return md5String, err
}
hash := md5.Sum(data)
md5String = hex.EncodeToString(hash[:])
return md5String, nil
}
func loadConfig(filePath string) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return err
}
return nil
}
func watchConfigFile(filePath string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
newHash, err := getFileMD5(filePath)
if err != nil {
logger.Println("Error reading file:", err)
continue
}
if newHash != currentHash {
currentHash = newHash
if err := loadConfig(filePath); err != nil {
logger.Printf("Error loading config: %v", err)
continue
}
if err := setupGRETunnels(); err != nil {
logger.Printf("Error setting up GRE tunnels: %v", err)
}
if err := setupStaticRoutes(); err != nil {
logger.Printf("Error setting up static routes: %v", err)
}
if err := setupECMPRoutes(); err != nil {
logger.Printf("Error setting up ECMP routes: %v", err)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
logger.Println("Error:", err)
}
}
}()
err = watcher.Add(filePath)
if err != nil {
logger.Fatal(err)
}
<-done
}
func main() {
configFilePath := "/etc/krouter/config.yml"
if err := loadConfig(configFilePath); err != nil {
log.Fatalf("Error loading initial config: %v", err)
}
initLogger(config.ProgramSettings.LogFilePath, config.ProgramSettings.Logging.Info, config.ProgramSettings.Logging.Error, config.ProgramSettings.Logging.Debug)
if err := setupGRETunnels(); err != nil {
logger.Fatalf("Error setting up initial GRE tunnels: %v", err)
}
if err := setupStaticRoutes(); err != nil {
logger.Fatalf("Error setting up initial static routes: %v", err)
}
if err := setupECMPRoutes(); err != nil {
logger.Fatalf("Error setting up initial ECMP routes: %v", err)
}
hash, err := getFileMD5(configFilePath)
if err != nil {
logger.Fatalf("Error computing initial file hash: %v", err)
}
currentHash = hash
watchConfigFile(configFilePath)
}