Compare commits

...

15 Commits

Author SHA1 Message Date
31801dd707 Update LICENSE
Changed to 0-BSD so anyone can copy-paste fragments as they please.
2024-12-10 08:11:35 +00:00
991306c726 Added title for logout 2022-04-28 11:10:40 +02:00
d07b1d4dba Added an option to disable https-only 2022-04-28 11:08:11 +02:00
ed9d9474e3 Icons, identify device by index, launch.json 2022-02-23 14:02:46 +01:00
c56ba425c7 Session refresh, shorter default ttl
- Session cookies also no longer expire on the client
2022-02-23 13:34:52 +01:00
68845fc715 Small improvements
- Removed fake favicon in favor of 204
- Improved table actions (mobile)
2022-02-22 10:34:45 +01:00
e171a7fa09 Update 'README.md' 2022-02-21 20:12:08 +00:00
010ec25477 Small improvements
- Added padding to table elements
- Added default broadcast
2022-02-21 19:09:02 +01:00
8cfc7b035c Update 'README.md' 2022-02-21 15:30:48 +00:00
48003532e7 Update 'README.md' 2022-02-21 15:23:30 +00:00
c23b39fcc2 Responsiveness improvements 2022-02-21 15:57:54 +01:00
a0efbfd06b update readme 2022-02-21 15:19:12 +01:00
2485c7ab7a Features, Polish, Improvements
- Added logout, add/remove devices
- Page structure w/ dark theme
- Template files now use target extension
- Accessibility improvements
- Semantic improvements
2022-02-21 15:16:06 +01:00
63906235a4 Added a basic systemd service 2022-02-18 16:08:31 +01:00
319c8dfcfb update README.md 2022-02-18 15:48:52 +01:00
17 changed files with 367 additions and 111 deletions

1
.gitignore vendored
View File

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

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// 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,9 +1,14 @@
MIT License
BSD Zero Clause License
Copyright (c) <year> <copyright holders>
Copyright (c) 2024 adroslice
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:
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
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.
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.

View File

@ -1,5 +1,4 @@
# miniwol
Small web server to send Wake-on-LAN requests to its local network
## Features
@ -7,19 +6,16 @@ Small web server to send Wake-on-LAN requests to its local network
- Binary includes everything except config
## Installation
Option 1: Run `go build` in this folder after cloning, resulting in a binary `miniwol` in the current directory.
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 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,
Examples include a simple systemd service to have it start automatically.
## Usage
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.
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.
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.
Set a password using `miniwol setpass <password>`. This will also add any missing defaults to the config.
Configure the devices you want to be able to wake up as per the [example](./example/config.toml).
Run the webserver using `miniwol` or `miniwol web` .
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.
Sessions are in-memory. Restarting the webserver clears all sessions.

View File

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

View File

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

11
example/miniwol.service Normal file
View File

@ -0,0 +1,11 @@
[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,7 +5,11 @@ 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
@ -33,9 +37,41 @@ 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, "/")
}

67
web/device.go Normal file
View File

@ -0,0 +1,67 @@
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")
}

69
web/template/device.html Normal file
View File

@ -0,0 +1,69 @@
<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

@ -1,22 +0,0 @@
<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>

14
web/template/login.html Normal file
View File

@ -0,0 +1,14 @@
<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

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

23
web/template/page.html Normal file
View File

@ -0,0 +1,23 @@
<!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

@ -1,13 +0,0 @@
<!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>

79
web/template/style.css Normal file
View File

@ -0,0 +1,79 @@
* {
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,19 +3,11 @@ 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/*
@ -25,7 +17,7 @@ var templates *template.Template
func init() {
var err error
templates, err = template.ParseFS(templateFS, "template/*.html.tmpl")
templates, err = template.ParseFS(templateFS, "template/*.html")
if err != nil {
panic(err)
}
@ -35,25 +27,32 @@ 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, data interface{}) error {
func Page(c echo.Context, code int, title string, page string, auth bool, 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.tmpl", struct {
err = templates.ExecuteTemplate(&pageBuffer, "page.html", struct {
Title string
Content template.HTML
Auth bool
}{
Title: title,
Content: template.HTML(contentBuffer.String()),
Auth: auth,
})
if err != nil {
return err
@ -65,44 +64,20 @@ func Page(c echo.Context, code int, title string, page string, data interface{})
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.tmpl", nil)
return Page(c, 200, "Login", "login.html", false, nil)
} else {
return Page(c, 200, "Device", "device.html.tmpl", config.Config)
return Page(c, 200, "Devices", "device.html", true, config.Config)
}
}
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")
func style(c echo.Context) error {
styleData, err := templateFS.ReadFile("template/style.css")
if err != nil {
return err
}
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, "/")
return c.Blob(200, "text/css", styleData)
}
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")
func favicon(c echo.Context) error {
return c.NoContent(204)
}