<?php

class wireguard
{
    public $db;
    public $device;

    public function __construct($device)
    {
        $this->device = $device;
        $this->db = database::getconnection();
    }

    public function getCIDR()
    {
        $cmd = "sudo cat /etc/wireguard/$this->device.conf | head -n 2";
        $output = shell_exec($cmd);
        $lines = explode("\n", $output);
        if (isset($lines[1])) {
            $line = explode("=", $lines[1]);
            return isset($line[1]) ? trim($line[1]) : null;
        }

        return null;
    }


    public function addpeer($public, $email)
    {
        $ip_net = new ipNetwork($this->getCIDR(),$this->device);
        $next_ip = $ip_net->getNextIp();
        $result = null;
        $cmd = "sudo wg set $this->device peer $public allowed-ips $next_ip/32";
        system($cmd,$result);
        if($result == 0){
            $res = $ip_net->allocateIp($next_ip,$email,$public);
            return $res;
        }else{
            return false;
        }
    }

    public function removepeer($public)
    {
        $cmd = "sudo wg set $this->device peer $public remove";
        $result = 0;
        trim(system($cmd, $result));
        if($result == 0){
            $remove = new ipNetwork($this->getCIDR(),$this->device);
            $remove->deallocate($public);
            return true;
        }
        else{
            return false;
        }
    }

    public function getPeers()
    {
        $cmd = "sudo wg show wg0";
        $output = trim(shell_exec($cmd));
        $result = explode(PHP_EOL, $output);
        $interface_out = array_slice($result, 0, 4);
        $peer_out = array_slice($result, 5);
        $peer_count = -1;
        $peer = [];
        $interface = [];

        foreach ($interface_out as $value) {
            $value = trim($value);
            $data = explode(": ", $value);
            $interface[trim($data[0])] = trim($data[1]);
        }

        foreach ($peer_out as $value) {
            $value = trim($value);
            if (!empty($value)) {
                if (startsWith($value, "peer:")) {
                    $peer_count++;
                }
                $data = explode(": ", $value);
                $peer[$peer_count][$data[0]] = $data[1];
            }
        }

        return [
            "interface" => $interface,
            "peers" => $peer
        ];
    }

    public function getPeer($public)
    {
        $cmd = "sudo wg show $this->device | grep -A4 $public";
        $output = trim(shell_exec($cmd));
        $result = explode(PHP_EOL, $output);
        $peer = [];
        $peer_count = 0;
        foreach ($result as $value) {
            if (!empty($value)) {
                $value = trim($value);
                if (startsWith($value, "peer:")) {
                    $peer_count++;
                    if ($peer_count > 1) {
                        break;
                    }
                }
                $data = explode(": ", $value);
                $peer[$data[0]] = $data[1];
            }
        }
        return $peer;
    }
}