generated from UlinoyaPed/RepoTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (175 loc) · 5.11 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
package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"time"
"github.com/dablelv/go-huge-util/zip"
"github.com/gin-gonic/gin"
"github.com/gookit/color"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/i18n"
)
func init() {
cancelProxy()
// 设置选项支持 ENV 解析
config.WithOptions(config.ParseEnv)
// 添加驱动程序以支持yaml内容解析(除了JSON是默认支持,其他的则是按需使用)
config.AddDriver(yaml.Driver)
//检查配置文件是否存在
_, err := os.Stat(QuickClashYml)
// 如果文件不存在则下载
if err != nil {
os.Mkdir("config", os.ModePerm)
color.FgLightBlue.Println("Downloading config file!")
Download(RepoBaseUrl+QuickClashYml, QuickClashYml)
}
// 加载配置,可以同时传入多个文件
err = config.LoadFiles(QuickClashYml)
if err != nil {
panic(err)
}
//多语言
DefaultLang := config.String("quickclash.lang")
Languages := map[string]string{
"ara": "العربية",
"de": "Deutsch",
"en": "English",
"fra": "français",
"jp": "日本語",
"kor": "한국어",
"ru": "русский",
"th": "ภาษาไทย",
"zh-CN": "简体中文",
"zh-TW": "繁體中文",
}
i18n.Init("lang/", DefaultLang, Languages)
}
func cancelProxy() {
//取消代理
if err := SetProxy(""); err == nil {
color.BgLightBlue.Println(i18n.Dtr("cancelProxySuccess"))
} else {
color.BgRed.Println(i18n.Dtr("cancelProxyFail", err))
}
//等待2秒,防止按下关闭后看不清取消代理的状态
time.Sleep(2 * time.Second)
}
func main() {
var err error
_, err = os.Stat("web/")
if err != nil {
color.BgLightBlue.Println(i18n.Dtr("downloading", "WebUI"))
Download(WebUIUrl, "web.zip")
color.BgLightBlue.Println(i18n.Dtr("unziping"))
zip.Unzip("web.zip", ".")
}
//设置Gin为发布模式
gin.SetMode(gin.ReleaseMode)
// 创建一个Gin实例
r := gin.Default()
// 定义路由
//静态文件路由,作为主页
r.Static("/", "./web")
//设置端口
GinPort := fmt.Sprintf(":%s", config.String("quickclash.port"))
// 使用协程启动Gin服务
go func() {
if err = r.Run(GinPort); err != nil {
panic(err)
}
}()
// 主协程可以继续执行其他操作
//检查配置文件是否存在
_, errStat := os.Stat(QuickClashSubYml)
var duration time.Duration
if errStat == nil {
//检查文件修改时间
fileInfo, _ := os.Stat(QuickClashSubYml)
modTime := fileInfo.ModTime()
duration = time.Since(modTime)
}
// 如果文件不存在或超过多少小时则下载
if errStat != nil || duration.Hours() > config.Float("quickclash.duration") {
SubUrl = config.String("quickclash.sublink")
if SubUrl == "" {
color.BgLightBlue.Println(i18n.Dtr("inputSublink"))
fmt.Scanln(&SubUrl)
}
SubUrl = AddHTTPSPrefix(SubUrl)
fmt.Println(SubUrl)
os.Mkdir("yaml", os.ModePerm)
color.BgLightBlue.Println(i18n.Dtr("downloading", i18n.Dtr("configFile")))
err = Download(SubUrl, QuickClashSubYml)
if err != nil {
panic(err)
}
color.BgLightBlue.Println(i18n.Dtr("dlComplete"))
} else {
color.BgLightBlue.Println(i18n.Dtr("noneedUpdateC"))
}
//加载配置文件
err = config.LoadFiles(QuickClashSubYml)
if err != nil {
os.Remove(QuickClashSubYml)
panic(err)
}
//检查内核
if runtime.GOARCH == "386" || config.String("quickclash.core") == "clash.metax86" {
Core = ClashMetaCorex86
color.BgLightBlue.Println(i18n.Dtr("currentCore", "ClashMeta X86"))
} else if runtime.GOARCH == "amd64" {
if config.String("quickclash.core") == "clash" {
Core = ClashCore
color.BgLightBlue.Println(i18n.Dtr("currentCore", "Clash"))
} else if config.String("quickclash.core") == "clash.meta" {
Core = ClashMetaCore
color.BgLightBlue.Println(i18n.Dtr("currentCore", "ClashMeta"))
}
} else {
panic(color.BgRed.Sprintf(i18n.Dtr("coreSetWrong", QuickClashYml)))
}
//检查内核是否存在
_, err = os.Stat(Core)
// 如果文件不存在则下载
if err != nil {
color.FgLightBlue.Println(i18n.Dtr("downloading", Core+" Core"))
err = DownloadWithBar(ReleaseBaseUrl+Core, Core)
if err != nil {
panic(err)
}
}
//启动Clash
color.BgLightBlue.Println(i18n.Dtr("clashOpening"))
command := "./" + Core
params := []string{"-f", QuickClashSubYml, "-secret", config.String("quickclash.secret")}
go ExecCommand(command, params)
//等待2秒
time.Sleep(2 * time.Second)
//关闭时自动取消代理
defer cancelProxy()
//设置系统代理
proxyport := config.String("port")
if proxyport == "" {
proxyport = config.String("mixed-port")
}
proxy := fmt.Sprintf("127.0.0.1:%s", proxyport)
if err := SetProxy(proxy); err == nil {
color.BgLightBlue.Println(i18n.Dtr("setProxySuccess", proxy))
} else {
color.BgRed.Println(i18n.Dtr("setProxyFail", proxy, err))
}
//提示信息
color.BgLightBlue.Println(i18n.Dtr("baseUrl", "127.0.0.1"+config.String("external-controller"), config.String("quickclash.secret")))
color.BgLightRed.Println(i18n.Dtr("webUIStarted", GinPort))
// 阻止主协程退出,以保持Gin服务的运行
Clog()
}
func Clog() {
c := make(chan os.Signal)
signal.Notify(c)
s := <-c
fmt.Println("get signal: ", s)
}