Check if domain is valid dynamically

This commit is contained in:
Pablo Ferreiro 2022-01-05 20:16:50 +01:00
parent c8d6516dd7
commit 31e3aafbd7
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
3 changed files with 22 additions and 30 deletions

View file

@ -1,17 +0,0 @@
<?php
$domains = [
"image" => [
"p16-sign-va.tiktokcdn.com",
"p16-sign-sg.tiktokcdn.com",
"p77-sign-va.tiktokcdn.com",
"p77-sign-sg.tiktokcdn.com",
"p77-sign-sg-lite.tiktokcdn.com"
],
"video" => [
"v16-webapp.tiktok.com"
],
"audio" => [
"sf16-ies-music-va.tiktokcdn.com",
"sf77-ies-music-va.tiktokcdn.com"
],
];

View file

@ -1,15 +1,26 @@
<?php <?php
require __DIR__ . "/../helpers/domains.php";
use Steampixel\Route; use Steampixel\Route;
Route::add('/images', function () use ($domains) { /**
* Check if an url has a valid domain
* @param string $url URL you want to check
* @return bool
*/
function isValidDomain(string $url): bool {
$valid_domains = [
"tiktokcdn.com", "tiktokcdn-us.com", "tiktok.com"
];
$host = parse_url($url, PHP_URL_HOST);
$host_split = explode('.', $host);
return count($host_split) === 3 && in_array($host_split[1] . '.' . $host_split[2], $valid_domains);
}
Route::add('/images', function () {
if (!isset($_GET['url'])) { if (!isset($_GET['url'])) {
die('You need to send a url!'); die('You need to send a url!');
} }
$url = $_GET['url']; $url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST); if (!filter_var($url, FILTER_VALIDATE_URL) || !isValidDomain($url)) {
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['image'])) {
die('Not a valid URL'); die('Not a valid URL');
} }
$img = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]])); $img = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]]));
@ -21,13 +32,13 @@ Route::add('/images', function () use ($domains) {
} }
}); });
Route::add('/audios', function () use ($domains) { Route::add('/audios', function () {
if (!isset($_GET['url'])) { if (!isset($_GET['url'])) {
die('You need to send a url!'); die('You need to send a url!');
} }
$url = $_GET['url']; $url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST);
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['audio'])) { if (!filter_var($url, FILTER_VALIDATE_URL) || !isValidDomain($url)) {
die('Not a valid URL'); die('Not a valid URL');
} }
$audio = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]])); $audio = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]]));
@ -39,15 +50,13 @@ Route::add('/audios', function () use ($domains) {
} }
}); });
Route::add('/stream', function () use ($domains) { Route::add('/stream', function () {
if (!isset($_GET['url'])) { if (!isset($_GET['url'])) {
die('You need to send a url!'); die('You need to send a url!');
} }
$url = $_GET['url']; $url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST); if (!filter_var($url, FILTER_VALIDATE_URL) || !isValidDomain($url)) {
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['video'])) {
die('Not a valid URL'); die('Not a valid URL');
} }

View file

@ -4,7 +4,7 @@ require __DIR__ . '/settings.php';
require __DIR__ . "/../helpers/settings_elements.php"; require __DIR__ . "/../helpers/settings_elements.php";
use Steampixel\Route; use Steampixel\Route;
// - ROUTING HELPERS - // // -- ROUTING HELPERS -- //
function getApi(array $proxy_elements): \Sovit\TikTok\Api { function getApi(array $proxy_elements): \Sovit\TikTok\Api {
$options = []; $options = [];
// Proxy config // Proxy config