77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Peer;
|
|
use App\Models\User;
|
|
use App\Models\VPN;
|
|
use App\Services\WGRest;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\View;
|
|
use IPTools\Network;
|
|
|
|
class VPNController extends BaseController
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
public function overview(Request $request) {
|
|
$id = $request->input("id");
|
|
$vpn = VPN::query()->where("id", "=", $id)->first();
|
|
session()->put("current_vpn", $vpn->id);
|
|
View::share("currentVPNName", $vpn->name);
|
|
|
|
//Calculate Free IPS
|
|
$network = Network::parse($vpn->network);
|
|
$usedIPs = Peer::query()->where("vpn_id", "=", $vpn->id)->count();
|
|
|
|
$percent = (100 / $network->count()) * $usedIPs ;
|
|
|
|
return view("vpn.overview", ["currentVPN"=>$vpn, "percent" => $percent ]);
|
|
}
|
|
|
|
public function accessView() {
|
|
$vpn_id = session()->get("current_vpn");
|
|
$users = User::query()->get();
|
|
return view("vpn.access", ["users" => $users, "vpn_id" => $vpn_id]);
|
|
}
|
|
|
|
public function setAccess(Request $request) {
|
|
$vpn_id = session()->get("current_vpn");
|
|
$user = User::query()->where("id", "=", $request->input("id"))->firstOrFail();
|
|
$user->setVPNRole($vpn_id, $request->input("access"));
|
|
|
|
return redirect("/vpn/access");
|
|
}
|
|
|
|
public function newView() {
|
|
return view("vpn.new");
|
|
}
|
|
|
|
public function new(Request $request) {
|
|
$vpn = new VPN();
|
|
$vpn->name = $request->input("name");
|
|
$vpn->displayName = $request->input("display_name");
|
|
$vpn->listen_port = $request->input("port");
|
|
$vpn->network = $request->input("network");
|
|
|
|
$privateKey = $request->input("private_key");
|
|
if(empty($privateKey)) {
|
|
$privateKey = exec('wg genkey');
|
|
}
|
|
$vpn->private_key = $privateKey;
|
|
$vpn->public_key = null;
|
|
$vpn->saveOrFail();
|
|
}
|
|
|
|
public function sendToRest(WGRest $WGRest) {
|
|
$vpn_id = session()->get("current_vpn");
|
|
$vpn = VPN::query()->where("id", "=", $vpn_id)->firstOrFail();
|
|
|
|
$WGRest->createDevice($vpn->name, $vpn->listen_port, $vpn->private_key, $vpn->network);
|
|
return redirect("/vpn?id=".$vpn->id);
|
|
}
|
|
}
|