Compare commits

..

No commits in common. "master" and "v0.0.1" have entirely different histories.

17 changed files with 111 additions and 367 deletions

1
.gitignore vendored
View File

@ -26,4 +26,3 @@ config.toml
miniwol
!example/**
__debug_bin

15
.vscode/launch.json vendored
View File

@ -1,15 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}"
}
]
}

17
LICENSE
View File

@ -1,14 +1,9 @@
BSD Zero Clause License
MIT License
Copyright (c) 2024 adroslice
Copyright (c) <year> <copyright holders>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,4 +1,5 @@
# miniwol
Small web server to send Wake-on-LAN requests to its local network
## Features
@ -6,16 +7,19 @@ Small web server to send Wake-on-LAN requests to its local network
- Binary includes everything except config
## Installation
Run `go install git.ulra.eu/adro/miniwol@latest`, resulting in a binary `~/go/bin/miniwol`.
Move this binary to e.G. `/usr/bin/miniwol` to run it like any command.
Option 1: Run `go build` in this folder after cloning, resulting in a binary `miniwol` in the current directory.
Examples include a simple systemd service to have it start automatically.
Option 2: Run `go get git.ulra.eu/adro/miniwol`, resulting in a binary in TODO.
Put the resulting binary in a place like `/usr/bin/miniwol` if you wish to run it like any command,
## Usage
Add an empty config. The program uses, in order of priority, `./config.toml`, `./config/config.toml` and `/etc/miniwol/config.toml`. Make sure miniwol can write to it as all persistant data is saved here.
Add an empty config, the program checks, in order of priority, for `./config.toml`, `./config/config.toml` and `/etc/miniwol/config.toml`. Make sure miniwol can write to it.
Set a password using `miniwol setpass <password>`. This will also add any missing defaults to the config.
Set a password and write its hash to the config using `miniwol setpass <password>`. This will also add the other default fields to the config.
Run the webserver using `miniwol` or `miniwol web` .
Configure the devices you want to be able to wake up as per the [example](./example/config.toml).
Sessions are in-memory. Restarting the webserver clears all sessions.
Now you can run the webserver using `miniwol` or `miniwol web` .
Sessions are simply stored in memory. In case you need to, you can clear all sessions by just restarting the webserver.

View File

@ -8,17 +8,16 @@ import (
)
type Device struct {
Alias string `form:"Alias"`
MAC string `form:"MAC"`
IP string `form:"IP"`
Alias string
MAC string
IP string
}
type config struct {
Server string
PassHash string
SessionTTL float64
StrictCookies bool
Devices []Device
Server string
PassHash string
SessionTTL float64
Device []Device
}
var Config config
@ -27,9 +26,8 @@ var configPath string
func init() {
Config = config{
Server: ":8080",
SessionTTL: 10,
StrictCookies: true,
Server: ":8080",
SessionTTL: 1440,
}
// Locations to look for a config file for

View File

@ -1,14 +1,13 @@
Server = ":8080" # The address the webserver should bind to
PassHash = "$2a$10$I.26oCzkjZ8qwfhbmeYM3.kppBjxtPsxkeE1Y.ULjVvA1IBPcQP42" # "password"
SessionTTL = 10 # How many minutes sessions last for
StrictCookies = true # Whether to use the strict cookie policy (HTTPS Only)
SessionTTL = 60 # How many minutes sessions last for
[[Devices]]
[[Device]]
Alias = "SomeDevice"
MAC = "DE-AD-BE-EF-F0-05" # Delimiter dashes/colons, upper/lowercase
IP = "192.168.178.255" # Broadcast for most home networks
[[Devices]]
[[Device]]
Alias = "Another Device"
MAC = ""
IP = ""

View File

@ -1,11 +0,0 @@
[Unit]
Description=Small web server to send Wake-on-LAN requests to its local network
[Service]
Type=simple
ExecStart=/usr/bin/miniwol
# User=miniwol
[Install]
WantedBy=multi-user.target

View File

@ -5,11 +5,7 @@ import (
"net/http"
"time"
"git.ulra.eu/adro/miniwol/config"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
var sessions map[string]time.Time
@ -37,41 +33,9 @@ func checkAuth(token string) error {
func withAuth(handler echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
session, err := c.Cookie("session")
// Redirect to login if session expired/invalid
if err != nil || checkAuth(session.Value) != nil {
return c.Redirect(http.StatusSeeOther, "/")
}
// Refresh session
sessions[session.Value] = time.Now().Add(time.Second * time.Duration(config.Config.SessionTTL*60))
return handler(c)
}
}
// Handlers
func auth(c echo.Context) error {
password := c.FormValue("Password")
if bcrypt.CompareHashAndPassword([]byte(config.Config.PassHash), []byte(password)) != nil {
return c.String(401, "Wrong Password")
}
token := uuid.New().String()
sessions[token] = time.Now().Add(time.Second * time.Duration(config.Config.SessionTTL*60))
c.SetCookie(&http.Cookie{
Name: "session",
Value: token,
Path: "/",
Secure: config.Config.StrictCookies,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})
return c.Redirect(http.StatusSeeOther, "/")
}
func deauth(c echo.Context) error {
session, err := c.Cookie("session")
if err != nil {
return err
}
delete(sessions, session.Value)
return c.Redirect(http.StatusSeeOther, "/")
}

View File

@ -1,67 +0,0 @@
package web
import (
"errors"
"net/http"
"strconv"
"strings"
"git.ulra.eu/adro/miniwol/config"
"git.ulra.eu/adro/miniwol/lib"
"github.com/labstack/echo/v4"
)
func add(c echo.Context) error {
device := config.Device{}
err := c.Bind(&device)
if err != nil {
return err
}
config.Config.Devices = append(config.Config.Devices, device)
config.Save()
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
func wake(c echo.Context) error {
index, err := strconv.Atoi(c.FormValue("Index"))
if err != nil {
return err
}
for i, device := range config.Config.Devices {
if i == index {
if !strings.Contains(device.IP, ":") {
device.IP += ":9"
}
err := lib.SendPacket(":0", device.IP, device.MAC)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
}
return errors.New("device not found")
}
func remove(c echo.Context) error {
index, err := strconv.Atoi(c.FormValue("Index"))
if err != nil {
return err
}
for i := range config.Config.Devices {
if i == index {
config.Config.Devices = append(config.Config.Devices[:i], config.Config.Devices[i+1:]...)
err := config.Save()
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
}
return errors.New("device not found")
}

View File

@ -1,69 +0,0 @@
<form action="/add" method="post">
<fieldset>
<legend>Add Device</legend>
<label for="alias">Alias</label><input id="alias" name="Alias">
<label for="mac">MAC</label><input id="mac" name="MAC">
<label for="ip">IP</label><input id="ip" name="IP" value="255.255.255.255">
<input type="submit" value="Submit">
</fieldset>
</form>
<fieldset>
<legend>Devices</legend>
<table>
<tr>
<th>Alias</th>
<th>MAC</th>
<th>IP/Broadcast</th>
<th>Actions</th>
</tr>
{{range $i, $d := .Devices}}
<tr>
<td>{{$d.Alias}}</td>
<td>{{$d.MAC}}</td>
<td>{{$d.IP}}</td>
<td>
<div class="actions">
<form action="/wake" method="post">
<input type="number" name="Index" value="{{$i}}" hidden>
<input type="submit" value="&#x23FB;" title="Wake">
</form>
<form action="/remove" method="post">
<input type="number" name="Index" value="{{$i}}" hidden>
<input type="submit" value="&#x1F5D1;" title="Delete">
</form>
</div>
</td>
</tr>
{{end}}
</table>
</fieldset>
<style>
body > div {
flex-flow: row wrap;
align-items: flex-start;
}
body > div > form {
flex-grow: 1;
}
table {
grid-column: 1 / 3;
border-collapse: collapse;
}
th, td {
padding: 0.25rem;
}
.actions {
flex-flow: row wrap;
justify-content: center;
padding: 0;
gap: 0.5rem;
}
</style>

View File

@ -0,0 +1,22 @@
<table>
<tr>
<th>Device Alias</th>
<th>MAC Address</th>
<th>IP/Broadcast</th>
<th>Actions</th>
</tr>
{{range $i, $d := .Device}}
<tr>
<td>{{$d.Alias}}</td>
<td>{{$d.MAC}}</td>
<td>{{$d.IP}}</td>
<td>
<form action="/wake" method="post">
<input type="text" name="index" value="{{$i}}" hidden>
<input type="text" name="alias" value="{{$d.Alias}}" hidden>
<input type="submit" value="Wake">
</form>
</td>
</tr>
{{end}}
</table>

View File

@ -1,14 +0,0 @@
<form action="/auth" method="post">
<fieldset>
<legend>Login</legend>
<label for="pw">Password</label><input type="password" id="pw" name="Password">
<input type="submit" value="Submit">
</fieldset>
</form>
<style>
form {
max-width: max-content;
}
</style>

View File

@ -0,0 +1,3 @@
<form action="/auth" method="post">
<label for="pw">Password: </label><input type="password" name="password">
</form>

View File

@ -1,23 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Small web server to send Wake-on-LAN requests to its local network">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/style.css">
<title>{{ .Title }} - miniwol</title>
</head>
<body>
<nav>
<h1>miniwol</h1>
<div style="flex-grow: 1;"></div>
{{ if .Auth }}
<form action="/deauth" method="post">
<input type="submit" value="&#x23FB;" title="Logout">
</form>
{{ end }}
</nav>
<div>{{ .Content }}</div>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="data:,">
<title>{{ .Title }}</title>
</head>
<body>
{{ .Content }}
</body>
</html>

View File

@ -1,79 +0,0 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
color: inherit;
line-height: 1;
}
nav, div, fieldset {
display: flex;
flex-flow: column nowrap;
padding: 0.5rem;
gap: 0.5rem;
}
nav {
flex-flow: row nowrap;
}
fieldset {
display: grid;
grid-template-columns: auto 1fr;
flex-grow: 999999;
overflow-x: auto;
}
fieldset > label:after {
content: ":";
pointer-events: none;
}
input[type=submit] {
grid-column: 1 / 3;
padding: 0.25rem;
cursor: pointer;
}
legend {
font-weight: bold;
}
/* Colors */
:root {
--cl-fg: #222;
--cl-bg-page: #eee;
--cl-bg-block: #ddd;
--cl-bg-input: #ccc;
}
@media (prefers-color-scheme: dark) {
:root {
--cl-fg: #ddd;
--cl-bg-page: #333;
--cl-bg-block: #222;
--cl-bg-input: #444;
}
}
:root {
font-family: sans-serif;
background: var(--cl-bg-page);
color: var(--cl-fg);
}
nav, fieldset, table {
background: var(--cl-bg-block);
}
input {
border: 1px solid var(--cl-fg);
background: var(--cl-bg-input);
}
input[type=submit]:hover,
tr:nth-child(even) {
background: var(--cl-bg-page);
}

