Compare commits

...

5 Commits

Author SHA1 Message Date
Urko c1b6714d57 feat: add README 2023-03-04 22:00:30 +01:00
Urko fb48c6a9cc feat: add missing Bin on config 2023-03-04 21:56:47 +01:00
Urko d69bfe4590 feat: browser binary path as parameter 2023-03-02 20:55:10 +01:00
Urko abf360a126 fix: load .env file when ENV is not prod 2023-03-02 18:39:53 +01:00
Urko cc3b454262 feat: add build for raspi and freebsd 2023-03-02 18:39:30 +01:00
5 changed files with 59 additions and 12 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ coverage/*
test_monitor.txt
bin
.vscode
.notes
bin

View File

@ -17,3 +17,9 @@ 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
build_freebsd:
rm -rf ${BINARY_DIR}
mkdir ${BINARY_DIR}
env GOOS=freebsd CGO_ENABLED=0 GOARCH=arm64 ENV=prod go build -o ./${BINARY_DIR}/${BINARY_NAME} main.go
list_build_types:
go tool dist list | grep arm

24
README.md Normal file
View File

@ -0,0 +1,24 @@
# go-wifi-switcher
## Huawei B2368-66
This software will turn wifi on/off based on current state.
Example: If wifi is **on** then it will turn **off**.
## How does it works
It uses web-scrapping thanks to amazing **[go-rod web scrapper](github.com/go-rod/rod)**
## How to use
You must build it using the `go build` command that better fits with the OS where this software will be consumed.
I'm using it on debian OS so I've created some build commands inside **[Makefile](https://gitea.urkob.com/urko/go-wifi-switcher/src/branch/main/Makefile)**
You can build and copy into your OS in `/usr/local/bin` for example, then create a cron task to automatically run this binary.
## Result
You can configure to switch **off** during **night** and **on** at **morning**. So you can get free of radiation while you sleep and don't have to manually switch on :)
## How to install and how to use
TODO
## Tests
TODO

View File

@ -12,6 +12,7 @@ type Config struct {
AdminUser string `required:"true" split_words:"true"`
Password string `required:"true" split_words:"true"`
LogFile bool `required:"true" split_words:"true"`
Bin string `required:"true" split_words:"true"`
}
func NewConfig(envFilePath string) *Config {

34
main.go
View File

@ -15,9 +15,14 @@ import (
)
func main() {
config := cfg.NewConfig("./.env")
envFile := ""
if os.Getenv("ENV") != "prod" {
envFile = "./.env"
}
config := cfg.NewConfig(envFile)
log.SetFlags(log.Lmicroseconds)
config.LogFile = false
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)
@ -29,21 +34,30 @@ func main() {
log.SetOutput(f)
}
// get the browser executable path
path := launcher.NewBrowser().MustGet()
args := launcher.New().FormatArgs()
var lc *launcher.Launcher
var u string
cmd := leakless.New().Command(path, args...)
if config.Bin != "" {
lc = launcher.New().Bin(config.Bin)
u = lc.MustLaunch()
log.Println("u = lc.MustLaunch()", u)
} 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())
u = launcher.MustResolveURL(<-parser.URL)
}
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)