@@ -14,16 +14,21 @@ import (
14
14
"github.com/charmbracelet/log"
15
15
"github.com/fsnotify/fsnotify"
16
16
"github.com/kitabisa/teler-proxy/common"
17
+ "github.com/kitabisa/teler-proxy/internal/cron"
17
18
"github.com/kitabisa/teler-proxy/pkg/tunnel"
19
+ "github.com/kitabisa/teler-waf"
20
+ "github.com/kitabisa/teler-waf/threat"
18
21
)
19
22
20
23
type Runner struct {
21
24
* common.Options
22
- * fsnotify. Watcher
25
+ * cron. Cron
23
26
* http.Server
24
27
25
28
shuttingDown bool
26
29
shutdownLock sync.Mutex
30
+ telerOpts teler.Options
31
+ watcher
27
32
}
28
33
29
34
func New (opt * common.Options ) error {
@@ -35,23 +40,17 @@ func New(opt *common.Options) error {
35
40
run := & Runner {Options : opt }
36
41
37
42
if opt .Config .Path != "" {
38
- watcher , err := fsnotify .NewWatcher ()
43
+ w , err := fsnotify .NewWatcher ()
39
44
if err != nil {
40
45
return err
41
46
}
42
47
43
- if err := watcher .Add (opt .Config .Path ); err != nil {
48
+ if err := w .Add (opt .Config .Path ); err != nil {
44
49
return err
45
50
}
46
51
47
- run .Watcher = watcher
48
- defer run .Watcher .Close ()
49
-
50
- go func () {
51
- if err := run .watch (); err != nil {
52
- opt .Logger .Fatal ("Something went wrong" , "err" , err )
53
- }
54
- }()
52
+ defer w .Close ()
53
+ run .watcher .config = w
55
54
}
56
55
57
56
dest := buildDest (opt .Destination )
@@ -70,7 +69,36 @@ func New(opt *common.Options) error {
70
69
Handler : tun ,
71
70
ErrorLog : logger ,
72
71
}
72
+
73
73
run .Server = server
74
+ run .telerOpts = tun .Options
75
+
76
+ if run .shouldCron () && run .Cron == nil {
77
+ w , err := fsnotify .NewWatcher ()
78
+ if err != nil {
79
+ return err
80
+ }
81
+
82
+ ds , err := threat .Location ()
83
+ if err != nil {
84
+ return err
85
+ }
86
+
87
+ if err := w .Add (ds ); err != nil {
88
+ return err
89
+ }
90
+
91
+ defer w .Close ()
92
+ run .watcher .datasets = w
93
+
94
+ run .cron ()
95
+ }
96
+
97
+ go func () {
98
+ if err := run .watch (); err != nil {
99
+ opt .Logger .Fatal ("Something went wrong" , "err" , err )
100
+ }
101
+ }()
74
102
75
103
go func () {
76
104
if err := run .start (); err != nil {
@@ -154,13 +182,32 @@ func (r *Runner) notify(sigCh chan os.Signal) error {
154
182
func (r * Runner ) watch () error {
155
183
for {
156
184
select {
157
- case event := <- r .Watcher .Events :
158
- if event .Op == 2 {
185
+ case event := <- r .watcher . config .Events :
186
+ if event .Op . Has ( fsnotify . Write ) {
159
187
r .Options .Logger .Warn ("Configuration file has changed" , "conf" , r .Options .Config .Path )
160
188
return r .restart ()
161
189
}
162
- case err := <- r .Watcher .Errors :
190
+ case event := <- r .watcher .datasets .Events :
191
+ if event .Op .Has (fsnotify .Write ) || event .Op .Has (fsnotify .Remove ) {
192
+ r .Options .Logger .Warn ("Threat datasets has updated" , "event" , event .Op )
193
+ return r .restart ()
194
+ }
195
+ case err := <- r .watcher .config .Errors :
196
+ return err
197
+ case err := <- r .watcher .datasets .Errors :
163
198
return err
164
199
}
165
200
}
166
201
}
202
+
203
+ func (r * Runner ) cron () error {
204
+ c , err := cron .New ()
205
+ if err != nil {
206
+ return err
207
+ }
208
+
209
+ r .Cron = c
210
+ c .Scheduler .StartAsync ()
211
+
212
+ return nil
213
+ }
0 commit comments