Skip to content

Commit

Permalink
feat: release v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Jun 17, 2022
1 parent 950ec1e commit 82761b8
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 107 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/release.yml
@@ -0,0 +1,26 @@
name: Release

on:
push:
tags:
- v*

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.4
- name: Create release on GitHub
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
13 changes: 13 additions & 0 deletions .goreleaser.yml
@@ -0,0 +1,13 @@
builds:
-
goos:
- darwin
- linux
- windows
goarch:
- 386
- amd64
- arm64
- arm
env:
- CGO_ENABLED=0
124 changes: 109 additions & 15 deletions README.md
@@ -1,47 +1,141 @@
## 介绍
`fetch-github-hosts` 是主要为解决研究及学习人员访问 `Github` 过慢或其他问题而提供的免费的 `Github Hosts` 同步服务。

`fetch-github-hosts` 是主要为解决研究及学习人员访问 `Github` 过慢或其他问题而提供的 `Github Hosts` 同步服务。

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

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

## 使用方法

### 自动(推荐)

[Releases](https://github.com/Licoy/fetch-github-hosts/releases)
[FastGit镜像](https://hub.fastgit.xyz/Licoy/fetch-github-hosts/releases) 中下载您的系统版本(目前支持`Windows`/`Linux`/`MacOS`
),包括`ARM`
架构的系统。

#### 参数

| 参数名 | 缩写 | 默认值 | 必填 | 描述 |
|------------|-----|--------------------------------------|-----|------------------------------------|
| `mode` | `m` | client || 启动模式 `server(服务端)` / `client(客户端)` |
| `interval` | `i` | 60 || 获取记录值间隔(分钟) |
| `port` | `p` | 9898 || 服务模式监听端口以访问HTTP服务 |
| `url` | `u` | `https://hosts.gitcdn.top/hosts.txt` || 客户端模式远程hosts获取链接 |

#### 启动客户端:

> 注意:
>
> Windows下需要右键【以管理员身份运行】;
>
> Linux/Macos下需要使用root或具有hosts写入权限的账户执行,或直接使用`sudo`运行;
- 直接运行

```bash
# Linux/Macos
fetch-github-hosts

# Windows
fetch-github-hosts.exe
```

- 自定义获取时间间隔

```bash
# Linux/Macos(10分钟获取一次)
fetch-github-hosts -i=10

# Windows(10分钟获取一次)
fetch-github-hosts.exe -i=10
```

- 自定义获取链接

```bash
# Linux/Macos
fetch-github-hosts -u=http://127.0.0.1:9898/hosts.json

# Windows
fetch-github-hosts.exe -u=http://127.0.0.1:9898/hosts.json
```

#### 启动服务端:

- 直接运行

```bash
# Linux/Macos
fetch-github-hosts -m=server

# Windows
fetch-github-hosts.exe -m=server
```

- 自定义监听端口

```bash
# Linux/Macos
fetch-github-hosts -m=server -p=6666

# Windows
fetch-github-hosts.exe -m=server -p=6666
```

### 手动

#### 添加hosts

访问 [https://hosts.gitcdn.top/hosts.txt](https://hosts.gitcdn.top/hosts.txt)
将其全部内容粘贴到你的hosts文件中,即可。

- `Linux / MacOS` hosts路径:`/etc/hosts`
- `Windows` hosts路径:`C:\Windows\System32\drivers\etc\hosts`

#### 刷新生效

- `Linux`: `/etc/init.d/network restart`
- `Windows`: `ipconfig /flushdns`
- `Macos`: `sudo killall -HUP mDNSResponder`
### Unix/Linux 一键使用

#### Unix/Linux 一键使用

```shell
sed -i "/# fetch-github-hosts begin/Q" /etc/hosts && curl https://hosts.gitcdn.top/hosts.txt >> /etc/hosts
```

> 提示:可以设置crontab定时任务定时获取更新即可,解放双手!
### Chrome
使用 [FasterHosts](https://github.com/gauseen/faster-hosts) 插件,若访问速度过慢可以直接使用
[点击此处](https://gitcdn.top/https://github.com/gauseen/faster-hosts/archive/refs/heads/master.zip) 来进行下载。
### 其他自动方式

#### Chrome浏览器下

使用 [FasterHosts](https://github.com/gauseen/faster-hosts) 插件,点击进入下载。

下载完成之后解压压缩包,Chrome地址栏输入`chrome://extensions/`回车进入,勾选`开发者模式`,选择`加载已解压的扩展程序`
选择刚才的解压目录即可。
### Windows /MacOS 及其他桌面端

#### Windows / MacOS 及其他桌面端

使用 [SwitchHosts](https://swh.app/) 桌面端应用,安装添加新规则:

- `Title`: 任意
- `Type`: `Remote`
- `Url`: `https://hosts.gitcdn.top/hosts.txt`
- `Auto refresh`: `1 hour`

## 私有部署
下载本仓库的代码:[fetch-github-hosts.zip](https://gitcdn.top/https://github.com/Licoy/fetch-github-hosts/archive/refs/heads/main.zip)
部署到任意一个含有PHP环境的服务器即可,部署完成之后可以计划任务脚本定时更新hosts:
```shell
cd /wwwroot/fetch-github-hosts #此处更换为你部署的项目路径
php fetch_hosts.php
```
> 注意:必须部署到非大陆的服务器节点!

下载最新的发行版(到 [Releases](https://github.com/Licoy/fetch-github-hosts/releases)
[FastGit镜像](https://hub.fastgit.xyz/Licoy/fetch-github-hosts/releases) 进行下载)
,并选择您的系统对应版本,直接以服务模式运行即可:`fetch-github-hosts -m=server -p=9898`,会自动监听`0.0.0.0:9898`,您可以直接浏览器访问 `http://127.0.0.1:9898`
以访问您自定义服务。
(具体方法可参见【启动服务端】小节详细说明)

> 注意:因网络影响,尽量部署到海外服务器节点!
### 开源协议
[GPL 3.0](./LICENSE)

[GPL 3.0](https://github.com/Licoy/fetch-github-hosts/blob/main/LICENSE)
2 changes: 2 additions & 0 deletions args.go
Expand Up @@ -10,6 +10,8 @@ import (
type CmdArgs struct {
Mode string `default:"client" short:"m" long:"mode" description:"启动模式(client或server)"`
FetchInterval int `default:"60" short:"i" long:"interval" description:"获取hosts的间隔时间,单位为分钟"`
Port int `default:"9898" short:"p" long:"port" description:"服务模式监听端口"`
Url string `default:"https://hosts.gitcdn.top/hosts.txt" short:"u" long:"url" description:"客户端模式远程hosts获取链接"`
Version bool `short:"v" long:"version" description:"查看当前版本"`
}

Expand Down
42 changes: 34 additions & 8 deletions fetch_hosts.go
Expand Up @@ -2,7 +2,9 @@ package main

import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
Expand All @@ -17,19 +19,24 @@ const (
Windows = "windows"
Linux = "linux"
Darwin = "darwin"
HostUrl = "https://hosts.gitcdn.top/hosts.txt"
)

//go:embed index.template
var indexTemplate embed.FS

//go:embed domains.json
var domainsJson embed.FS

// ClientFetchHosts 获取最新的host并写入hosts文件
func ClientFetchHosts() (err error) {
func ClientFetchHosts(url string) (err error) {
hostsPath := GetSystemHostsPath()
hostsBytes, err := ioutil.ReadFile(hostsPath)
if err != nil {
err = ComposeError("读取文件hosts错误", err)
return
}

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

hosts := string(hostsBytes)
fetchHostsStr := strings.Trim(string(fetchHosts), "\n")

mth, err := regexp.Compile(`# fetch-github-hosts begin(([\s\S])*.?)# fetch-github-hosts end`)
if err != nil {
err = ComposeError("创建内容正则匹配失败", err)
return
}

if len(mth.FindStringSubmatch(fetchHostsStr)) == 0 {
err = errors.New("无效的远程hosts链接,未通过格式校验")
return
}

hosts := string(hostsBytes)

findStr := mth.FindStringSubmatch(hosts)
if len(findStr) > 0 {
hosts = strings.Replace(hosts, findStr[0], string(fetchHosts), 1)
hosts = strings.Replace(hosts, findStr[0], fetchHostsStr, 1)
} else {
hosts += "\n\n" + string(fetchHosts)
hosts += "\n\n" + fetchHostsStr + "\n"
}

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

var templateFile []byte
templateFile, err = ioutil.ReadFile(execDir + "/index.template")
templateFile, err = getExecOrEmbedFile(&indexTemplate, "index.template")
if err != nil {
err = ComposeError("读取首页模板文件失败", err)
return
Expand Down Expand Up @@ -131,3 +146,14 @@ func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err
hostsJson, err = json.Marshal(hosts)
return
}

func getExecOrEmbedFile(fs *embed.FS, filename string) (template []byte, err error) {
exeDirFile := AppExecDir() + "/" + filename
_, err = os.Stat(exeDirFile)
if err == nil {
template, err = ioutil.ReadFile(exeDirFile)
return
}
template, err = fs.ReadFile(filename)
return
}
4 changes: 1 addition & 3 deletions go.mod
Expand Up @@ -4,6 +4,4 @@ go 1.18

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

require (
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
)
require golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
4 changes: 0 additions & 4 deletions go.sum
@@ -1,8 +1,4 @@
github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef h1:UD99BBEz19F21KhOFHLNAI6KodDWUvXaPr4Oqu8yMV8=
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit 82761b8

Please sign in to comment.