Overview
Hooks in WireGuard Easy allow you to run custom shell commands at specific points in the interface and client lifecycle. These are powerful automation tools for configuring firewalls, routing, logging, and integrating with other systems.Hooks execute with the same privileges as the WireGuard Easy process. Ensure your commands are secure and properly tested.
Hook Types
WireGuard Easy supports four types of hooks:PreUp
Executed before the WireGuard interface is brought upUse for: Initial setup, firewall rules preparation
PostUp
Executed after the WireGuard interface is brought upUse for: Firewall rules, NAT configuration, routing
PreDown
Executed before the WireGuard interface is brought downUse for: Cleanup preparation, logging
PostDown
Executed after the WireGuard interface is brought downUse for: Firewall cleanup, connection notifications
Configuration Levels
Hooks can be configured at two levels:Server-Level Hooks
Apply to the entire WireGuard interface. Configure via:- Web UI: Admin → Hooks
- API:
POST /api/admin/hooks
Client-Level Hooks
Apply to individual client configurations. Configure via:- Web UI: Edit Client → Advanced Settings
- API:
POST /api/client/{clientId}(include hook parameters)
.conf file and run on the client device.
Server-Level Hook Configuration
Via Web Interface
- Navigate to Admin → Hooks
- Enter commands for each hook type
- Click Save
- Restart the WireGuard interface to apply changes
Via API
Template Variables
Hooks support template variables that are replaced at runtime:| Variable | Description | Example |
|---|---|---|
{{port}} | WireGuard listening port | 51820 |
{{device}} | Network device name | wg0 |
{{interface}} | Interface name (alias for device) | wg0 |
{{ipv4Cidr}} | IPv4 CIDR range | 10.8.0.0/24 |
{{ipv6Cidr}} | IPv6 CIDR range | fd42:42:42::/64 |
Example Usage
Common Use Cases
Firewall Rules with iptables
Basic forwarding and NAT: PostUp:Firewall Rules with nftables
Modern nftables-based firewall: PostUp:Routed Setup (No NAT)
For routed configurations where clients keep their real IPs: PostUp:Split Tunneling
Route only specific traffic through VPN: PostUp (Client-side):DNS Override
Set custom DNS servers: PostUp (Client-side):Connection Logging
Log when the interface comes up/down: PostUp:Webhook Notifications
Notify external services: PostUp:Port Forwarding
Forward specific ports to WireGuard clients: PostUp:Client-Level Hooks
Configuration via API
Client Configuration Example
The generated.conf file will include:
Advanced Examples
Dynamic DNS Update
PostUp:Traffic Accounting
PostUp:QoS (Quality of Service)
Prioritize certain traffic: PostUp:Conditional Routing
Route based on source: PostUp:Database Storage
Hooks are stored in the SQLite database:Server Hooks Table
Client Hooks Storage
Client hooks are stored in theclient table as individual columns:
pre_uppost_uppre_downpost_down
Security Considerations
Command Injection Prevention
Command Injection Prevention
- Never use user input directly in hooks without validation
- Avoid using hooks that execute based on client-provided data
- Use template variables instead of string concatenation
- Validate all external data before use in commands
Privilege Management
Privilege Management
- Hooks run with the same privileges as WireGuard Easy
- Minimize required permissions using principle of least privilege
- Consider running sensitive operations through sudo with specific rules
- Audit hook commands regularly
Error Handling
Error Handling
- Test hooks thoroughly before deployment
- Failed hooks may prevent interface startup
- Use
|| trueto allow hook failures:command || true - Log hook output for debugging
Client-Side Hooks
Client-Side Hooks
- Clearly document what client hooks do
- Avoid distributing configs with destructive hooks
- Consider security implications of hooks running on client devices
- Educate users about hook functionality
Troubleshooting
Hooks Not Executing
-
Check WireGuard Easy logs:
-
Verify hook syntax:
-
Check permissions:
Interface Fails to Start
- A failed PostUp hook can prevent the interface from starting
- Check logs for error messages
- Temporarily remove hooks to isolate the issue
- Test hooks individually
Template Variables Not Replaced
- Ensure you’re using double curly braces:
{{variable}} - Variables are case-sensitive
- Only supported variables are replaced (see table above)
Client Hooks Not Working
- Client hooks only run on the client device
- Client must have appropriate permissions (e.g., root for iptables)
- Check client-side WireGuard logs
- Some mobile WireGuard apps don’t support hooks
Best Practices
Use Idempotent Commands
Ensure hooks can be run multiple times without causing issues. Use
-D to delete before -A to add rules.Related Documentation
- API Reference - Programmatically manage hooks
- Security Best Practices - Secure your WireGuard deployment
- Environment Variables - Configure WireGuard Easy
- WireGuard Documentation - Official WireGuard docs