feat: handle retries on switch wifi

This commit is contained in:
Urko 2023-03-07 22:50:07 +01:00
parent c1b6714d57
commit e626e79882
2 changed files with 142 additions and 36 deletions

63
main.go
View File

@ -8,7 +8,6 @@ import (
"time" "time"
"gitea.urkob.com/urko/go-wifi-switcher/cfg" "gitea.urkob.com/urko/go-wifi-switcher/cfg"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher" "github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/utils" "github.com/go-rod/rod/lib/utils"
"github.com/ysmood/leakless" "github.com/ysmood/leakless"
@ -35,12 +34,12 @@ func main() {
} }
var lc *launcher.Launcher var lc *launcher.Launcher
var u string var remoteControlBrowserURL string
if config.Bin != "" { if config.Bin != "" {
lc = launcher.New().Bin(config.Bin) lc = launcher.New().Bin(config.Bin)
u = lc.MustLaunch() remoteControlBrowserURL = lc.MustLaunch()
log.Println("u = lc.MustLaunch()", u) log.Println("u = lc.MustLaunch()", remoteControlBrowserURL)
} else { } else {
lc = launcher.New() lc = launcher.New()
path := launcher.NewBrowser().MustGet() path := launcher.NewBrowser().MustGet()
@ -51,40 +50,32 @@ func main() {
parser := launcher.NewURLParser() parser := launcher.NewURLParser()
cmd.Stderr = parser cmd.Stderr = parser
utils.E(cmd.Start()) utils.E(cmd.Start())
u = launcher.MustResolveURL(<-parser.URL) remoteControlBrowserURL = launcher.MustResolveURL(<-parser.URL)
} }
browser := rod.New(). sw := newSwitcher(remoteControlBrowserURL, config)
ControlURL(u). if err := sw.switchOff(); err != nil {
MustConnect(). err := retry(5, time.Second*5, sw.switchOff)
MustIgnoreCertErrors(true) if err != nil {
defer browser.MustClose() log.Fatalln("couldn't retry:", err)
}
page := browser.MustPage(config.Page) }
page.MustElement(`#username`).MustInput(config.AdminUser)
page.MustElement(`#userpassword`).MustInput(config.Password)
log.Println("INPUT DONE")
page.MustElement(`input[value="Login"]`).MustClick()
log.Println("LOGIN DONE")
page.MustElement(`input[value="Skip"]`).MustClick()
log.Println("SKIP DONE")
page.MustElement("#Net").MustClick()
log.Println("Net DONE")
page.MustElement("#Net-WLAN").MustClick()
log.Println("Net-WLAN DONE")
mainFrame := page.MustElement("#mainFrame").MustFrame()
mainFrame.MustElement("#wlanEnable").MustClick()
log.Println("wlanEnable DONE")
mainFrame.MustElement("#sysSubmit").MustClick()
// TODO: improve this and wait until something should happens (refresh)
time.Sleep(time.Second * 3)
log.Println("task completed") log.Println("task completed")
} }
func retry(attempts int, sleep time.Duration, f func() error) error {
var err error
for i := 0; i < attempts; i++ {
if i > 0 {
log.Println("retrying after error:", err)
time.Sleep(sleep)
sleep *= 2
}
err = f()
if err == nil {
return nil
}
}
return fmt.Errorf("after %d attempts, last error: %s", attempts, err)
}

115
switcher.go Normal file
View File

@ -0,0 +1,115 @@
package main
import (
"fmt"
"log"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/cfg"
"github.com/go-rod/rod"
)
type switcher struct {
remoteControlBrowserUrl string
config *cfg.Config
}
var (
usernameID = "#username"
userpasswordID = "#userpassword"
loginText = "Login"
skipText = "Skip"
netID = "#Net"
netWlanID = "#Net-WLAN"
mainFrameID = "#mainFrame"
wlanEnableID = "#wlanEnable"
)
func newSwitcher(remoteControlBrowserUrl string, config *cfg.Config) *switcher {
return &switcher{
remoteControlBrowserUrl: remoteControlBrowserUrl,
config: config,
}
}
func (s *switcher) switchOff() error {
browser := rod.New().
ControlURL(s.remoteControlBrowserUrl).
MustConnect().
MustIgnoreCertErrors(true)
defer browser.MustClose()
page := browser.MustPage(s.config.Page)
username, err := page.Element(usernameID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", userpasswordID, err)
}
username.MustInput(s.config.AdminUser)
log.Println(usernameID, "DONE")
userpassword, err := page.Element(userpasswordID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", userpasswordID, err)
}
userpassword.MustInput(s.config.Password)
log.Println(userpasswordID, "DONE")
login, err := page.Element(`input[value=` + loginText + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", loginText, err)
}
login.MustClick()
log.Println(loginText, "DONE")
skip, err := page.Element(`input[value="` + skipText + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", skipText, err)
}
skip.MustClick()
log.Println(skipText, "DONE")
net, err := page.Element(netID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", netID, err)
}
net.MustClick()
log.Println(netID, "DONE")
netWlan, err := page.Element(netWlanID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", netWlanID, err)
}
netWlan.MustClick()
log.Println(netWlanID, "DONE")
frameElement, err := page.Element(mainFrameID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", mainFrameID, err)
}
mainFrame, err := frameElement.Frame()
if err != nil {
return fmt.Errorf("frameElement.Frame: %s", err)
}
wlanEnable, err := mainFrame.Element(wlanEnableID)
if err != nil {
return fmt.Errorf("mainFrame.Element %s: %s", wlanEnableID, err)
}
wlanEnable.MustClick()
log.Println(wlanEnableID, "DONE")
sysSubmitID := "#sysSubmit"
sysSubmit, err := mainFrame.Element(sysSubmitID)
if err != nil {
return fmt.Errorf("mainFrame.Element %s: %s", sysSubmitID, err)
}
sysSubmit.MustClick()
log.Println(sysSubmitID, "DONE")
// TODO: improve this and wait until something should happens (refresh)
time.Sleep(time.Second * 3)
return nil
}