├── .github └── workflows │ └── release.yaml ├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── config.yaml ├── go.mod ├── go.sum ├── internal ├── api │ └── api.go ├── config │ └── config.go └── services │ └── netservice.go ├── main.go └── ui ├── ui.go └── wolweb ├── .gitignore ├── README.md ├── eslint.config.js ├── index.html ├── package-lock.json ├── package.json ├── postcss.config.js ├── public └── vite.svg ├── src ├── App.jsx ├── index.css └── main.jsx ├── tailwind.config.js └── vite.config.js /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | build: 10 | name: Build 11 | permissions: 12 | contents: write 13 | pull-requests: write 14 | repository-projects: write 15 | packages: write 16 | attestations: write 17 | id-token: write 18 | runs-on: ubuntu-latest 19 | if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') 20 | steps: 21 | 22 | - name: Set up Go 1.x 23 | uses: actions/setup-go@v4 24 | with: 25 | go-version: ^1.23 26 | id: go 27 | 28 | - name: Check out code into the Go module directory 29 | uses: actions/checkout@v4 30 | 31 | - name: Get dependencies 32 | run: | 33 | go get -v -t -d ./... 34 | if [ -f Gopkg.toml ]; then 35 | curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 36 | dep ensure 37 | fi 38 | 39 | - name: Build 40 | run: | 41 | make clean all 42 | 43 | - run: | 44 | set -x 45 | assets=() 46 | tag_name="${GITHUB_REF##*/}" 47 | gh release create "$tag_name" build/* 48 | env: 49 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 50 | 51 | # Log in to GitHub Container Registry 52 | - name: Log in to GitHub Container Registry 53 | uses: docker/login-action@v2 54 | with: 55 | registry: ghcr.io 56 | username: ${{ github.actor }} 57 | password: ${{ secrets.GITHUB_TOKEN }} 58 | 59 | # Build the Docker image 60 | - name: Build Docker image 61 | run: | 62 | docker build -t ghcr.io/ajnasz/wolweb:${{ github.ref_name }} . 63 | 64 | # Push the Docker image to GHCR 65 | - name: Push Docker image 66 | run: | 67 | docker push ghcr.io/ajnasz/wolweb:${{ github.ref_name }} 68 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:23-alpine AS ui 2 | 3 | COPY ui/wolweb /app 4 | WORKDIR /app 5 | 6 | RUN npm install 7 | RUN npm run build 8 | 9 | 10 | FROM golang:1.23-alpine AS server 11 | 12 | COPY . /app 13 | COPY --from=ui /app/dist /app/ui/wolweb/dist 14 | WORKDIR /app 15 | 16 | RUN go build -o /wolweb 17 | 18 | FROM scratch 19 | 20 | COPY --from=server /wolweb /app/wolweb 21 | WORKDIR /app 22 | 23 | EXPOSE 8951 24 | 25 | CMD ["/app/wolweb"] 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright 2024 Lajos Koszti 3 | 4 | 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: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | 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. 9 | 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all build-ui install-ui 2 | 3 | all: install-ui build-ui build/wolweb build/wolweb.sum 4 | 5 | install-ui: 6 | cd ui/wolweb && npm install 7 | 8 | build-ui: 9 | cd ui/wolweb && npm run build 10 | 11 | build/wolweb: 12 | go build -o build/wolweb 13 | 14 | build/wolweb.sum: build/wolweb 15 | @cd $(@D) && sha256sum $( $(@F) 16 | 17 | clean: 18 | rm -rf build 19 | rm -rf ui/wolweb/node_modules 20 | rm -rf ui/wolweb/dist 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WolWeb 2 | 3 | Wake on LAN web application. WolWeb allows you to remotely wake up devices on your network using their MAC addresses. This is particularly useful for managing devices that support Wake-on-LAN (WoL) functionality. 4 | 5 | ![image](https://github.com/user-attachments/assets/277d750e-89e2-4f22-a2cc-59f9986ab206) 6 | 7 | 8 | ## Configuration 9 | 10 | To configure WolWeb, create a YAML file with the following structure. This file contains the MAC addresses of the devices you want to wake up. 11 | 12 | 13 | config.yaml 14 | 15 | ```yaml 16 | MacAddresses: 17 | - name: "Display Name of Device 1" 18 | address: "00:11:22:00:00:00" 19 | - name: "Display Name of Device 2" 20 | address: "00:11:22:00:00:01" 21 | - name: "Display Name of Device 3" 22 | address: "00:11:22:00:00:02" 23 | - name: "Display Name of Device 4" 24 | address: "00:11:22:00:00:03" 25 | ``` 26 | 27 | ## Usage 28 | 29 | ```bash 30 | $ wolweb -config /path/to/config.yaml 31 | ``` 32 | 33 | ## Building the Golang App 34 | 35 | To build the WolWeb application from source, follow these steps: 36 | 37 | 1. Ensure you have Go installed on your system. You can download it from [golang.org](https://golang.org/dl/). 38 | 2. Clone the repository: 39 | 40 | ```bash 41 | git clone https://github.com/Ajnasz/wolweb.git 42 | cd wolweb 43 | ``` 44 | 45 | 3. Build the UI 46 | 4. 47 | ```bash 48 | cd ui/wolweb 49 | npm install 50 | npm run build 51 | ``` 52 | 53 | 4. Build the application: 54 | 55 | ```bash 56 | go build -o wolweb 57 | ``` 58 | 59 | 5. The `wolweb` executable will be created in the current directory. You can now run it using: 60 | 61 | ```bash 62 | ./wolweb 63 | ``` 64 | 65 | ## Building the Docker Image 66 | 67 | To build the WolWeb application as a Docker image, follow these steps: 68 | 69 | 1. Clone the repository: 70 | 71 | ```bash 72 | git clone https://github.com/Ajnasz/wolweb.git 73 | ``` 74 | 75 | 2. Build the Docker image: 76 | 77 | ```bash 78 | docker build -t wolweb . 79 | ``` 80 | 81 | ## Running the Docker Container 82 | 83 | 1. Create a configuration file named `config.yaml` with the MAC addresses of the devices you want to wake up. You can use the example configuration provided above. 84 | 85 | 2. Pull the Docker image from the GitHub Container Registry or use the image you built in the previous step: 86 | 87 | ```bash 88 | docker pull ghcr.io/ajnasz/wolweb:v1.1.2 89 | ``` 90 | 91 | 3. Run the Docker container: 92 | 93 | ```bash 94 | docker run -d --network host -v $PWD/config.yaml:/app/config.yaml ghcr.io/ajnasz/wolweb:v1.1.2 95 | ``` 96 | 97 | You must mount the configuration file as `/app/config.yaml` in the container. You can replace `$PWD/config.yaml` with the actual path to your configuration file. 98 | You will need to set the network mode to `host` to allow the container to access network devices. 99 | 100 | ## Running as a Systemd Service 101 | 102 | To run the WolWeb application as a systemd service, create a file named `wolweb.service` in `/etc/systemd/system/` with the following content: 103 | 104 | ```ini 105 | [Unit] 106 | Description=WolWeb Service 107 | After=network.target 108 | 109 | [Service] 110 | ExecStart=/path/to/wolweb 111 | Restart=always 112 | User=nobody 113 | Group=nogroup 114 | Environment=PATH=/usr/bin:/usr/local/bin 115 | WorkingDirectory=/path/to/working/directory 116 | 117 | [Install] 118 | WantedBy=multi-user.target 119 | ``` 120 | 121 | Replace `/path/to/wolweb` with the actual path to the `wolweb` executable and `/path/to/working/directory` with the working directory for the service. 122 | 123 | You can now create the systemd service file and enable it using the following commands: 124 | 125 | ```bash 126 | sudo systemctl daemon-reload 127 | sudo systemctl enable wolweb.service 128 | sudo systemctl start wolweb.service 129 | ``` 130 | -------------------------------------------------------------------------------- /config.yaml: -------------------------------------------------------------------------------- 1 | MacAddresses: 2 | - name: "Display Name of Device 1" 3 | address: "00:11:22:00:00:00" 4 | - name: "Display Name of Device 2" 5 | address: "00:11:22:00:00:01" 6 | - name: "Display Name of Device 3" 7 | address: "00:11:22:00:00:02" 8 | - name: "Display Name of Device 4" 9 | address: "00:11:22:00:00:03" 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Ajnasz/wolweb 2 | 3 | go 1.23.4 4 | 5 | require ( 6 | github.com/Ajnasz/wol v1.0.0 7 | github.com/spf13/viper v1.19.0 8 | golang.org/x/sync v0.10.0 9 | ) 10 | 11 | require ( 12 | github.com/fsnotify/fsnotify v1.8.0 // indirect 13 | github.com/hashicorp/hcl v1.0.0 // indirect 14 | github.com/magiconair/properties v1.8.9 // indirect 15 | github.com/mitchellh/mapstructure v1.5.0 // indirect 16 | github.com/pelletier/go-toml/v2 v2.2.3 // indirect 17 | github.com/sagikazarmark/locafero v0.6.0 // indirect 18 | github.com/sagikazarmark/slog-shim v0.1.0 // indirect 19 | github.com/sourcegraph/conc v0.3.0 // indirect 20 | github.com/spf13/afero v1.11.0 // indirect 21 | github.com/spf13/cast v1.7.1 // indirect 22 | github.com/spf13/pflag v1.0.5 // indirect 23 | github.com/subosito/gotenv v1.6.0 // indirect 24 | go.uber.org/multierr v1.11.0 // indirect 25 | golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect 26 | golang.org/x/sys v0.28.0 // indirect 27 | golang.org/x/text v0.21.0 // indirect 28 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 29 | gopkg.in/ini.v1 v1.67.0 // indirect 30 | gopkg.in/yaml.v3 v3.0.1 // indirect 31 | ) 32 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/Ajnasz/wol v1.0.0 h1:NlwdLZHkbBxb9aA2ZqCbNj3DkT/RjK/beXAdQNsD25k= 2 | github.com/Ajnasz/wol v1.0.0/go.mod h1:Sr76A3oH1TrFt0ovycYtwPO3feQNE5lra0EFK+t2Wcs= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= 5 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= 7 | github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= 8 | github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= 9 | github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= 10 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 11 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 12 | github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= 13 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 14 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 15 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 16 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 17 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 18 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 19 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 20 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 21 | github.com/magiconair/properties v1.8.9 h1:nWcCbLq1N2v/cpNsy5WvQ37Fb+YElfq20WJ/a8RkpQM= 22 | github.com/magiconair/properties v1.8.9/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= 23 | github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= 24 | github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 25 | github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= 26 | github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= 27 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= 28 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 29 | github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= 30 | github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 31 | github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= 32 | github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= 33 | github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= 34 | github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= 35 | github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= 36 | github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= 37 | github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= 38 | github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= 39 | github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= 40 | github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= 41 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 42 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 43 | github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= 44 | github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= 45 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 46 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 47 | github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= 48 | github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 49 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= 50 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 51 | golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo= 52 | golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c= 53 | golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= 54 | golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 55 | golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= 56 | golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 57 | golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= 58 | golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 59 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 60 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 61 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 62 | gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= 63 | gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= 64 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 65 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 66 | -------------------------------------------------------------------------------- /internal/api/api.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "log/slog" 7 | "net/http" 8 | 9 | "github.com/Ajnasz/wol" 10 | "github.com/Ajnasz/wolweb/internal/config" 11 | "github.com/Ajnasz/wolweb/internal/services" 12 | "github.com/Ajnasz/wolweb/ui" 13 | ) 14 | 15 | type WoLRequestDAL struct { 16 | MacAddr string `json:"mac_addr"` 17 | BroadcastAddr string `json:"broadcast_addr"` 18 | } 19 | 20 | type API struct{} 21 | 22 | func wakeOnLan(req WoLRequestDAL) error { 23 | netService := services.NetService{} 24 | return netService.WoL(req.MacAddr, req.BroadcastAddr) 25 | } 26 | 27 | func handleWoL(w http.ResponseWriter, r *http.Request) { 28 | var req WoLRequestDAL 29 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 30 | http.Error(w, err.Error(), http.StatusBadRequest) 31 | return 32 | } 33 | 34 | if err := wakeOnLan(req); err != nil { 35 | slog.Error("Failed to send WoL", "error", err, "mac", req.MacAddr) 36 | if errors.Is(err, wol.ErrInvalidMACAddress) { 37 | w.WriteHeader(http.StatusBadRequest) 38 | json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) 39 | return 40 | } 41 | http.Error(w, err.Error(), http.StatusInternalServerError) 42 | return 43 | } 44 | 45 | w.WriteHeader(http.StatusOK) 46 | json.NewEncoder(w).Encode(map[string]string{"message": "success"}) 47 | } 48 | 49 | func handleMacs(conf *config.Config) http.HandlerFunc { 50 | return func(w http.ResponseWriter, r *http.Request) { 51 | w.WriteHeader(http.StatusOK) 52 | json.NewEncoder(w).Encode(map[string]interface{}{"macs": conf.MacAddresses}) 53 | } 54 | } 55 | 56 | func New(conf *config.Config) (*http.ServeMux, error) { 57 | mux := http.NewServeMux() 58 | static, err := ui.Get() 59 | if err != nil { 60 | return nil, err 61 | } 62 | mux.Handle("/", http.FileServer(http.FS(static))) 63 | mux.HandleFunc("POST /api/wol", handleWoL) 64 | mux.HandleFunc("GET /api/macs", handleMacs(conf)) 65 | 66 | return mux, nil 67 | } 68 | -------------------------------------------------------------------------------- /internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "errors" 5 | 6 | "github.com/spf13/viper" 7 | ) 8 | 9 | // ErrFailedToReadConfig is an error that is returned when the config file is not able to be read 10 | var ErrFailedToReadConfig = errors.New("failed to read config") 11 | 12 | // ErrFailedToParseConfig is an error that is returned when the config file is not able to be parsed 13 | var ErrFailedToParseConfig = errors.New("failed to parse config") 14 | 15 | type MacAddress struct { 16 | Address string 17 | Name string 18 | } 19 | 20 | type Config struct { 21 | MacAddresses []MacAddress 22 | } 23 | 24 | func New(configFile string) (*Config, error) { 25 | viperConfig := viper.GetViper() 26 | 27 | if configFile != "" { 28 | viperConfig.SetConfigFile(configFile) 29 | } else { 30 | viperConfig.SetConfigName("config") 31 | viperConfig.SetConfigType("yaml") 32 | viperConfig.AddConfigPath(".") 33 | } 34 | err := viperConfig.ReadInConfig() 35 | if err != nil { 36 | return nil, errors.Join(err, ErrFailedToReadConfig) 37 | } 38 | 39 | var c Config 40 | err = viperConfig.Unmarshal(&c) 41 | if err != nil { 42 | return nil, errors.Join(err, ErrFailedToParseConfig) 43 | } 44 | 45 | return &c, nil 46 | } 47 | -------------------------------------------------------------------------------- /internal/services/netservice.go: -------------------------------------------------------------------------------- 1 | package services 2 | 3 | import ( 4 | "net" 5 | 6 | "github.com/Ajnasz/wol" 7 | "golang.org/x/sync/errgroup" 8 | ) 9 | 10 | type NetService struct{} 11 | 12 | func (NetService) GetAvailableBroadcastAddresses() ([]string, error) { 13 | var broadcastAddresses []string 14 | ifaces, err := net.Interfaces() 15 | if err != nil { 16 | return broadcastAddresses, err 17 | } 18 | 19 | for _, iface := range ifaces { 20 | addrs, err := iface.Addrs() 21 | if err != nil { 22 | continue 23 | } 24 | 25 | for _, addr := range addrs { 26 | ipNet, ok := addr.(*net.IPNet) 27 | if !ok || ipNet.IP.To4() == nil { 28 | continue 29 | } 30 | 31 | ip := ipNet.IP.To4() 32 | mask := ipNet.Mask 33 | broadcast := make(net.IP, len(ip)) 34 | for i := 0; i < len(ip); i++ { 35 | broadcast[i] = ip[i] | ^mask[i] 36 | } 37 | broadcastAddresses = append(broadcastAddresses, broadcast.String()) 38 | } 39 | } 40 | 41 | return broadcastAddresses, nil 42 | } 43 | 44 | func (n NetService) WoL(macAddr string, broadcastAddress string) error { 45 | if broadcastAddress != "" { 46 | return wol.SendPacket(macAddr, broadcastAddress) 47 | } 48 | 49 | broadcastAddresses, err := n.GetAvailableBroadcastAddresses() 50 | if err != nil { 51 | return err 52 | } 53 | 54 | g := new(errgroup.Group) 55 | 56 | for _, broadcastAddress := range broadcastAddresses { 57 | g.Go(func() error { 58 | return wol.SendPacket(macAddr, broadcastAddress) 59 | }) 60 | 61 | } 62 | 63 | err = g.Wait() 64 | return err 65 | } 66 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log/slog" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/Ajnasz/wolweb/internal/api" 10 | "github.com/Ajnasz/wolweb/internal/config" 11 | ) 12 | 13 | func main() { 14 | configPath := flag.String("config", "", "Path to the configuration file") 15 | address := flag.String("address", ":8951", "Address to listen on") 16 | flag.Parse() 17 | 18 | conf, err := config.New(*configPath) 19 | if err != nil { 20 | slog.Error("Failed to load configuration", "error", err) 21 | } 22 | 23 | webApi, err := api.New(conf) 24 | if err != nil { 25 | slog.Error("Failed to create web api", "error", err) 26 | } 27 | 28 | slog.Info("Starting server", "address", *address) 29 | server := &http.Server{ 30 | Addr: *address, 31 | Handler: webApi, 32 | ReadTimeout: 5 * time.Second, 33 | WriteTimeout: 10 * time.Second, 34 | IdleTimeout: 15 * time.Second, 35 | } 36 | 37 | if err := server.ListenAndServe(); err != nil { 38 | slog.Error("Failed to start server", "error", err) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ui/ui.go: -------------------------------------------------------------------------------- 1 | package ui 2 | 3 | import ( 4 | "embed" 5 | "io/fs" 6 | ) 7 | 8 | // Content holds our static web server content. 9 | // 10 | //go:embed wolweb/dist/* 11 | var Content embed.FS 12 | 13 | // Get returns the ui file system 14 | func Get() (fs.FS, error) { 15 | return fs.Sub(Content, "wolweb/dist") 16 | } 17 | -------------------------------------------------------------------------------- /ui/wolweb/.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | -------------------------------------------------------------------------------- /ui/wolweb/README.md: -------------------------------------------------------------------------------- 1 | # React + Vite 2 | 3 | This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. 4 | 5 | Currently, two official plugins are available: 6 | 7 | - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh 8 | - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh 9 | -------------------------------------------------------------------------------- /ui/wolweb/eslint.config.js: -------------------------------------------------------------------------------- 1 | import js from '@eslint/js' 2 | import globals from 'globals' 3 | import react from 'eslint-plugin-react' 4 | import reactHooks from 'eslint-plugin-react-hooks' 5 | import reactRefresh from 'eslint-plugin-react-refresh' 6 | 7 | export default [ 8 | { ignores: ['dist'] }, 9 | { 10 | files: ['**/*.{js,jsx}'], 11 | languageOptions: { 12 | ecmaVersion: 2020, 13 | globals: globals.browser, 14 | parserOptions: { 15 | ecmaVersion: 'latest', 16 | ecmaFeatures: { jsx: true }, 17 | sourceType: 'module', 18 | }, 19 | }, 20 | settings: { react: { version: '18.3' } }, 21 | plugins: { 22 | react, 23 | 'react-hooks': reactHooks, 24 | 'react-refresh': reactRefresh, 25 | }, 26 | rules: { 27 | ...js.configs.recommended.rules, 28 | ...react.configs.recommended.rules, 29 | ...react.configs['jsx-runtime'].rules, 30 | ...reactHooks.configs.recommended.rules, 31 | 'react/jsx-no-target-blank': 'off', 32 | 'react-refresh/only-export-components': [ 33 | 'warn', 34 | { allowConstantExport: true }, 35 | ], 36 | }, 37 | }, 38 | ] 39 | -------------------------------------------------------------------------------- /ui/wolweb/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | WoL 7 | 8 | 9 | 10 |

WoL

11 |
12 |
13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ui/wolweb/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wolweb", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vite", 8 | "build": "vite build", 9 | "lint": "eslint .", 10 | "preview": "vite preview" 11 | }, 12 | "dependencies": { 13 | "classnames": "^2.5.1", 14 | "react": "^18.3.1", 15 | "react-dom": "^18.3.1" 16 | }, 17 | "devDependencies": { 18 | "@eslint/js": "^9.17.0", 19 | "@types/react": "^18.3.18", 20 | "@types/react-dom": "^18.3.5", 21 | "@vitejs/plugin-react": "^4.3.4", 22 | "autoprefixer": "^10.4.20", 23 | "eslint": "^9.17.0", 24 | "eslint-plugin-react": "^7.37.2", 25 | "eslint-plugin-react-hooks": "^5.0.0", 26 | "eslint-plugin-react-refresh": "^0.4.16", 27 | "globals": "^15.14.0", 28 | "postcss": "^8.4.49", 29 | "tailwindcss": "^3.4.17", 30 | "vite": "^6.0.5" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ui/wolweb/postcss.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /ui/wolweb/public/vite.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ui/wolweb/src/App.jsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from 'react'; 2 | import classNames from 'classnames'; 3 | 4 | const STATUS = { 5 | LOADING: 'loading', 6 | SUCCESS: 'success', 7 | ERROR: 'error', 8 | IDLE: 'idle', 9 | }; 10 | 11 | function StatusIcon({ status }) { 12 | switch (status) { 13 | case STATUS.LOADING: 14 | return ; 15 | case STATUS.SUCCESS: 16 | return ; 17 | case STATUS.ERROR: 18 | return ; 19 | default: 20 | return null; 21 | } 22 | } 23 | 24 | const AnimatedStatusIcon = ({ status }) => { 25 | const [show, setShow] = useState(true); 26 | const [currentStatus, setCurrentStatus] = useState(status); 27 | 28 | useEffect(() => { 29 | if (status !== currentStatus) { 30 | setShow(false); 31 | const timer = setTimeout(() => { 32 | setCurrentStatus(status); 33 | setShow(true); 34 | }, 200); 35 | return () => clearTimeout(timer); 36 | } 37 | }, [status, currentStatus]); 38 | 39 | return ( 40 |
46 | ); 47 | }; 48 | 49 | 50 | function getApiUrl(path) { 51 | if (window.location.hostname.includes('localhost')) { 52 | return new URL(path, 'http://localhost:8951'); 53 | } 54 | 55 | return new URL(path, window.location.origin + window.location.pathname); 56 | } 57 | 58 | function useFetchMacAddresses() { 59 | const [macs, setMacs] = useState([]); 60 | const [loading, setLoading] = useState(true); 61 | const [error, setError] = useState(null); 62 | useEffect(() => { 63 | const controller = new AbortController(); 64 | const signal = controller.signal; 65 | 66 | const fetchData = async () => { 67 | try { 68 | const response = await fetch(getApiUrl('./api/macs'), { signal }); 69 | if (response.ok) { 70 | const { macs } = await response.json(); 71 | setMacs(macs || []); 72 | } else { 73 | throw new Error(response.statusText); 74 | } 75 | } catch (error) { 76 | if (error.name !== 'AbortError') { 77 | setError(error); 78 | } 79 | } finally { 80 | setLoading(false); 81 | } 82 | }; 83 | 84 | fetchData(); 85 | 86 | return () => { 87 | controller.abort(); 88 | }; 89 | }, []); 90 | 91 | return { macs, loading, error }; 92 | } 93 | 94 | function StatusIconWrapper({ children, className }) { 95 | return ( 96 |
{children}
97 | ); 98 | } 99 | 100 | function SuccessIcon() { 101 | return ; 102 | } 103 | 104 | function LoadingIcon() { 105 | return ; 106 | } 107 | 108 | function ErrorIcon() { 109 | return !; 110 | } 111 | 112 | function MacAddressButton({ mac, onMacSelect, status, wolError }) { 113 | const { Address, Name } = mac; 114 | return ; 138 | } 139 | 140 | function useSendWol() { 141 | const [loading, setLoading] = useState(null); 142 | const [error, setError] = useState(null); 143 | const [macAddr, setMac] = useState(null); 144 | 145 | async function sendWol(macAddr) { 146 | const body = { 147 | mac_addr: macAddr, 148 | }; 149 | 150 | setLoading(true); 151 | setError(null); 152 | setMac(macAddr); 153 | 154 | let json; 155 | try { 156 | const response = await fetch(getApiUrl('./api/wol'), { 157 | method: 'POST', 158 | body: JSON.stringify(body), 159 | headers: { 160 | 'Content-Type': 'application/json', 161 | }, 162 | }) 163 | json = await response.json(); 164 | if (response.ok) { 165 | await new Promise((r) => requestAnimationFrame(r)); 166 | return macAddr; 167 | } 168 | 169 | if (json?.error) { 170 | throw new Error(json.error); 171 | } 172 | 173 | throw new Error('Failed to send WOL'); 174 | } catch (error) { 175 | setError(error); 176 | } finally { 177 | setLoading(null); 178 | } 179 | } 180 | 181 | return { sendWol, loading, error, macAddr }; 182 | } 183 | 184 | function determineStatus(isError, isLoading, isSuccess) { 185 | return isError ? STATUS.ERROR : 186 | isLoading ? STATUS.LOADING : 187 | isSuccess ? STATUS.SUCCESS : 188 | STATUS.IDLE; 189 | } 190 | 191 | function App() { 192 | const { macs, loading, error: macError } = useFetchMacAddresses(); 193 | const { sendWol, loading: wolLoading, error: wolError, macAddr } = useSendWol(); 194 | const [isSuccess, setIsSuccess] = useState(false); 195 | 196 | function handleMacSelect(address) { 197 | setIsSuccess(false); 198 | sendWol(address).then(() => { 199 | setIsSuccess(address); 200 | }); 201 | } 202 | 203 | if (loading) { 204 | return
Loading...
; 205 | } 206 | 207 | if (macError) { 208 | return
Error: {macError.message}
; 209 | } 210 | 211 | return ( 212 |
213 | {macs.map((mac) => )} 220 |
221 | ); 222 | } 223 | 224 | export default App 225 | -------------------------------------------------------------------------------- /ui/wolweb/src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /ui/wolweb/src/main.jsx: -------------------------------------------------------------------------------- 1 | import { StrictMode } from 'react' 2 | import { createRoot } from 'react-dom/client' 3 | import './index.css' 4 | import App from './App.jsx' 5 | 6 | createRoot(document.getElementById('root')).render( 7 | 8 | 9 | 10 | ); 11 | -------------------------------------------------------------------------------- /ui/wolweb/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | export default { 3 | content: [ 4 | "./index.html", 5 | "./src/**/*.{js,ts,jsx,tsx}", 6 | ], 7 | theme: { 8 | extend: {}, 9 | }, 10 | plugins: [], 11 | } 12 | -------------------------------------------------------------------------------- /ui/wolweb/vite.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite' 2 | import react from '@vitejs/plugin-react' 3 | 4 | // https://vite.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()], 7 | base: './', 8 | }) 9 | --------------------------------------------------------------------------------