feat: complete SyncServer implementation

Full-stack Go monolith with embedded React frontend for orchestrating
rsync-over-SSH file synchronization with Wake-on-LAN support.

Features:
- JWT auth (HS256) with bcrypt password hashing
- CRUD for machines (with WoL config) and sync_pairs
- Ed25519 SSH key generation and known_hosts management
- WoL magic packet sender + TCP-connect waiter with backoff
- Sync engine: rsync subprocess, per-pair job queue, progress parsing
- Homebrew cron parser for scheduled syncs
- SSE stream for live job status (queued/waking_up/running/success/failed)
- React+TS+Vite+Tailwind SPA embedded via embed.FS
- Debian packaging with systemd unit, postinst/prerm/postrm

Tech stack:
- Go 1.22+ (CGO_ENABLED=0, pure SQLite via modernc.org/sqlite)
- chi router for HTTP API
- TypeScript + React 18 + Tailwind CSS frontend
- Cross-compiled to Linux amd64 for Proxmox LXC deployment

Tests: wol (MAC parsing, magic packet), syncengine/queue, scheduler/cron
This commit is contained in:
2026-07-07 15:03:22 -04:00
parent 1a66ac58cd
commit 8e08c73f60
69 changed files with 7949 additions and 152 deletions
+68
View File
@@ -0,0 +1,68 @@
package wol
import (
"context"
"fmt"
"net"
"os/exec"
"time"
)
var ErrTimeout = fmt.Errorf("timeout waiting for machine to respond")
func WaitUntilReady(ctx context.Context, host string, sshPort int, maxWait, checkInterval time.Duration, usePing bool) error {
deadline := time.Now().Add(maxWait)
interval := checkInterval
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
addr := fmt.Sprintf("%s:%d", host, sshPort)
dialer := net.Dialer{Timeout: 3 * time.Second}
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err == nil {
conn.Close()
return nil
}
if usePing {
cmd := exec.CommandContext(ctx, "ping", "-c", "1", "-W", "1", host)
if err := cmd.Run(); err == nil {
return nil
}
}
if time.Now().After(deadline) {
return ErrTimeout
}
elapsed := time.Since(time.Now().Add(-maxWait))
if elapsed > maxWait/2 && interval < 10*time.Second {
interval = interval * 3 / 2
if interval > 10*time.Second {
interval = 10 * time.Second
}
ticker.Reset(interval)
}
}
}
func IsHostReachable(host string, port int, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
addr := fmt.Sprintf("%s:%d", host, port)
dialer := net.Dialer{Timeout: timeout}
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err == nil {
conn.Close()
return true
}
return false
}
+80
View File
@@ -0,0 +1,80 @@
package wol
import (
"fmt"
"net"
"regexp"
"strings"
"time"
)
var macRegex = regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$`)
func ParseMAC(s string) ([6]byte, error) {
s = strings.ReplaceAll(s, "-", ":")
s = strings.ToLower(s)
if !macRegex.MatchString(s) {
return [6]byte{}, fmt.Errorf("invalid MAC address: %s", s)
}
parts := strings.Split(s, ":")
var mac [6]byte
for i := 0; i < 6; i++ {
var b int
if _, err := fmt.Sscanf(parts[i], "%x", &b); err != nil {
return [6]byte{}, fmt.Errorf("invalid MAC address: %s", s)
}
mac[i] = byte(b)
}
return mac, nil
}
func FormatMAC(mac [6]byte) string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
}
func BuildMagicPacket(mac [6]byte) []byte {
packet := make([]byte, 6+16*6)
for i := 0; i < 6; i++ {
packet[i] = 0xFF
}
for i := 0; i < 16; i++ {
offset := 6 + i*6
copy(packet[offset:offset+6], mac[:])
}
return packet
}
func Send(addr string, mac [6]byte, broadcastAddr string) error {
packet := BuildMagicPacket(mac)
udpAddr := &net.UDPAddr{
IP: net.IPv4bcast,
Port: 9,
}
if broadcastAddr != "" {
udpAddr.IP = net.ParseIP(broadcastAddr)
if udpAddr.IP == nil {
return fmt.Errorf("invalid broadcast address: %s", broadcastAddr)
}
}
conn, err := net.DialUDP("udp4", nil, udpAddr)
if err != nil {
return fmt.Errorf("creating UDP connection: %w", err)
}
defer conn.Close()
if err := conn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return fmt.Errorf("setting broadcast mode: %w", err)
}
n, err := conn.Write(packet)
if err != nil {
return fmt.Errorf("sending magic packet: %w", err)
}
if n != len(packet) {
return fmt.Errorf("incomplete write: sent %d/%d bytes", n, len(packet))
}
return nil
}
+72
View File
@@ -0,0 +1,72 @@
package wol
import (
"testing"
)
func TestParseMAC(t *testing.T) {
tests := []struct {
input string
wantOK bool
wantBytes [6]byte
}{
{"AA:BB:CC:DD:EE:FF", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
{"aa:bb:cc:dd:ee:ff", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
{"AA-BB-CC-DD-EE-FF", true, [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}},
{"11:22:33:44:55:66", true, [6]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}},
{"not:a:mac:addre", false, [6]byte{}},
{"GG:HH:II:JJ:KK:LL", false, [6]byte{}},
{"aa:bb:cc:dd", false, [6]byte{}},
{"", false, [6]byte{}},
}
for _, tt := range tests {
mac, err := ParseMAC(tt.input)
if tt.wantOK {
if err != nil {
t.Errorf("ParseMAC(%q) unexpected error: %v", tt.input, err)
continue
}
if mac != tt.wantBytes {
t.Errorf("ParseMAC(%q) = %v, want %v", tt.input, mac, tt.wantBytes)
}
} else {
if err == nil {
t.Errorf("ParseMAC(%q) expected error, got nil", tt.input)
}
}
}
}
func TestFormatMAC(t *testing.T) {
mac := [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
got := FormatMAC(mac)
want := "aa:bb:cc:dd:ee:ff"
if got != want {
t.Errorf("FormatMAC() = %q, want %q", got, want)
}
}
func TestBuildMagicPacket(t *testing.T) {
mac := [6]byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
packet := BuildMagicPacket(mac)
if len(packet) != 102 {
t.Errorf("BuildMagicPacket length = %d, want 102", len(packet))
}
for i := 0; i < 6; i++ {
if packet[i] != 0xFF {
t.Errorf("packet[%d] = %02x, want FF", i, packet[i])
}
}
for i := 0; i < 16; i++ {
offset := 6 + i*6
for j := 0; j < 6; j++ {
if packet[offset+j] != mac[j] {
t.Errorf("packet[%d] = %02x, want %02x (rep %d)", offset+j, packet[offset+j], mac[j], i)
}
}
}
}