dimkinthepro/wireguard-bundle
Laravel bundle for managing WireGuard configuration and operations in PHP. Provides integration utilities to generate and handle VPN keys, peers, and configs, helping you automate WireGuard setup and provisioning from your Laravel apps.
For a Laravel developer integrating dimkinthepro/wireguard-bundle, start with these minimal steps:
Installation:
composer require dimkinthepro/wireguard-bundle
Note: Since this is a Symfony bundle, ensure your Laravel project uses Symfony components (e.g., symfony/process, symfony/filesystem) if not already present.
Enable the Bundle:
Add the bundle to config/bundles.php (Symfony-style) or register its service provider in config/app.php under providers:
DimkinThePro\WireguardBundle\WireguardBundle::class,
Publish Configuration:
php artisan vendor:publish --provider="DimkinThePro\WireguardBundle\WireguardBundle" --tag="config"
This generates a config/wireguard.php file. Review defaults (e.g., server_path, port, keys_dir).
First Use Case: Use the bundle’s facade (if provided) or service container binding to interact with WireGuard. Example:
use DimkinThePro\WireguardBundle\Facades\Wireguard;
// Generate a new key pair
$keyPair = Wireguard::generateKeyPair();
Fallback: If no facade exists, access the service directly:
$keyPair = app('wireguard')->generateKeyPair();
Service Container Integration:
The bundle likely binds WireGuard services (e.g., wireguard.client, wireguard.server) to the container. Extend or replace bindings in AppServiceProvider:
public function register()
{
$this
->app->extend('wireguard', function ($app) {
return new CustomWireguardManager($app['wireguard.config']);
});
}
Command-Line Interaction:
The bundle may expose Artisan commands (e.g., wireguard:generate-key). Extend them by publishing and overriding:
php artisan vendor:publish --provider="DimkinThePro\WireguardBundle\WireguardBundle" --tag="commands"
Then modify app/Console/Commands/WireguardGenerateKey.php.
Configuration-Driven:
Use config('wireguard.key') to access settings. Override defaults in config/wireguard.php:
'keys_dir' => storage_path('app/wireguard/keys'),
'server_port' => env('WIREGUARD_PORT', 51820),
Event-Driven (If Supported):
Listen for WireGuard events (e.g., WireguardKeyGenerated) in EventServiceProvider:
protected $listen = [
'WireguardKeyGenerated' => [
\App\Listeners\LogWireguardKey::class,
],
];
File System Handling:
The bundle likely uses storage_path() for key storage. Customize paths in config or bind a Filesystem instance:
$this->app->bind('wireguard.filesystem', function () {
return Storage::disk('wireguard');
});
Process Management:
If the bundle spawns WireGuard processes, wrap them in Laravel’s Process component for better control:
use Symfony\Component\Process\Process;
$process = new Process(['wg', 'syncconf', $configPath]);
$process->run();
Blade Integration (If Applicable): Add a directive to display WireGuard status:
Blade::directive('wireguardStatus', function ($expr) {
return "<?php echo app('wireguard')->status(); ?>";
});
Usage in Blade:
@wireguardStatus
Testing: Mock the WireGuard service in tests:
$this->app->instance('wireguard', Mockery::mock('overload:DimkinThePro\WireguardBundle\WireguardManager'));
Symfony vs. Laravel Incompatibilities:
Process, Filesystem, or Config components. If missing, install them:
composer require symfony/process symfony/filesystem symfony/config
symfony/console conflicts by using Laravel’s Artisan instead.Permission Issues: WireGuard requires root/sudo access. Handle errors gracefully:
try {
Wireguard::generateKeyPair();
} catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
Log::error('WireGuard requires elevated privileges', ['error' => $e->getMessage()]);
abort(500, 'Permission denied');
}
Key Management:
WireguardManager to encrypt keys:
public function generateKeyPair()
{
$keyPair = parent::generateKeyPair();
$keyPair['private_key'] = encrypt($keyPair['private_key']);
return $keyPair;
}
Configuration Overrides:
$this->app->singleton('wireguard.config', function () {
return [
'server_path' => '/usr/bin/wg',
'keys_dir' => storage_path('app/wireguard'),
];
});
Process Output: Log WireGuard process output for debugging:
$process = new Process(['wg', 'show']);
$process->run();
Log::debug('WireGuard output', ['output' => $process->getOutput()]);
Service Container Inspection: Dump the container to verify bindings:
php artisan container:dump | grep wireguard
Facade Overrides: Temporarily replace the facade for testing:
Wireguard::swap(new class {
public function generateKeyPair() { return ['public' => 'test', 'private' => 'test']; }
});
Custom Key Generation:
Extend the WireguardManager to add metadata to keys:
namespace App\Services;
use DimkinThePro\WireguardBundle\WireguardManager;
class CustomWireguardManager extends WireguardManager
{
public function generateKeyPair()
{
$keyPair = parent::generateKeyPair();
$keyPair['metadata'] = ['created_at' => now()];
return $keyPair;
}
}
Bind it in AppServiceProvider:
$this->app->bind('wireguard', function () {
return new CustomWireguardManager();
});
Database Integration: Store WireGuard keys in a database by extending the manager:
public function saveKeyPair($keyPair)
{
$model = new WireguardKey($keyPair);
$model->save();
return $model;
}
API Endpoints: Create a controller to expose WireGuard functionality:
Route::middleware('auth')->post('/wireguard/key', function () {
return response()->json(app('wireguard')->generateKeyPair());
});
Initial Release Caveats:
Wireguard::syncConfig()) may change.Laravel-Specific Adjustments:
Console/Application with Laravel’s Artisan if commands are involved.Storage facade instead of Symfony’s Filesystem for consistency:
Storage::disk('wireguard')->put('key.pub', $publicKey);
Environment Variables:
Load config from .env:
'port' => env('WIREGUARD_PORT', 51820),
Add to .env:
WIREGUARD_PORT=51821
How can I help you explore Laravel packages today?