-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.go
69 lines (51 loc) · 953 Bytes
/
tracker.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
package tracker
import (
"time"
"github.com/dekoch/gouniversal/module/iptracker/global"
"github.com/dekoch/gouniversal/shared/console"
"github.com/dekoch/gouniversal/shared/getpublicip"
)
var (
chanStart = make(chan bool)
chanFinish = make(chan bool)
lastIP string
)
func LoadConfig() {
go run()
go job()
}
func job() {
intvl := global.Config.GetUpdInterval()
timer := time.NewTimer(intvl)
for {
if intvl != 0 {
select {
case <-timer.C:
timer.Stop()
chanStart <- true
case <-chanFinish:
intvl = global.Config.GetUpdInterval()
timer.Reset(intvl)
}
} else {
// wait until enabled
time.Sleep(1 * time.Minute)
intvl = global.Config.GetUpdInterval()
}
}
}
func run() {
for {
<-chanStart
ip, err := getpublicip.Get()
if err != nil {
console.Log(err, "")
} else {
if ip != lastIP {
lastIP = ip
console.Log(ip, "IPTracker")
}
}
chanFinish <- true
}
}