Skip to content

Commit

Permalink
Merge pull request #90 from CosmicDNS/master
Browse files Browse the repository at this point in the history
PhpDnsServer phar and cli phar
  • Loading branch information
samuelwilliams authored Feb 21, 2020
2 parents 8bca2ee + 5f5a581 commit 47f9e36
Show file tree
Hide file tree
Showing 21 changed files with 771 additions and 4 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ Unit tests using PHPUnit are provided. A simple script is located in the root.
* run `composer install` to install PHPUnit and dependencies
* run `vendor/bin/phpunit` from the root to run the tests

## Building .phar
* run `composer run-script build-server` to build the phpdnsserver.phar file, outputs in the bin folder.
* run `composer run-script build-console` to build the phpdnscli.phar file, outputs in the bin folder.
* run `composer run-script build-installer` to build the installer. Windows support for the installer is currently limited.

## Running the .phar files
To run the new .phar files, download them from the release and move them to the desired folder.
* `phpdnsserver.phar` to run the phpdnsserver, uses the new filesystem by default
* `phpdnscli.phar` to run cli commands
* `phpdnsinstaller.phar` as root to create required folders and default config.

## Supported command line switches
* `--bind:b` - bind to a specific ip. Uses `0.0.0.0` by default
* `--port:p` - bind to a specific port. Uses port `53` by default
* `--config:c` - specify the config file. Uses `phpdns.json` on windows
and `/etc/phpdns.json` on unix systems by default
* `--storage:s` - specify the path to the storage for zones, and logs. Uses `/etc/phpdnsserver` on unix,
and current working directory on windows.

## Supported Record Types

* A
Expand Down
13 changes: 13 additions & 0 deletions bin/PhpDnsConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require '../vendor/autoload.php';

// Create the eventDispatcher and add the event subscribers
$eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$eventDispatcher->addSubscriber(new \yswery\DNS\Event\Subscriber\EchoLogger());

// Create a new instance of Server class
$server = new yswery\DNS\Console\CommandServer();

// Start DNS server
$server->run();
65 changes: 65 additions & 0 deletions bin/PhpDnsInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

require '../vendor/autoload.php';

echo "Preparing installation of PhpDnsServer.\n";

// detect os
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$isWindows = true;
echo "Detected windows environment, please note that running the installer on windows is not fully supported at this time.";
} else {
$isWindows = false;
}

// make sure we are running as root if os is linux
if (!$isWindows) {
if (posix_getuid() != 0) {
die("This script must be run as root.\n");
}

// create the default config file
$defaultConfig = [
'host' => '0.0.0.0',
'port' => 53,
'storage' => '/etc/phpdnsserver',
'backend' => 'file'
];

$filesystem = new \Symfony\Component\Filesystem\Filesystem;

try {
echo "Creating required directories and config files...\n";

$filesystem->mkdir('/etc/phpdnsserver');
$filesystem->mkdir('/etc/phpdnsserver/zones');
$filesystem->mkdir('/etc/phpdnsserver/logs');

// create default config
file_put_contents('/etc/phpdns.json', json_encode(getcwd()));
} catch (\Symfony\Component\Filesystem\Exception\IOException $e){
die("An error occurred during installation\n".$e->getMessage());
}

} else {
// create the default config file
$defaultConfig = [
'host' => '0.0.0.0',
'port' => 53,
'backend' => 'file'
];

$filesystem = new \Symfony\Component\Filesystem\Filesystem;

try {
echo "Creating required directories and config files...\n";

$filesystem->mkdir(getcwd().'\\zones');
$filesystem->mkdir(getcwd().'\\logs');

// create default config
file_put_contents(getcwd().'\\phpdns.json', json_encode(getcwd()));
} catch (\Symfony\Component\Filesystem\Exception\IOException $e){
die("An error occurred during installation\n".$e->getMessage());
}
}
65 changes: 65 additions & 0 deletions bin/PhpDnsServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

require '../vendor/autoload.php';

use Garden\Cli\Cli;

// parse cli args
$cli = new Cli();

$cli
->opt('bind:b', 'Bind to a specific ip interface', false)
->opt('port:p', 'specify the port to bind to', false)
->opt('config:c', 'specify the path to the phpdns.json file', false)
->opt('storage:s', 'specify the location to zone storage folder', false);

$args = $cli->parse($argv, true);

// defaults
$host = $args->getOpt('bind', '0.0.0.0');
$port = $args->getOpt('port', 53);

// figure out config location
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// default to current working directory on windows
$configFile = $args->getOpt('config', getcwd() . '/phpdns.json');
$storageDirectory = $args->getOpt('storage', getcwd());
} else {
// default to /etc/phpdns.json and /etc/phpdnsserver if not on windows
$configFile = $args->getOpt('config', '/etc/phpdns.json');
$storageDirectory = $args->getOpt('storage', '/etc/phpdnserver');
}

