Skip to content
Snippets Groups Projects
addpeer.php 928 B
Newer Older
root's avatar
root committed
<?php

${basename(__FILE__, '.php')} = function () {
    if (
        $this->get_request_method() == "POST"
        and $this->isAuthenticated()
        and isset($this->_request['peer'])
        and !empty($this->_request['peer'])
    ) {
        try {
            $wg = new wireguard('wg0');
            $ip_net = new ipNetwork($wg->getCIDR(),$wg->device);
root's avatar
root committed
            $data = [
                "data" => $wg->addPeer($this->_request['peer'],$ip_net->getNextIp())
root's avatar
root committed
            ];
            $data = $this->json($data);
            $this->response($data, 200);
        } catch (Exception $e) {
            $data = [
                "error" => $e->getMessage()
            ];
            $data = $this->json($data);
            $this->response($data, 403);
        }
    } else {
        $data = [
            "error" => "Bad request"
        ];
        $data = $this->json($data);
        $this->response($data, 400);
    }
};