Skip to content

Commit 0ffaa5d

Browse files
committed
Refactor auth configuration and add enhanced state saving
Reorganized authentication settings into a nested structure for better clarity and maintainability. Introduced periodic authentication state saving with timestamp checks and directory creation support. Updated logic to include validation for authentication presence via configurable selectors.
1 parent 0defa2d commit 0ffaa5d

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

internal/browser/manager.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ func (m *Manager) LaunchBrowserAndContext() error {
5252
log.Debug("Browser launched successfully.")
5353

5454
contextOptions := playwright.BrowserNewContextOptions{}
55-
if m.Cfg.Instance[m.cfgIndex].AuthFile != "" {
56-
if _, err = os.Stat(m.Cfg.Instance[m.cfgIndex].AuthFile); err == nil {
57-
log.Infof("Loading authentication state from: %s", m.Cfg.Instance[m.cfgIndex].AuthFile)
58-
contextOptions.StorageStatePath = playwright.String(m.Cfg.Instance[m.cfgIndex].AuthFile)
55+
if m.Cfg.Instance[m.cfgIndex].Auth.File != "" {
56+
if _, err = os.Stat(m.Cfg.Instance[m.cfgIndex].Auth.File); err == nil {
57+
log.Infof("Loading authentication state from: %s", m.Cfg.Instance[m.cfgIndex].Auth.File)
58+
contextOptions.StorageStatePath = playwright.String(m.Cfg.Instance[m.cfgIndex].Auth.File)
5959
} else if os.IsNotExist(err) {
60-
log.Infof("Authentication state file not found at %s. Proceeding without loading state. It will be created if you save state.", m.Cfg.Instance[m.cfgIndex].AuthFile)
60+
log.Infof("Authentication state file not found at %s. Proceeding without loading state. It will be created if you save state.", m.Cfg.Instance[m.cfgIndex].Auth.File)
6161
} else {
62-
log.Infof("Error checking auth state file %s: %v. Proceeding without loading state.", m.Cfg.Instance[m.cfgIndex].AuthFile, err)
62+
log.Infof("Error checking auth state file %s: %v. Proceeding without loading state.", m.Cfg.Instance[m.cfgIndex].Auth.File, err)
6363
}
6464
}
6565
contextOptions.Proxy = &playwright.Proxy{

internal/config/config.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ type AppConfigRunner struct {
2020
ContextCanceled string `yaml:"context_canceled"`
2121
}
2222
type AppConfigInstance struct {
23-
Name string `yaml:"name"`
24-
Adapter string `yaml:"adapter"`
25-
ProxyURL string `yaml:"proxy-url"`
26-
URL string `yaml:"url"`
27-
SniffPort string `yaml:"sniff-port"`
28-
SniffDomain string `yaml:"sniff-domain"`
29-
AuthFile string `yaml:"auth-file"`
30-
Runner AppConfigRunner `yaml:"runner"`
23+
Name string `yaml:"name"`
24+
Adapter string `yaml:"adapter"`
25+
ProxyURL string `yaml:"proxy-url"`
26+
URL string `yaml:"url"`
27+
SniffPort string `yaml:"sniff-port"`
28+
SniffDomain string `yaml:"sniff-domain"`
29+
Auth AppConfigInstanceAuth `yaml:"auth"`
30+
Runner AppConfigRunner `yaml:"runner"`
31+
}
32+
33+
type AppConfigInstanceAuth struct {
34+
File string `yaml:"file"`
35+
Check string `yaml:"check"`
3136
}
3237

3338
// LoadConfig loads configuration from environment variables or defaults.

main.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/signal"
1111
"path"
12+
"path/filepath"
1213
"strings"
1314
"syscall"
1415
"time"
@@ -213,28 +214,57 @@ func main() {
213214
os.Exit(0)
214215
case <-time.After(5 * time.Second):
215216
for instanceName, p := range pages {
216-
if p.URL() == mapCfg[instanceName].URL {
217-
if _, err = os.Stat(mapCfg[instanceName].AuthFile); os.IsNotExist(err) {
218-
storageState, errStorageState := p.Context().StorageState()
219-
if errStorageState != nil {
220-
log.Debugf("Error getting storage state: %v", err)
221-
continue
222-
}
223-
jsonData, errMarshalIndent := json.MarshalIndent(storageState, "", " ")
224-
if errMarshalIndent != nil {
225-
log.Debugf("Error marshalling storage state to JSON: %v", err)
226-
continue
227-
}
217+
if mapCfg[instanceName].Auth.Check != "" {
218+
checkLocator := pages[instanceName].Locator(mapCfg[instanceName].Auth.Check)
219+
count, errCount := checkLocator.Count()
220+
if errCount != nil || count == 0 {
221+
continue
222+
}
223+
}
224+
225+
saveState := false
226+
if fileInfo, errStat := os.Stat(mapCfg[instanceName].Auth.File); os.IsNotExist(errStat) {
227+
saveState = true
228+
} else {
229+
lastModified := fileInfo.ModTime()
230+
now := time.Now()
231+
duration := now.Sub(lastModified)
232+
if duration > 5*time.Minute {
233+
saveState = true
234+
}
235+
}
236+
237+
if saveState {
238+
storageState, errStorageState := p.Context().StorageState()
239+
if errStorageState != nil {
240+
log.Debugf("Error getting storage state: %v", err)
241+
continue
242+
}
243+
jsonData, errMarshalIndent := json.MarshalIndent(storageState, "", " ")
244+
if errMarshalIndent != nil {
245+
log.Debugf("Error marshalling storage state to JSON: %v", err)
246+
continue
247+
}
228248

229-
err = os.WriteFile(mapCfg[instanceName].AuthFile, jsonData, 0644)
249+
authAbsPath, errAbs := filepath.Abs(mapCfg[instanceName].Auth.File)
250+
if errAbs != nil {
251+
continue
252+
}
253+
authDirName := filepath.Dir(authAbsPath)
254+
_, errStat := os.Stat(filepath.Dir(authAbsPath))
255+
if os.IsNotExist(errStat) {
256+
err = os.MkdirAll(authDirName, 0755)
230257
if err != nil {
231-
log.Debugf("Error writing storage state to file %s: %v", mapCfg[instanceName].AuthFile, err)
232-
continue
233-
} else {
234-
log.Debugf("Successfully writing storage state to file %s", mapCfg[instanceName].AuthFile)
235258
continue
236259
}
237260
}
261+
262+
err = os.WriteFile(mapCfg[instanceName].Auth.File, jsonData, 0644)
263+
if err != nil {
264+
log.Debugf("Error writing storage state to file %s: %v", mapCfg[instanceName].Auth.File, err)
265+
} else {
266+
log.Debugf("Successfully writing storage state to file %s", mapCfg[instanceName].Auth.File)
267+
}
238268
}
239269
}
240270
}

runner

Submodule runner updated 1 file

0 commit comments

Comments
 (0)