56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
class WGRest
|
|
{
|
|
/* @var \GuzzleHttp\Client $client */
|
|
private $client;
|
|
|
|
private $token;
|
|
|
|
public function __construct(Client $client, string $token)
|
|
{
|
|
$this->client = $client;
|
|
$this->token = $token;
|
|
}
|
|
|
|
public function createDevice(string $name, int $listen_port, string $private_key, string $network) {
|
|
$data = [];
|
|
$data["name"] = $name;
|
|
$data["listen_port"] = $listen_port;
|
|
$data["private_key"] = $private_key;
|
|
$data["network"] = $network;
|
|
|
|
$res = $this->client->request("POST", "/devices/", ["json" => $data, "headers" => ["Token" => $this->token]]);
|
|
}
|
|
|
|
public function listDevices() {
|
|
$res = $this->client->request("GET", "/devices/", ["headers" => ["Token" => $this->token]]);
|
|
|
|
return json_decode($res->getBody(), true);
|
|
}
|
|
|
|
public function createPeer(string $vpn_name, string $public_key, string $preshared_key, array $allowed_ips) {
|
|
$data = [];
|
|
$data["public_key"] = $public_key;
|
|
$data["allowed_ips"] = $allowed_ips;
|
|
$data["preshared_key"] = $preshared_key;
|
|
|
|
$res = $this->client->request("POST", "/devices/".$vpn_name."/peers", ["json" => $data, "headers" => ["Token" => $this->token]]);
|
|
}
|
|
|
|
public function getPeers(string $vpn_name) {
|
|
$res = $this->client->request("GET", "/devices/".$vpn_name."/peers", ["headers" => ["Token" => $this->token]]);
|
|
|
|
return json_decode($res->getBody(), true);
|
|
}
|
|
|
|
public function deletePeer(string $vpn_name, string $peer_id) {
|
|
$res = $this->client->request("DELETE", "/devices/".$vpn_name."/peers/".$peer_id, ["headers" => ["Token" => $this->token]]);
|
|
}
|
|
|
|
|
|
|
|
}
|