Browse Source

drop not maintained build-in server solution

fs
yggverse 7 months ago
parent
commit
e6781f867f
  1. 2
      .gitignore
  2. 63
      README.md
  3. 0
      example/config.json
  4. 12
      example/host.json
  5. 2
      src/crawler.php
  6. 258
      src/server.php

2
.gitignore vendored

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
/composer.lock
/crawler.json
/config.json
/server/
/vendor/

63
README.md

@ -6,8 +6,7 @@ Simple RSS feed converter to static Gemtext format, useful for news portals or l @@ -6,8 +6,7 @@ Simple RSS feed converter to static Gemtext format, useful for news portals or l
## Example
* `gemini://[301:23b4:991a:634d::feed]` - [Yggdrasil](https://github.com/yggdrasil-network/yggdrasil-go) instance by YGGverse
* `gemini://pulsar.yggverse.dedyn.io` - Internet alias
* `nex://[301:23b4:991a:634d::feed]` - [Yggdrasil](https://github.com/yggdrasil-network/yggdrasil-go) instance by YGGverse
## Usage
@ -31,62 +30,4 @@ Configuration file supports multiple feed channels with custom settings: @@ -31,62 +30,4 @@ Configuration file supports multiple feed channels with custom settings:
* `{title}` - item title
* `{description}` - item description
Resulting files could be placed to any local folder (for personal reading) or shared with others (using [gmid](https://github.com/omar-polo/gmid), [twins](https://code.rocket9labs.com/tslocum/twins) or any other [server](https://github.com/kr1sp1n/awesome-gemini#servers) for `gemtext` statics)
## Server
Pulsar comes with build-in [Titan-II](https://github.com/YGGverse/titan-II) server implementation.
It's especially useful for [Yggdrasil](https://github.com/yggdrasil-network/yggdrasil-go) users, who wish to host their feeds using plain IPv6 `0200::/7` addresses without domain in `CN` record. Build-in server contain this feature implemented from the box.
For NEX protocol users, here is the [next](https://github.com/YGGverse/next) server solution!
### Setup
* `cd Pulsar` - navigate to the project folder
* `composer update` - download server dependencies with Composer
* `mkdir server/127.0.0.1` - init server location (you can define any other destination, `server` just git ignored)
* `cp example/host.json server/127.0.0.1/host.json` - copy configuration example to the destination folder
* `cd server/127.0.0.1` - navigate to server folder created and generate new self-signed certificate
On example above, certificate could be generated with following command:
```
openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem -days 365 -nodes -subj "/CN=127.0.0.1"
```
* _tip: for IPv6 address, just skip square brackets from `CN` value_
### Launch
* `php src/server.php server/127.0.0.1` - supported relative or absolute paths (for systemd service)
Open `gemini://127.0.0.1` in [Gemini browser](https://github.com/kr1sp1n/awesome-gemini#clients)!
### Autostart
Launch server as `systemd` service
Following example means you have Pulsar installed in home directory of `pulsar` user (`useradd -m pulsar`)
1. `sudo nano /etc/systemd/system/pulsar.service` - create new service file by following example:
``` /etc/systemd/system/pulsar.service
[Unit]
After=network.target
[Service]
Type=simple
User=pulsar
ExecStart=/usr/bin/php /home/pulsar/Pulsar/src/server.php /home/pulsar/Pulsar/server/127.0.0.1
StandardOutput=file:/home/pulsar/Pulsar/server/127.0.0.1/debug.log
StandardError=file:/home/pulsar/Pulsar/server/127.0.0.1/error.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
```
2. `sudo systemctl daemon-reload` - reload systemd configuration
3. `sudo systemctl enable pulsar` - enable Pulsar service on system startup
4. `sudo systemctl start pulsar` - start Pulsar server
Resulting files could be placed to any local folder (for personal reading) or shared with others (using [gmid](https://github.com/omar-polo/gmid), [twins](https://code.rocket9labs.com/tslocum/twins) or any other [server](https://github.com/kr1sp1n/awesome-gemini#servers) for `gemtext` statics)

0
example/crawler.json → example/config.json

12
example/host.json

@ -1,12 +0,0 @@ @@ -1,12 +0,0 @@
{
"host":"127.0.0.1",
"port":1965,
"cert":"cert.pem",
"key":"key.rsa",
"data":
{
"directory":"public",
"index":"index.gmi",
"listing":true
}
}

2
src/crawler.php

@ -15,7 +15,7 @@ if (false === sem_acquire($semaphore, true)) @@ -15,7 +15,7 @@ if (false === sem_acquire($semaphore, true))
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'crawler.json'
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config.json'
)
);

258
src/server.php

@ -1,258 +0,0 @@ @@ -1,258 +0,0 @@
<?php
// Load dependencies
require_once __DIR__ .
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'vendor' .
DIRECTORY_SEPARATOR . 'autoload.php';
// Init required arguments
if (empty($argv[1]))
{
throw new \Exception(
_('Configured hostname required as argument')
);
}
// Init server path
define(
'PULSAR_SERVER_DIRECTORY',
rtrim(
str_starts_with($argv[1], DIRECTORY_SEPARATOR) ? $argv[1] :
realpath(
__DIR__ .
DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . $argv[1]
),
DIRECTORY_SEPARATOR
) . DIRECTORY_SEPARATOR
);
// Init server config
if (!file_exists(PULSAR_SERVER_DIRECTORY . 'host.json'))
{
throw new \Exception(
_('Host not configured')
);
}
$config = json_decode(
file_get_contents(
PULSAR_SERVER_DIRECTORY . 'host.json'
)
);
// Init server certificate
define(
'PULSAR_SERVER_CERT',
str_starts_with(
$config->cert,
DIRECTORY_SEPARATOR
) ? $config->cert : PULSAR_SERVER_DIRECTORY . $config->cert
);
if (!file_exists(PULSAR_SERVER_CERT))
{
throw new \Exception(
_('Certificate file not found')
);
}
// Init server key
define(
'PULSAR_SERVER_KEY',
str_starts_with(
$config->key,
DIRECTORY_SEPARATOR
) ? $config->key : PULSAR_SERVER_DIRECTORY . $config->key
);
if (!file_exists(PULSAR_SERVER_KEY))
{
throw new \Exception(
_('Key file not found')
);
}
// Init data directory
define(
'PULSAR_SERVER_DATA_DIRECTORY',
rtrim(
str_starts_with(
$config->data->directory,
DIRECTORY_SEPARATOR
) ? $config->data->directory : PULSAR_SERVER_DIRECTORY . $config->data->directory,
DIRECTORY_SEPARATOR
) . DIRECTORY_SEPARATOR
);
if (!is_dir(PULSAR_SERVER_DATA_DIRECTORY))
{
throw new \Exception(
_('Data directory not found')
);
}
// Init server
$server = new \Yggverse\TitanII\Server();
$server->setCert(
PULSAR_SERVER_CERT
);
$server->setKey(
PULSAR_SERVER_KEY
);
$server->setHandler(
function (\Yggverse\TitanII\Request $request): \Yggverse\TitanII\Response
{
global $config;
$response = new \Yggverse\TitanII\Response;
// Filter path request
$path = preg_replace(
[
'/\\\/', // unify separators
'/(^|\/)[\.]+/', // hidden items started with dot
'/[\.]+\//', // relative directory paths
'/[\/]+\//', // remove extra slashes
],
DIRECTORY_SEPARATOR,
filter_var(
$request->getPath(),
FILTER_SANITIZE_URL
)
);
if ($path != $request->getPath() || in_array($path, ['', null, false]))
{
$response->setCode(
30
);
$response->setMeta(
sprintf(
'gemini://%s%s/%s',
$config->host,
$config->port == 1965 ? null : ':' . $config->port,
trim(
(string) $path,
'/'
)
)
);
return $response;
}
// Directory request
if (is_dir(PULSAR_SERVER_DATA_DIRECTORY . $path))
{
// Try index
if (file_exists(PULSAR_SERVER_DATA_DIRECTORY . $path . $config->data->index))
{
$response->setContent(
file_get_contents(
PULSAR_SERVER_DATA_DIRECTORY . $path . $config->data->index
)
);
$response->setCode(
20
);
$response->setMeta(
'text/gemini; charset=utf-8'
);
return $response;
}
// Build listing
if ($config->data->listing)
{
$response->setCode(
20
);
$response->setMeta(
'text/gemini; charset=utf-8'
);
$links = [];
foreach ((array) scandir(PULSAR_SERVER_DATA_DIRECTORY . $path) as $link)
{
if (!str_starts_with($link, '.'))
{
if (is_dir(PULSAR_SERVER_DATA_DIRECTORY . $path . $link))
{
$links[] = sprintf(
'=> %s/',
$link
);
}
else
{
$links[] = sprintf(
'=> %s',
$link
);
}
}
}
$response->setContent(
implode(
PHP_EOL,
$links
)
);
return $response;
}
}
// File request
if (file_exists(PULSAR_SERVER_DATA_DIRECTORY . $path))
{
$response->setCode(
20
);
$response->setMeta(
'text/gemini; charset=utf-8'
);
$response->setContent(
file_get_contents(
PULSAR_SERVER_DATA_DIRECTORY . $path
)
);
return $response;
}
// Noting found
$response->setCode(
51
);
return $response;
}
);
// Start server
echo sprintf(
_('Server started on %s:%d'),
$config->host,
$config->port
);
$server->start(
$config->host,
$config->port
);
Loading…
Cancel
Save