-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmain.go
207 lines (172 loc) · 4.71 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/bestruirui/mihomo-check/check"
"github.com/bestruirui/mihomo-check/config"
"github.com/bestruirui/mihomo-check/save"
"github.com/bestruirui/mihomo-check/utils"
"github.com/fsnotify/fsnotify"
"github.com/metacubex/mihomo/log"
"gopkg.in/yaml.v3"
)
// App 结构体用于管理应用程序状态
type App struct {
configPath string
interval int
watcher *fsnotify.Watcher
reloadTimer *time.Timer
lastReload time.Time
}
// NewApp 创建新的应用实例
func NewApp() *App {
configPath := flag.String("f", "", "配置文件路径")
flag.Parse()
return &App{
configPath: *configPath,
}
}
// Initialize 初始化应用程序
func (app *App) Initialize() error {
// 初始化配置文件路径
if err := app.initConfigPath(); err != nil {
return fmt.Errorf("初始化配置文件路径失败: %w", err)
}
// 加载配置文件
if err := app.loadConfig(); err != nil {
return fmt.Errorf("加载配置文件失败: %w", err)
}
// 初始化配置文件监听
if err := app.initConfigWatcher(); err != nil {
return fmt.Errorf("初始化配置文件监听失败: %w", err)
}
app.interval = config.GlobalConfig.CheckInterval
return nil
}
// initConfigPath 初始化配置文件路径
func (app *App) initConfigPath() error {
if app.configPath == "" {
execPath := utils.GetExecutablePath()
configDir := filepath.Join(execPath, "config")
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("创建配置目录失败: %w", err)
}
app.configPath = filepath.Join(configDir, "config.yaml")
}
return nil
}
// loadConfig 加载配置文件
func (app *App) loadConfig() error {
yamlFile, err := os.ReadFile(app.configPath)
if err != nil {
if os.IsNotExist(err) {
return app.createDefaultConfig()
}
return fmt.Errorf("读取配置文件失败: %w", err)
}
if err := yaml.Unmarshal(yamlFile, &config.GlobalConfig); err != nil {
return fmt.Errorf("解析配置文件失败: %w", err)
}
log.Infoln("配置文件读取成功")
return nil
}
// createDefaultConfig 创建默认配置文件
func (app *App) createDefaultConfig() error {
log.Infoln("配置文件不存在,创建默认配置文件")
if err := os.WriteFile(app.configPath, []byte(config.DefaultConfigTemplate), 0644); err != nil {
return fmt.Errorf("写入默认配置文件失败: %w", err)
}
log.Infoln("默认配置文件创建成功")
log.Infoln("请编辑配置文件: %v", app.configPath)
os.Exit(0)
return nil
}
// initConfigWatcher 初始化配置文件监听
func (app *App) initConfigWatcher() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("创建文件监听器失败: %w", err)
}
app.watcher = watcher
app.reloadTimer = time.NewTimer(0)
<-app.reloadTimer.C
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
if app.reloadTimer != nil {
app.reloadTimer.Stop()
}
app.reloadTimer.Reset(100 * time.Millisecond)
go func() {
<-app.reloadTimer.C
log.Infoln("配置文件发生变化,正在重新加载")
if err := app.loadConfig(); err != nil {
log.Errorln("重新加载配置文件失败: %v", err)
return
}
// 更新检查间隔
app.interval = config.GlobalConfig.CheckInterval
}()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Errorln("配置文件监听错误: %v", err)
}
}
}()
// 开始监听配置文件
if err := watcher.Add(app.configPath); err != nil {
return fmt.Errorf("添加配置文件监听失败: %w", err)
}
log.Infoln("配置文件监听已启动")
return nil
}
// Run 运行应用程序主循环
func (app *App) Run() {
defer func() {
app.watcher.Close()
if app.reloadTimer != nil {
app.reloadTimer.Stop()
}
}()
log.Infoln("进度展示: %v", config.GlobalConfig.PrintProgress)
for {
if err := app.checkProxies(); err != nil {
log.Errorln("检测代理失败: %v", err)
os.Exit(1)
}
nextCheck := time.Now().Add(time.Duration(app.interval) * time.Minute)
log.Infoln("下次检查时间: %v", nextCheck.Format("2006-01-02 15:04:05"))
time.Sleep(time.Duration(app.interval) * time.Minute)
}
}
// checkProxies 执行代理检测
func (app *App) checkProxies() error {
log.Infoln("开始检测代理")
results, err := check.Check()
if err != nil {
return fmt.Errorf("检测代理失败: %w", err)
}
log.Infoln("检测完成")
save.SaveConfig(results)
utils.UpdateSubs()
return nil
}
func main() {
app := NewApp()
if err := app.Initialize(); err != nil {
log.Errorln("初始化失败: %v", err)
os.Exit(1)
}
app.Run()
}