2023-03-07 22:57:30 +01:00
|
|
|
package switcher
|
2023-03-07 22:50:07 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2023-03-07 23:07:20 +01:00
|
|
|
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) switcher {
|
|
|
|
return switcher{
|
2023-03-07 22:50:07 +01:00
|
|
|
remoteControlBrowserUrl: remoteControlBrowserUrl,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:07:20 +01:00
|
|
|
func (s switcher) SwitchWIFI() error {
|
2023-03-07 22:50:07 +01:00
|
|
|
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")
|
|
|
|
|
2023-03-07 23:07:20 +01:00
|
|
|
login, err := page.Element(`input[value="` + loginText + `"]`)
|
2023-03-07 22:50:07 +01:00
|
|
|
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
|
|
|
|
}
|