go-wifi-switcher/main.go

68 lines
1.5 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"
2023-06-02 03:24:12 +02:00
"gitea.urkob.com/urko/go-wifi-switcher/internal/providers"
2023-03-07 22:57:30 +01:00
"gitea.urkob.com/urko/go-wifi-switcher/internal/switcher"
2023-03-01 22:23:04 +01:00
"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)
2023-03-02 20:55:10 +01:00
config.LogFile = false
2023-03-01 22:23:04 +01:00
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)
}
2023-03-02 20:55:10 +01:00
var lc *launcher.Launcher
2023-03-07 22:50:07 +01:00
var remoteControlBrowserURL string
2023-03-01 22:23:04 +01:00
2023-06-02 03:24:12 +02:00
log.Println("config", config)
2023-03-02 20:55:10 +01:00
if config.Bin != "" {
lc = launcher.New().Bin(config.Bin)
2023-03-07 22:50:07 +01:00
remoteControlBrowserURL = lc.MustLaunch()
log.Println("u = lc.MustLaunch()", remoteControlBrowserURL)
2023-03-02 20:55:10 +01:00
} else {
lc = launcher.New()
path := launcher.NewBrowser().MustGet()
args := lc.FormatArgs()
cmd := leakless.New().Command(path, args...)
parser := launcher.NewURLParser()
cmd.Stderr = parser
utils.E(cmd.Start())
2023-03-07 22:50:07 +01:00
remoteControlBrowserURL = launcher.MustResolveURL(<-parser.URL)
2023-03-02 20:55:10 +01:00
}
2023-03-01 22:23:04 +01:00
2023-06-02 03:24:12 +02:00
sw := switcher.NewSwitcher(remoteControlBrowserURL, config)
prv := providers.NewNucom(sw.Page)
err := sw.SwitchWIFI(prv)
if err != nil {
panic(err)
2023-03-07 22:50:07 +01:00
}
2023-03-01 22:23:04 +01:00
log.Println("task completed")
}