-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (187 loc) · 5 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
package main
import (
"errors"
"io"
"net/http"
"os"
"os/signal"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/robfig/cron/v3"
"github.com/DarkOnion0/IpMonitor/config"
)
var (
currentIP string
previousIP string
)
// This is the response type for every request sent to the / endpoint when the API is enabled
type JSONRespT struct {
CurrentIP string
PreviousIP string
IpChanged bool
}
func init() {
// enable or not the debug level (default is Info)
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *config.Debug == "true" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.With().Caller().Logger()
// activate the pretty logger for dev purpose only if the debug mode is enabled
if *config.Debug == "true" {
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
log.Info().
Str("type", "main").
Str("function", "init").
Msg("Logger is configured!")
log.Debug().
Str("type", "main").
Str("function", "init").
Msg("Debug mode is enabled!")
}
func init() {
log.Debug().
Str("Debug", *config.Debug).
Str("Cron", *config.Cron).
Str("EnableCron", *config.EnableCron).
Str("APIPort", *config.APIPort).
Str("EnableAPI", *config.EnableAPI).
Msg("Printing the default settings")
}
// This function check if the public server IP has changed since the last lookup and return a warn log if it's the case
func ipChecker() (err error, ipChanged bool) {
funcLog := log.With().
Str("function", "IpChecker").
Logger()
funcLog.Debug().
Msg("Start the function")
funcLog.Debug().
Msg("Send a GET request to the Cloudflare API")
resp, err1 := http.Get("https://cloudflare.com/cdn-cgi/trace")
if err1 != nil {
funcLog.Error().
Err(err1).
Msg("Something bad append while getting the server if from Cloudlfare server, is your internet down ?")
return errors.New("Something bad append while getting the server if from Cloudlfare server"), ipChanged
}
defer resp.Body.Close()
funcLog.Debug().
Msg("Start reading the response body")
requestBody, err2 := io.ReadAll(resp.Body)
if err2 != nil {
funcLog.Error().
Err(err2).
Msg("Something bad append while reading the response body")
return errors.New("Something bad append while reading the response body"), ipChanged
}
currentIP = strings.Split(strings.Split(string(requestBody), "\n")[2], "=")[1]
switch previousIP {
case currentIP:
funcLog.Info().
Str("currentIP", currentIP).
Str("previousIP", previousIP).
Bool("ipChanged", false).
Msg("The ip has not changed since the last check: function finished successfully")
return err, false
case "":
funcLog.Info().
Str("currentIP", currentIP).
Str("previousIP", previousIP).
Bool("ipChanged", false).
Msg("This is the first run of the function, previousIP is not set: function finished successfully")
previousIP = currentIP
currentIP = ""
return err, false
default:
funcLog.Warn().
Str("currentIP", currentIP).
Str("previousIP", previousIP).
Bool("ipChanged", true).
Msg("The ip has changed since last check: function finished successfully")
previousIP = currentIP
currentIP = ""
return err, true
}
}
func main() {
funcLog := log.With().
Str("function", "Main").
Logger()
funcLog.Info().
Msg("Start the app")
if *config.EnableCron == "true" {
c := cron.New()
// set a cron job to check the public IP every 10 minutes
// nolint
c.AddFunc(*config.Cron, func() {
err, _ := ipChecker()
if err != nil {
funcLog.Error().
Str("type", "cron").
Err(err).
Msg("Something bad append while running the ipChecker function")
}
})
// start all the cron jobs
funcLog.Debug().
Str("type", "cron").
Msg("Start the cron jobs")
c.Start()
defer func(c *cron.Cron) {
funcLog.Debug().
Str("type", "cron").
Msg("Closing cron jobs")
c.Stop()
}(c)
}
if *config.EnableAPI == "true" {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
funcLog.Debug().
Str("type", "API").
Str("endpoint", "/").
Msg("Endpoint has been triggered")
err, ipChanged := ipChecker()
if err != nil {
funcLog.Error().
Str("type", "API").
Err(err).
Msg("Something bad append while running the ipChecker function")
return err
}
resp := JSONRespT{
CurrentIP: currentIP,
PreviousIP: previousIP,
IpChanged: ipChanged,
}
return c.JSON(resp)
})
funcLog.Debug().
Str("type", "API").
Msg("Starting fiber API server")
err := app.Listen(":" + *config.APIPort)
if err != nil {
funcLog.Error().
Str("type", "API").
Err(err).
Msg("Something bad append while starting the fiber API")
return
}
}
err, _ := ipChecker()
if err != nil {
funcLog.Error().
Err(err).
Msg("Something bad append while running the ipChecker function")
return
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
funcLog.Info().
Msg("Closing the app")
}