Skip to content

Commit e2061c4

Browse files
committed
v1.0: Initial Release
1 parent abc07f9 commit e2061c4

37 files changed

+3369
-1
lines changed

.env.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
API_URL="dns.domain.tld:5380"
2+
USERNAME="api_user"
3+
PASSWORD="your-password"
4+
TOKEN="mytoken"
5+
INCLUDE_INFO=false
6+
USE_POST=true
7+
USE_HTTPS=true

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1-
# technitium-dnsserver-php-api
1+
# technitium-dnsserver-php-api
2+
3+
This API client is intended to be used with Technitiums DNS Server
4+
5+
## Installation
6+
7+
Via Git: `git clone https://github.com/ente/technitium-dnsserver-php-api.git`
8+
Then: `require_once "/path/to/API.dnsserver.ente.php";` & `use Technitium\DNSServer\API\API;`
9+
10+
## Configuration
11+
12+
### .env
13+
14+
- `API_URL`: The API URL of your Technitium DNS Server (with port), e.g. `localhost:5380`, `192.168.1.2:5380` or `server.domain.tld:5380`
15+
- `USERNAME`: The username for the user account. (You should create a dedicated one)
16+
- `PASSWORD`: The password for the user account.
17+
- `INCLUDE_INFO`: Returns basic information that might be relevant for the queried request.
18+
- `TOKEN`: Your API token, if already existent. (`[Your Username]` > `[Create API Token]`)
19+
- `USE_POST`: Specify if you want to access the API via POST (`true`) instead of GET (`false`) in default.
20+
- `USE_HTTPS`: Enable (`true`) HTTPS for the API connection.
21+
22+
## General Usage
23+
24+
```php
25+
require_once "/vendor/autoload.php";
26+
use Technitium\DNSServer\API;
27+
28+
$api = new API();
29+
30+
// Get all zones
31+
$zones = $api->zones()->get();
32+
// Get all zone records
33+
$records = $api->zones->records()->get("example.com");
34+
35+
// Install an app
36+
37+
$sampleApp = $api->apps()->listStoreApps()["storeApps"][0];
38+
if($api->apps->install($sampleApp["name"])) {
39+
echo "App installed successfully!";
40+
}
41+
42+
// OR
43+
44+
$sampleApp = $api->apps()->listStoreApps()["storeApps"][0];
45+
if($api->apps->downloadAndInstall($sampleApp["name"], $sampleApp["url"])) {
46+
echo "App installed successfully!";
47+
}
48+
49+
```
50+
51+
## DDNS
52+
53+
You can use the `DDNS.Helper.API.dnsserver.ente.php` class to configure records to point to the current IP address.
54+
55+
```php
56+
<?php
57+
58+
require_once "/vendor/autoload.php";
59+
use Technitium\DNSServer\API;
60+
use Technitium\DNSServer\API\Helper\DDNS;
61+
62+
$path_to_configJSON = "/my/config.json";
63+
$ddns = new DDNS(new API());
64+
$ddns->updateRecords($path_to_configJSON);
65+
66+
// OR
67+
68+
$ddns_result = new DDNS(new API(), file_get_contents($path_to_configJSON)); // starts automatically updating the records
69+
70+
// OR
71+
$api = new API();
72+
$ddns_result = $api->ddns()->updateRecords($path_to_configJSON);
73+
74+
```
75+
76+
Your DDNS configuration file should look like this:
77+
78+
```json
79+
{
80+
"domanin": "example.com",
81+
"records": [
82+
"sub.example.com"
83+
]
84+
}
85+
```
86+
87+
You can also set up multiple environments by using different configuration files:
88+
89+
```php
90+
<?php
91+
92+
require_once "/vendor/autoload.php";
93+
use Technitium\DNSServer\API;
94+
use Technitium\DNSServer\API\Helper\DDNS;
95+
96+
DDNS(new API(), file_get_contents("/my/config.json"));
97+
DDNS(new API(), file_get_contents("/my/config2.json"));
98+
DDNS(new API("/my/.env"), file_get_contents("/my/config3.json"));
99+
100+
```
101+
102+
## Changes
103+
104+
### v1.0: Initial Release
105+
106+
- initial release supporting each endpoint

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "ente/technitium-dnsserver-php-api",
3+
"type": "library",
4+
"license": "GNU General Public License 3.0",
5+
"version": "1.0",
6+
"authors": [
7+
{
8+
"name": "Ente",
9+
"homepage": "https://openducks.org",
10+
"email": "[email protected]"
11+
}
12+
],
13+
14+
"require": {
15+
"php": ">=8.0",
16+
"vlucas/phpdotenv": "5.6.1"
17+
},
18+
"autoload": {
19+
"files": [
20+
"src/API.dnsserver.ente.php"
21+
]
22+
}
23+
}

