Skip to content

Commit 82761b8

Browse files
committed
feat: release v1.0
1 parent 950ec1e commit 82761b8

File tree

10 files changed

+433
-107
lines changed

10 files changed

+433
-107
lines changed

.github/workflows/release.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
with:
15+
fetch-depth: 0
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.17.4
20+
- name: Create release on GitHub
21+
uses: goreleaser/goreleaser-action@v2
22+
with:
23+
version: latest
24+
args: release --rm-dist
25+
env:
26+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

.goreleaser.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
builds:
2+
-
3+
goos:
4+
- darwin
5+
- linux
6+
- windows
7+
goarch:
8+
- 386
9+
- amd64
10+
- arm64
11+
- arm
12+
env:
13+
- CGO_ENABLED=0

README.md

Lines changed: 109 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,141 @@
11
## 介绍
2-
`fetch-github-hosts` 是主要为解决研究及学习人员访问 `Github` 过慢或其他问题而提供的免费的 `Github Hosts` 同步服务。
2+
3+
`fetch-github-hosts` 是主要为解决研究及学习人员访问 `Github` 过慢或其他问题而提供的 `Github Hosts` 同步服务。
34

45
## 原理
5-
此项目是通过部署此项目本身的服务器来获取 `github.com``hosts`,而不是通过第三方ip地址接口来进行获取,例如 `ipaddress.com`
6+
7+
此项目是通过部署此项目本身的服务器来获取 `github.com``hosts`,而不是通过第三方ip地址接口来进行获取,例如 `ipaddress.com` 等。
68

79
## 使用方法
10+
11+
### 自动(推荐)
12+
13+
[Releases](https://github.com/Licoy/fetch-github-hosts/releases)
14+
[FastGit镜像](https://hub.fastgit.xyz/Licoy/fetch-github-hosts/releases) 中下载您的系统版本(目前支持`Windows`/`Linux`/`MacOS`
15+
),包括`ARM`
16+
架构的系统。
17+
18+
#### 参数
19+
20+
| 参数名 | 缩写 | 默认值 | 必填 | 描述 |
21+
|------------|-----|--------------------------------------|-----|------------------------------------|
22+
| `mode` | `m` | client || 启动模式 `server(服务端)` / `client(客户端)` |
23+
| `interval` | `i` | 60 || 获取记录值间隔(分钟) |
24+
| `port` | `p` | 9898 || 服务模式监听端口以访问HTTP服务 |
25+
| `url` | `u` | `https://hosts.gitcdn.top/hosts.txt` || 客户端模式远程hosts获取链接 |
26+
27+
#### 启动客户端:
28+
29+
> 注意:
30+
>
31+
> Windows下需要右键【以管理员身份运行】;
32+
>
33+
> Linux/Macos下需要使用root或具有hosts写入权限的账户执行,或直接使用`sudo`运行;
34+
35+
- 直接运行
36+
37+
```bash
38+
# Linux/Macos
39+
fetch-github-hosts
40+
41+
# Windows
42+
fetch-github-hosts.exe
43+
```
44+
45+
- 自定义获取时间间隔
46+
47+
```bash
48+
# Linux/Macos(10分钟获取一次)
49+
fetch-github-hosts -i=10
50+
51+
# Windows(10分钟获取一次)
52+
fetch-github-hosts.exe -i=10
53+
```
54+
55+
- 自定义获取链接
56+
57+
```bash
58+
# Linux/Macos
59+
fetch-github-hosts -u=http://127.0.0.1:9898/hosts.json
60+
61+
# Windows
62+
fetch-github-hosts.exe -u=http://127.0.0.1:9898/hosts.json
63+
```
64+
65+
#### 启动服务端:
66+
67+
- 直接运行
68+
69+
```bash
70+
# Linux/Macos
71+
fetch-github-hosts -m=server
72+
73+
# Windows
74+
fetch-github-hosts.exe -m=server
75+
```
76+
77+
- 自定义监听端口
78+
79+
```bash
80+
# Linux/Macos
81+
fetch-github-hosts -m=server -p=6666
82+
83+
# Windows
84+
fetch-github-hosts.exe -m=server -p=6666
85+
```
86+
887
### 手动
88+
989
#### 添加hosts
90+
1091
访问 [https://hosts.gitcdn.top/hosts.txt](https://hosts.gitcdn.top/hosts.txt)
1192
将其全部内容粘贴到你的hosts文件中,即可。
93+
1294
- `Linux / MacOS` hosts路径:`/etc/hosts`
1395
- `Windows` hosts路径:`C:\Windows\System32\drivers\etc\hosts`
96+
1497
#### 刷新生效
98+
1599
- `Linux`: `/etc/init.d/network restart`
16100
- `Windows`: `ipconfig /flushdns`
17101
- `Macos`: `sudo killall -HUP mDNSResponder`
18-
### Unix/Linux 一键使用
102+
103+
#### Unix/Linux 一键使用
104+
19105
```shell
20106
sed -i "/# fetch-github-hosts begin/Q" /etc/hosts && curl https://hosts.gitcdn.top/hosts.txt >> /etc/hosts
21107
```
108+
22109
> 提示:可以设置crontab定时任务定时获取更新即可,解放双手!
23110
24-
### Chrome
25-
使用 [FasterHosts](https://github.com/gauseen/faster-hosts) 插件,若访问速度过慢可以直接使用
26-
[点击此处](https://gitcdn.top/https://github.com/gauseen/faster-hosts/archive/refs/heads/master.zip) 来进行下载。
111+
### 其他自动方式
112+
113+
#### Chrome浏览器下
114+
115+
使用 [FasterHosts](https://github.com/gauseen/faster-hosts) 插件,点击进入下载。
27116

28117
下载完成之后解压压缩包,Chrome地址栏输入`chrome://extensions/`回车进入,勾选`开发者模式`,选择`加载已解压的扩展程序`
29118
选择刚才的解压目录即可。
30-
### Windows /MacOS 及其他桌面端
119+
120+
#### Windows / MacOS 及其他桌面端
121+
31122
使用 [SwitchHosts](https://swh.app/) 桌面端应用,安装添加新规则:
123+
32124
- `Title`: 任意
33125
- `Type`: `Remote`
34126
- `Url`: `https://hosts.gitcdn.top/hosts.txt`
35127
- `Auto refresh`: `1 hour`
36128

37129
## 私有部署
38-
下载本仓库的代码:[fetch-github-hosts.zip](https://gitcdn.top/https://github.com/Licoy/fetch-github-hosts/archive/refs/heads/main.zip)
39-
部署到任意一个含有PHP环境的服务器即可,部署完成之后可以计划任务脚本定时更新hosts:
40-
```shell
41-
cd /wwwroot/fetch-github-hosts #此处更换为你部署的项目路径
42-
php fetch_hosts.php
43-
```
44-
> 注意:必须部署到非大陆的服务器节点!
130+
131+
下载最新的发行版(到 [Releases](https://github.com/Licoy/fetch-github-hosts/releases)
132+
[FastGit镜像](https://hub.fastgit.xyz/Licoy/fetch-github-hosts/releases) 进行下载)
133+
,并选择您的系统对应版本,直接以服务模式运行即可:`fetch-github-hosts -m=server -p=9898`,会自动监听`0.0.0.0:9898`,您可以直接浏览器访问 `http://127.0.0.1:9898`
134+
以访问您自定义服务。
135+
(具体方法可参见【启动服务端】小节详细说明)
136+
137+
> 注意:因网络影响,尽量部署到海外服务器节点!
45138
46139
### 开源协议
47-
[GPL 3.0](./LICENSE)
140+
141+
[GPL 3.0](https://github.com/Licoy/fetch-github-hosts/blob/main/LICENSE)

args.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type CmdArgs struct {
1111
Mode string `default:"client" short:"m" long:"mode" description:"启动模式(client或server)"`
1212
FetchInterval int `default:"60" short:"i" long:"interval" description:"获取hosts的间隔时间,单位为分钟"`
13+
Port int `default:"9898" short:"p" long:"port" description:"服务模式监听端口"`
14+
Url string `default:"https://hosts.gitcdn.top/hosts.txt" short:"u" long:"url" description:"客户端模式远程hosts获取链接"`
1315
Version bool `short:"v" long:"version" description:"查看当前版本"`
1416
}
1517

fetch_hosts.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"bytes"
5+
"embed"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"io/ioutil"
810
"net"
@@ -17,19 +19,24 @@ const (
1719
Windows = "windows"
1820
Linux = "linux"
1921
Darwin = "darwin"
20-
HostUrl = "https://hosts.gitcdn.top/hosts.txt"
2122
)
2223

24+
//go:embed index.template
25+
var indexTemplate embed.FS
26+
27+
//go:embed domains.json
28+
var domainsJson embed.FS
29+
2330
// ClientFetchHosts 获取最新的host并写入hosts文件
24-
func ClientFetchHosts() (err error) {
31+
func ClientFetchHosts(url string) (err error) {
2532
hostsPath := GetSystemHostsPath()
2633
hostsBytes, err := ioutil.ReadFile(hostsPath)
2734
if err != nil {
2835
err = ComposeError("读取文件hosts错误", err)
2936
return
3037
}
3138

32-
resp, err := http.Get(HostUrl)
39+
resp, err := http.Get(url)
3340
if err != nil || resp.StatusCode != http.StatusOK {
3441
err = ComposeError("获取最新的hosts失败", err)
3542
return
@@ -41,18 +48,26 @@ func ClientFetchHosts() (err error) {
4148
return
4249
}
4350

44-
hosts := string(hostsBytes)
51+
fetchHostsStr := strings.Trim(string(fetchHosts), "\n")
4552

4653
mth, err := regexp.Compile(`# fetch-github-hosts begin(([\s\S])*.?)# fetch-github-hosts end`)
4754
if err != nil {
4855
err = ComposeError("创建内容正则匹配失败", err)
4956
return
5057
}
58+
59+
if len(mth.FindStringSubmatch(fetchHostsStr)) == 0 {
60+
err = errors.New("无效的远程hosts链接,未通过格式校验")
61+
return
62+
}
63+
64+
hosts := string(hostsBytes)
65+
5166
findStr := mth.FindStringSubmatch(hosts)
5267
if len(findStr) > 0 {
53-
hosts = strings.Replace(hosts, findStr[0], string(fetchHosts), 1)
68+
hosts = strings.Replace(hosts, findStr[0], fetchHostsStr, 1)
5469
} else {
55-
hosts += "\n\n" + string(fetchHosts)
70+
hosts += "\n\n" + fetchHostsStr + "\n"
5671
}
5772

5873
if err = ioutil.WriteFile(hostsPath, []byte(hosts), os.ModeType); err != nil {
@@ -66,7 +81,7 @@ func ClientFetchHosts() (err error) {
6681
// ServerFetchHosts 服务端获取github最新的hosts并写入到对应文件及更新首页
6782
func ServerFetchHosts() (err error) {
6883
execDir := AppExecDir()
69-
fileData, err := ioutil.ReadFile(execDir + "/domains.json")
84+
fileData, err := getExecOrEmbedFile(&domainsJson, "domains.json")
7085
if err != nil {
7186
err = ComposeError("读取文件domains.json错误", err)
7287
return
@@ -95,7 +110,7 @@ func ServerFetchHosts() (err error) {
95110
}
96111

97112
var templateFile []byte
98-
templateFile, err = ioutil.ReadFile(execDir + "/index.template")
113+
templateFile, err = getExecOrEmbedFile(&indexTemplate, "index.template")
99114
if err != nil {
100115
err = ComposeError("读取首页模板文件失败", err)
101116
return
@@ -131,3 +146,14 @@ func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err
131146
hostsJson, err = json.Marshal(hosts)
132147
return
133148
}
149+
150+
func getExecOrEmbedFile(fs *embed.FS, filename string) (template []byte, err error) {
151+
exeDirFile := AppExecDir() + "/" + filename
152+
_, err = os.Stat(exeDirFile)
153+
if err == nil {
154+
template, err = ioutil.ReadFile(exeDirFile)
155+
return
156+
}
157+
template, err = fs.ReadFile(filename)
158+
return
159+
}

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ go 1.18
44

55
require github.com/jessevdk/go-flags v1.5.0
66

7-
require (
8-
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
9-
)
7+
require golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
2-
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
31
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
42
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
5-
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef h1:UD99BBEz19F21KhOFHLNAI6KodDWUvXaPr4Oqu8yMV8=
6-
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
73
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
84
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)