-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
272 lines (236 loc) · 7.48 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"context"
"fmt"
"github.com/Masterminds/semver"
"github.com/google/gopacket/pcap"
"github.com/jackpal/gateway"
"github.com/pkg/browser"
"github.com/wailsapp/wails/v2/pkg/runtime"
"net"
"netctrl.io/monitor/communication"
"netctrl.io/monitor/errors"
"netctrl.io/monitor/identification"
"netctrl.io/monitor/networking/network"
"netctrl.io/monitor/networking/poison"
"netctrl.io/monitor/networking/remote"
"netctrl.io/monitor/networking/scan"
"netctrl.io/monitor/permissions"
"os"
"time"
)
var Version, _ = semver.NewVersion("0.2.1")
// App struct
type App struct {
ctx context.Context
JWT string
pcapHandle *pcap.Handle
stopScanHandle chan interface{}
stopPoisonHandle chan interface{}
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called at application startup
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.ctx = ctx
}
// domReady is called after the front-end dom has been loaded
func (b *App) domReady(ctx context.Context) {
// Launch error handler
go b.errorHandler()
// Check for updates
b.UpdateCheck()
}
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
func (b *App) errorHandler() {
for {
select {
case appErr := <-errors.GlobalErrChan:
runtime.EventsEmit(b.ctx, "error", appErr)
}
}
}
// Greet returns a greeting for the given name
func (b *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (b *App) SetJWT(JWT string) {
runtime.LogInfo(b.ctx, fmt.Sprintf("Called SetJWT() from frontend with jwt: %s", JWT))
b.JWT = JWT
}
func (b *App) GetVersion() string {
return Version.String()
}
func (b *App) Quit() {
os.Exit(0)
}
func (b *App) UpdateCheck() {
u, err := remote.UpdateAvailable(Version)
if err != nil {
runtime.LogError(b.ctx, fmt.Sprintf("Error checking for update: %v", err))
return
}
if !u {
return
}
selected, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "An update is available",
Message: "Do you want to download the newest NetCTRL Monitor update?",
Buttons: []string{"Yes", "No"},
DefaultButton: "Yes",
CancelButton: "No",
Icon: nil,
})
if err != nil {
runtime.LogError(b.ctx, fmt.Sprintf("Error creating update modal: %v", err))
}
if selected == "Yes" {
_ = browser.OpenURL(remote.APIBaseURL + "/user/download")
}
}
func (b *App) NeedsPermissions() bool {
return permissions.NeedsPermissions()
}
func (b *App) SetPermissions() bool {
return permissions.SetPermissions()
}
/// Machine identification:
func (b *App) GetOS() string {
return identification.GetOS()
}
func (b *App) GetMachineID() string {
return identification.GetMachineID()
}
func (b *App) GetMachineHostname() string {
return identification.GetMachineHostname()
}
/// Networking:
func (b *App) Initialize(iface net.Interface) {
runtime.LogInfo(b.ctx, "Called InitializePcap() from frontend")
h, err := pcap.OpenLive(iface.Name, 65536, true, pcap.BlockForever)
if err != nil {
runtime.LogError(b.ctx, err.Error())
errors.HandleFatalError(err)
return
}
b.pcapHandle = h
scan.InitializeOuiDb()
runtime.EventsEmit(b.ctx, communication.EmitTypeInitialized)
}
func (b *App) GetDefaultLocalIP() net.IP {
runtime.LogInfo(b.ctx, "Called GetDefaultLocalIP() from frontend")
ip, err := network.GetLocalIP()
if err != nil {
runtime.LogError(b.ctx, err.Error())
}
return ip.To4() // Ensure it's IPv4
}
func (b *App) GetIfaceFromIP(ip net.IP) net.Interface {
runtime.LogInfo(b.ctx, "Called GetIfaceFromIP() from frontend")
iface, err := network.GetLocalIfaceFromIP(ip)
if err != nil {
runtime.LogError(b.ctx, err.Error())
}
return iface
}
func (b *App) GetIPNetFromIP(ip net.IP) net.IPNet {
runtime.LogInfo(b.ctx, "Called GetIPNetFromIP() from frontend")
return net.IPNet{
IP: ip,
Mask: net.CIDRMask(24, 32),
}
}
func (b *App) GetGatewayIP() net.IP {
runtime.LogInfo(b.ctx, "Called GetGatewayIP() from frontend")
gatewayIP, err := gateway.DiscoverGateway()
if err != nil {
runtime.LogError(b.ctx, err.Error())
}
return gatewayIP
}
type MACInfo struct {
Bytes net.HardwareAddr `json:"bytes"`
String string `json:"string"`
}
func (b *App) LookupARPTable(ip net.IP) MACInfo {
runtime.LogInfo(b.ctx, "Called LookupARPTable() from frontend")
mac, err := network.LookupArpTable(ip.String())
if err != nil {
runtime.LogError(b.ctx, err.Error())
}
return MACInfo{
Bytes: mac,
String: mac.String(),
}
}
func (b *App) GetMACFromString(s string) net.HardwareAddr {
m, err := net.ParseMAC(s)
if err != nil {
runtime.LogError(b.ctx, fmt.Sprintf("Could not parse MAC: %s", s))
}
return m
}
// StartListening starts listening to the network to get information about other hosts on the network.
func (b *App) StartListening() {
// TODO: implement
}
// Scan starts a simple network scan on the selected interface, with a duration of scanTimeoutSeconds in seconds.
// It's preferred to implement new functionality that constantly listens to the network, where Scan() only
// sends out ARP request packets.
func (b *App) Scan(localIface net.Interface, localIPNet net.IPNet, scanTimeoutSeconds int) {
runtime.LogInfo(b.ctx, fmt.Sprintf("Called Scan() from frontend with params: %v, %v, %v", localIface, localIPNet, scanTimeoutSeconds))
b.stopScanHandle = make(chan interface{})
results := make(chan scan.ArpScanResult)
scanDone := make(chan interface{})
// Start scanning
timeoutDur := time.Second * time.Duration(scanTimeoutSeconds)
runtime.LogInfo(b.ctx, fmt.Sprintf("Calling ARPScan with params: %v, %v, %v, %v, %v", b.pcapHandle, localIface, localIPNet, timeoutDur, b.JWT))
go scan.ARPScan(b.pcapHandle, localIface, localIPNet, timeoutDur, b.JWT, results, scanDone, b.stopScanHandle)
// Spawn receiving handler
go func(resultsHandle chan scan.ArpScanResult, stopHandle, doneHandle chan interface{}) {
for {
select {
case device := <-resultsHandle: // ARP scan result
// Emit scanned device to frontend
runtime.EventsEmit(b.ctx, communication.EmitTypeScanResult, device)
case <-scanDone: // ARP scan is done
// Emit scan done to frontend
runtime.EventsEmit(b.ctx, communication.EmitTypeScanDone)
return
}
}
}(results, b.stopScanHandle, scanDone)
}
// StopScan stops active scan
func (b *App) StopScan() {
close(b.stopPoisonHandle)
}
type PoisonParams struct {
TargetIP net.IP `json:"targetIp"`
TargetMAC net.HardwareAddr `json:"targetMac"`
LocalIface net.Interface `json:"localIface"`
GatewayMAC net.HardwareAddr `json:"gatewayMac"`
GatewayIP net.IP `json:"gatewayIp"`
BlockSleepSeconds int `json:"blockSleepSeconds"`
}
// Poison poisons the device, and currently just blocks the connection.
func (b *App) Poison(p PoisonParams) {
runtime.LogInfo(b.ctx, fmt.Sprintf("Called Poison() with params: %v", p))
// Make new stop chan
b.stopPoisonHandle = make(chan interface{})
// Call Poison
sleepDur := time.Second * time.Duration(p.BlockSleepSeconds)
go poison.Poison(b.pcapHandle, b.JWT, p.LocalIface, p.TargetIP, p.TargetMAC, p.GatewayMAC, p.GatewayIP, sleepDur, b.stopPoisonHandle)
}
func (b *App) StopPoison(p PoisonParams) {
close(b.stopPoisonHandle)
go poison.ResetPoison(b.pcapHandle, p.TargetIP, p.TargetMAC, p.GatewayMAC, p.GatewayIP, b.JWT)
runtime.EventsEmit(b.ctx, communication.EmitTypeBlockDone)
}