-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyncgooglehosts.go
More file actions
96 lines (77 loc) · 1.47 KB
/
Copy pathsyncgooglehosts.go
File metadata and controls
96 lines (77 loc) · 1.47 KB
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
/*
syncGoogleHosts
for the acccess to google against the Chinese wall
@author JaSonS (JaSonS_toy@foxmail.com)
*/
package main
import (
"bytes"
"github.com/Unknwon/goconfig"
"io/ioutil"
"log"
"net/http"
)
const (
hostsPath = "C:/Windows/System32/drivers/etc/hosts"
config = "config.ini"
)
var resAddr string
var startStr string
var endStr string
func GetConfig() {
c, err := goconfig.LoadConfigFile(config)
if err != nil {
log.Fatal(err)
}
resAddr, err = c.GetValue("hosts", "url")
if err != nil {
log.Fatal(err)
}
startStr, err = c.GetValue("hosts", "start")
if err != nil {
log.Fatal(err)
}
endStr, err = c.GetValue("hosts", "end")
if err != nil {
log.Fatal(err)
}
}
func main() {
GetConfig()
res, err := http.Get(resAddr)
if err != nil {
log.Fatal(err)
}
buff, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
//remove the head and the tail
starti := bytes.Index(buff, []byte(startStr))
endi := bytes.Index(buff, []byte(endStr))
stepone := buff[starti : endi+len(endStr)]
//remove all of the <> tags
var steptwo []byte
isTag := false
for _, v := range stepone {
if isTag {
if v == '>' {
isTag = false
}
continue
} else {
if v == '<' {
isTag = true
continue
}
steptwo = append(steptwo, v)
}
}
//remove the
result := bytes.Replace(steptwo, []byte(" "), []byte(""), -1)
err = ioutil.WriteFile(hostsPath, result, 0722)
if err != nil {
log.Fatal(err)
}
}