-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (193 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"encoding/json"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/systray"
"github.com/Ullaakut/nmap"
"go-scan/ui"
"os"
"strings"
"sync"
"time"
)
func Emain() {
App := app.NewWithID(ui.AppName)
mainWindow := App.NewWindow(ui.AppName + " " + ui.VerSion)
//App.SetIcon(ui.Logo)
ui.MainWindow(mainWindow)
if desk, ok := App.(desktop.App); ok {
ui.SetupSystray(desk, mainWindow)
}
mainWindow.Resize(fyne.Size{Width: ui.Width, Height: ui.Height})
mainWindow.SetFixedSize(true)
mainWindow.CenterOnScreen()
//mainWindow.SetIcon(ui.Logo)
// setting intercept not to close app, but hide window,
// and close only via tray
mainWindow.SetCloseIntercept(func() {
ui.Notification(ui.AppName, fmt.Sprintf("%s minimized!", ui.AppName))
mainWindow.Hide()
})
mainWindow.Show()
App.Lifecycle().SetOnStarted(func() {
systray.SetTooltip(ui.AppName)
})
App.Run()
err := os.Unsetenv("FYNE_FONT")
if err != nil {
return
}
}
var (
ListHostIP []string
wg sync.WaitGroup
rwlock sync.RWMutex
)
type Port struct {
Id uint16 `json:"id"`
Name string `json:"name"`
State string `json:"state"`
}
type HostInfo struct {
Ip string `json:"ip"`
OsName string `json:"os_name"`
Ports []*Port `json:"ports"`
}
// 初始化连接池
//func initPool(server, pass string, database int) *redis.Pool {
// return &redis.Pool{
// // 设置最大空闲
// MaxIdle: 64,
// // 设置最大活跃数 0代表无限
// MaxActive:0,
// // 闲置空闲时间,单位秒
// IdleTimeout:3600,
// Dial: func() (redis.Conn, error) {
// conn, err := redis.Dial("tcp", server,
// redis.DialReadTimeout(time.Second*10),
// redis.DialConnectTimeout(time.Second*30),
// redis.DialPassword(pass),
// redis.DialDatabase(database),
// )
// if err != nil {
// fmt.Println("ERROR: fail init redis pool:", err.Error())
// return nil, fmt.Errorf("ERROR: fail init redis pool: %s", err.Error())
// }
// return conn, err
// },
// }
//}
func main() {
start := time.Now()
// redis创建连接池 ip, pass ,db
//pool := initPool("192.168.1.5:6379", "", 11)
//// 池子的关闭
//defer pool.Close()
//
//// 从池子取连接
//conn := pool.Get()
//
//// 当前一个连接的关闭,用完即放回去池子并不是真的关闭
//defer conn.Close()
//go func() {
// // 清理在线列表
// rep, err := redis.Values(conn.Do("lrange", "iplist", 0, -1))
// for _, v := range rep {
// _, err = conn.Do("del", v.([]byte))
// }
// _, err = conn.Do("del", "iplist")
// if err != nil {
// fmt.Println("del err ", err)
// }
//}()
// nmap -O 192.168.0.0/24
scanner, err := nmap.NewScanner(
nmap.WithTargets("192.168.100.0/24"),
nmap.WithPingScan(),
)
if err != nil {
fmt.Printf("unable to create nmap scanner: %v", err)
}
result, _, err := scanner.Run()
if err != nil {
fmt.Printf("run nmap scan failed: %v", err)
}
for _, host := range result.Hosts {
// 查询出所有在线 IP
ip := fmt.Sprintf("%s", host.Addresses[0])
// 返回给数组
ListHostIP = append(ListHostIP, ip)
}
for _, ip := range ListHostIP {
// 遍历每个ip 开启多个 goroutine
go func(ip string) {
defer wg.Done()
data := HostsInfo(ip)
rwlock.RLock()
if data != "" {
fmt.Println(ip)
//_, err = conn.Do("set", ip, data)
//_, err = conn.Do("rpush", "iplist", ip)
if err != nil {
fmt.Println("set err ", err)
}
}
rwlock.RUnlock()
}(ip)
wg.Add(1)
}
// 等待所有完成
wg.Wait()
fmt.Println(time.Now().Sub(start))
}
// 扫描具体信息
func HostsInfo(ips string) (data string) {
scanner, err := nmap.NewScanner(
nmap.WithTargets(ips),
// 开启快速查询 -F
//nmap.WithFastMode(),
// 标准查询 -O
nmap.WithOSDetection(),
)
if err != nil {
fmt.Println("unable to create nmap scanner: %v", err)
}
result, _, err := scanner.Run()
if err != nil {
fmt.Println("run nmap scan failed: %v", err)
}
// 初始化结构体
hosts := new(HostInfo)
for _, host := range result.Hosts {
// 过滤 主机 条件
for _, match := range host.OS.Matches {
os_name := match.Name
if strings.Contains(os_name, "Linux") && !strings.Contains(os_name, "Android") {
rwlock.Lock()
hosts.OsName = match.Name
hosts.Ip = ips
rwlock.Unlock()
// 查主机 端口 和服务 信息
for _, port := range host.Ports {
if port.Service.Name != "" {
rwlock.Lock()
hosts.Ports = append(hosts.Ports, &Port{
Id: port.ID,
State: port.State.State,
Name: port.Service.Name,
})
rwlock.Unlock()
}
}
}
}
}
if hosts.Ports != nil {
json_data, _ := json.Marshal(hosts)
return string(json_data)
}
return ""
}