Skip to content

Commit

Permalink
Merge pull request #17 from Mizzrym/issue-15
Browse files Browse the repository at this point in the history
issue-15
  • Loading branch information
Mizzrym authored Sep 30, 2021
2 parents 3d59a7b + fffbe4c commit dab23b9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"name": "Uni-tel",
"homepage": "https://github.com/Uni-tel",
"role": "Developer"
},
{
"name": "geoffroya",
"homepage": "https://github.com/geoffroya",
"role": "Developer"
}
],
"require": {
Expand Down
11 changes: 3 additions & 8 deletions src/SendNsca.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SendNsca implements NagiosCodes {
* @param string $connectionString Examples: 127.0.0.1, localhost:5667, some.server.local:5555, nagios.local
* @param null|EncryptorInterface $encryptor (optional) Class to encrypt the package with
*/
public function __construct(string $connectionString, EncryptorInterface $encryptor = null) {
public function __construct(string $connectionString, EncryptorInterface $encryptor) {
if (strpos($connectionString, ':')) {
$connect = explode(':', $connectionString);
$this->hostname = $connect[0];
Expand Down Expand Up @@ -121,8 +121,7 @@ public function sendHostCheck(string $host, int $returncode, string $message = n
* @param string $message Message (optional).
* @return bool
*/
public function send(string $host, string $service, int $returncode, string $message = null): bool {
$message = $message ?? '';
public function send(string $host, string $service, int $returncode, string $message = ''): bool {
try {
if ($this->hostname === null) {
throw new Exception('No hostname for NSCA daemon given, don\'t know where to connect to - class not properly initialized');
Expand Down Expand Up @@ -166,11 +165,7 @@ public function send(string $host, string $service, int $returncode, string $mes
$crcPacket = pack('nxxxxxxNna64a128a512xx', 3, $timestamp, $returncode, $host, $service, $message);
$crc = crc32($crcPacket);
$packet = pack('nxxNNna64a128a512xx', 3, $crc, $timestamp, $returncode, $host, $service, $message);

// encrypt
if ($this->encryptor) {
$packet = $this->encryptor->encryptPacket($packet, $iv);
}
$packet = $this->encryptor->encryptPacket($packet, $iv);

// send it
fflush($connection);
Expand Down
17 changes: 8 additions & 9 deletions src/SendNscaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpSendNsca;

use PhpSendNsca\encryptors\NullEncryptor;
use PhpSendNsca\interfaces\Ciphers;
use PhpSendNsca\encryptors\XorEncryptor;
use PhpSendNsca\encryptors\LegacyEncryptor;
Expand Down Expand Up @@ -31,16 +32,14 @@ class SendNscaFactory implements Ciphers {
* @return SendNsca
* @throws \Exception
*/
public function getSendNsca(string $connectionString, int $encryptionCipher = null, string $encryptionPassword = null): SendNsca {
$password = $encryptionPassword ?? '';
$cipher = $encryptionCipher ?? Ciphers::ENCRYPT_NONE;
$key = md5($connectionString . ':' . $cipher . ':' . $password);
public function getSendNsca(string $connectionString, int $encryptionCipher = Ciphers::ENCRYPT_NONE, string $encryptionPassword = ''): SendNsca {
$key = md5($connectionString . ':' . $encryptionCipher . ':' . $encryptionPassword);
if (false === isset(static::$instances[$key])) {
static::$instances[$key] = new SendNsca($connectionString, $this->getEncryptor($cipher, $password));
static::$instances[$key] = new SendNsca($connectionString, $this->getEncryptor($encryptionCipher, $encryptionPassword));
}
return static::$instances[$key];
}
/**
* Tries to figure out correct encryptor for a given cipher
*
Expand All @@ -49,17 +48,17 @@ public function getSendNsca(string $connectionString, int $encryptionCipher = nu
* @return EncryptorInterface
* @throws \Exception
*/
protected function getEncryptor(int $cipher, string $password): ?EncryptorInterface {
protected function getEncryptor(int $cipher, string $password): EncryptorInterface {
try {
if ($cipher === Ciphers::ENCRYPT_NONE) {
return null;
return new NullEncryptor(Ciphers::ENCRYPT_NONE, '');
}
if ($cipher === Ciphers::ENCRYPT_XOR) {
return $this->getXorEncryptor($cipher, $password);
}
$encryptor = $this->getOpenSslEncryptor($cipher, $password);
} catch (\Exception $exc) {
trigger_error('Falling back to legacy encryption, openssl failed: ' . $exc->getMessage(), \E_DEPRECATED);
trigger_error('Falling back to legacy encryption, openssl failed: ' . $exc->getMessage(), E_USER_DEPRECATED);
$encryptor = $this->getLegacyEncryptor($cipher, $password);
}
return $encryptor;
Expand Down
32 changes: 32 additions & 0 deletions src/encryptors/NullEncryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpSendNsca\encryptors;

use PhpSendNsca\interfaces\EncryptorInterface;

/**
* Doesn't really encrypt.
* Is needed so typehints work with PHP 7.0, where nullable typehints aren't available
*/
class NullEncryptor implements EncryptorInterface
{
public function __construct(int $encryptionCipher, string $password)
{
}

public function encryptPacket(string $packet, string $initialisationVector): string
{
return $packet;
}

public function getSupportedEncryptionCiphers(): array
{
return [ self::ENCRYPT_NONE ];
}

public function isEncryptionCipherSupported(int $encryptionCipher): bool
{
return $encryptionCipher === self::ENCRYPT_NONE;
}

}

0 comments on commit dab23b9

Please sign in to comment.