-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappservice.go
More file actions
651 lines (604 loc) · 15.9 KB
/
Copy pathappservice.go
File metadata and controls
651 lines (604 loc) · 15.9 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"nfa-tool/internal/gdrive"
"nfa-tool/internal/steam"
"nfa-tool/internal/storage"
"nfa-tool/internal/token"
"nfa-tool/internal/update"
"github.com/wailsapp/wails/v3/pkg/application"
)
// AppName / AppVersion shown in the UI title bar.
const AppName = "NFA Tool Recode v2"
const AppVersion = "2.1.2"
// GetAppName returns the product display name.
func (s *AppService) GetAppName() string {
return AppName
}
// AppService is the Wails v3 backend service.
type AppService struct {
store *storage.Store
gdrive *gdrive.Client
}
// AccountDTO is exposed to the frontend.
type AccountDTO struct {
Name string `json:"name"`
ExpiresIn string `json:"expiresIn"`
Valid bool `json:"valid"`
}
// Result is a generic UI response.
type Result struct {
OK bool `json:"ok"`
Message string `json:"message"`
}
func NewAppService() *AppService {
base, err := os.Executable()
if err != nil {
base, _ = os.Getwd()
} else {
base = filepath.Dir(base)
}
if _, err := os.Stat(filepath.Join(base, "go.mod")); err != nil {
if wd, err := os.Getwd(); err == nil {
if _, err := os.Stat(filepath.Join(wd, "go.mod")); err == nil {
base = wd
}
}
}
store, err := storage.New(base)
if err != nil {
// Fall back to cwd so the app still starts; operations will fail with clear errors if store is nil-checked.
// storage.New rarely fails; panic is worse for a GUI app.
store, _ = storage.New(".")
base = "."
}
return &AppService{
store: store,
gdrive: gdrive.New(base),
}
}
func (s *AppService) GetVersion() string {
return AppVersion
}
// CheckForUpdates queries GitHub Releases for a newer version.
func (s *AppService) CheckForUpdates() update.Info {
return update.CheckLatest(AppVersion)
}
// InstallUpdate downloads the release exe and restarts into it.
func (s *AppService) InstallUpdate(downloadURL string) Result {
if err := update.ApplyDownload(downloadURL); err != nil {
return s.fail(err.Error())
}
// quit so the bat can replace the file
go func() {
time.Sleep(600 * time.Millisecond)
if app := application.Get(); app != nil {
app.Quit()
} else {
os.Exit(0)
}
}()
return s.ok("Update downloaded. Restarting…")
}
// OpenURL opens a link in the default browser.
func (s *AppService) OpenURL(url string) {
_ = openBrowser(url)
}
func (s *AppService) ListAccounts() ([]AccountDTO, error) {
// Do NOT harvest Steam ConnectCache here — that re-imported deleted accounts
// after every refresh. Harvest only once when the local DB is empty.
if m0, err := s.store.Load(); err == nil && len(m0) == 0 {
if harvested, err := steam.HarvestConnectCache(); err == nil && len(harvested) > 0 {
_ = s.store.Merge(harvested)
}
}
m, err := s.store.Load()
if err != nil {
return nil, err
}
out := make([]AccountDTO, 0, len(m))
for name, tok := range m {
dto := AccountDTO{Name: name, Valid: false, ExpiresIn: "unknown"}
if info, err := token.ParseAndValidate(tok); err == nil {
dto.Valid = true
dto.ExpiresIn = formatExpiryUntil(info.ExpiresAt)
} else {
dto.ExpiresIn = "expired/invalid"
}
out = append(out, dto)
}
return out, nil
}
func (s *AppService) LoginFromKey(accountKey string, keepExisting bool) Result {
account, rawTok, err := token.ParseAccountKey(accountKey)
if err != nil {
return s.fail(err.Error())
}
info, err := token.ParseAndValidate(rawTok)
if err != nil {
return s.fail(err.Error())
}
if account == "" {
return s.fail("account name required (use login----token)")
}
err = steam.Login(steam.LoginOptions{
AccountName: account,
Token: info.Raw,
SteamID: info.SteamID,
KeepExisting: keepExisting,
})
if err != nil {
return s.fail(err.Error())
}
_ = s.store.Put(account, info.Raw)
msg := fmt.Sprintf("Logged in as %s · token valid until %s", account, formatExpiryUntil(info.ExpiresAt))
if !steam.IsSteamRunning() {
msg += " · warning: steam.exe not detected after launch"
}
return s.ok(msg)
}
func (s *AppService) LoginSaved(account string, keepExisting bool) Result {
tok, ok, err := s.store.Get(account)
if err != nil {
return s.fail(err.Error())
}
if !ok {
return s.fail("account not found")
}
info, err := token.ParseAndValidate(tok)
if err != nil {
return s.fail(err.Error())
}
err = steam.Login(steam.LoginOptions{
AccountName: account,
Token: info.Raw,
SteamID: info.SteamID,
KeepExisting: keepExisting,
})
if err != nil {
return s.fail(err.Error())
}
_ = s.store.Put(account, info.Raw)
msg := fmt.Sprintf("Logged in as %s · token valid until %s", account, formatExpiryUntil(info.ExpiresAt))
if !steam.IsSteamRunning() {
msg += " · warning: steam.exe not detected after launch"
}
return s.ok(msg)
}
// Notify shows a native Windows message box. Frontend should pass already-translated title/message.
func (s *AppService) Notify(success bool, title, message string) {
app := application.Get()
if app == nil {
return
}
if title == "" {
if success {
title = "Success"
} else {
title = "Error"
}
}
var dlg *application.MessageDialog
if success {
dlg = app.Dialog.Info().SetTitle(title).SetMessage(message)
} else {
dlg = app.Dialog.Error().SetTitle(title).SetMessage(message)
}
dlg.AddButton("OK")
dlg.Show()
}
func (s *AppService) ok(msg string) Result {
return Result{OK: true, Message: msg}
}
func (s *AppService) fail(msg string) Result {
return Result{OK: false, Message: msg}
}
func (s *AppService) DeleteAccount(account string) Result {
account = strings.TrimSpace(account)
if account == "" {
return s.fail("account name required")
}
if err := s.store.Delete(account); err != nil {
return s.fail(err.Error())
}
return s.ok("Account deleted")
}
// DeleteAccounts removes multiple saved accounts from the local DB.
func (s *AppService) DeleteAccounts(names []string) Result {
okN := 0
var lastErr string
seen := map[string]bool{}
for _, n := range names {
n = strings.TrimSpace(n)
if n == "" {
continue
}
key := strings.ToLower(n)
if seen[key] {
continue
}
seen[key] = true
if err := s.store.Delete(n); err != nil {
lastErr = err.Error()
continue
}
okN++
}
if okN == 0 {
if lastErr != "" {
return s.fail(lastErr)
}
return s.fail("no accounts to delete")
}
msg := fmt.Sprintf("Deleted %d account(s)", okN)
if lastErr != "" {
msg += " · some failed"
}
return s.ok(msg)
}
// ImportTokens imports multi-line login----token text into the local store (no Steam login).
func (s *AppService) ImportTokens(text string) Result {
keys, errs := token.ParseBulkKeys(text)
if len(keys) == 0 {
if len(errs) > 0 {
return s.fail(fmt.Sprintf("import failed: %s", errs[0]))
}
return s.fail("no accounts to import")
}
okN := 0
for _, k := range keys {
if err := s.store.Put(k.Account, k.Token); err != nil {
errs = append(errs, fmt.Sprintf("%s: %v", k.Account, err))
continue
}
okN++
}
if okN == 0 {
msg := "import failed"
if len(errs) > 0 {
msg = errs[0]
}
return s.fail(msg)
}
msg := fmt.Sprintf("Imported %d account(s)", okN)
if len(errs) > 0 {
msg += fmt.Sprintf(" · %d skipped", len(errs))
}
return s.ok(msg)
}
// ImportTokensFromFile opens a .txt file and imports login----token lines.
func (s *AppService) ImportTokensFromFile() Result {
app := application.Get()
if app == nil {
return s.fail("dialog unavailable")
}
dlg := app.Dialog.OpenFile()
if dlg == nil {
return s.fail("dialog unavailable")
}
dlg.SetTitle("Import tokens")
dlg.AddFilter("Text files", "*.txt")
dlg.AddFilter("All files", "*.*")
path, err := dlg.PromptForSingleSelection()
if err != nil {
return s.fail(err.Error())
}
path = strings.TrimSpace(path)
if path == "" {
return s.fail("Cancelled")
}
raw, err := os.ReadFile(path)
if err != nil {
return s.fail(err.Error())
}
return s.ImportTokens(string(raw))
}
// ExportTokens returns login----token lines for the given names (empty = all).
func (s *AppService) ExportTokens(names []string) (string, error) {
text, n, err := s.buildExport(names)
if err != nil {
return "", err
}
if n == 0 {
return "", fmt.Errorf("no accounts to export")
}
return text, nil
}
// GoogleDriveStatus returns OAuth setup / connection state.
func (s *AppService) GoogleDriveStatus() gdrive.Status {
if s.gdrive == nil {
return gdrive.Status{}
}
return s.gdrive.GetStatus()
}
// SaveGoogleCredentials stores Desktop OAuth client id + secret.
func (s *AppService) SaveGoogleCredentials(clientID, clientSecret string) Result {
if s.gdrive == nil {
return s.fail("google drive unavailable")
}
if err := s.gdrive.SaveCredentials(clientID, clientSecret); err != nil {
return s.fail(err.Error())
}
return s.ok("Google OAuth saved")
}
// ImportGoogleCredentials opens a file picker for google-oauth.json from Cloud Console.
func (s *AppService) ImportGoogleCredentials() Result {
if s.gdrive == nil {
return s.fail("google drive unavailable")
}
app := application.Get()
if app == nil {
return s.fail("dialog unavailable")
}
dlg := app.Dialog.OpenFile()
if dlg == nil {
return s.fail("dialog unavailable")
}
dlg.SetTitle("Select google-oauth.json")
dlg.AddFilter("JSON", "*.json")
path, err := dlg.PromptForSingleSelection()
if err != nil {
return s.fail(err.Error())
}
path = strings.TrimSpace(path)
if path == "" {
return s.fail("Cancelled")
}
if err := s.gdrive.ImportCredentialsFile(path); err != nil {
return s.fail(err.Error())
}
return s.ok("Google OAuth imported")
}
// ConnectGoogleDrive runs browser OAuth (drive.file) and stores the refresh token.
func (s *AppService) ConnectGoogleDrive() Result {
if s.gdrive == nil {
return s.fail("google drive unavailable")
}
if err := s.gdrive.Connect(openBrowser); err != nil {
return s.fail(err.Error())
}
return s.ok("Google Drive connected")
}
// CancelGoogleAuth aborts waiting for Google browser login (unblocks the UI).
func (s *AppService) CancelGoogleAuth() Result {
if s.gdrive != nil {
s.gdrive.CancelAuth()
}
return s.ok("Cancelled")
}
// GoogleAuthBusy is true while Connect waits on the browser.
func (s *AppService) GoogleAuthBusy() bool {
if s.gdrive == nil {
return false
}
return s.gdrive.AuthInProgress()
}
// DisconnectGoogleDrive removes the stored Google session.
func (s *AppService) DisconnectGoogleDrive() Result {
if s.gdrive == nil {
return s.fail("google drive unavailable")
}
s.gdrive.CancelAuth()
_ = s.gdrive.Disconnect()
return s.ok("Google Drive disconnected")
}
// ExportTokensToGoogleDrive uploads login----token text for selected (or all) accounts.
func (s *AppService) ExportTokensToGoogleDrive(names []string) Result {
if s.gdrive == nil {
return s.fail("google drive unavailable")
}
text, n, err := s.buildExport(names)
if err != nil {
return s.fail(err.Error())
}
if n == 0 {
return s.fail("no accounts to export")
}
st := s.gdrive.GetStatus()
if !st.HasCredentials {
return s.fail("Google OAuth not configured")
}
if !st.Connected {
if err := s.gdrive.Connect(openBrowser); err != nil {
return s.fail(err.Error())
}
}
name := fmt.Sprintf("nfa-tokens-%s.txt", time.Now().Format("2006-01-02_150405"))
link, err := s.gdrive.UploadText(name, text)
if err != nil {
// one reconnect attempt
if strings.Contains(err.Error(), "expired") || strings.Contains(err.Error(), "invalid_grant") {
if err2 := s.gdrive.Connect(openBrowser); err2 == nil {
link, err = s.gdrive.UploadText(name, text)
}
}
if err != nil {
return s.fail(err.Error())
}
}
msg := fmt.Sprintf("Uploaded %d account(s) to Google Drive", n)
if link != "" {
msg += ": " + link
_ = openBrowser(link)
}
return s.ok(msg)
}
// ExportTokensToFile writes selected (or all) tokens to a user-chosen .txt file.
func (s *AppService) ExportTokensToFile(names []string) Result {
text, n, err := s.buildExport(names)
if err != nil {
return s.fail(err.Error())
}
if n == 0 {
return s.fail("no accounts to export")
}
app := application.Get()
path := ""
dialogShown := false
if app != nil {
if dlg := app.Dialog.SaveFile(); dlg != nil {
dialogShown = true
dlg.SetFilename("nfa-tokens.txt")
dlg.AddFilter("Text files", "*.txt")
p, err := dlg.PromptForSingleSelection()
if err != nil {
return s.fail(err.Error())
}
path = strings.TrimSpace(p)
if path == "" {
return s.fail("Cancelled")
}
}
}
if path == "" && !dialogShown {
base := filepath.Dir(s.store.Path())
path = filepath.Join(base, fmt.Sprintf("nfa-tokens-%d.txt", time.Now().Unix()))
}
if path == "" {
return s.fail("Cancelled")
}
if !strings.HasSuffix(strings.ToLower(path), ".txt") {
path += ".txt"
}
if err := os.WriteFile(path, []byte(text), 0o600); err != nil {
return s.fail(err.Error())
}
return s.ok(fmt.Sprintf("Exported %d account(s) to %s", n, path))
}
func (s *AppService) buildExport(names []string) (string, int, error) {
all, err := s.store.Load()
if err != nil {
return "", 0, err
}
want := map[string]bool{}
if len(names) == 0 {
for k := range all {
want[strings.ToLower(k)] = true
}
} else {
for _, n := range names {
n = strings.ToLower(strings.TrimSpace(n))
if n != "" {
want[n] = true
}
}
}
var lines []string
keys := make([]string, 0, len(all))
for k := range all {
keys = append(keys, k)
}
// case-insensitive stable-ish order
for i := 0; i < len(keys); i++ {
for j := i + 1; j < len(keys); j++ {
if strings.ToLower(keys[j]) < strings.ToLower(keys[i]) {
keys[i], keys[j] = keys[j], keys[i]
}
}
}
for _, name := range keys {
if !want[strings.ToLower(name)] {
continue
}
tok := strings.TrimSpace(all[name])
if tok == "" {
continue
}
lines = append(lines, name+"----"+tok)
}
return strings.Join(lines, "\n") + "\n", len(lines), nil
}
func (s *AppService) ResetSteam() Result {
app := application.Get()
if app == nil {
return Result{OK: false, Message: "app not ready"}
}
confirmed := make(chan bool, 1)
var once sync.Once
done := func(v bool) {
once.Do(func() { confirmed <- v })
}
dlg := app.Dialog.Question().
SetTitle("Reset Steam").
SetMessage("This will delete Steam config and userdata, then relaunch Steam. Continue?")
yes := dlg.AddButton("Yes").OnClick(func() { done(true) })
no := dlg.AddButton("No").OnClick(func() { done(false) })
dlg.SetDefaultButton(no)
dlg.SetCancelButton(no)
_ = yes
dlg.Show()
if !<-confirmed {
return Result{OK: false, Message: "Cancelled"}
}
if err := steam.ResetSteam(); err != nil {
return s.fail(err.Error())
}
return s.ok("Steam has been reset")
}
func (s *AppService) WindowMinimise() {
if app := application.Get(); app != nil {
if w := app.Window.Current(); w != nil {
w.Minimise()
}
}
}
func (s *AppService) WindowClose() {
if s.gdrive != nil {
s.gdrive.CancelAuth()
}
if app := application.Get(); app != nil {
// close guide window first if open
if w, ok := app.Window.GetByName("drive-guide"); ok && w != nil {
w.Close()
}
app.Quit()
}
}
// OpenDriveGuide opens (or focuses) the Google Drive setup guide window.
func (s *AppService) OpenDriveGuide() {
app := application.Get()
if app == nil {
return
}
if w, ok := app.Window.GetByName("drive-guide"); ok && w != nil {
w.Show()
w.Focus()
return
}
app.Window.NewWithOptions(application.WebviewWindowOptions{
Name: "drive-guide",
Title: "Google Drive — setup",
Width: 720,
Height: 640,
MinWidth: 720,
MinHeight: 640,
MaxWidth: 720,
MaxHeight: 640,
Frameless: true,
BackgroundColour: application.NewRGB(15, 15, 26),
URL: "/?page=drive-guide",
DisableResize: true,
})
}
// CloseDriveGuide closes the guide window only (main app stays open).
func (s *AppService) CloseDriveGuide() {
app := application.Get()
if app == nil {
return
}
if w, ok := app.Window.GetByName("drive-guide"); ok && w != nil {
w.Close()
}
}
// formatExpiryUntil returns a stable UTC timestamp for UI/i18n, e.g. 2026-09-15 14:30 UTC
func formatExpiryUntil(t time.Time) string {
if t.IsZero() {
return "unknown"
}
return t.UTC().Format("2006-01-02 15:04 UTC")
}