-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto.go
118 lines (102 loc) · 2.2 KB
/
auto.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
package main
import (
"encoding/json"
"os"
"sync"
"time"
"github.com/astaxie/beego/logs"
"github.com/easymesh/autoproxy/engin"
)
const (
CACHE_FILE = "autocache.json"
)
type LocalAccessInfo struct {
Hostname string `json:"hostname"`
Access bool `json:"access"`
}
type AutoCtrl struct {
sync.RWMutex
cache map[string]LocalAccessInfo
}
var autoCtrl AutoCtrl
func AutoInit() {
autoCtrl.cache = make(map[string]LocalAccessInfo, 100)
syncFromFile()
go syncAutoTask()
}
func syncAutoTask() {
time1 := engin.GetFileTimestamp(CACHE_FILE)
for {
time.Sleep(time.Second)
time2 := engin.GetFileTimestamp(CACHE_FILE)
if time2 != time1 {
autoCtrl.Lock()
syncFromFile()
autoCtrl.Unlock()
}
time1 = time2
}
}
func syncFromFile() {
var list []LocalAccessInfo
body, err := os.ReadFile(CACHE_FILE)
if err != nil {
logs.Warning(err.Error())
return
}
err = json.Unmarshal(body, &list)
if err != nil {
logs.Error(err.Error())
}
for _, access := range list {
old_access, exist := autoCtrl.cache[access.Hostname]
if exist {
if old_access.Access != access.Access {
logs.Info("update %s from %v to %v", access.Hostname, old_access.Access, access.Access)
}
}
autoCtrl.cache[access.Hostname] = access
}
logs.Info("sync %d from cache file success", len(list))
}
func syncToFile() {
var list []LocalAccessInfo
for _, access := range autoCtrl.cache {
list = append(list, access)
}
body, err := json.MarshalIndent(list, "", "\t")
if err != nil {
logs.Error(err.Error())
return
}
err = os.WriteFile(CACHE_FILE, body, 0644)
if err != nil {
logs.Error(err.Error())
}
}
func AutoCheck(address string) bool {
autoCtrl.RLock()
result, ok := autoCtrl.cache[address]
autoCtrl.RUnlock()
if ok {
return result.Access
}
result.Hostname = address
result.Access = engin.IsConnect(address, 3)
autoCtrl.Lock()
autoCtrl.cache[address] = result
syncToFile()
autoCtrl.Unlock()
return result.Access
}
func AutoCheckUpdate(address string, access bool) {
autoCtrl.RLock()
result, ok := autoCtrl.cache[address]
autoCtrl.RUnlock()
if !ok || result.Access != access {
autoCtrl.Lock()
autoCtrl.cache[address] = LocalAccessInfo{address, access}
syncToFile()
autoCtrl.Unlock()
}
}