forked from Licoy/fetch-github-hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_hosts.go
380 lines (350 loc) · 9.25 KB
/
fetch_hosts.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/h2non/filetype"
"github.com/nicksnyder/go-i18n/v2/i18n"
"io"
"net"
"net/http"
"os"
"strings"
"time"
)
const (
Windows = "windows"
Linux = "linux"
Darwin = "darwin"
)
func startClient(ticker *FetchTicker, url string, flog *FetchLog) {
flog.Print(tfs(&i18n.Message{
ID: "RemoteHostsUrlLog",
Other: "远程hosts获取链接: {{.Url}}",
}, map[string]interface{}{
"Url": url,
}))
fn := func() {
if err := ClientFetchHosts(url); err != nil {
flog.Print(tfs(&i18n.Message{
ID: "RemoteHostsFetchErrorLog",
Other: "更新Github-Hosts失败: {{.E}}",
}, map[string]interface{}{
"E": err.Error(),
}))
} else {
flog.Print(t(&i18n.Message{
ID: "RemoteHostsFetchSuccessLog",
Other: "更新Github-Hosts成功!",
}))
}
}
fn()
for {
select {
case <-ticker.Ticker.C:
fn()
case <-ticker.CloseChan:
flog.Print(t(&i18n.Message{
ID: "RemoteHostsFetchStopLog",
Other: "停止获取hosts",
}))
return
}
}
}
func startServer(ticker *FetchTicker, port int, flog *FetchLog) {
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
flog.Print(tfs(&i18n.Message{
ID: "ServerStartErrorLog",
Other: "服务启动失败(可能是目标端口已被占用):{{.E}}",
}, map[string]interface{}{
"E": err.Error(),
}))
return
}
flog.Print(tfs(&i18n.Message{
ID: "ServerStartSuccessLog",
Other: "已监听HTTP服务成功:http://127.0.0.1:{{.Port}}",
}, map[string]interface{}{
"Port": port,
}))
flog.Print(tfs(&i18n.Message{
ID: "ServerStartSuccessHostsLinkLog",
Other: "hosts文件链接:http://127.0.0.1:{{.Port}}/hosts.txt",
}, map[string]interface{}{
"Port": port,
}))
flog.Print(tfs(&i18n.Message{
ID: "ServerStartSuccessHostsJsonLinkLog",
Other: "hosts的JSON格式链接:http://127.0.0.1:{{.Port}}/hosts.json",
}, map[string]interface{}{
"Port": port,
}))
go http.Serve(listen, &serverHandle{flog})
fn := func() {
if err := ServerFetchHosts(); err != nil {
flog.Print(tfs(&i18n.Message{
ID: "ServerFetchHostsErrorLog",
Other: "执行更新Github-Hosts失败:{{.E}}",
}, map[string]interface{}{
"E": err.Error(),
}))
} else {
flog.Print(t(&i18n.Message{
ID: "ServerFetchHostsSuccessLog",
Other: "执行更新Github-Hosts成功!",
}))
}
}
fn()
for {
select {
case <-ticker.Ticker.C:
fn()
case <-ticker.CloseChan:
flog.Print(t(&i18n.Message{
ID: "ServerFetchHostsStopLog",
Other: "正在停止更新hosts服务",
}))
if err := listen.Close(); err != nil {
flog.Print(t(&i18n.Message{
ID: "ServerFetchHostsStopErrorLog",
Other: "关闭端口监听失败",
}))
}
flog.Print(t(&i18n.Message{
ID: "ServerFetchHostsStopSuccessLog",
Other: "已停止更新hosts服务",
}))
return
}
}
}
type serverHandle struct {
flog *FetchLog
}
func (s *serverHandle) ServeHTTP(resp http.ResponseWriter, request *http.Request) {
p := request.URL.Path
if p == "/" || p == "/hosts.txt" || p == "/hosts.json" {
if p == "/" {
p = "/index.html"
}
file, err := os.ReadFile(AppExecDir() + p)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
resp.Write([]byte("server error"))
s.flog.Print(tfs(&i18n.Message{
ID: "ServerFetchIndexFileErr",
Other: "获取首页文件失败:{{.E}}",
}, map[string]interface{}{
"E": err.Error(),
}))
return
}
resp.Write(file)
return
}
if strings.HasPrefix(p, "/public/") {
file, _ := assetsFs.ReadFile("assets" + p)
kind, _ := filetype.Match(file)
resp.Header().Set("Content-Type", kind.MIME.Value)
resp.Write(file)
return
}
http.Redirect(resp, request, "/", http.StatusMovedPermanently)
}
// ClientFetchHosts 获取最新的host并写入hosts文件
func ClientFetchHosts(url string) (err error) {
hosts, err := getCleanGithubHosts()
if err != nil {
return
}
resp, err := http.Get(url)
if err != nil || resp.StatusCode != http.StatusOK {
err = ComposeError(t(&i18n.Message{
ID: "ClientFetchHostsGetErrorLog",
Other: "获取最新的hosts失败",
}), err)
return
}
fetchHosts, err := io.ReadAll(resp.Body)
if err != nil {
err = ComposeError(t(&i18n.Message{
ID: "ClientFetchHostsReadErrorLog",
Other: "读取最新的hosts失败",
}), err)
return
}
newlineChar := GetNewlineChar()
fetchHostsLines := strings.Split(string(fetchHosts), "\n")
for i, fetchLine := range fetchHostsLines {
line := strings.TrimSpace(fetchLine)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
hosts.WriteString(fetchLine)
if i != len(fetchHostsLines)-1 {
hosts.WriteString(newlineChar)
}
}
if err = os.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "WriteHostsNoPermission",
Other: "写入hosts文件失败,请用超级管理员身份启动本程序!",
}), err)
return
}
return
}
// ServerFetchHosts 服务端获取github最新的hosts并写入到对应文件及更新首页
func ServerFetchHosts() (err error) {
execDir := AppExecDir()
domains, err := getGithubDomains()
if err != nil {
return
}
hostJson, hostFile, now, err := FetchHosts(domains)
if err != nil {
err = ComposeError(t(&i18n.Message{
ID: "FetchGithubHostsFail",
Other: "获取Github的Host失败",
}), err)
return
}
if err = os.WriteFile(execDir+"/hosts.json", hostJson, 0775); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "WriteHostsJsonFileErr",
Other: "写入数据到hosts.json文件失败",
}), err)
return
}
if err = os.WriteFile(execDir+"/hosts.txt", hostFile, 0775); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "WriteHostsTxtFileErr",
Other: "写入数据到hosts.txt文件失败",
}), err)
return
}
var templateFile []byte
templateFile, err = GetExecOrEmbedFile(&assetsFs, "assets/index.html")
if err != nil {
err = ComposeError(t(&i18n.Message{
ID: "ReadIndexFileErr",
Other: "读取首页模板文件失败",
}), err)
return
}
templateData := strings.Replace(string(templateFile), "<!--time-->", now, 1)
if err = os.WriteFile(execDir+"/index.html", []byte(templateData), 0775); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "WriteIndexFileErr",
Other: "写入更新信息到首页文件失败",
}), err)
return
}
return
}
func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err error) {
now = time.Now().Format("2006-01-02 15:04:05")
hosts := make([][]string, 0, len(domains))
hostsFileData := bytes.NewBufferString("# fetch-github-hosts begin\n")
for _, domain := range domains {
ipList, sErr := net.LookupIP(domain)
if sErr != nil {
fmt.Printf("%s: %s\b", t(&i18n.Message{
ID: "GetHostRecordErr",
Other: "获取主机记录失败",
}), sErr.Error())
continue
}
for _, ip := range ipList {
ipv4 := ip.To4()
if ipv4 != nil {
item := []string{ipv4.String(), domain}
hosts = append(hosts, item)
hostsFileData.WriteString(fmt.Sprintf("%-28s%s\n", item[0], item[1]))
break
}
}
}
hostsFileData.WriteString("# last fetch time: ")
hostsFileData.WriteString(now)
hostsFileData.WriteString("\n# update url: https://hosts.gitcdn.top/hosts.txt\n# fetch-github-hosts end\n\n")
hostsFile = hostsFileData.Bytes()
hostsJson, err = json.Marshal(hosts)
return
}
func getCleanGithubHosts() (hosts *bytes.Buffer, err error) {
hostsPath := GetSystemHostsPath()
hostsBytes, err := os.ReadFile(hostsPath)
if err != nil {
err = ComposeError(t(&i18n.Message{
ID: "ReadHostsErr",
Other: "读取文件hosts错误",
}), err)
return
}
domains, err := getGithubDomains()
if err != nil {
return
}
newlineChar := GetNewlineChar()
// clear local hosts github domain
localHostsLines := strings.Split(string(hostsBytes), newlineChar)
hosts = &bytes.Buffer{}
for i, localLine := range localHostsLines {
line := strings.TrimSpace(localLine)
if line == "" || strings.HasPrefix(line, "#") {
hosts.WriteString(localLine)
if i != len(localHostsLines)-1 || !strings.HasSuffix(hosts.String(), newlineChar) {
hosts.WriteString(newlineChar)
}
continue
}
var clearLine bool
for _, domain := range domains {
if strings.Contains(line, domain) {
clearLine = true
break
}
}
if !clearLine {
hosts.WriteString(localLine)
hosts.WriteString(newlineChar)
}
}
return
}
func getGithubDomains() (domains []string, err error) {
fileData, err := GetExecOrEmbedFile(&assetsFs, "assets/domains.json")
if err != nil {
err = ComposeError(t(&i18n.Message{
ID: "ReadDomainsJsonErr",
Other: "读取文件domains.json错误",
}), err)
return
}
if err = json.Unmarshal(fileData, &domains); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "ParseDomainsJsonErr",
Other: "domain.json解析失败",
}), err)
return
}
return
}
func flushCleanGithubHosts() (err error) {
hosts, err := getCleanGithubHosts()
if err != nil {
return
}
if err = os.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
err = ComposeError(t(&i18n.Message{
ID: "WriteHostsNoPermission",
Other: "写入hosts文件失败,请用超级管理员身份启动本程序!",
}), err)
}
return
}