Skip to content

Commit 4b768a6

Browse files
mlugo-apxclaude
andcommitted
Add automatic local subnet detection for systemd service
Problem: - Hardcoded IPAddressAllow=192.168.1.0/24 prevented users on different subnets (10.0.0.x, 192.168.0.x, 172.16.x.x) from connecting to Pi - Service would install successfully but SSH connections would fail silently Solution: - Add %LOCAL_SUBNET% placeholder to service template - Auto-detect local subnet in install_service.sh using multiple methods: 1. Parse default gateway from `ip route` 2. Fallback: Detect from primary interface IP 3. Final fallback: Use 192.168.1.0/24 with warning - Display detected subnet during installation for transparency - Provide manual override instructions if detection fails This ensures the systemd network hardening works correctly across different network configurations without manual editing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 78cec67 commit 4b768a6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

gcode-monitor.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# %HOME_DIR% - Your home directory
88
# %PROJECT_DIR% - Pi-gcode-server installation directory
99
# %WATCH_DIR% - Directory to monitor for .gcode files
10+
# %LOCAL_SUBNET% - Auto-detected local network subnet (e.g., 192.168.1.0/24)
1011

1112
[Unit]
1213
Description=GCode File Monitor and Sync Service
@@ -34,7 +35,7 @@ TasksMax=20
3435
RestrictAddressFamilies=AF_INET AF_INET6
3536
IPAddressDeny=any
3637
IPAddressAllow=localhost
37-
IPAddressAllow=192.168.1.0/24
38+
IPAddressAllow=%LOCAL_SUBNET%
3839

3940
# Filesystem protection (minimal access)
4041
NoNewPrivileges=true

install_service.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,48 @@ PROJECT_DIR="$SCRIPT_DIR"
2323
# Use WATCH_DIR from config, or default to Desktop
2424
WATCH_DIR="${WATCH_DIR:-$HOME/Desktop}"
2525

26+
# Detect local subnet for systemd network restrictions
27+
# Try multiple methods to find the default gateway/subnet
28+
LOCAL_SUBNET=""
29+
if command -v ip >/dev/null 2>&1; then
30+
# Method 1: Get default route gateway and derive subnet
31+
DEFAULT_GW=$(ip route | grep default | awk '{print $3}' | head -n1)
32+
if [ -n "$DEFAULT_GW" ]; then
33+
# Convert gateway IP to subnet (e.g., 192.168.1.1 -> 192.168.1.0/24)
34+
LOCAL_SUBNET=$(echo "$DEFAULT_GW" | sed 's/\.[0-9]*$/\.0\/24/')
35+
fi
36+
fi
37+
38+
# Fallback: If ip command failed, try to detect from primary interface
39+
if [ -z "$LOCAL_SUBNET" ] && command -v ip >/dev/null 2>&1; then
40+
PRIMARY_IP=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -n1)
41+
if [ -n "$PRIMARY_IP" ]; then
42+
LOCAL_SUBNET=$(echo "$PRIMARY_IP" | sed 's/\.[0-9]*$/\.0\/24/')
43+
fi
44+
fi
45+
46+
# Final fallback: Use common private network range
47+
if [ -z "$LOCAL_SUBNET" ]; then
48+
echo "WARNING: Could not auto-detect local subnet. Using default 192.168.1.0/24"
49+
echo " If your network uses a different subnet, edit /etc/systemd/system/gcode-monitor.service"
50+
echo " and change the IPAddressAllow line, then run: sudo systemctl daemon-reload"
51+
LOCAL_SUBNET="192.168.1.0/24"
52+
fi
53+
2654
echo "Generating service file with your configuration..."
2755
echo " User: $USERNAME"
2856
echo " Home: $HOME_DIR"
2957
echo " Project: $PROJECT_DIR"
3058
echo " Watch Dir: $WATCH_DIR"
59+
echo " Local Subnet: $LOCAL_SUBNET"
3160
echo
3261

3362
# Generate service file from template
3463
sed -e "s|%USERNAME%|$USERNAME|g" \
3564
-e "s|%PROJECT_DIR%|$PROJECT_DIR|g" \
3665
-e "s|%HOME_DIR%|$HOME_DIR|g" \
3766
-e "s|%WATCH_DIR%|$WATCH_DIR|g" \
67+
-e "s|%LOCAL_SUBNET%|$LOCAL_SUBNET|g" \
3868
"$SCRIPT_DIR/gcode-monitor.service" > /tmp/gcode-monitor.service
3969

4070
echo "Installing gcode-monitor.service..."

0 commit comments

Comments
 (0)