View File

@ -3,11 +3,19 @@ package web
import (
"bytes"
"embed"
"errors"
"fmt"
"html/template"
"net/http"
"strings"
"time"
"git.ulra.eu/adro/miniwol/config"
"git.ulra.eu/adro/miniwol/lib"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
//go:embed template/*
@ -17,7 +25,7 @@ var templates *template.Template
func init() {
var err error
templates, err = template.ParseFS(templateFS, "template/*.html")
templates, err = template.ParseFS(templateFS, "template/*.html.tmpl")
if err != nil {
panic(err)
}
@ -27,32 +35,25 @@ func Run() error {
e := echo.New()
e.GET("/", index)
e.GET("/style.css", style)
e.GET("/favicon.ico", favicon)
e.POST("/auth", auth)
e.POST("/deauth", withAuth(deauth))
e.POST("/add", withAuth(add))
e.POST("/wake", withAuth(wake))
e.POST("/remove", withAuth(remove))
return e.Start(config.Config.Server)
}
func Page(c echo.Context, code int, title string, page string, auth bool, data interface{}) error {
func Page(c echo.Context, code int, title string, page string, data interface{}) error {
var contentBuffer bytes.Buffer
err := templates.ExecuteTemplate(&contentBuffer, page, data)
if err != nil {
return err
}
var pageBuffer bytes.Buffer
err = templates.ExecuteTemplate(&pageBuffer, "page.html", struct {
err = templates.ExecuteTemplate(&pageBuffer, "page.html.tmpl", struct {
Title string
Content template.HTML
Auth bool
}{
Title: title,
Content: template.HTML(contentBuffer.String()),
Auth: auth,
})
if err != nil {
return err
@ -64,20 +65,44 @@ func Page(c echo.Context, code int, title string, page string, auth bool, data i
func index(c echo.Context) error {
session, err := c.Cookie("session")
if err != nil || checkAuth(session.Value) != nil {
return Page(c, 200, "Login", "login.html", false, nil)
return Page(c, 200, "Login", "login.html.tmpl", nil)
} else {
return Page(c, 200, "Devices", "device.html", true, config.Config)
return Page(c, 200, "Device", "device.html.tmpl", config.Config)
}
}
func style(c echo.Context) error {
styleData, err := templateFS.ReadFile("template/style.css")
if err != nil {
return err
func auth(c echo.Context) error {
password := c.FormValue("password")
if bcrypt.CompareHashAndPassword([]byte(config.Config.PassHash), []byte(password)) != nil {
return c.String(401, "Wrong Password")
}
return c.Blob(200, "text/css", styleData)
token := uuid.New().String()
sessions[token] = time.Now().Add(time.Second * time.Duration(config.Config.SessionTTL*60))
c.SetCookie(&http.Cookie{
Name: "session",
Value: token,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: sessions[token],
})
return c.Redirect(http.StatusSeeOther, "/")
}
func favicon(c echo.Context) error {
return c.NoContent(204)
func wake(c echo.Context) error {
for i, device := range config.Config.Device {
if c.FormValue("alias") == device.Alias && c.FormValue("index") == fmt.Sprint(i) {
if !strings.Contains(device.IP, ":") {
device.IP += ":9"
}
err := lib.SendPacket(":0", device.IP, device.MAC)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
}
return errors.New("device not found")
}