go-wifi-switcher/main.go

77 lines
1.8 KiB
Go

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() {
config := cfg.NewConfig("./.env")
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()
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")
}