|
@@ -1,9 +1,12 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "bufio"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
@@ -24,6 +27,27 @@ func (w *WebServer) Init(tv, host, port string) {
|
|
|
w.Host = host
|
|
|
w.Port = port
|
|
|
w.WakeOnLan = make(map[string]string)
|
|
|
+ if FileExists("WOL.txt") {
|
|
|
+ log.Print("Loading WOLs from WOL.txt")
|
|
|
+ file, err := os.Open("WOL.txt")
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Failed reading WOL.txt got %v", err)
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ var scanner *bufio.Scanner = bufio.NewScanner(file)
|
|
|
+ for scanner.Scan() {
|
|
|
+ var line string = scanner.Text()
|
|
|
+ if len(line) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var parts []string = strings.Split(line, " = ")
|
|
|
+ var ip string = parts[0]
|
|
|
+ var mac string = parts[1]
|
|
|
+ log.Printf("IP='%s' MAC='%s'", ip, mac)
|
|
|
+ w.WakeOnLan[ip] = mac
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func (w *WebServer) homepage(c echo.Context) error {
|
|
@@ -63,8 +87,28 @@ func (w *WebServer) con_refresh(c echo.Context) error {
|
|
|
list, w.WakeOnLan = GetDevices(w.WakeOnLan)
|
|
|
log.Printf("Found %d devices", len(*list))
|
|
|
log.Printf("Obtained %d WOLs", len(w.WakeOnLan))
|
|
|
+ var save_file []byte
|
|
|
for ip, mac := range w.WakeOnLan {
|
|
|
- fmt.Printf("IP='%v' MAC='%v'\n", ip, mac)
|
|
|
+ log.Printf("IP='%v' MAC='%v'\n", ip, mac)
|
|
|
+ if ip != "" && mac != "" {
|
|
|
+ for _, r := range ip {
|
|
|
+ save_file = append(save_file, byte(r))
|
|
|
+ }
|
|
|
+ save_file = append(save_file, ' ')
|
|
|
+ save_file = append(save_file, '=')
|
|
|
+ save_file = append(save_file, ' ')
|
|
|
+ for _, r := range mac {
|
|
|
+ save_file = append(save_file, byte(r))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(save_file) != 0 {
|
|
|
+ err := os.WriteFile("WOL.txt", save_file, 0775)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Saving WOL.txt failed with %v", err)
|
|
|
+ } else {
|
|
|
+ log.Print("Saved WOL to WOL.txt")
|
|
|
+ }
|
|
|
}
|
|
|
for _, ip := range *list {
|
|
|
if ip != w.Tv_ip {
|