go-wifi-switcher/main.go

81 lines
1.9 KiB
Go
Raw Normal View History

2023-03-01 22:23:04 +01:00
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"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/utils"
"github.com/ysmood/leakless"
)
func main() {
envFile := ""
if os.Getenv("ENV") != "prod" {
envFile = "./.env"
}
2023-03-01 22:23:04 +01:00
config := cfg.NewConfig(envFile)
2023-03-01 22:23:04 +01:00
log.SetFlags(log.Lmicroseconds)
if config.LogFile {
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
}
// get the browser executable path
path := launcher.NewBrowser().MustGet()
args := launcher.New().FormatArgs()
cmd := leakless.New().Command(path, args...)
parser := launcher.NewURLParser()
cmd.Stderr = parser
utils.E(cmd.Start())
u := launcher.MustResolveURL(<-parser.URL)
browser := rod.New().
ControlURL(u).
MustConnect().
MustIgnoreCertErrors(true)
defer browser.MustClose()
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()
2023-03-01 22:23:04 +01:00
log.Println("Net DONE")
page.MustElement("#Net-WLAN").MustClick()
2023-03-01 22:23:04 +01:00
log.Println("Net-WLAN DONE")
mainFrame := page.MustElement("#mainFrame").MustFrame()
mainFrame.MustElement("#wlanEnable").MustClick()
log.Println("wlanEnable DONE")
return
2023-03-01 22:23:04 +01:00
mainFrame.MustElement("#sysSubmit").MustClick()
// TODO: improve this and wait until something should happens (refresh)
time.Sleep(time.Second * 3)
2023-03-01 22:23:04 +01:00
log.Println("task completed")
}