Skip to content

Instantly share code, notes, and snippets.

@kaitwalla
Created February 20, 2024 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaitwalla/3693e223fdbe0a8a2d49f189443879f4 to your computer and use it in GitHub Desktop.
Save kaitwalla/3693e223fdbe0a8a2d49f189443879f4 to your computer and use it in GitHub Desktop.
A PHP script for sending URLs to NextDNS deny/allow lists
<?php
$requestBody = $_REQUEST;
$token = 'INSERT-YOUR-VALUES-HERE';
$apiKey = 'INSERT-YOUR-VALUES-HERE';
$profileId = 'INSERT-YOUR-VALUES-HERE';
$requred = ['token', 'action', 'domain'];
foreach ($requred as $key) {
if (!isset($requestBody[$key])) {
send(json_encode(array('error' => $key . ' is required')));
}
}
if ($requestBody['token'] != $token) {
send(json_encode(array('error' => 'Invalid token')));
}
$action = $requestBody['action'];
switch ($action) {
case 'block':
send(make_request('deny', $requestBody['domain']));
break;
case 'allow':
send(make_request('allow', $requestBody['domain']));
break;
}
function send($response) {
header('Content-Type: application/json');
echo $response;
exit();
}
function make_request(string $allowOrDeny, string $domain) {
global $apiKey, $profileId;
$curl = curl_init();
$allowOrDeny .= 'list';
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.nextdns.io/profiles/$profileId/$allowOrDeny",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t\"id\": \"$domain\",\n\t\"active\": true\n}",
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-Api-Key: $apiKey"
]
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment