Make calls live, default config relays to ntfy.sh
This commit is contained in:
@@ -24,6 +24,12 @@ class NtfiDaemon
|
|||||||
|
|
||||||
private array $channels = [];
|
private array $channels = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the configuration file to use
|
||||||
|
*
|
||||||
|
* @param string|null $filename
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
public function setConfigFile(?string $filename): self
|
public function setConfigFile(?string $filename): self
|
||||||
{
|
{
|
||||||
if ($filename && !file_exists($filename)) {
|
if ($filename && !file_exists($filename)) {
|
||||||
@@ -33,6 +39,11 @@ class NtfiDaemon
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the configuration, and/or return with defaults.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
private function readConfig(): array
|
private function readConfig(): array
|
||||||
{
|
{
|
||||||
$config = [
|
$config = [
|
||||||
@@ -58,6 +69,11 @@ class NtfiDaemon
|
|||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the daemon after trying to read the config.
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
public function start(): self
|
public function start(): self
|
||||||
{
|
{
|
||||||
$config = $this->readConfig();
|
$config = $this->readConfig();
|
||||||
@@ -74,6 +90,12 @@ class NtfiDaemon
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup the HTTP server
|
||||||
|
*
|
||||||
|
* @param string $listen
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
private function setupHttp(string $listen): void
|
private function setupHttp(string $listen): void
|
||||||
{
|
{
|
||||||
$this->httpServer = new HttpServer(
|
$this->httpServer = new HttpServer(
|
||||||
@@ -83,6 +105,12 @@ class NtfiDaemon
|
|||||||
$this->httpServer->listen($listener);
|
$this->httpServer->listen($listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle incoming request
|
||||||
|
*
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
private function onRequest(ServerRequestInterface $request)
|
private function onRequest(ServerRequestInterface $request)
|
||||||
{
|
{
|
||||||
$path = trim($request->getUri()->getPath(), '/');
|
$path = trim($request->getUri()->getPath(), '/');
|
||||||
@@ -98,8 +126,17 @@ class NtfiDaemon
|
|||||||
return Response::plaintext("OK");
|
return Response::plaintext("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to publish a message based on an incoming topic
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $type
|
||||||
|
* @param string $body
|
||||||
|
* @return PromiseInterface
|
||||||
|
*/
|
||||||
private function doPublish(string $path, string $type, string $body): PromiseInterface
|
private function doPublish(string $path, string $type, string $body): PromiseInterface
|
||||||
{
|
{
|
||||||
|
printf("in: %s (%s) %d bytes\n", $path, $type, strlen($body));
|
||||||
foreach ($this->channels as $pattern => $channel) {
|
foreach ($this->channels as $pattern => $channel) {
|
||||||
$re = '/^'.preg_replace('/\{(.+?)\}/', '(?P<$1>[a-zA-Z0-9-_]+)', $pattern).'$/';
|
$re = '/^'.preg_replace('/\{(.+?)\}/', '(?P<$1>[a-zA-Z0-9-_]+)', $pattern).'$/';
|
||||||
// printf("pattern={%s} re={%s}\n", $pattern, $re);
|
// printf("pattern={%s} re={%s}\n", $pattern, $re);
|
||||||
@@ -119,20 +156,30 @@ class NtfiDaemon
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function publish(string $connection, string $topic, string $type, string $body)
|
/**
|
||||||
|
* Publish a message to a defined server, specificed in $connection.
|
||||||
|
*
|
||||||
|
* @param string $connection
|
||||||
|
* @param string $topic
|
||||||
|
* @param string $type
|
||||||
|
* @param string $body
|
||||||
|
* @return PromiseInterface
|
||||||
|
* @throws \Exception if the configuration is invalid
|
||||||
|
*/
|
||||||
|
private function publish(string $connection, string $topic, string $type, string $body): PromiseInterface
|
||||||
{
|
{
|
||||||
|
printf("out: %s %s (%s) %d bytes\n", $connection, $topic, $type, strlen($body));
|
||||||
|
|
||||||
$d = new Deferred();
|
$d = new Deferred();
|
||||||
$serverInfo = $this->servers[$connection] ?? throw new \Exception("The server '{$connection}' is not configured");
|
$serverInfo = $this->servers[$connection] ?? throw new \Exception("The server '{$connection}' is not configured");
|
||||||
|
|
||||||
|
|
||||||
if (isset($serverInfo['token']) && $serverInfo['token']) {
|
if (isset($serverInfo['token']) && $serverInfo['token']) {
|
||||||
$browser = new Browser();
|
$browser = new Browser();
|
||||||
$browser->withHeader("authorization", "Bearer {$serverInfo['token']}");
|
$browser->withHeader("authorization", "Bearer {$serverInfo['token']}");
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = sprintf("https://%s/%s", $serverInfo['server'], $topic);
|
$url = sprintf("https://%s/%s", $serverInfo['server'], $topic);
|
||||||
echo $url;
|
|
||||||
$d->resolve(new Response());
|
|
||||||
return $d->promise();
|
|
||||||
|
|
||||||
$browser->post($url, [
|
$browser->post($url, [
|
||||||
'content-type' => $type,
|
'content-type' => $type,
|
||||||
|
|||||||
Reference in New Issue
Block a user