-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
192 lines (180 loc) · 4.68 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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)
var domains = []string{
"github.com",
"gist.github.com",
"assets-cdn.github.com",
"raw.githubusercontent.com",
"gist.githubusercontent.com",
"cloud.githubusercontent.com",
"camo.githubusercontent.com",
"avatars.githubusercontent.com",
"avatars0.githubusercontent.com",
"avatars1.githubusercontent.com",
"avatars2.githubusercontent.com",
"avatars3.githubusercontent.com",
"avatars4.githubusercontent.com",
"avatars5.githubusercontent.com",
"avatars6.githubusercontent.com",
"avatars7.githubusercontent.com",
"avatars8.githubusercontent.com",
"github.githubassets.com",
}
var startTag = "# GitHub Start\r\n"
var endTag = "# GitHub End\r\n"
func main() {
var mode int
fmt.Println("1. Create the host file to current directory (default)")
fmt.Println("2. Append the host to system host file in linux (/etc/hosts)")
fmt.Println("3. Append the host to system host file in windows (/mnt/c/Windows/System32/drivers/etc/hosts)")
for {
fmt.Print("Choose the mode [1-3]: ")
fmt.Scanln(&mode)
if mode >= 0 && mode <= 3 {
break
}
}
filePath := ""
if mode == 1 || mode == 0 {
filePath = "hosts"
WriteHostToFile("", filePath)
return
} else if mode == 2 {
filePath = "/etc/hosts"
} else {
filePath = "/mnt/c/Windows/System32/drivers/etc/hosts"
}
filePathBak := filePath + "_bak"
// backup
Copy(filePathBak, filePath)
// read content
in, err := os.Open(filePath)
if err != nil {
fmt.Println("open file fail:", err)
os.Exit(-1)
}
defer in.Close()
reader := bufio.NewReader(in)
var strSlice []string
line := 0
startLine := 0
endLine := 0
for {
line = line + 1
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
strSlice = append(strSlice, str)
if str == startTag {
startLine = line
} else if str == endTag {
endLine = line
}
}
if startLine > 0 && endLine > 0 {
strSlice = append(strSlice[:startLine-1], strSlice[endLine:]...)
}
str := strings.Join(strSlice, "")
// write content
WriteHostToFile(str, filePath)
}
type HostChan struct {
Domain string
Ip string
Err error
}
func WriteHostToFile(str string, filePath string) {
str += startTag
ch := make(chan *HostChan)
for _, v := range domains {
go httpPostForm(v, ch)
}
fmt.Println("================\nstart get host:\n================")
hostMap := make(map[string]string)
for range domains {
chRec := <-ch
if chRec.Err != nil {
fmt.Println(chRec.Err.Error() + " " + chRec.Domain)
return
}
hostMap[chRec.Domain] = chRec.Ip
fmt.Println(chRec.Ip + " " + chRec.Domain)
}
for _, v := range domains {
str += hostMap[v] + " " + v + "\r\n"
}
str += endTag
out, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
fmt.Println("open file fail:", err)
return
}
defer out.Close()
writer := bufio.NewWriter(out)
writer.WriteString(str)
writer.Flush()
fmt.Println("================\ndone\n================")
}
func Copy(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}
func httpPostForm(domain string, ch chan<- *HostChan) {
client := &http.Client{}
req, err := http.NewRequest("POST", "https://www.ipaddress.com/ip-lookup", strings.NewReader("host="+domain))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
ch <- &HostChan{Domain: domain, Err: err}
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ch <- &HostChan{Domain: domain, Err: err}
return
}
shortStr := string(body)[12000:30000]
index := strings.Index(shortStr, " ("+domain+")")
var res string
if index > 0 {
strStart := "IP Lookup : "
indexStart := strings.Index(shortStr, strStart)
res = shortStr[indexStart+len(strStart) : index]
} else {
strStart := "<input name=\"host\" type=\"radio\" value=\""
indexStart := strings.Index(shortStr, strStart)
indexEnd := strings.Index(shortStr[indexStart+len(strStart):indexStart+len(strStart)+100], "\"")
if indexEnd > 0 {
res = shortStr[indexStart+len(strStart) : indexStart+len(strStart)+indexEnd]
} else {
ch <- &HostChan{Domain: domain, Err: errors.New("get indexEnd error")}
}
}
if res == "" {
ch <- &HostChan{Domain: domain, Err: errors.New("empty host")}
return
}
ch <- &HostChan{Domain: domain, Ip: res, Err: nil}
return
}