-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
412 lines (382 loc) · 10.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
wails "github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/twsnmp/twsnmpfk/backend"
"github.com/twsnmp/twsnmpfk/datastore"
"github.com/twsnmp/twsnmpfk/i18n"
"github.com/twsnmp/twsnmpfk/logger"
"github.com/twsnmp/twsnmpfk/notify"
"github.com/twsnmp/twsnmpfk/ping"
"github.com/twsnmp/twsnmpfk/polling"
)
// App struct
type App struct {
ctx context.Context
cancel context.CancelFunc
wg *sync.WaitGroup
settings Settings
}
type Settings struct {
Kiosk bool `json:"Kiosk"`
Lock string `json:"Lock"`
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
settings: Settings{
Kiosk: kiosk,
Lock: lock,
},
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.wg = &sync.WaitGroup{}
a.ctx, a.cancel = context.WithCancel(ctx)
if dataStorePath != "" {
a.startTWSNMP()
}
a.setMenu()
}
func (a *App) shutdown(ctx context.Context) {
if dataStorePath == "" {
return
}
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "info",
Event: i18n.Trans("Stop TWSNMP"),
})
if a.cancel != nil {
log.Println("shutdown call cancel")
a.cancel()
if a.wg != nil {
log.Println("shutdown wait start")
a.wg.Wait()
log.Println("shutdown wait end")
datastore.CloseDB()
}
}
}
// Menu
func (a *App) setMenu() {
myMenu := menu.NewMenu()
if runtime.GOOS == "darwin" {
myMenu.Append(menu.AppMenu())
myMenu.Append(menu.EditMenu())
if !kiosk {
winMenu := myMenu.AddSubmenu("Window")
winMenu.AddText("Minimize", keys.CmdOrCtrl("m"), func(cd *menu.CallbackData) {
wails.WindowMinimise(a.ctx)
})
winMenu.AddCheckbox("Zoom", wails.WindowIsMaximised(a.ctx), nil, func(cd *menu.CallbackData) {
if wails.WindowIsMaximised(a.ctx) {
wails.WindowUnmaximise(a.ctx)
} else {
wails.WindowMaximise(a.ctx)
}
})
winMenu.AddCheckbox("Full Screen", wails.WindowIsFullscreen(a.ctx), keys.CmdOrCtrl("f"), func(cd *menu.CallbackData) {
if wails.WindowIsFullscreen(a.ctx) {
wails.WindowUnfullscreen(a.ctx)
} else {
wails.WindowFullscreen(a.ctx)
}
})
winMenu.AddSeparator()
winMenu.AddText("Reload", nil, func(cd *menu.CallbackData) {
wails.WindowReload(a.ctx)
})
winMenu.AddSeparator()
winMenu.AddText("TWSNMP FK -"+datastore.MapConf.MapName, nil, func(cd *menu.CallbackData) {
wails.WindowUnminimise(a.ctx)
})
}
wails.MenuSetApplicationMenu(a.ctx, myMenu)
wails.MenuUpdateApplicationMenu(a.ctx)
} else {
fileMenu := myMenu.AddSubmenu("File")
fileMenu.AddText("About", nil, func(cd *menu.CallbackData) {
wails.MessageDialog(a.ctx, wails.MessageDialogOptions{
Type: wails.InfoDialog,
Title: "About TWSNMP FK",
Message: fmt.Sprintf("TWSNMP FK %s(%s)", version, commit),
})
})
fileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(cd *menu.CallbackData) {
wails.Quit(a.ctx)
})
if !kiosk {
winMenu := myMenu.AddSubmenu("Window")
winMenu.AddText("Minimize", keys.CmdOrCtrl("m"), func(cd *menu.CallbackData) {
wails.WindowMinimise(a.ctx)
})
winMenu.AddCheckbox("Zoom", wails.WindowIsMaximised(a.ctx), nil, func(cd *menu.CallbackData) {
if wails.WindowIsMaximised(a.ctx) {
wails.WindowUnmaximise(a.ctx)
} else {
wails.WindowMaximise(a.ctx)
}
})
winMenu.AddCheckbox("Full Screen", wails.WindowIsFullscreen(a.ctx), keys.CmdOrCtrl("f"), func(cd *menu.CallbackData) {
if wails.WindowIsFullscreen(a.ctx) {
wails.WindowUnfullscreen(a.ctx)
} else {
wails.WindowFullscreen(a.ctx)
}
})
winMenu.AddSeparator()
winMenu.AddText("Reload", nil, func(cd *menu.CallbackData) {
wails.WindowReload(a.ctx)
})
}
}
wails.MenuSetApplicationMenu(a.ctx, myMenu)
wails.MenuUpdateApplicationMenu(a.ctx)
if datastore.MapConf.MapName != "" {
wails.WindowSetTitle(a.ctx, "TWSNMP FK - "+datastore.MapConf.MapName)
}
}
// GetVersin returns version
func (a *App) GetVersion() string {
return fmt.Sprintf("%s(%s)", version, commit)
}
// GetSettings returns settings
func (a *App) GetSettings() Settings {
return a.settings
}
// SelectFile returns select local file
func (a *App) SelectFile(title string, image bool) string {
filter := []wails.FileFilter{}
if image {
filter = append(filter, wails.FileFilter{
DisplayName: "Image File(*.png,*.jpg)",
Pattern: "*.png;*.jpg;",
})
}
file, err := wails.OpenFileDialog(a.ctx, wails.OpenDialogOptions{
Title: title,
Filters: filter,
})
if err != nil {
log.Printf("SelectFile err=%v", err)
}
return file
}
// GetImage returns image data
func (a *App) GetImage(path string) string {
b, err := os.ReadFile(path)
if err != nil {
log.Println(err)
return ""
}
t := "png"
if filepath.Ext(path) == "jpg" {
t = "jpeg"
}
return fmt.Sprintf("data:image/%s;base64,%s", t, base64.StdEncoding.EncodeToString(b))
}
// GetImageIconList return IconMap
func (a *App) GetImageIconList() []string {
ret := []string{}
if dataStorePath == "" {
return ret
}
if files, err := filepath.Glob(filepath.Join(dataStorePath, "icons", "*.png")); err == nil {
for _, p := range files {
ret = append(ret, filepath.Base(p))
}
}
if files, err := filepath.Glob(filepath.Join(dataStorePath, "icons", "*.jpg")); err == nil {
for _, p := range files {
ret = append(ret, filepath.Base(p))
}
}
return ret
}
// GetImageIcon returns icon image data
func (a *App) GetImageIcon(icon string) string {
return a.GetImage(filepath.Join(dataStorePath, "icons", icon))
}
// GetLangは言語を返します。
func (a *App) GetLang() string {
return i18n.GetLang()
}
// IsDarkは,Darkモードの状態を返します。
func (a *App) IsDark() bool {
return datastore.Dark
}
// IsLatestは,TWSNMPが最新版であることを返します。
func (a *App) IsLatest() bool {
return backend.IsLatest()
}
// SetDarkは,Darkモードの状態を設定します。
func (a *App) SetDark(d bool) {
if err := datastore.SetDark(d); err != nil {
log.Println(err)
}
}
// HasDatastoreは,データストアの選択状態を返します。
func (a *App) HasDatastore() bool {
return dataStorePath != ""
}
// GetMonitorDatasは、システムリソースのモニター情報を返します。
func (a *App) GetMonitorDatas() []*backend.MonitorDataEnt {
return backend.MonitorDataes
}
// SelectDatastore は、データストアのディレクトリを選択してサービスを起動します。
func (a *App) SelectDatastore() bool {
if dataStorePath != "" {
return true
}
p, _ := os.UserHomeDir()
var err error
dataStorePath, err = wails.OpenDirectoryDialog(a.ctx,
wails.OpenDialogOptions{
Title: i18n.Trans("Select data store path"),
DefaultDirectory: p,
CanCreateDirectories: true,
})
if err != nil {
log.Println(err)
return false
}
if dataStorePath != "" {
a.startTWSNMP()
a.setMenu()
return true
}
return false
}
// startTWSNMP は、データストアのディレクトリを設定します。
func (a *App) startTWSNMP() {
log.Println("call datastore.Init")
if err := datastore.Init(a.ctx, dataStorePath, a.wg); err != nil {
log.Fatalf("init db err=%v", err)
}
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "info",
Event: i18n.Trans("Start TWSNMP"),
})
log.Println("call ping.Start")
if err := ping.Start(a.ctx, a.wg, pingMode); err != nil {
log.Fatalf("start ping err=%v", err)
}
log.Println("call logger.Start")
if err := logger.Start(a.ctx, a.wg, syslogPort, trapPort, sshdPort, netflowPort, sFlowPort); err != nil {
log.Fatalf("start logger err=%v", err)
}
log.Println("call polling.Start")
if err := polling.Start(a.ctx, a.wg); err != nil {
log.Fatalf("start polling err=%v", err)
}
log.Println("call backend.Start")
if err := backend.Start(a.ctx, dataStorePath, version, a.wg); err != nil {
log.Fatalf("start backend err=%v", err)
}
log.Println("call notify.Start")
if err := notify.Start(a.ctx, a.wg); err != nil {
log.Fatalf("start notify err=%v", err)
}
}
// GetAudio returns image data
func (a *App) GetAudio(path string) string {
if s, err := os.Stat(path); err != nil || s.Size() > 1024*1024 || s.Size() < 100 {
return ""
}
b, err := os.ReadFile(path)
if err != nil {
log.Println(err)
return ""
}
t := "mp3"
if filepath.Ext(path) == "wav" {
t = "wav"
}
return fmt.Sprintf("data:audio/%s;base64,%s", t, base64.StdEncoding.EncodeToString(b))
}
// SelectAudioFile returns select local file
func (a *App) SelectAudioFile(title string) string {
filter := []wails.FileFilter{
{DisplayName: "Audio File(*.mp3,*.wav)", Pattern: "*.mp3;*.wav"},
}
file, err := wails.OpenFileDialog(a.ctx, wails.OpenDialogOptions{
Title: title,
Filters: filter,
})
if err != nil {
log.Printf("SelectAudioFile err=%v", err)
}
return file
}
// SendFeedback send feedback to twsnmp
func (a *App) SendFeedback(message string) bool {
msg := message
msg += fmt.Sprintf("\n-----\nTWSNMP FK\nGOOS=%s,GOARCH=%s\n", runtime.GOOS, runtime.GOARCH)
if len(backend.MonitorDataes) > 0 {
i := len(backend.MonitorDataes) - 1
msg += fmt.Sprintf("CPU=%f,Mem=%f,Load=%f,Disk=%f\n",
backend.MonitorDataes[i].CPU,
backend.MonitorDataes[i].Mem,
backend.MonitorDataes[i].Load,
backend.MonitorDataes[i].Disk,
)
}
values := url.Values{}
values.Set("msg", msg)
values.Add("hash", calcHash(msg))
req, err := http.NewRequest(
"POST",
"https://lhx98.linkclub.jp/twise.co.jp/cgi-bin/twsnmpfb.cgi",
strings.NewReader(values.Encode()),
)
if err != nil {
log.Println(err)
return false
}
// Content-Type 設定
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return false
}
defer resp.Body.Close()
r, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return false
}
if string(r) != "OK" {
log.Println(r)
return false
}
return true
}
func calcHash(msg string) string {
h := sha256.New()
if _, err := h.Write([]byte(msg + time.Now().Format("2006/01/02T15"))); err != nil {
log.Printf("calc hash err=%v", err)
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}