Browse Source

implement node settings

main
ghost 11 months ago
parent
commit
7e3f7c6df0
  1. 6
      README.md
  2. 2
      composer.json
  3. 14
      composer.lock
  4. 86
      config.json
  5. 77
      src/public/net/dig.php
  6. 59
      src/public/net/socket.php

6
README.md

@ -10,6 +10,12 @@ cd web-api @@ -10,6 +10,12 @@ cd web-api
composer install
```
## Config
```
nano config.json
```
## Run
```

2
composer.json

@ -8,6 +8,6 @@ @@ -8,6 +8,6 @@
}
},
"require": {
"yggverse/net": "^1.0"
"yggverse/net": "^1.2"
}
}

14
composer.lock generated

@ -4,20 +4,20 @@ @@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0bf82e6e2a05c49f8a6c7e89da62a373",
"content-hash": "c062f15666280a924bcafbc19443d914",
"packages": [
{
"name": "yggverse/net",
"version": "1.0.0",
"version": "1.2.0",
"source": {
"type": "git",
"url": "https://github.com/YGGverse/net-php.git",
"reference": "3a89da61d56fba97e5f48c2d840dd9f70b4fd295"
"reference": "60114e9d1e19731b79b7a0de7824164d7a288d7e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/YGGverse/net-php/zipball/3a89da61d56fba97e5f48c2d840dd9f70b4fd295",
"reference": "3a89da61d56fba97e5f48c2d840dd9f70b4fd295",
"url": "https://api.github.com/repos/YGGverse/net-php/zipball/60114e9d1e19731b79b7a0de7824164d7a288d7e",
"reference": "60114e9d1e19731b79b7a0de7824164d7a288d7e",
"shasum": ""
},
"type": "library",
@ -32,9 +32,9 @@ @@ -32,9 +32,9 @@
],
"support": {
"issues": "https://github.com/YGGverse/net-php/issues",
"source": "https://github.com/YGGverse/net-php/tree/1.0.0"
"source": "https://github.com/YGGverse/net-php/tree/1.2.0"
},
"time": "2023-12-16T10:55:48+00:00"
"time": "2023-12-17T05:59:26+00:00"
}
],
"packages-dev": [],

86
config.json

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
{
"net":
{
"dig":
{
"response":
{
"headers":
{
"Access-Control-Allow-Origin":"*"
}
},
"request":
{
"name":{
"regex":
[
"/\\.ygg$/i",
"/\\.btn$/i",
"/\\.anon$/i",
"/\\.conf$/i",
"/\\.index$/i",
"/\\.index$/i",
"/\\.merch$/i",
"/\\.mirror$/i",
"/\\.mob$/i",
"/\\.screen$/i",
"/\\.srv$/i"
]
},
"record":{
"regex":
[
"/^A$/i",
"/^AAAA$/i",
"/^SRV$/i"
]
},
"records":{
"regex":
[
"/^A$/i",
"/^AAAA$/i",
"/^SRV$/i"
]
}
}
},
"socket":
{
"response":
{
"headers":
{
"Access-Control-Allow-Origin":"*"
}
},
"request":
{
"port":{
"regex":
[
"/^[\\d]{2,6}$/"
]
},
"host":{
"regex":
[
"/^0{0,1}[2-3][a-f0-9]{0,2}:/",
"/\\.ygg$/i",
"/\\.btn$/i",
"/\\.anon$/i",
"/\\.conf$/i",
"/\\.index$/i",
"/\\.index$/i",
"/\\.merch$/i",
"/\\.mirror$/i",
"/\\.mob$/i",
"/\\.screen$/i",
"/\\.srv$/i"
]
}
}
}
}
}

77
src/public/net/dig.php

@ -1,11 +1,42 @@ @@ -1,11 +1,42 @@
<?php
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . '/../../../config.json'
)
);
// Init helpers
function isRegex(array $regex, string $value): bool
{
foreach ($regex as $regex)
{
if (preg_match($regex, $value))
{
return true;
}
}
return false;
}
// Set headers
header('Access-Control-Allow-Origin: *');
foreach ($config->net->dig->response->headers as $key => $value)
{
header(
sprintf(
'%s: %s',
$key,
$value
)
);
}
header('Content-Type: application/json; charset=utf-8');
// Load dependencies
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../../vendor/autoload.php');
// Valid name required to continue
if (empty($_GET['name']) || !is_string($_GET['name']) || !\Yggverse\Net\Dig::isHostName($_GET['name']))
@ -20,11 +51,38 @@ if (empty($_GET['name']) || !is_string($_GET['name']) || !\Yggverse\Net\Dig::isH @@ -20,11 +51,38 @@ if (empty($_GET['name']) || !is_string($_GET['name']) || !\Yggverse\Net\Dig::isH
);
}
if (!isRegex($config->net->dig->request->name->regex, $_GET['name']))
{
exit(
json_encode(
[
'success' => false,
'message' => _('name not supported')
]
)
);
}
// Valid records required to continue
$records = [];
if (isset($_GET['record']) && is_string($_GET['record']) && \Yggverse\Net\Dig::isRecord($_GET['record']))
{
if (!isRegex($config->net->dig->request->record->regex, $_GET['record']))
{
exit(
json_encode(
[
'success' => false,
'message' => sprintf(
_('record "%s" not supported'),
$_GET['record']
)
]
)
);
}
$records[] = $_GET['record'];
}
@ -34,6 +92,21 @@ if (isset($_GET['records']) && is_array($_GET['records'])) @@ -34,6 +92,21 @@ if (isset($_GET['records']) && is_array($_GET['records']))
{
if (is_string($record) && \Yggverse\Net\Dig::isRecord($record))
{
if (!isRegex($config->net->dig->request->records->regex, $record))
{
exit(
json_encode(
[
'success' => false,
'message' => sprintf(
_('record "%s" not supported'),
$record
)
]
)
);
}
$records[] = $record;
}
}

59
src/public/net/socket.php

@ -1,11 +1,42 @@ @@ -1,11 +1,42 @@
<?php
// Init config
$config = json_decode(
file_get_contents(
__DIR__ . '/../../../config.json'
)
);
// Init helpers
function isRegex(array $regex, string $value): bool
{
foreach ($regex as $regex)
{
if (preg_match($regex, $value))
{
return true;
}
}
return false;
}
// Set headers
header('Access-Control-Allow-Origin: *');
foreach ($config->net->socket->response->headers as $key => $value)
{
header(
sprintf(
'%s: %s',
$key,
$value
)
);
}
header('Content-Type: application/json; charset=utf-8');
// Load dependencies
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../../vendor/autoload.php');
// Valid port required to continue
if (empty($_GET['port']) || !\Yggverse\Net\Socket::isPort($_GET['port']))
@ -20,6 +51,18 @@ if (empty($_GET['port']) || !\Yggverse\Net\Socket::isPort($_GET['port'])) @@ -20,6 +51,18 @@ if (empty($_GET['port']) || !\Yggverse\Net\Socket::isPort($_GET['port']))
);
}
if (!isRegex($config->net->socket->request->port->regex, $_GET['port']))
{
exit(
json_encode(
[
'success' => false,
'message' => _('port not supported')
]
)
);
}
// Set client address if optional host not provided
if (empty($_GET['host']))
{
@ -63,6 +106,18 @@ else @@ -63,6 +106,18 @@ else
}
}
if (!isRegex($config->net->socket->request->host->regex, $host))
{
exit(
json_encode(
[
'success' => false,
'message' => _('host not supported')
]
)
);
}
// Connection test
exit(
json_encode(

Loading…
Cancel
Save