// initialize the configuration
$config = new yswery\DNS\Config\FileConfig($configFile);
try {
$config->load();

if ($config->has('host')) {
$host = $config->get('host');
}

if ($config->has('port')) {
$port = $config->get('port');
}

if ($config->has('storage')) {
$storageDirectory = $config->get('storage');
}
} catch (\yswery\DNS\Exception\ConfigFileNotFoundException $e) {
echo $e->getMessage();
}

// Create the eventDispatcher and add the event subscribers
$eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$eventDispatcher->addSubscriber(new \yswery\DNS\Event\Subscriber\EchoLogger());

try {
// Create a new instance of Server class
$server = new yswery\DNS\Server(null, $eventDispatcher, $config, $storageDirectory, true, $host, $port);

// Start DNS server
$server->run();
} catch (\Exception $e) {
echo $e->getMessage();
}
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "A DNS server implementation in pure PHP.",
"require-dev": {
"phpunit/phpunit": "~7.3",
"php-coveralls/php-coveralls": "~2.1"
"php-coveralls/php-coveralls": "~2.1",
"bamarni/composer-bin-plugin": "^1.3"
},
"license": "MIT",
"authors": [
Expand Down Expand Up @@ -31,7 +32,10 @@
"ext-json": "~1.0",
"ext-simplexml": "*",
"symfony/event-dispatcher": "~4.0",
"psr/log": "^1.0"
"psr/log": "^1.0",
"symfony/filesystem": "^4.3",
"symfony/console": "^4.3",
"vanilla/garden-cli": "^2.2"
},
"autoload": {
"psr-4": {
Expand All @@ -42,5 +46,10 @@
"psr-4": {
"yswery\\DNS\\Tests\\": "tests/"
}
},
"scripts": {
"build-server": "box compile -c server.box.json",
"build-console": "box compile -c console.box.json",
"build-installer": "box compile -c installer.box.json"
}
}
13 changes: 13 additions & 0 deletions console.box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chmod": "0755",
"main": "bin/PhpDnsConsole.php",
"output": "bin/phpdnscli.phar",
"directories": ["src"],
"finder": [
{
"name": "*.php",
"exclude": ["test", "tests"],
"in": "vendor"
}
], "stub": true
}
13 changes: 13 additions & 0 deletions installer.box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chmod": "0755",
"main": "bin/PhpDnsInstaller.php",
"output": "bin/phpdnsinstaller.phar",
"finder": [
{
"name": "*.php",
"exclude": ["test", "tests"],
"in": "vendor"
}
],
"stub": true
}
13 changes: 13 additions & 0 deletions server.box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chmod": "0755",
"main": "bin/PhpDnsServer.php",
"output": "bin/phpdnsserver.phar",
"directories": ["src"],
"finder": [
{
"name": "*.php",
"exclude": ["test", "tests"],
"in": "vendor"
}
], "stub": true
}
76 changes: 76 additions & 0 deletions src/Config/FileConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of PHP DNS Server.
*
* (c) Yif Swery <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace yswery\DNS\Config;

use yswery\DNS\Exception\ConfigFileNotFoundException;

class FileConfig
{
/**
* @var string
*/
protected $configFile;

/**
* @var object
*/
protected $config;

public function __construct(string $configFile)
{
$this->configFile = $configFile;
}

/**
* @throws ConfigFileNotFoundException
*/
public function load()
{
// make sure the file exists before loading the config
if (file_exists($this->configFile)) {
$data = file_get_contents($this->configFile);
$this->config = json_decode($data);
} else {
throw new ConfigFileNotFoundException('Config file not found.');
}
}

public function save()
{
file_put_contents($this->configFile, json_encode($this->config));
}

public function get($key, $default = null)
{
if (isset($this->config->{$key})) {
return $this->config->{$key};
}

return $default;
}

public function set(array $data)
{
$configObject = new RecursiveArrayObject($this->config);
$configArray = $configObject->getArrayCopy();

//$origional = json_decode(json_encode($this->config), true);
$new = array_merge($configArray, $data);

$this->config = json_decode(json_encode($new), false);
}

public function has($key)
{
return isset($this->config->{$key});
}
}
31 changes: 31 additions & 0 deletions src/Config/RecursiveArrayObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of PHP DNS Server.
*
* (c) Yif Swery <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace yswery\DNS\Config;

use ArrayObject;

class RecursiveArrayObject extends ArrayObject
{
public function getArrayCopy()
{
$resultArray = parent::getArrayCopy();
foreach ($resultArray as $key => $val) {
if (!is_object($val)) {
continue;
}
$o = new RecursiveArrayObject($val);
$resultArray[$key] = $o->getArrayCopy();
}

return $resultArray;
}
}
31 changes: 31 additions & 0 deletions src/Console/CommandServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of PHP DNS Server.
*
* (c) Yif Swery <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace yswery\DNS\Console;

use Symfony\Component\Console\Application;
use yswery\DNS\Console\Commands\VersionCommand;
use yswery\DNS\Server;

class CommandServer extends Application
{
public function __construct()
{
parent::__construct('PhpDnsServer', Server::VERSION);

$this->registerCommands();
}

protected function registerCommands()
{
$this->add(new VersionCommand());
}
}
Loading

0 comments on commit 47f9e36

Please sign in to comment.