diff --git a/go.mod b/go.mod index 1776a07..cb20615 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/creasty/defaults v1.5.2 github.com/electricbubble/gidevice v0.6.2 github.com/fatih/color v1.9.0 + github.com/ggwhite/go-masker/v2 v2.1.0 github.com/glebarez/sqlite v1.11.0 github.com/go-errors/errors v1.5.1 github.com/go-resty/resty/v2 v2.6.0 diff --git a/go.sum b/go.sum index fc95026..ec9609b 100644 --- a/go.sum +++ b/go.sum @@ -131,6 +131,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/ggwhite/go-masker/v2 v2.1.0 h1:tmpVa2dWjgzdmlLDFBBWlJ/KkzdT3snd2QmFcR9qk+c= +github.com/ggwhite/go-masker/v2 v2.1.0/go.mod h1:Xky8hDSkBwV7pwCJvFsJaiFVwU9GCt0yIAx6yuAc6xY= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= diff --git a/internal/manager/install_manager.go b/internal/manager/install_manager.go index cffd6a4..0374950 100644 --- a/internal/manager/install_manager.go +++ b/internal/manager/install_manager.go @@ -139,8 +139,12 @@ func (t *InstallManager) OutputLog() string { return t.outputStdout.String() } -func (t *InstallManager) WriteLog(id uint) { - data := t.OutputLog() +func (t *InstallManager) WriteLog(msg string) { + t.outputStdout.Write([]byte(msg)) +} + +func (t *InstallManager) SaveLog(id uint) { + data := t.OutputLog() + t.ErrorLog() // Hide log password string // data = strings.Replace(data, v.Password, "******", -1) diff --git a/internal/model/installed_app.go b/internal/model/installed_app.go index 3c62e01..17d2568 100644 --- a/internal/model/installed_app.go +++ b/internal/model/installed_app.go @@ -4,6 +4,7 @@ import ( "encoding/json" "time" + masker "github.com/ggwhite/go-masker/v2" "gorm.io/gorm" ) @@ -37,3 +38,8 @@ func (t InstalledApp) MarshalJSON() ([]byte, error) { Password: "", }) } + +func (t InstalledApp) MaskAccount() string { + m := masker.EmailMasker{} + return m.Marshal("*", t.Account) +} diff --git a/internal/service/websocket.go b/internal/service/websocket.go index 325deef..33ed345 100644 --- a/internal/service/websocket.go +++ b/internal/service/websocket.go @@ -78,7 +78,7 @@ func runInstallMessage(mgr *manager.WebsocketManager, installMgr *manager.Instal mgr.WriteMessage(msg) return } else { - installMgr.WriteLog(app.ID) + installMgr.SaveLog(app.ID) } } diff --git a/internal/task/task.go b/internal/task/task.go index 49b542a..00b6968 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -24,6 +24,7 @@ type Task struct { InstallingApps sync.Map InstallAppQueue chan model.InstalledApp chExitQueue chan bool + InvalidAccounts map[string]bool } func new() *Task { @@ -68,6 +69,7 @@ func (t *Task) Stop() { } func (t *Task) Run() { + t.InvalidAccounts = make(map[string]bool) installedApps, err := service.GetEnableAppList() if err != nil { log.Err(err).Msg("Failed to get the installation list") @@ -156,22 +158,34 @@ func (t *Task) tryInstallApp(v model.InstalledApp) { } func (t *Task) runInternal(v model.InstalledApp) error { + installMgr := manager.NewInstallManager() + defer func() { + installMgr.SaveLog(v.ID) + installMgr.Close() + }() + if v.Account == "" || v.Password == "" || v.UDID == "" { - log.Info("account or password or UDID is empty") + installMgr.WriteLog("account or password or UDID is empty") return fmt.Errorf("%s", "account or password or UDID is empty") } - installMgr := manager.NewInstallManager() - defer installMgr.Close() + if _, ok := t.InvalidAccounts[v.Account]; ok { + installMgr.WriteLog(fmt.Sprintf("The install account (%s) is invalid, skip install.", v.MaskAccount())) + return fmt.Errorf("The install account (%s) is invalid, skip install.", v.MaskAccount()) + } + err := installMgr.TryStart(context.Background(), v.UDID, v.Account, v.Password, v.IpaPath) - installMgr.WriteLog(v.ID) if err != nil { log.Err(err).Msgf("Error executing installation script. %s", installMgr.ErrorLog()) + installMgr.WriteLog(err.Error()) return err } if strings.Contains(installMgr.OutputLog(), "Installation Succeeded") { return nil } else { + if strings.Contains(installMgr.OutputLog(), "ERROR") && strings.Contains(installMgr.OutputLog(), "Can't log-in") { + t.InvalidAccounts[v.Account] = true + } return fmt.Errorf("%s", installMgr.ErrorLog()) } }