Skip to content

Commit

Permalink
feat: 增加启动自动获取及优化UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Jun 1, 2023
1 parent 9b6923a commit a7250bb
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 85 deletions.
3 changes: 3 additions & 0 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type FetchConf struct {
Method string
SelectOrigin string
CustomUrl string
AutoFetch bool
}
Server struct {
Interval int
Expand All @@ -22,6 +23,7 @@ func (f *FetchConf) Storage() {
viper.Set("client.method", f.Client.Method)
viper.Set("client.selectorigin", f.Client.SelectOrigin)
viper.Set("client.customurl", f.Client.CustomUrl)
viper.Set("client.autofetch", f.Client.AutoFetch)
viper.Set("server.interval", f.Server.Interval)
viper.Set("server.port", f.Server.Port)
if err := viper.WriteConfigAs("conf.yaml"); err != nil {
Expand All @@ -36,6 +38,7 @@ func LoadFetchConf() *FetchConf {
viper.SetDefault("client.interval", 60)
viper.SetDefault("client.method", "官方指定hosts源")
viper.SetDefault("client.selectorigin", "FetchGithubHosts")
viper.SetDefault("client.autofetch", false)
viper.SetDefault("server.interval", 60)
viper.SetDefault("server.port", 9898)
var fileNotExits bool
Expand Down
24 changes: 12 additions & 12 deletions fetch_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/h2non/filetype"
"io/ioutil"
"io"
"net"
"net/http"
"os"
Expand All @@ -19,7 +19,7 @@ const (
Darwin = "darwin"
)

func startClient(ticker *FetchTicker, url string, flog *fetchLog) {
func startClient(ticker *FetchTicker, url string, flog *FetchLog) {
flog.Print("远程hosts获取链接:" + url)
fn := func() {
if err := ClientFetchHosts(url); err != nil {
Expand All @@ -40,7 +40,7 @@ func startClient(ticker *FetchTicker, url string, flog *fetchLog) {
}
}

func startServer(ticker *FetchTicker, port int, flog *fetchLog) {
func startServer(ticker *FetchTicker, port int, flog *FetchLog) {
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Println("服务启动失败(可能是目标端口已被占用):", err.Error())
Expand Down Expand Up @@ -74,7 +74,7 @@ func startServer(ticker *FetchTicker, port int, flog *fetchLog) {
}

type serverHandle struct {
flog *fetchLog
flog *FetchLog
}

func (s *serverHandle) ServeHTTP(resp http.ResponseWriter, request *http.Request) {
Expand All @@ -83,7 +83,7 @@ func (s *serverHandle) ServeHTTP(resp http.ResponseWriter, request *http.Request
if p == "/" {
p = "/index.html"
}
file, err := ioutil.ReadFile(AppExecDir() + p)
file, err := os.ReadFile(AppExecDir() + p)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
resp.Write([]byte("server error"))
Expand Down Expand Up @@ -116,7 +116,7 @@ func ClientFetchHosts(url string) (err error) {
return
}

fetchHosts, err := ioutil.ReadAll(resp.Body)
fetchHosts, err := io.ReadAll(resp.Body)
if err != nil {
err = ComposeError("读取最新的hosts失败", err)
return
Expand All @@ -136,7 +136,7 @@ func ClientFetchHosts(url string) (err error) {
hosts.WriteString(newlineChar)
}
}
if err = ioutil.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
if err = os.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
err = ComposeError("写入hosts文件失败,请用超级管理员身份启动本程序!", err)
return
}
Expand All @@ -158,12 +158,12 @@ func ServerFetchHosts() (err error) {
return
}

if err = ioutil.WriteFile(execDir+"/hosts.json", hostJson, 0775); err != nil {
if err = os.WriteFile(execDir+"/hosts.json", hostJson, 0775); err != nil {
err = ComposeError("写入数据到hosts.json文件失败", err)
return
}

if err = ioutil.WriteFile(execDir+"/hosts.txt", hostFile, 0775); err != nil {
if err = os.WriteFile(execDir+"/hosts.txt", hostFile, 0775); err != nil {
err = ComposeError("写入数据到hosts.txt文件失败", err)
return
}
Expand All @@ -176,7 +176,7 @@ func ServerFetchHosts() (err error) {
}

templateData := strings.Replace(string(templateFile), "<!--time-->", now, 1)
if err = ioutil.WriteFile(execDir+"/index.html", []byte(templateData), 0775); err != nil {
if err = os.WriteFile(execDir+"/index.html", []byte(templateData), 0775); err != nil {
err = ComposeError("写入更新信息到首页文件失败", err)
return
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err

func getCleanGithubHosts() (hosts *bytes.Buffer, err error) {
hostsPath := GetSystemHostsPath()
hostsBytes, err := ioutil.ReadFile(hostsPath)
hostsBytes, err := os.ReadFile(hostsPath)
if err != nil {
err = ComposeError("读取文件hosts错误", err)
return
Expand Down Expand Up @@ -269,7 +269,7 @@ func flushCleanGithubHosts() (err error) {
if err != nil {
return
}
if err = ioutil.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
if err = os.WriteFile(GetSystemHostsPath(), hosts.Bytes(), os.ModeType); err != nil {
err = ComposeError("写入hosts文件失败,请用超级管理员身份启动本程序!", err)
}
return
Expand Down
7 changes: 0 additions & 7 deletions fetch_hosts_test.go

This file was deleted.

57 changes: 33 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ module github.com/Licoy/fetch-github-hosts
go 1.18

require (
fyne.io/fyne/v2 v2.2.2
github.com/getlantern/elevate v0.0.0-20210901195629-ce58359e4d0e
fyne.io/fyne/v2 v2.3.4
github.com/getlantern/elevate v0.0.0-20220903142053-479ab992b264
github.com/h2non/filetype v1.1.3
github.com/jessevdk/go-flags v1.5.0
github.com/spf13/viper v1.12.0
)

require (
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect
fyne.io/systray v1.10.1-0.20230403195833-7dc3c09283d6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect
github.com/fredbi/uri v0.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
github.com/getlantern/byteexec v0.0.0-20170405023437-4cfb26ec74f4 // indirect
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirect
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799 // indirect
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 // indirect
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirect
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirect
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirect
github.com/getlantern/byteexec v0.0.0-20220903142956-e6ed20032cfd // indirect
github.com/getlantern/context v0.0.0-20220418194847-3d5e7a086201 // indirect
github.com/getlantern/errors v1.0.3 // indirect
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c // indirect
github.com/getlantern/golog v0.0.0-20230503153817-8e72de7e0a65 // indirect
github.com/getlantern/hex v0.0.0-20220104173244-ad7e4b9194dc // indirect
github.com/getlantern/hidden v0.0.0-20220104173330-f221c5a24770 // indirect
github.com/getlantern/ops v0.0.0-20230519221840-1283e026181c // indirect
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-text/typesetting v0.0.0-20230405155246-bf9c697c6e16 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand All @@ -44,17 +47,23 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect
github.com/stretchr/testify v1.7.2 // indirect
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/yuin/goldmark v1.4.0 // indirect
golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
github.com/yuin/goldmark v1.4.13 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/image v0.3.0 // indirect
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.6.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit a7250bb

Please sign in to comment.