-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from CosmicDNS/master
PhpDnsServer phar and cli phar
- Loading branch information
Showing
21 changed files
with
771 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.