commit 8cb3e3b9f6f978a238e7e1b04b5ec84b85cb37ad Author: Urko Date: Wed Mar 1 22:23:04 2023 +0100 feat: init repo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bc433a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +coverage/* +test_monitor.txt +bin +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7bd101f --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +BINARY_DIR=bin +BINARY_NAME=wifi-switcher +COVERAGE_DIR=coverage + +lint: + golangci-lint run ./... +goreportcard: + goreportcard-cli -v +test: + go test ./... +test-coverage: + rm -rf ${COVERAGE_DIR} + mkdir ${COVERAGE_DIR} + go test -v -coverprofile ${COVERAGE_DIR}/cover.out ./... + go tool cover -html ${COVERAGE_DIR}/cover.out -o ${COVERAGE_DIR}/cover.html +build: + rm -rf ${BINARY_DIR} + mkdir ${BINARY_DIR} + env GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o ./${BINARY_DIR}/${BINARY_NAME} main.go \ No newline at end of file diff --git a/cfg/cfg.go b/cfg/cfg.go new file mode 100644 index 0000000..6e8f538 --- /dev/null +++ b/cfg/cfg.go @@ -0,0 +1,32 @@ +package cfg + +import ( + "log" + + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" +) + +type Config struct { + Page string `required:"true" split_words:"true"` + AdminUser string `required:"true" split_words:"true"` + Password string `required:"true" split_words:"true"` + LogFile bool `required:"true" split_words:"true"` +} + +func NewConfig(envFilePath string) *Config { + if envFilePath != "" { + err := godotenv.Load(envFilePath) + if err != nil { + log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file\n") + } + } + + cfg := &Config{} + err := envconfig.Process("", cfg) + if err != nil { + log.Fatalf("envconfig.Process: %s\n", err) + } + + return cfg +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..99b80ba --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module gitea.urkob.com/urko/go-wifi-switcher + +go 1.19 + +require ( + github.com/go-rod/rod v0.112.6 + github.com/joho/godotenv v1.5.1 + github.com/kelseyhightower/envconfig v1.4.0 + github.com/ysmood/leakless v0.8.0 +) + +require ( + github.com/ysmood/goob v0.4.0 // indirect + github.com/ysmood/gson v0.7.3 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..39dbac8 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/go-rod/rod v0.112.6 h1:zMirUmhsBeshMWyf285BD0UGtGq54HfThLDGSjcP3lU= +github.com/go-rod/rod v0.112.6/go.mod h1:ElViL9ABbcshNQw93+11FrYRH92RRhMKleuILo6+5V0= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ= +github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18= +github.com/ysmood/got v0.32.0 h1:aAHdQgfgMb/lo4v+OekM+SSqEJYFI035h5YYvLXsVyU= +github.com/ysmood/got v0.32.0/go.mod h1:pE1l4LOwOBhQg6A/8IAatkGp7uZjnalzrZolnlhhMgY= +github.com/ysmood/gotrace v0.6.0 h1:SyI1d4jclswLhg7SWTL6os3L1WOKeNn/ZtzVQF8QmdY= +github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM= +github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE= +github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg= +github.com/ysmood/leakless v0.8.0 h1:BzLrVoiwxikpgEQR0Lk8NyBN5Cit2b1z+u0mgL4ZJak= +github.com/ysmood/leakless v0.8.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ad1e914 --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +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.Println("config", config) + + 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") + + time.Sleep(time.Second * 3) + + printContent(page) + + net := page.MustElement("#Net") + log.Println("Net", net) + net.MustClick() + log.Println("Net DONE") + + wlan := page.MustElement("#Net-WLAN") + log.Println("wlan", wlan) + wlan.MustClick() + log.Println("Net-WLAN DONE") + + mainFrame := page.MustElement("#mainFrame").MustFrame() + mainFrame.MustElement("#wlanEnable").MustClick() + log.Println("wlanEnable DONE") + + printContent(mainFrame) + mainFrame.MustElement("#sysSubmit").MustClick() + + log.Println("task completed") +} + +func printContent(page *rod.Page) { + content, err := page.HTML() + if err != nil { + log.Fatalf("page.HTML: %s\n", err) + } + + log.Println("content", content) +}