-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
218 lines (193 loc) · 4.45 KB
/
app.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package autohosts
import (
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"time"
"github.com/ggymm/ping"
"github.com/pkg/errors"
"github.com/ying32/govcl/vcl"
)
type App struct {
wd string
view *MainForm
scanner *Scanner
hosts []string
domains []string
nameservers []string
}
func NewApp() *App {
return &App{
scanner: NewScanner(),
}
}
func (a *App) init() {
dir := ""
exe, err := os.Executable()
if err != nil {
panic(err)
}
path := filepath.Base(exe)
if !strings.HasPrefix(exe, os.TempDir()) && !strings.HasPrefix(path, "___") {
dir = filepath.Dir(exe)
} else {
_, filename, _, ok := runtime.Caller(0)
if ok {
// 需要根据当前文件所处目录,修改相对位置
dir = filepath.Dir(filename)
}
}
a.wd = filepath.Join(dir, "data")
// 设置 app 工作目录
err = os.Chdir(a.wd)
if err != nil {
panic(errors.WithStack(err))
}
}
func (a *App) showView() {
vcl.DEBUG = false
vcl.Application.SetScaled(true)
vcl.Application.Initialize()
vcl.Application.SetMainFormOnTaskBar(true)
// 创建主窗口
vcl.Application.CreateForm(&a.view)
// 设置窗口显示事件
a.view.SetOnShow(func(sender vcl.IObject) {
go func() {
a.hosts = LoadHosts()
vcl.ThreadSync(func() {
for _, host := range a.hosts {
if len(host) > 0 {
a.view.resultMemo.Lines().Add(host)
}
}
})
log.Info().Msg("init hosts")
}()
go func() {
a.domains = LoadDomains()
vcl.ThreadSync(func() {
for _, domain := range a.domains {
if len(domain) > 0 {
a.view.domainMemo.Lines().Add(domain)
}
}
})
log.Info().Msg("init domains")
}()
go func() {
a.nameservers = LoadNameservers()
log.Info().Msg("init nameservers")
}()
a.view.renewButton.SetOnClick(func(sender vcl.IObject) {
a.view.disableView()
go func() {
defer a.view.enableView()
//RenewNameservers()
//a.nameservers = LoadNameservers()
}()
})
a.view.searchButton.SetOnClick(func(sender vcl.IObject) {
a.view.disableView()
a.view.resultMemo.Clear()
go func() {
// 重置 view 按钮状态
defer func() {
a.view.enableView()
a.view.searchButton.SetCaption("开始查询")
}()
l := len(a.domains)
for i, domain := range a.domains {
log.Info().Str("domain", domain).Msg("scan ip")
vcl.ThreadSync(func() {
a.view.searchButton.SetCaption(fmt.Sprintf("正在查询 (%d/%d)", i+1, l))
})
ip := ""
list := a.scanner.Scan(domain, a.nameservers)
if len(list) > 0 {
wg := &sync.WaitGroup{}
for _, item := range list {
wg.Add(1)
log.Info().
Str("1.ip", item.Addr).
Str("3.domain", domain).Msg("ping ip")
go func(item *Info) {
defer wg.Done()
p, _ := ping.NewPinger(item.Addr)
p.Count = 4 // 尝试次数
p.Timeout = 1 * time.Second
p.SetPrivileged(true)
err := p.Run()
if err != nil {
return
}
stats := p.Statistics()
if stats.PacketsRecv != 0 {
item.Rtt = stats.AvgRtt
} else {
item.Rtt = 99 * time.Second
}
}(item)
}
wg.Wait()
// 排序
slices.SortFunc(list, func(i, j *Info) int {
if i.Rtt < j.Rtt {
return -1
} else {
return 1
}
})
ip = list[0].Addr + " " + domain
// 保存到文件
ips := make([]string, 0)
for _, item := range list {
ips = append(ips, item.String())
}
err := WriteLines(fmt.Sprintf("ips/%s.txt", domain), ips)
if err != nil {
log.Error().
Str("domain", domain).
Err(errors.WithStack(err)).Msg("write domain ips error")
continue
}
} else {
ip = "unknown"
}
vcl.ThreadSync(func() {
a.view.resultMemo.Lines().Add(ip)
})
}
}()
})
// 生成 hosts 文件
a.view.generateButton.SetOnClick(func(sender vcl.IObject) {
go func() {
list := make([]string, 0)
lines := a.view.resultMemo.Lines()
count := lines.Count()
for i := int32(0); i < count; i++ {
list = append(list, lines.S(i))
}
err := WriteLines(hostsFile, list)
if err != nil {
log.Error().Err(errors.WithStack(err)).Msg("write hosts error")
return
}
vcl.ThreadSync(func() {
vcl.ShowMessage("生成 hosts 文件成功")
})
}()
})
})
// 启动应用
vcl.Application.Run()
}
func (a *App) Run() {
a.init()
a.showView()
}