mirror of
https://github.com/seriousm4x/UpSnap.git
synced 2026-11-27 12:21:56 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b9decd572 | |||
| 9a1a816f25 | |||
| 8d43554e0e | |||
| b88f567d9f | |||
| cb168f2f47 | |||
| 376220c557 | |||
| ef064e9335 | |||
| 67870d8114 | |||
| 7bd4712ddd | |||
| f3b740d092 | |||
| a721af02b4 | |||
| b9aa9feb1d | |||
| 59770cf013 | |||
| 8b65cd69bc | |||
| 0f7026f3d0 | |||
| 66b41170e8 | |||
| 27b1a10e86 | |||
| 482a9884a4 | |||
| ed285f502e | |||
| b6f76c55c9 | |||
| 7bdb99c066 | |||
| e07755f3b9 | |||
| 9b7a7632e6 | |||
| 8c8671908c | |||
| 288baf0217 | |||
| 647dfe3e9e | |||
| b0452aa82d | |||
| b5258bbb03 | |||
| f77c2c0146 | |||
| b68bb20e06 |
@@ -0,0 +1,2 @@
|
||||
github: seriousm4x
|
||||
ko_fi: seriousm4x
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
name: "⭐ Feature request"
|
||||
about: Suggest an idea for this project
|
||||
title: "[REQUEST]"
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**The problem you currently have with this project**
|
||||
A clear and concise description of what the problem is.
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: "\U0001F577️ Bug report"
|
||||
about: Create a report to help the project improve
|
||||
title: "[BUG]"
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Logs**
|
||||
```
|
||||
formatted logs here
|
||||
```
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Host OS (and version)**
|
||||
eg. Windows 10, Ubuntu 22.10, macOS 13
|
||||
|
||||
**Host Architecture**
|
||||
e.g. amd64/arm64/arm7
|
||||
|
||||
**Brower (if relevant)**
|
||||
e.g. Firefox 109.0.1
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
@@ -1,4 +1,4 @@
|
||||
name: Deploy release
|
||||
name: v3 build
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
- name: Build frontend
|
||||
run: |
|
||||
npm --prefix=./frontend i
|
||||
UPSNAP_VERSION=${GITHUB_REF##*/} npm --prefix=./frontend run build
|
||||
UPSNAP_VERSION=${GITHUB_REF##*/} npm --prefix=./frontend run build
|
||||
cp -r ./frontend/build/* ./backend/pb_public/
|
||||
|
||||
# setup go
|
||||
@@ -41,7 +41,7 @@ jobs:
|
||||
with:
|
||||
distribution: goreleaser
|
||||
version: latest
|
||||
args: release --rm-dist
|
||||
args: release --clean
|
||||
workdir: backend
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
+3
-1
@@ -12,8 +12,10 @@ RUN wget https://github.com/seriousm4x/UpSnap/releases/download/${VERSION}/UpSna
|
||||
|
||||
FROM alpine:3
|
||||
RUN apk update &&\
|
||||
apk add --no-cache tzdata ca-certificates nmap samba-common-tools sshpass curl &&\
|
||||
apk add --no-cache tzdata ca-certificates nmap samba-common-tools openssh sshpass curl &&\
|
||||
rm -rf /var/cache/apk/*
|
||||
WORKDIR /app
|
||||
COPY --from=downloader /app/upsnap upsnap
|
||||
HEALTHCHECK --interval=10s \
|
||||
CMD curl -fs "http://localhost:8090/api/health" || exit 1
|
||||
ENTRYPOINT ["./upsnap", "serve", "--http=0.0.0.0:8090"]
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
<p>A simple wake on lan app written with SvelteKit, Go, PocketBase and nmap.</p>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/upsnap"><img src="https://img.shields.io/github/stars/seriousm4x/upsnap" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/releases"><img src="https://img.shields.io/github/downloads/seriousm4x/upsnap/total" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/releases"><img src="https://img.shields.io/github/go-mod/go-version/seriousm4x/UpSnap?filename=backend/go.mod" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/releases"><img src="https://img.shields.io/github/v/release/seriousm4x/upsnap?display_name=tag" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/actions"><img src="https://img.shields.io/github/actions/workflow/status/seriousm4x/upsnap/deploy.yml" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/actions"><img src="https://github.com/seriousm4x/upsnap/actions/workflows/deploy.yml/badge.svg?event=push" /></a>
|
||||
<a target="_blank" href="https://github.com/seriousm4x/UpSnap/commits/master"><img src="https://img.shields.io/github/last-commit/seriousm4x/upsnap" /></a>
|
||||
<h3><a href="https://github.com/seriousm4x/UpSnap/releases/tag/3.0.0">v3 is here! 🤩 Check it out</a></h3>
|
||||
</div>
|
||||
|
||||
## ✨ Features
|
||||
@@ -31,16 +33,18 @@
|
||||
|
||||
## 🚀 Run the binary
|
||||
|
||||
Just download the latest binary from the [release page](https://github.com/seriousm4x/UpSnap/releases) and run it.
|
||||
Just download the latest binary from the [release page](https://github.com/seriousm4x/UpSnap/releases) and run it `./upsnap serve --http=0.0.0.0:8090`.
|
||||
|
||||
Run `./upsnap serve --http=0.0.0.0:8090`. For more options check `./upsnap --help`.
|
||||
For more options check `./upsnap --help` or visit [PocketBase documentation](https://pocketbase.io/docs).
|
||||
|
||||
If you need network discovery, make sure to have nmap installed and run upsnap as root/admin.
|
||||
If you need network discovery, make sure to have `nmap` installed and run upsnap as root/admin.
|
||||
|
||||
## 🐳 Run in docker
|
||||
|
||||
Alternatively use the [docker-compose](docker-compose.yml) example. See the comments in the file for customization.
|
||||
|
||||
If you need additional packages inside the container, uncomment the `entrypoint` inside the compose file. You can search for your needed package [here](https://pkgs.alpinelinux.org/packages).
|
||||
|
||||
### Reverse Proxy
|
||||
|
||||
**Caddy example**
|
||||
@@ -51,7 +55,7 @@ upsnap.example.com {
|
||||
}
|
||||
```
|
||||
|
||||
## Help developing
|
||||
## 🔧 Help developing
|
||||
|
||||
Fork this branch and clone it.
|
||||
|
||||
|
||||
+1
-1
@@ -3,11 +3,11 @@ module github.com/seriousm4x/upsnap
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/go-ping/ping v1.1.0
|
||||
github.com/labstack/echo/v5 v5.0.0-20220201181537-ed2888cfa198
|
||||
github.com/mdlayher/wol v0.0.0-20220221231636-b763a792253a
|
||||
github.com/pocketbase/dbx v1.9.0
|
||||
github.com/pocketbase/pocketbase v0.12.0
|
||||
github.com/prometheus-community/pro-bing v0.1.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
)
|
||||
|
||||
|
||||
+2
-2
@@ -934,8 +934,6 @@ github.com/go-openapi/swag v0.21.1/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/e
|
||||
github.com/go-openapi/validate v0.21.0/go.mod h1:rjnrwK57VJ7A8xqfpAOEKRH8yQSGUriMu5/zuPSQ1hg=
|
||||
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
|
||||
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
|
||||
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
|
||||
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
@@ -1583,6 +1581,8 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
|
||||
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
|
||||
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
|
||||
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
|
||||
github.com/prometheus-community/pro-bing v0.1.0 h1:zjzLGhfNPP0bP1OlzGB+SJcguOViw7df12LPg2vUJh8=
|
||||
github.com/prometheus-community/pro-bing v0.1.0/go.mod h1:BpWlHurD9flHtzq8wrh8QGWYz9ka9z9ZJAyOel8ej58=
|
||||
github.com/prometheus/alertmanager v0.24.0/go.mod h1:r6fy/D7FRuZh5YbnX6J3MBY0eI4Pb5yPYS7/bPSXXqI=
|
||||
github.com/prometheus/client_golang v0.0.0-20180209125602-c332b6f63c06/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package networking
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -17,18 +16,10 @@ func SendMagicPacket(device *models.Record) error {
|
||||
password := device.GetString("password")
|
||||
|
||||
// parse inputs
|
||||
parsedIP := net.ParseIP(ip)
|
||||
if parsedIP == nil {
|
||||
return fmt.Errorf("error: invalid ip address")
|
||||
}
|
||||
parsedMac, err := net.ParseMAC(mac)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parsedNetmask := net.ParseIP(netmask)
|
||||
if parsedNetmask == nil {
|
||||
return fmt.Errorf("error: invalid netmask")
|
||||
}
|
||||
var bytePassword []byte
|
||||
if len(password) == 0 || len(password) == 4 || len(password) == 6 {
|
||||
bytePassword = []byte(password)
|
||||
@@ -37,11 +28,7 @@ func SendMagicPacket(device *models.Record) error {
|
||||
}
|
||||
|
||||
// get target addr
|
||||
ipNet := &net.IPNet{
|
||||
IP: parsedIP,
|
||||
Mask: parsedNetmask.DefaultMask(),
|
||||
}
|
||||
broadcastIp, err := getBroadcastIp(ipNet)
|
||||
broadcastIp, err := getBroadcastIp(ip, netmask)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -64,11 +51,30 @@ func wakeUDP(addr string, target net.HardwareAddr, password []byte) error {
|
||||
return c.WakePassword(addr, target, password)
|
||||
}
|
||||
|
||||
func getBroadcastIp(n *net.IPNet) (net.IP, error) {
|
||||
if n.IP.To4() == nil {
|
||||
return net.IP{}, errors.New("does not support IPv6 addresses")
|
||||
func getBroadcastIp(ipStr, maskStr string) (string, error) {
|
||||
ip := net.ParseIP(ipStr)
|
||||
if ip == nil {
|
||||
return "", errors.New("ip not a valid ipv4 address")
|
||||
}
|
||||
ip := make(net.IP, len(n.IP.To4()))
|
||||
binary.BigEndian.PutUint32(ip, binary.BigEndian.Uint32(n.IP.To4())|^binary.BigEndian.Uint32(net.IP(n.Mask).To4()))
|
||||
return ip, nil
|
||||
ip = ip.To4()
|
||||
if ip == nil {
|
||||
return "", errors.New("ip not a valid ipv4 address")
|
||||
}
|
||||
|
||||
mask := net.ParseIP(maskStr)
|
||||
if mask == nil {
|
||||
return "", errors.New("subnet mask not a valid ipv4 address")
|
||||
}
|
||||
mask = mask.To4()
|
||||
ip = ip.To4()
|
||||
if ip == nil {
|
||||
return "", errors.New("subnet mask not a valid ipv4 address")
|
||||
}
|
||||
|
||||
broadcast := make(net.IP, 4)
|
||||
for i := range ip {
|
||||
broadcast[i] = ip[i] | ^mask[i]
|
||||
}
|
||||
|
||||
return broadcast.String(), nil
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-ping/ping"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
probing "github.com/prometheus-community/pro-bing"
|
||||
"github.com/seriousm4x/upsnap/logger"
|
||||
)
|
||||
|
||||
func PingDevice(device *models.Record) bool {
|
||||
pinger, err := ping.NewPinger(device.GetString("ip"))
|
||||
pinger, err := probing.NewPinger(device.GetString("ip"))
|
||||
if err != nil {
|
||||
logger.Error.Println(err)
|
||||
return false
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package networking
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
@@ -28,8 +29,10 @@ func ShutdownDevice(device *models.Record) error {
|
||||
}
|
||||
|
||||
cmd := exec.Command(shell, shell_arg, shutdown_cmd)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("%s", stderr.String())
|
||||
}
|
||||
|
||||
// check state every second for 2 min
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/seriousm4x/upsnap/logger"
|
||||
"github.com/seriousm4x/upsnap/networking"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,7 @@ func HandlerWake(c echo.Context) error {
|
||||
record.Set("status", "pending")
|
||||
App.Dao().SaveRecord(record)
|
||||
if err := networking.WakeDevice(record); err != nil {
|
||||
logger.Error.Println(err)
|
||||
record.Set("status", "offline")
|
||||
} else {
|
||||
record.Set("status", "online")
|
||||
@@ -41,6 +43,7 @@ func HandlerShutdown(c echo.Context) error {
|
||||
record.Set("status", "pending")
|
||||
App.Dao().SaveRecord(record)
|
||||
if err := networking.ShutdownDevice(record); err != nil {
|
||||
logger.Error.Println(err)
|
||||
record.Set("status", "online")
|
||||
} else {
|
||||
record.Set("status", "offline")
|
||||
|
||||
+1
-2
@@ -2,7 +2,6 @@ package pb
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
@@ -112,7 +111,7 @@ func StartPocketBase(distDirFS fs.FS) {
|
||||
|
||||
// start pocketbase
|
||||
if err := App.Start(); err != nil {
|
||||
log.Fatal(err)
|
||||
logger.Error.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-6
@@ -1,19 +1,16 @@
|
||||
version: "3"
|
||||
services:
|
||||
upsnap:
|
||||
container_name: upsnap
|
||||
image: ghcr.io/seriousm4x/upsnap:3
|
||||
network_mode: host
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./data:/app/pb_data
|
||||
healthcheck:
|
||||
test: wget --no-verbose --tries=1 --spider http://localhost:8090/api/health || exit 1
|
||||
interval: 1m
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
# environment:
|
||||
# - TZ=Europe/Berlin # Set container timezone for cron schedules
|
||||
# - UPSNAP_INTERVAL=@every 10s # Sets the interval in which the devices are pinged
|
||||
# - UPSNAP_SCAN_RANGE=192.168.1.0/24 # Scan range is used for device discovery on local network
|
||||
# - UPSNAP_WEBSITE_TITLE=Custom name # Custom website title
|
||||
# command: ["./upsnap", "serve", "--http", "0.0.0.0:8090"] # Uncomment this if you want to change the port for example
|
||||
# # you can change the listen ip:port inside the container or install custom packages for shutdown via a custom entrypoint
|
||||
# entrypoint: /bin/sh -c "apk update && apk add --no-cache ipmitool && rm -rf /var/cache/apk/* && ./upsnap serve --http 0.0.0.0:8090"
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
faLock
|
||||
} from '@fortawesome/free-solid-svg-icons';
|
||||
import Fa from 'svelte-fa';
|
||||
import { sortPorts } from '../sorts';
|
||||
|
||||
export let device;
|
||||
export let now;
|
||||
@@ -21,10 +22,6 @@
|
||||
function wake() {
|
||||
fetch(`${dev ? 'http://localhost:8090' : ''}/api/upsnap/wake/${device.id}`);
|
||||
}
|
||||
|
||||
function sortPorts(a, b) {
|
||||
return a.number - b.number;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 g-4">
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { pocketbase } from '@stores/pocketbase';
|
||||
import { pocketbase, devices } from '@stores/pocketbase';
|
||||
import { faEye, faEyeSlash, faPlus, faTrashCan } from '@fortawesome/free-solid-svg-icons';
|
||||
import Fa from 'svelte-fa';
|
||||
|
||||
export let device;
|
||||
export let mode;
|
||||
|
||||
let pb;
|
||||
let timeout;
|
||||
let button = {
|
||||
state: 'none',
|
||||
@@ -25,12 +23,6 @@
|
||||
let passwordShow = false;
|
||||
$: passwordType = passwordShow ? 'text' : 'password';
|
||||
|
||||
onMount(async () => {
|
||||
pocketbase.subscribe((conn) => {
|
||||
pb = conn;
|
||||
});
|
||||
});
|
||||
|
||||
async function addOrUpdateDevice() {
|
||||
button.state = 'waiting';
|
||||
try {
|
||||
@@ -47,16 +39,18 @@
|
||||
for (let i = 0; i < device.expand.ports.length; i++) {
|
||||
const port = device.expand.ports[i];
|
||||
if (!port.id) {
|
||||
const result = await pb.collection('ports').create(port);
|
||||
const result = await $pocketbase.collection('ports').create(port);
|
||||
device.ports = [...device.ports, result.id];
|
||||
} else {
|
||||
await $pocketbase.collection('ports').update(port.id, port);
|
||||
}
|
||||
}
|
||||
|
||||
// create or update device
|
||||
if (mode === 'add') {
|
||||
await pb.collection('devices').create(device);
|
||||
await $pocketbase.collection('devices').create(device);
|
||||
} else {
|
||||
await pb.collection('devices').update(device.id, device);
|
||||
await $pocketbase.collection('devices').update(device.id, device);
|
||||
}
|
||||
|
||||
// show button with timeout
|
||||
@@ -79,10 +73,11 @@
|
||||
async function deleteDevice() {
|
||||
deleteButton.state = 'waiting';
|
||||
try {
|
||||
await $pocketbase.collection('devices').delete(device.id);
|
||||
device.ports.forEach(async (port) => {
|
||||
await pb.collection('ports').delete(port);
|
||||
await $pocketbase.collection('ports').delete(port);
|
||||
});
|
||||
await pb.collection('devices').delete(device.id);
|
||||
delete $devices[device.id];
|
||||
goto('/');
|
||||
} catch (error) {
|
||||
clearTimeout(timeout);
|
||||
@@ -100,7 +95,7 @@
|
||||
// ports with id exist in db, ports without id are not created yet
|
||||
const port = device.expand.ports[idx];
|
||||
if (port.id) {
|
||||
await pb.collection('ports').delete(port.id);
|
||||
await $pocketbase.collection('ports').delete(port.id);
|
||||
const i = device.ports.indexOf(port.id);
|
||||
if (i !== -1) {
|
||||
device.ports.splice(i, 1);
|
||||
|
||||
@@ -1,31 +1,22 @@
|
||||
<script>
|
||||
import { page } from '$app/stores';
|
||||
import { theme } from '@stores/theme';
|
||||
import { pocketbase } from '@stores/pocketbase';
|
||||
import { pocketbase, settings } from '@stores/pocketbase';
|
||||
import { faSun, faMoon, faCircleHalfStroke, faBrush } from '@fortawesome/free-solid-svg-icons';
|
||||
import Fa from 'svelte-fa';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let pb;
|
||||
let website_title = '';
|
||||
|
||||
onMount(async () => {
|
||||
pocketbase.subscribe((conn) => {
|
||||
pb = conn;
|
||||
});
|
||||
const result = await pb.collection('settings').getList(1, 1);
|
||||
website_title = result.items[0].website_title;
|
||||
|
||||
pb.collection('settings').subscribe('*', function (e) {
|
||||
website_title = e.record.website_title;
|
||||
document.title = website_title;
|
||||
$pocketbase.collection('settings').subscribe('*', function (e) {
|
||||
settings.set(e.record);
|
||||
document.title = $settings.website_title;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
{#if website_title !== ''}
|
||||
<title>{website_title}</title>
|
||||
{#if $settings.website_title !== ''}
|
||||
<title>{$settings.website_title}</title>
|
||||
{:else}
|
||||
<title>UpSnap</title>
|
||||
{/if}
|
||||
@@ -39,8 +30,8 @@
|
||||
alt="UpSnap"
|
||||
width="30"
|
||||
height="30"
|
||||
class:me-2={website_title !== ''}
|
||||
/>{website_title ? website_title : ''}
|
||||
class:me-2={$settings.website_title !== ''}
|
||||
/>{$settings.website_title ? $settings.website_title : ''}
|
||||
</a>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
|
||||
@@ -4,9 +4,26 @@
|
||||
import { theme } from '@stores/theme';
|
||||
import Navbar from '@components/Navbar.svelte';
|
||||
import Transition from '@components/Transition.svelte';
|
||||
import { pocketbase, settings, devices } from '@stores/pocketbase';
|
||||
|
||||
let preferesDark;
|
||||
|
||||
onMount(async () => {
|
||||
let settingsRes = {};
|
||||
settingsRes = await $pocketbase.collection('settings').getList(1, 1);
|
||||
settings.set(settingsRes.items[0]);
|
||||
|
||||
let tempDevices = {};
|
||||
const devicesRes = await $pocketbase.collection('devices').getFullList(200, {
|
||||
sort: 'name',
|
||||
expand: 'ports'
|
||||
});
|
||||
devicesRes.forEach((device) => {
|
||||
tempDevices[device.id] = device;
|
||||
});
|
||||
devices.set(tempDevices);
|
||||
});
|
||||
|
||||
onMount(() => {
|
||||
// import bootstrap js
|
||||
import('bootstrap/js/dist/dropdown');
|
||||
|
||||
@@ -1,46 +1,35 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import DeviceCard from '@components/DeviceCard.svelte';
|
||||
import { pocketbase } from '@stores/pocketbase';
|
||||
|
||||
let devices = {};
|
||||
import { pocketbase, devices } from '@stores/pocketbase';
|
||||
import { sortDevices } from '../sorts';
|
||||
|
||||
onMount(async () => {
|
||||
let pb;
|
||||
pocketbase.subscribe((conn) => {
|
||||
pb = conn;
|
||||
});
|
||||
|
||||
// get all devices in pocketbase
|
||||
const result = await pb.collection('devices').getFullList(200, {
|
||||
expand: 'ports',
|
||||
sort: 'name'
|
||||
});
|
||||
result.forEach((device) => {
|
||||
devices[device.id] = device;
|
||||
});
|
||||
|
||||
// subscribe to database events
|
||||
pb.collection('devices').subscribe('*', async (e) => {
|
||||
$pocketbase.collection('devices').subscribe('*', async (e) => {
|
||||
if (e.action === 'create') {
|
||||
devices[e.record.id] = e.record;
|
||||
$devices[e.record.id] = e.record;
|
||||
} else if (e.action === 'update') {
|
||||
const device = await pb.collection('devices').getOne(e.record.id, {
|
||||
const device = await $pocketbase.collection('devices').getOne(e.record.id, {
|
||||
expand: 'ports'
|
||||
});
|
||||
devices[device.id] = device;
|
||||
$devices[device.id] = device;
|
||||
} else if (e.action === 'delete') {
|
||||
delete devices[e.record.id];
|
||||
delete $devices[e.record.id];
|
||||
}
|
||||
});
|
||||
pb.collection('ports').subscribe('*', async (e) => {
|
||||
$pocketbase.collection('ports').subscribe('*', async (e) => {
|
||||
if (e.action === 'update') {
|
||||
const device = await pb
|
||||
.collection('devices')
|
||||
.getFirstListItem(`ports.id ?= "${e.record.id}"`, {
|
||||
expand: 'ports'
|
||||
});
|
||||
devices[device.id] = device;
|
||||
const device = Object.values($devices).find((dev) =>
|
||||
dev.ports.includes(e.record.id)
|
||||
);
|
||||
if (!device) {
|
||||
return;
|
||||
}
|
||||
|
||||
// replace device.expand.ports with updated record
|
||||
const portIdx = device.expand.ports.findIndex((port) => port.id === e.record.id);
|
||||
$devices[device.id].expand.ports[portIdx] = e.record;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -56,10 +45,10 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="container text-body-emphasis">
|
||||
{#if Object.keys(devices).length > 0}
|
||||
<div class="container text-body-emphasis mb-4">
|
||||
{#if Object.keys($devices).length > 0}
|
||||
<div class="row">
|
||||
{#each Object.entries(devices) as [_, device]}
|
||||
{#each Object.values($devices).sort(sortDevices) as device}
|
||||
<DeviceCard {device} {now} />
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
@@ -1,30 +1,21 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { pocketbase } from '@stores/pocketbase';
|
||||
import { devices } from '@stores/pocketbase';
|
||||
import DeviceForm from '@components/DeviceForm.svelte';
|
||||
|
||||
export let data;
|
||||
const id = data.params.id;
|
||||
let pb;
|
||||
let device = {
|
||||
id: id
|
||||
};
|
||||
let device;
|
||||
|
||||
onMount(async () => {
|
||||
pocketbase.subscribe((conn) => {
|
||||
pb = conn;
|
||||
});
|
||||
|
||||
const result = await pb.collection('devices').getOne(id, {
|
||||
expand: 'ports'
|
||||
});
|
||||
if (!result.expand.ports) {
|
||||
result.expand.ports = [];
|
||||
$: {
|
||||
device = $devices[data.params.id];
|
||||
if (device && !device?.expand?.ports) {
|
||||
device.expand.ports = [];
|
||||
}
|
||||
device = result;
|
||||
});
|
||||
}
|
||||
$:;
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
<DeviceForm bind:device mode="edit" />
|
||||
</div>
|
||||
{#if device}
|
||||
<div class="container">
|
||||
<DeviceForm bind:device mode="edit" />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { dev } from '$app/environment';
|
||||
import { pocketbase } from '@stores/pocketbase';
|
||||
import { pocketbase, settings, devices } from '@stores/pocketbase';
|
||||
import DeviceForm from '@components/DeviceForm.svelte';
|
||||
import { faPlus } from '@fortawesome/free-solid-svg-icons';
|
||||
import Fa from 'svelte-fa';
|
||||
|
||||
let version = import.meta.env.UPSNAP_VERSION;
|
||||
|
||||
let pb;
|
||||
let files;
|
||||
let settings = {};
|
||||
let timeout;
|
||||
let buttons = {
|
||||
settings: {
|
||||
@@ -45,22 +42,13 @@
|
||||
};
|
||||
let scannedDevices = {};
|
||||
|
||||
onMount(async () => {
|
||||
pocketbase.subscribe((conn) => {
|
||||
pb = conn;
|
||||
});
|
||||
|
||||
const result = await pb.collection('settings').getList(1, 1);
|
||||
settings = result.items[0];
|
||||
});
|
||||
|
||||
async function saveSettings() {
|
||||
buttons.settings.state = 'waiting';
|
||||
try {
|
||||
if (settings.interval === '') {
|
||||
settings.interval = '@every 3s';
|
||||
if ($settings.interval === '') {
|
||||
$settings.interval = '@every 3s';
|
||||
}
|
||||
await pb.collection('settings').update(settings.id, settings);
|
||||
await $pocketbase.collection('settings').update($settings.id, $settings);
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
buttons.settings.state = 'none';
|
||||
@@ -79,7 +67,7 @@
|
||||
|
||||
async function scanDevices() {
|
||||
buttons.scan.state = 'waiting';
|
||||
await pb.collection('settings').update(settings.id, settings);
|
||||
await $pocketbase.collection('settings').update($settings.id, $settings);
|
||||
|
||||
fetch(`${dev ? 'http://localhost:8090' : ''}/api/upsnap/scan`)
|
||||
.then((res) => res.json())
|
||||
@@ -113,7 +101,8 @@
|
||||
}
|
||||
|
||||
async function addDevice(device) {
|
||||
await pb.collection('devices').create(device);
|
||||
await $pocketbase.collection('devices').create(device);
|
||||
$devices[device.id] = device;
|
||||
}
|
||||
|
||||
async function restoreV2Backup() {
|
||||
@@ -121,14 +110,14 @@
|
||||
|
||||
try {
|
||||
// delete all devices in pocketbase
|
||||
let result = await pb.collection('devices').getFullList();
|
||||
let result = await $pocketbase.collection('devices').getFullList();
|
||||
for (let index = 0; index < result.length; index++) {
|
||||
pb.collection('devices').delete(result[index].id);
|
||||
$pocketbase.collection('devices').delete(result[index].id);
|
||||
}
|
||||
// delete all ports in pocketbase
|
||||
result = await pb.collection('ports').getFullList();
|
||||
result = await $pocketbase.collection('ports').getFullList();
|
||||
for (let index = 0; index < result.length; index++) {
|
||||
await pb.collection('ports').delete(result[index].id);
|
||||
await $pocketbase.collection('ports').delete(result[index].id);
|
||||
}
|
||||
|
||||
let reader = new FileReader();
|
||||
@@ -148,7 +137,7 @@
|
||||
let thisDevicePorts = [];
|
||||
for (let index = 0; index < device.ports.length; index++) {
|
||||
const port = device.ports[index];
|
||||
const record = await pb.collection('ports').create({
|
||||
const record = await $pocketbase.collection('ports').create({
|
||||
name: port.name,
|
||||
number: port.number
|
||||
});
|
||||
@@ -156,7 +145,7 @@
|
||||
}
|
||||
|
||||
// create device
|
||||
await pb.collection('devices').create({
|
||||
await $pocketbase.collection('devices').create({
|
||||
name: device.name,
|
||||
ip: device.ip,
|
||||
mac: device.mac,
|
||||
@@ -201,7 +190,7 @@
|
||||
aria-label="Interval"
|
||||
aria-describedby="addon-wrapping"
|
||||
type="text"
|
||||
bind:value={settings.interval}
|
||||
bind:value={$settings.interval}
|
||||
/>
|
||||
</div>
|
||||
<h3 class="my-3">Website title</h3>
|
||||
@@ -214,7 +203,7 @@
|
||||
placeholder="e.g. 'UpSnap'"
|
||||
aria-label="Website title"
|
||||
aria-describedby="website-title"
|
||||
bind:value={settings.website_title}
|
||||
bind:value={$settings.website_title}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -275,7 +264,7 @@
|
||||
placeholder="e.g. '192.168.1.0/24'"
|
||||
aria-label="ip-range"
|
||||
aria-describedby="ip-range"
|
||||
bind:value={settings.scan_range}
|
||||
bind:value={$settings.scan_range}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
@@ -284,7 +273,7 @@
|
||||
class:btn-success={buttons.scan.state === 'success' ? true : false}
|
||||
class:btn-warning={buttons.scan.state === 'waiting' ? true : false}
|
||||
class:btn-danger={buttons.scan.state === 'failed' ? true : false}
|
||||
disabled={settings.scan_range === '' || buttons.scan.state !== 'none'
|
||||
disabled={$settings.scan_range === '' || buttons.scan.state !== 'none'
|
||||
? true
|
||||
: false}
|
||||
>
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
export function sortDevices(a, b) {
|
||||
return a.name.localeCompare(b.name, undefined, {
|
||||
numeric: true,
|
||||
sensitivity: 'base'
|
||||
});
|
||||
}
|
||||
|
||||
export function sortPorts(a, b) {
|
||||
return a.number - b.number;
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
let backend_url;
|
||||
// set backend url based on environment
|
||||
let backend_url = '/';
|
||||
let isDevMode = import.meta.env.DEV;
|
||||
if (isDevMode) {
|
||||
backend_url = 'http://localhost:8090';
|
||||
} else {
|
||||
backend_url = '/';
|
||||
backend_url = 'http://127.0.0.1:8090';
|
||||
}
|
||||
|
||||
// get default values for stores
|
||||
const pb = new PocketBase(backend_url);
|
||||
pb.autoCancellation(false);
|
||||
|
||||
// export stores
|
||||
export const pocketbase = writable(pb);
|
||||
export const settings = writable({
|
||||
website_title: ''
|
||||
});
|
||||
export const devices = writable({});
|
||||
|
||||
Reference in New Issue
Block a user