-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (176 loc) · 4.65 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
200
201
202
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime/debug"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
BlockHeader = "### hosts-go ###"
BlockFooter = "### hosts-go end ###"
)
var (
version = "dev"
systemHostsPath = "/etc/hosts"
)
func main() {
i, _ := debug.ReadBuildInfo()
if i != nil && i.Main.Version != "(devel)" {
version = i.Main.Version
}
rootCmd := newCmd()
rootCmd.Version = version
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
os.Exit(1)
}
}
func newCmd() *cobra.Command {
var (
testOnly bool
contentOnly bool
serviceAction string
urls []string
duration time.Duration
reloadCommand string
)
cmd := &cobra.Command{
Use: "hosts-go",
Short: "Fetch and merge hosts files from the internet",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if len(urls) == 0 {
return errors.New("please provide at least one URL using --url flag")
}
switch serviceAction {
case "install":
return installService(duration, urls)
case "uninstall":
return uninstallService(duration, urls)
case "":
default:
return errors.New("invalid service action")
}
if !testOnly {
runLoop(urls, duration, reloadCommand)
return nil
}
hostsBytes, err := FetchAndMergeHosts(urls)
if err != nil {
return err
}
if contentOnly {
fmt.Println(string(hostsBytes))
return nil
}
hostsContent, err := RenderHostsFile(systemHostsPath, hostsBytes)
if err != nil {
return err
}
fmt.Println(hostsContent)
return nil
},
}
cmd.Flags().StringSliceVarP(&urls, "url", "u", nil, "URLs to fetch hosts files from")
cmd.Flags().BoolVarP(&testOnly, "test", "t", false, "Only output the rendered hosts content")
cmd.Flags().BoolVar(&contentOnly, "content-only", false, "Output only the fetched hosts content")
cmd.Flags().StringVarP(&serviceAction, "service", "s", "", "Install or uninstall service")
cmd.Flags().DurationVarP(&duration, "duration", "d", 1*time.Hour, "Duration between each fetch")
cmd.Flags().StringVar(&reloadCommand, "reload-command", "", "Command to execute after hosts file updated")
return cmd
}
func FetchAndMergeHosts(urls []string) ([]byte, error) {
var merged bytes.Buffer
for _, url := range urls {
resp, err := http.Get(url)
if err == nil && resp.StatusCode != http.StatusOK {
err = fmt.Errorf("http status %s", resp.Status)
}
if err != nil {
return nil, fmt.Errorf("failed to fetch hosts file from %s: %v", url, err)
}
_, err = io.Copy(&merged, resp.Body)
_ = resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("failed to read hosts file from %s: %v", url, err)
}
}
return merged.Bytes(), nil
}
func RenderHostsFile(hostsFile string, content []byte) (string, error) {
file, err := os.OpenFile(hostsFile, os.O_RDONLY, 0644)
if err != nil {
return "", fmt.Errorf("failed to open hosts file: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var result, after strings.Builder
var step int
for scanner.Scan() {
line := scanner.Text()
if line == BlockHeader && step == 0 {
step = 1
} else if line == BlockFooter && step == 1 {
step = 2
continue
}
if step == 0 {
result.WriteString(line + "\n")
} else if step == 2 {
after.WriteString(line + "\n")
}
}
if scanner.Err() != nil {
return "", fmt.Errorf("failed to read hosts file: %v", scanner.Err())
}
result.WriteString(BlockHeader + "\n")
result.Write(content)
result.WriteString("\n\n" + "# hosts-go updated at " + time.Now().Format(time.RFC3339) + "\n")
result.WriteString(BlockFooter + "\n")
result.WriteString(after.String())
return result.String(), nil
}
func WriteHostsFile(hostsFile, content string) error {
if err := os.WriteFile(hostsFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write hosts file: %v", err)
}
return nil
}
func runLoop(urls []string, duration time.Duration, reloadCommand string) {
tick := time.Tick(duration)
for ; true; <-tick {
logrus.Info("start service")
if err := update(urls); err != nil {
logrus.Error(err)
continue
}
logrus.Info("update hosts file successfully")
if reloadCommand != "" {
logrus.Info("run reload command")
cmd := exec.Command("sh", "-c", reloadCommand)
if err := cmd.Run(); err != nil {
logrus.Error(err)
}
}
}
}
func update(urls []string) error {
hostsBytes, err := FetchAndMergeHosts(urls)
if err != nil {
return err
}
hostsContent, err := RenderHostsFile(systemHostsPath, hostsBytes)
if err != nil {
return err
}
return WriteHostsFile(systemHostsPath, hostsContent)
}