src/API.dnsserver.ente.php

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
namespace Technitium\DNSServer\API;
3+
use Technitium\DNSServer\API\admin;
4+
use Technitium\DNSServer\API\allowed;
5+
use Technitium\DNSServer\API\apps;
6+
use Technitium\DNSServer\API\blocked;
7+
use Technitium\DNSServer\API\cache;
8+
use Technitium\DNSServer\API\dashboard;
9+
use Technitium\DNSServer\API\dhcp;
10+
use Technitium\DNSServer\API\dnsClient;
11+
use Technitium\DNSServer\API\settings;
12+
use Technitium\DNSServer\API\users;
13+
use Technitium\DNSServer\API\zones;
14+
use Technitium\DNSServer\API\Helper\DDNS;
15+
use Technitium\DNSServer\API\Helper\Log;
16+
use \Dotenv\Dotenv;
17+
18+
class API {
19+
20+
private $protocol;
21+
22+
private $admin;
23+
private $allowed;
24+
private $apps;
25+
private $blocked;
26+
private $cache;
27+
private $dashboard;
28+
private $dhcp;
29+
private $dnsClient;
30+
private $settings;
31+
private $users;
32+
private $zones;
33+
private $ddns;
34+
35+
private $log;
36+
37+
public function __construct($confPath = null){
38+
$this->loader();
39+
$this->loadConf($confPath = null);
40+
$this->setProtocol();
41+
}
42+
43+
public function setProtocol(){
44+
if($_ENV["USE_HTTPS"] == "true"){
45+
$this->protocol = "https";
46+
} else {
47+
$this->protocol = "http";
48+
}
49+
}
50+
51+
public function loadConf($path = null){
52+
if($path != null){
53+
$env = \Dotenv\Dotenv::createImmutable($path);
54+
$env->safeLoad();
55+
} else {
56+
$env = \Dotenv\Dotenv::createImmutable(dirname(__DIR__, 1));
57+
$env->safeLoad();
58+
}
59+
}
60+
61+
public function loader(){
62+
require_once __DIR__ . "/endpoints/admin/Admin.php";
63+
require_once __DIR__ . "/endpoints/allowed/Allowed.php";
64+
require_once __DIR__ . "/endpoints/apps/Apps.php";
65+
require_once __DIR__ . "/endpoints/blocked/Blocked.php";
66+
require_once __DIR__ . "/endpoints/cache/Cache.php";
67+
require_once __DIR__ . "/endpoints/dashboard/Dashboard.php";
68+
require_once __DIR__ . "/endpoints/dhcp/DHCP.php";
69+
require_once __DIR__ . "/endpoints/dnsClient/DnsClient.php";
70+
require_once __DIR__ . "/endpoints/settings/Settings.php";
71+
require_once __DIR__ . "/endpoints/users/Users.php";
72+
require_once __DIR__ . "/endpoints/zones/Zones.php";
73+
74+
require_once __DIR__ . "/helper/DDNS.Helper.API.dnsserver.ente.php";
75+
require_once __DIR__ . "/helper/Log.Helper.API.dnsserver.ente.php";
76+
}
77+
78+
public function sendCall($data, $endpoint, $method = "POST"){
79+
$c = curl_init();
80+
$endpoint = $this->prepareEndpoint($endpoint);
81+
if($_ENV["USE_POST"]){
82+
$method = "POST";
83+
}
84+
switch($method){
85+
case "POST":
86+
curl_setopt($c, CURLOPT_URL, $endpoint . $this->appendAuth());
87+
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
88+
curl_setopt($c, CURLOPT_POST, true);
89+
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
90+
break;
91+
case "GET":
92+
$data = http_build_query($data);
93+
curl_setopt($c, CURLOPT_URL, $endpoint . "?" . $data . $this->appendAuth());
94+
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
95+
break;
96+
}
97+
98+
$response = curl_exec($c);
99+
if(!$response){
100+
return curl_error($c);
101+
}
102+
curl_close($c);
103+
if($this->checkResponse($response)){
104+
return json_decode($response, true);
105+
}
106+
return [
107+
"error" => "An error occurred",
108+
];
109+
}
110+
111+
public function appendAuth($m = "POST"){
112+
if($_ENV["TOKEN"] != ""){
113+
switch($m){
114+
case "POST":
115+
return "?token=" . $_ENV["TOKEN"] . "&user=" . $_ENV["USERNAME"] . "&password=" . $_ENV["PASSWORD"];
116+
case "GET":
117+
return "&token=" . $_ENV["TOKEN"] . "&user=" . $_ENV["USERNAME"] . "&password=" . $_ENV["PASSWORD"];
118+
}
119+
}
120+
}
121+
122+
public function checkResponse($response){
123+
if(is_null($response)){
124+
return false;
125+
} else {
126+
$re = json_decode($response, true)["status"];
127+
return $re != null || $re !== "error" || $re !== "invalid-token";
128+
}
129+
}
130+
131+
public function prepareEndpoint($endpoint){
132+
$endpoints = json_decode(file_get_contents(__DIR__ . "/helper/endpoints.json"));
133+
134+
if(in_array($endpoint, $endpoints)){
135+
return $this->protocol . "://" . $_ENV["API_URL"] . "/api/" . $endpoint;
136+
} else {
137+
return false;
138+
}
139+
}
140+
141+
142+
public function admin(): admin {
143+
if(!$this->admin) $this->admin = new admin($this);
144+
return $this->admin;
145+
}
146+
147+
148+
public function allowed(): allowed {
149+
if(!$this->allowed) $this->allowed = new allowed($this);
150+
return $this->allowed;
151+
}
152+
153+
public function apps(): apps {
154+
if(!$this->apps) $this->apps = new apps($this);
155+
return $this->apps;
156+
}
157+
158+
public function blocked(): blocked {
159+
if(!$this->blocked) $this->blocked = new blocked($this);
160+
return $this->blocked;
161+
}
162+
163+
public function cache(): cache {
164+
if(!$this->cache) $this->cache = new cache($this);
165+
return $this->cache;
166+
}
167+
168+
169+
public function dashboard(): dashboard {
170+
if(!$this->dashboard) $this->dashboard = new dashboard($this);
171+
return $this->dashboard;
172+
}
173+
174+
public function dhcp(): dhcp {
175+
if(!$this->dhcp) $this->dhcp = new dhcp($this);
176+
return $this->dhcp;
177+
}
178+
179+
public function dnsClient(): dnsClient {
180+
if(!$this->dnsClient) $this->dnsClient = new dnsClient($this);
181+
return $this->dnsClient;
182+
}
183+
184+
public function settings(): settings {
185+
if(!$this->settings) $this->settings = new settings($this);
186+
return $this->settings;
187+
}
188+
189+
public function users(): users {
190+
if(!$this->users) $this->users = new users($this);
191+
return $this->users;
192+
}
193+
194+
public function zones(): zones {
195+
if(!$this->zones) $this->zones = new zones($this);
196+
return $this->zones;
197+
}
198+
199+
public function ddns(): DDNS {
200+
if(!$this->ddns) $this->ddns = new DDNS($this);
201+
return $this->ddns;
202+
}
203+
204+
public function log(): Log {
205+
if(!$this->log) $this->log = new Log($this);
206+
return $this->log;
207+
}
208+
}

0 commit comments

Comments
 (0)