Skip to content

Commit

Permalink
added capability to add or remove clients
Browse files Browse the repository at this point in the history
  • Loading branch information
parthibd committed Mar 18, 2020
1 parent 2087774 commit b6804e5
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* PhpStorm Meta file, to provide autocomplete information for PhpStorm
* Generated on 2020-03-17 08:47:45.
* Generated on 2020-03-18 18:43:38.
*
* @author Barry vd. Heuvel <[email protected]>
* @see https://github.com/barryvdh/laravel-ide-helper
Expand Down
2 changes: 1 addition & 1 deletion _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* A helper file for Laravel 5, to provide autocomplete information to your IDE
* Generated for Laravel 5.8.37 on 2020-03-17 08:47:41.
* Generated for Laravel 5.8.37 on 2020-03-18 18:43:34.
*
* This file should not be included in your code, only analyzed by your IDE!
*
Expand Down
14 changes: 13 additions & 1 deletion app/AvailableIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@

use Illuminate\Database\Eloquent\Model;

/**
* App\AvailableIp
*
* @property int $id
* @property string $ip
* @method static \Illuminate\Database\Eloquent\Builder|\App\AvailableIp newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\AvailableIp newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\AvailableIp query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\AvailableIp whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\AvailableIp whereIp($value)
* @mixin \Eloquent
*/
class AvailableIp extends Model
{
//
public $timestamps = false;
}
53 changes: 52 additions & 1 deletion app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,62 @@

namespace App\Http\Controllers;

use App\AvailableIp;
use App\Util\WireGuardWrapper;
use Composer\Config;
use Illuminate\Http\Request;

class ClientController extends Controller
{
public function addClient(){
public function addClient()
{
$wireGuardServerPublicIp = config('wireguard.WIREGUARD_PUBLIC_IP');
$wireGuardServerPublicKey = config('wireguard.WIREGUARD_PUBLIC_KEY');
$listenPort = config('wireguard.WIREGUARD_LISTEN_PORT');
$keyPair = WireGuardWrapper::getInstance()->generateKeyPair();
$ip = AvailableIp::where('is_assigned', false)->first();

if (!$ip) {
return response()->json(["success" => false, "status" => "error", "message" => "IP range exhausted."]);
}
$ipToAssign = "$ip->ip/32";

WireGuardWrapper::getInstance()->addClientToServer($keyPair[0], $ipToAssign);

$config = <<<EOD
[Interface]
PrivateKey = $keyPair[1]
Address = $ipToAssign
DNS = 8.8.8.8
[Peer]
PublicKey = $wireGuardServerPublicKey
Endpoint = $wireGuardServerPublicIp:$listenPort
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOD;
$ip->public_key = $keyPair[0];
$ip->endpoint = $ipToAssign;
$ip->is_assigned = true;
$ip->save();
return response()->json(["config" => $config, "key_pair" => $keyPair]);
}

public function removeClient(Request $request)
{
$publicKey = $request->query("public_key");
$ip = AvailableIp::where('public_key', $publicKey)->first();
if ($ip) {

WireGuardWrapper::getInstance()->removeClientFromServer($publicKey);

$ip->public_key = null;
$ip->endpoint = null;
$ip->is_assigned = false;
$ip->save();
return response()->json(["success" => true, "status" => "ok", "message" => "Client removed from server"]);
} else {
return response()->json(["success" => false, "status" => "error", "message" => "No such peer exists."]);
}
}
}
1 change: 1 addition & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUsername($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereWgInterfaceIp($value)
* @mixin \Eloquent
* @property-read \App\UserRole $role
*/
class User extends Authenticatable
{
Expand Down
14 changes: 12 additions & 2 deletions app/Util/WireGuardWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class WireGuardWrapper

private function __construct()
{
$this->ssh = new SSH2('192.168.2.5');
$this->ssh->login("parthib", "parthib");
$this->ssh = new SSH2(config('wireguard.WIREGUARD_PUBLIC_IP'));
$this->ssh->login(config('wireguard.WIREGUARD_USERNAME'), config('wireguard.WIREGUARD_PASSWORD'));
}

public static function getInstance()
Expand All @@ -40,6 +40,16 @@ public function generateKeyPair()
return [trim($publicKey), trim($privateKey)];
}

public function addClientToServer($publicKey, $ip)
{
$this->executeCommand("sudo wg set wg0 peer $publicKey allowed-ips $ip");
}

public function removeClientFromServer($publicKey)
{
$this->executeCommand("sudo wg set wg0 peer $publicKey remove");
}

public function showWireGuardStatus()
{
return $this->executeCommand("sudo wg");
Expand Down
4 changes: 3 additions & 1 deletion config/wireguard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
'WIREGUARD_PUBLIC_KEY' => env('WIREGUARD_PUBLIC_KEY'),
'WIREGUARD_PUBLIC_IP' => env('WIREGUARD_PUBLIC_IP'),
'WIREGUARD_LISTEN_PORT' => env('WIREGUARD_LISTEN_PORT'),
'WIREGUARD_INTERFACE_IP' => env('WIREGUARD_INTERFACE_IP')
'WIREGUARD_INTERFACE_IP' => env('WIREGUARD_INTERFACE_IP'),
'WIREGUARD_USERNAME' => env('WIREGUARD_USERNAME'),
'WIREGUARD_PASSWORD' => env('WIREGUARD_PASSWORD')
];
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function up()
Schema::create('available_ips', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("ip");
$table->string("endpoint")->nullable();
$table->string("public_key")->nullable();
$table->boolean("is_assigned")->nullable();
});
}

Expand Down
4 changes: 3 additions & 1 deletion database/seeds/AvailableIpsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ private function getIpsInRange($cidrRange)
$arr = [];
$maxPossibleHostCount = ip2long("255.255.255.255") - ip2long($networkMask);
for ($i = 1; $i < $maxPossibleHostCount; $i++) {
$arr[] = ["ip" => long2ip((ip2long($cidr[0]) & ip2long($networkMask)) | ip2long(long2ip($i)))];
$arr[] = [
"ip" => long2ip((ip2long($cidr[0]) & ip2long($networkMask)) | ip2long(long2ip($i))),
"is_assigned" => false];
}
$serverIp = explode("/", config('wireguard.WIREGUARD_INTERFACE_IP'))[0];
$index = array_search($serverIp, $arr);
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@

Route::post("/login", "LoginController@login");
Route::put("/client", "ClientController@addClient");
Route::delete("/client", "ClientController@removeClient");

0 comments on commit b6804e5

Please sign in to comment.