Browse Source

init composer/psr-4 package

yggdrasil-0.4.7
ghost 1 year ago
parent
commit
ade3d281bf
  1. 12
      .gitignore
  2. 17
      composer.json
  3. 82
      library/url.php
  4. 55
      library/yggdrasil.php
  5. 0
      src/config/app.php.example
  6. 6
      src/crontab/crawler.php
  7. 0
      src/library/mysql.php
  8. 0
      src/public/index.html

12
.gitignore vendored

@ -1,5 +1,9 @@
.vscode /.vscode/
.ftpignore /vendor/
/database/yggstate.mwb.bak
/config/app.php /src/config/app.php
/database/yggstate.mwb.bak
.ftpignore
composer.lock

17
composer.json

@ -0,0 +1,17 @@
{
"name": "yggverse/yggstate",
"description": "Yggdrasil Network Analytics",
"type": "project",
"require": {
"php": ">=8.1",
"yggverse/yggdrasilctl": ">=0.1.0",
"yggverse/parser": ">=0.1.0"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Yggverse\\Yggstate\\": "src/"
}
},
"minimum-stability": "alpha"
}

82
library/url.php

@ -1,82 +0,0 @@
<?php
class URL {
public static function is(string $url) : bool {
return filter_var($url, FILTER_VALIDATE_URL);
}
public static function parse(string $url) : mixed {
$result = (object)
[
'host' => (object)
[
'url' => null,
'scheme' => null,
'name' => null,
'port' => null,
],
'page' => (object)
[
'url' => null,
'uri' => null,
'path' => null,
'query' => null,
]
];
// Validate URL
if (!self::is($url)) {
return false;
}
// Parse host
if ($scheme = parse_url($url, PHP_URL_SCHEME)) {
$result->host->url = $scheme . '://';
$result->host->scheme = $scheme;
} else {
return false;
}
if ($host = parse_url($url, PHP_URL_HOST)) {
$result->host->url .= $host;
$result->host->name = $host;
} else {
return false;
}
if ($port = parse_url($url, PHP_URL_PORT)) {
$result->host->url .= ':' . $port;
$result->host->port = $port;
// port is optional
}
// Parse page
if ($path = parse_url($url, PHP_URL_PATH)) {
$result->page->uri = $path;
$result->page->path = $path;
}
if ($query = parse_url($url, PHP_URL_QUERY)) {
$result->page->uri .= '?' . $query;
$result->page->query = '?' . $query;
}
$result->page->url = $result->host->url . $result->page->uri;
return $result;
}
}

55
library/yggdrasil.php

@ -1,55 +0,0 @@
<?php
class Yggdrasil {
private static function _exec(string $cmd) : mixed {
if (false !== exec('yggdrasilctl -json getPeers', $output)) {
$rows = [];
foreach($output as $row){
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows))) {
return $result;
}
}
return false;
}
public static function getPeers() : mixed {
if (false === $result = self::_exec('yggdrasilctl -json getPeers')) {
return false;
}
if (empty($result->peers)) {
return false;
}
foreach ((object) $result->peers as $peer) {
switch (false) {
case isset($peer->bytes_recvd):
case isset($peer->bytes_sent):
case isset($peer->remote):
case isset($peer->port):
case isset($peer->key):
case isset($peer->uptime):
case !empty($peer->coords):
return false;
}
}
return $result->peers;
}
}

0
config/app.php.example → src/config/app.php.example

6
crontab/crawler.php → src/crontab/crawler.php

@ -20,7 +20,7 @@ if (false === sem_acquire($semaphore, true)) {
require_once(__DIR__ . '/../config/app.php'); require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/yggdrasil.php'); require_once(__DIR__ . '/../library/yggdrasil.php');
require_once(__DIR__ . '/../library/mysql.php'); require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/url.php'); require_once __DIR__ . '/../vendor/autoload.php';
// Check disk quota // Check disk quota
if (CRAWL_STOP_DISK_QUOTA_MB_LEFT > disk_free_space('/') / 1000000) { if (CRAWL_STOP_DISK_QUOTA_MB_LEFT > disk_free_space('/') / 1000000) {
@ -63,7 +63,7 @@ try {
} }
// Collect connected peers // Collect connected peers
if ($connectedPeers = Yggdrasil::getPeers()) { if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) {
foreach ($connectedPeers as $connectedPeerAddress => $connectedPeerInfo) { foreach ($connectedPeers as $connectedPeerAddress => $connectedPeerInfo) {
@ -84,7 +84,7 @@ if ($connectedPeers = Yggdrasil::getPeers()) {
} }
// Init peer data // Init peer data
if ($connectedPeerRemoteUrl = URL::parse($connectedPeerInfo->remote)) { if ($connectedPeerRemoteUrl = Yggverse\parser\Url::parse($connectedPeerInfo->remote)) {
if ($dbPeerRemote = $db->findPeerRemote($dbPeerId, if ($dbPeerRemote = $db->findPeerRemote($dbPeerId,
$connectedPeerRemoteUrl->host->scheme, $connectedPeerRemoteUrl->host->scheme,

0
library/mysql.php → src/library/mysql.php

0
public/index.html → src/public/index.html

Loading…
Cancel
Save