-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (216 loc) · 7.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"GoSniff/front"
"GoSniff/sniffer"
"fmt"
"log"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
func CreateNewListenWindow(myApp fyne.App, listenChannel chan sniffer.SniffPacket, stopChannel chan int) {
var packetData = []string{}
var packDetailData = []sniffer.SniffPacket{}
listData := binding.BindStringList(&packetData)
// listData.Set(packetData)
list := widget.NewListWithData(listData,
func() fyne.CanvasObject {
return widget.NewLabel("")
},
func(i binding.DataItem, o fyne.CanvasObject) {
o.(*widget.Label).Bind(i.(binding.String))
o.(*widget.Label).TextStyle = fyne.TextStyle{Monospace: true, Bold: true}
// o.(*widget.Label).Alignment = fyne.TextAlignLeading
// o.(*widget.Label).Resize(fyne.NewSize(1000, o.MinSize().Height))
})
list.OnSelected = func(id widget.ListItemID) {
if id == 0 {
list.Unselect(id)
return
}
packet := packDetailData[id-1]
info := packet.Info
var showInfo string
showInfo += "Protocol: " + packet.Protocol + "\n"
showInfo += "Source MAC address: " + info.SourceMac + "\n"
showInfo += "Destination MAC address: " + info.DestinationMac + "\n"
if info.SourceIP != "" && info.DestinationIP != "" {
showInfo += "Source IP address: " + info.SourceIP + "\n"
showInfo += "Destination IP address: " + info.DestinationIP + "\n"
}
if info.SourcePort != "" && info.DestinationPort != "" {
showInfo += "Source Port: " + info.SourcePort + "\n"
showInfo += "Destination Port: " + info.DestinationPort + "\n"
}
showInfo += "Data length: " + info.Size + " Bytes \n"
showInfo += "Details: \n" + info.Detail
detailWindow := front.CreateDetailWindow(myApp, showInfo, info.Dump)
detailWindow.Show()
detailWindow.SetOnClosed(func() {
list.Unselect(id)
})
}
isStop := false // 是否停止
// 创建一个顶层容器,将滚动容器放入其中
MaxList := container.NewStack(list)
var Button *widget.Button // 停止/开始按钮
// 停止抓包
Button = widget.NewButton("Stop", func() {
if Button.Text == "Stop" {
Button.SetText("Start")
stopChannel <- 1
isStop = true
log.Println("Stop Listen!")
} else {
Button.SetText("Stop")
stopChannel <- 0
isStop = false
log.Println("Start Listen!")
}
})
SaveButton := widget.NewButton("Save", func() {
if !isStop {
front.CreateTips(myApp, 1)
return
}
stopChannel <- 2
log.Println("Saved!")
})
bottom := container.NewGridWithColumns(2, Button, SaveButton)
container := container.NewBorder(nil, bottom, nil, nil, MaxList)
listenWindow := myApp.NewWindow("Listening")
listenWindow.SetOnClosed(func() {
stopChannel <- 3
})
listenWindow.SetContent(container)
listenWindow.Resize(fyne.NewSize(1100, 600))
listenWindow.Show()
str := fmt.Sprintf("%-5s%-30s%-40s%-40s%-10s", "No.", "Time", "Source", "Destination", "Protocol")
listData.Append(str)
for packet := range listenChannel {
No := len(packDetailData) + 1
packDetailData = append(packDetailData, packet)
str := fmt.Sprintf("%-5d%-30s%-40s%-40s%-10s", No, packet.Time, packet.Source, packet.Destination, packet.Protocol)
// str := "Time\t\t" + packet.Source + "\t\t" + packet.Destination + "\t\t" + packet.Protocol
// fmt.Println("recv packet")
listData.Append(str)
}
}
func ShowPcapFile(myApp fyne.App, filePath string) {
var packetData = []string{}
var parsedPacket = sniffer.ParsePcapFile(filePath)
var packDetailData = []sniffer.SniffPacket{}
listData := binding.BindStringList(&packetData)
// listData.Set(packetData)
list := widget.NewListWithData(listData,
func() fyne.CanvasObject {
return widget.NewLabel("")
},
func(i binding.DataItem, o fyne.CanvasObject) {
o.(*widget.Label).Bind(i.(binding.String))
o.(*widget.Label).TextStyle = fyne.TextStyle{Monospace: true, Bold: true}
// o.(*widget.Label).Alignment = fyne.TextAlignLeading
// o.(*widget.Label).Resize(fyne.NewSize(1000, o.MinSize().Height))
})
list.OnSelected = func(id widget.ListItemID) {
if id == 0 {
list.Unselect(id)
return
}
packet := packDetailData[id-1]
info := packet.Info
var showInfo string
showInfo += "Protocol: " + packet.Protocol + "\n"
showInfo += "Source MAC address: " + info.SourceMac + "\n"
showInfo += "Destination MAC address: " + info.DestinationMac + "\n"
if info.SourceIP != "" && info.DestinationIP != "" {
showInfo += "Source IP address: " + info.SourceIP + "\n"
showInfo += "Destination IP address: " + info.DestinationIP + "\n"
}
if info.SourcePort != "" && info.DestinationPort != "" {
showInfo += "Source Port: " + info.SourcePort + "\n"
showInfo += "Destination Port: " + info.DestinationPort + "\n"
}
showInfo += "Data length: " + info.Size + " Bytes \n"
showInfo += "Details: \n" + info.Detail
detailWindow := front.CreateDetailWindow(myApp, showInfo, info.Dump)
detailWindow.Show()
detailWindow.SetOnClosed(func() {
list.Unselect(id)
})
}
// 创建一个顶层容器,将滚动容器放入其中
MaxList := container.NewStack(list)
// 停止抓包
container := container.NewBorder(nil, nil, nil, nil, MaxList)
listenWindow := myApp.NewWindow("Listening")
listenWindow.SetContent(container)
listenWindow.Resize(fyne.NewSize(1100, 600))
listenWindow.Show()
str := fmt.Sprintf("%-5s%-30s%-40s%-40s%-10s", "No.", "Time", "Source", "Destination", "Protocol")
listData.Append(str)
for _, packet := range parsedPacket {
No := len(packDetailData) + 1
packDetailData = append(packDetailData, packet)
str := fmt.Sprintf("%-5d%-30s%-40s%-40s%-10s", No, packet.Time, packet.Source, packet.Destination, packet.Protocol)
// str := "Time\t\t" + packet.Source + "\t\t" + packet.Destination + "\t\t" + packet.Protocol
// fmt.Println("recv packet")
listData.Append(str)
}
}
func main() {
myApp := app.New()
mainWindow := myApp.NewWindow("主菜单")
// myApp.Settings().SetTheme(&front.MyTheme{})
mainWindow.Resize(fyne.NewSize(400, 300))
log.Println("app created......")
log.Println("app running......")
devicesName := sniffer.GetAllDeviceName()
Tips := widget.NewLabel("Choose an interface to listen:")
filterEntry := widget.NewEntry()
filterEntry.SetPlaceHolder("Input filter...")
SelectedDeviceName := ""
selectedInterface := widget.NewSelect(devicesName, func(value string) {
SelectedDeviceName = value
log.Println("Select set to", value)
})
startButton := widget.NewButton("start listen", func() {
log.Println("Start listen.....")
listenChannel := make(chan sniffer.SniffPacket)
stopChannel := make(chan int)
if SelectedDeviceName == "" {
front.CreateTips(myApp, 3)
return
}
if !sniffer.CheckBPFSyntax(SelectedDeviceName, filterEntry.Text) {
front.CreateTips(myApp, 2)
return
}
go CreateNewListenWindow(myApp, listenChannel, stopChannel)
go sniffer.Sniff(SelectedDeviceName, listenChannel, stopChannel, filterEntry.Text)
})
// var filePath string
fileDialog := dialog.NewFileOpen(func(file fyne.URIReadCloser, err error) {
// filePath := file.URI().Path()
if file != nil {
filePath := file.URI().Path()
go ShowPcapFile(myApp, filePath)
}
mainWindow.Resize(fyne.NewSize(400, 300))
// // fmt.Println(file.URI().Path())
// fmt.Println(filePath)
// go ShowPcapFile(myApp, filePath)
}, mainWindow)
sperateLabel := widget.NewLabelWithStyle("or", fyne.TextAlignCenter, fyne.TextStyle{})
openfileButton := widget.NewButton("open a pcap file", func() {
fileDialog.SetFilter(storage.NewExtensionFileFilter([]string{".pcap"}))
mainWindow.Resize(fyne.NewSize(800, 600))
fileDialog.Show()
})
mainWindow.SetContent(container.NewVBox(Tips, filterEntry, selectedInterface, startButton, sperateLabel, openfileButton))
mainWindow.ShowAndRun()
}