1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-06 03:50:04 +02:00
Daniel Neto 2025-01-15 09:51:39 -03:00
parent 673b7f0316
commit 66c7658f0f
10 changed files with 193 additions and 79 deletions

View file

@ -320,7 +320,15 @@ function zipDirectory($source, $destination)
return file_exists($destinationOriginal);
}
function isPortOpenInternal($host, $port) {
function isPortOpenInternal($host, $port)
{
$output = [];
$result = null;
// Check if 'nc' is available
exec("command -v nc", $output, $result);
if ($result === 0) {
// Use 'nc' to check the port
$output = [];
$result = null;
exec("nc -zv {$host} {$port} 2>&1", $output, $result);
@ -328,17 +336,36 @@ function isPortOpenInternal($host, $port) {
error_log($line);
}
return $result === 0;
} else {
// Fallback to PHP socket method
//error_log("nc command not found, falling back to socket connection.");
$connection = @fsockopen($host, $port, $errno, $errstr, 5); // 5 seconds timeout
if ($connection) {
fclose($connection);
return true;
} else {
error_log("Socket error: $errstr");
return false;
}
}
}
function isPortOpenExternal($port, $timeout = 10) {
function isLocalPortOpen($port)
{
return isPortOpenInternal('127.0.0.1', $port);
}
function isPortOpenExternal($port, $timeout = 10)
{
global $isPortOpenExternalResponse;
$ports = array($port);
//postVariables($url, $array, $httpcodeOnly = true, $timeout = 10)
$isPortOpenExternalResponse = new stdClass();
$response = postVariables('https://search.ypt.me/checkPorts.json.php', $ports, false, $timeout);
if(!empty($response)){
if (!empty($response)) {
$json = json_decode($response);
if(!empty($json)){
if (!empty($json)) {
$isPortOpenExternalResponse = $json;
return $json->ports[0]->isOpen;
}
@ -382,6 +409,21 @@ function getPIDUsingPort($port)
return false;
}
function killProcessRuningOnPort($port) {
if (!empty($port)) {
_error_log('Searching for port: ' . $port);
//$command = 'netstat -ano | findstr ' . $port;
//exec($command, $output, $retval);
$pid = getPIDUsingPort($port);
if (!empty($pid)) {
_error_log('Server is already runing on port '.$port.' Killing, PID ' . $pid);
killProcess($pid);
} else {
_error_log('No Need to kill, port NOT found');
}
}
}
function canExecutePgrep()
{
// Check if we can successfully pgrep the init or systemd process

View file

@ -114,9 +114,9 @@ function checkVideosDir()
function getVideosDir()
{
global $isStandAlone, $global;
if(empty($isStandAlone)){
if (empty($isStandAlone)) {
return Video::getStoragePath();
}else{
} else {
return "{$global['systemRootPath']}videos/";
}
}
@ -1220,3 +1220,14 @@ function findMP3File($folderPath)
// Return false if no .mp4 file is found
return false;
}
// Helper function to read JSON files
function readJsonFile($filePath)
{
if (!file_exists($filePath)) {
return null;
}
$content = file_get_contents($filePath);
return json_decode($content, true);
}

View file

@ -48,6 +48,7 @@ class WebRTC extends PluginAbstract
function executeEveryMinute()
{
self::checkAndUpdate();
if (empty($obj->autoStartServerIfIsInactive)) {
self::startIfIsInactive();
}
@ -56,6 +57,7 @@ class WebRTC extends PluginAbstract
static function startIfIsInactive()
{
if (!self::checkIfIsActive()) {
_error_log('WebRTC server is inactive');
self::startServer();
}
}
@ -132,6 +134,73 @@ class WebRTC extends PluginAbstract
return "{$global['systemRootPath']}videos/WebRTC2RTMP.log";
}
static function updateFileIfNeed()
{
$json = self::getJson();
if (!empty($json)) {
return ($json->phpTimestamp > strtotime('-2 min')) ? $json->phpTimestamp : false;
}
return false;
}
public static function checkAndUpdate() {
// Define file paths
$availableFilePath = self::getWebRTC2RTMPAssetVersionFile();
$sourceExecutablePath = self::getWebRTC2RTMPAssetFile();
$executablePath = self::getWebRTC2RTMPFile();
$currentFilePath = self::getWebRTC2RTMPJsonFile();
try {
// Read the JSON files
$currentData = readJsonFile($currentFilePath);
$availableData = readJsonFile($availableFilePath);
// Skip if any of the JSON files do not exist
if (empty($currentData) || empty($availableData)) {
_error_log("WebRTC::checkAndUpdate: Required JSON file(s) missing. Skipping update.");
return false;
}
// Compare versions
if ($currentData['version'] != $availableData['version']) {
_error_log("WebRTC::checkAndUpdate: A new version is available Current={$currentData['version']} available={$availableData['version']}. Updating...");
// Stop the current server
_error_log("WebRTC::checkAndUpdate: Stopping current server...");
exec("pkill WebRTC2RTMP", $output, $status);
if ($status !== 0) {
_error_log("WebRTC::checkAndUpdate: Warning: Could not stop the server or it was not running.");
}
self::stopServer();
// Remove old executable
if (file_exists($executablePath)) {
_error_log("WebRTC::checkAndUpdate: Removing old executable...");
unlink($executablePath);
}
// Copy new executable
_error_log("WebRTC::checkAndUpdate: Copying new executable...");
copy($sourceExecutablePath, $executablePath);
// Make new executable runnable
_error_log("WebRTC::checkAndUpdate: Making new executable runnable...");
chmod($executablePath, 0755);
_error_log("WebRTC::checkAndUpdate: Update completed successfully!");
return true; // Indicates that an update was performed
} else {
_error_log("WebRTC::checkAndUpdate: You are already running the latest version.");
return false; // No update needed
}
} catch (Exception $e) {
_error_log("WebRTC::checkAndUpdate: Error: " . $e->getMessage());
return false; // Indicates failure or no update performed
}
}
static function startServer()
{
_error_log('Starting WebRTC Server');
@ -154,11 +223,25 @@ class WebRTC extends PluginAbstract
// Try to execute the command
if (is_executable($file)) {
if(isLocalPortOpen($obj->port)){
error_log("Port $obj->port is already open");
return false;
}else{
error_log("Port $obj->port is not open, start the server");
$command = "{$file} --port={$obj->port} > $log ";
return execAsync($command);
}
} else {
error_log("Unable to make {$file} executable.");
return false;
}
}
static function stopServer()
{
_error_log('Starting WebRTC Server');
global $global;
$obj = AVideoPlugin::getDataObject('WebRTC');
return killProcessRuningOnPort($obj->port);
}
}

Binary file not shown.

Binary file not shown.

View file

@ -1,5 +1,5 @@
{
"version": "3.4",
"date": "2025-01-14T18:04:12.910Z",
"phpTimestamp": 1736877852
"version": "3.5",
"date": "2025-01-15T00:37:21.724Z",
"phpTimestamp": 1736901441
}

View file

@ -0,0 +1,23 @@
<?php
$config = dirname(__FILE__) . '/../../../videos/configuration.php';
require_once $config;
if (!isCommandLineInterface()) {
return die('Command Line only');
}
ob_end_flush();
$obj = AVideoPlugin::getDataObjectIfEnabled('WebRTC');
if (empty($obj)) {
return die('Plugin disabled');
}
$global['printLogs'] = 1;
// Enable error reporting
error_reporting(E_ALL); // Report all PHP errors
ini_set('display_errors', '1'); // Display errors on the browser
ini_set('display_startup_errors', '1'); // Display startup errors
WebRTC::checkAndUpdate();
WebRTC::stopServer();
WebRTC::startServer();

View file

@ -1,12 +1,11 @@
<?php
require_once dirname(__FILE__) . '/../../videos/configuration.php';
require_once dirname(__FILE__) . '/../../../videos/configuration.php';
if(!isCommandLineInterface()){
forbiddenPage('Must be a command line');
}
$objP = AVideoPlugin::getDataObject('WebRTC');
WebRTC::startServer();
?>

View file

@ -0,0 +1,11 @@
<?php
require_once dirname(__FILE__) . '/../../../videos/configuration.php';
if(!isCommandLineInterface()){
forbiddenPage('Must be a command line');
}
$objP = AVideoPlugin::getDataObject('WebRTC');
WebRTC::stopServer();
?>

View file

@ -11,67 +11,12 @@ $obj = AVideoPlugin::getDataObjectIfEnabled('WebRTC');
if (empty($obj)) {
return die('Plugin disabled');
}
$global['printLogs'] = 1;
// Define file paths
$availableFilePath = WebRTC::getWebRTC2RTMPAssetVersionFile();
$sourceExecutablePath = WebRTC::getWebRTC2RTMPAssetFile();
$executablePath = WebRTC::getWebRTC2RTMPFile();
$currentFilePath = WebRTC::getWebRTC2RTMPJsonFile();
$log = WebRTC::getWebRTC2RTMPLogFile();
// Enable error reporting
error_reporting(E_ALL); // Report all PHP errors
ini_set('display_errors', '1'); // Display errors on the browser
ini_set('display_startup_errors', '1'); // Display startup errors
// Helper function to read JSON files
function readJsonFile($filePath) {
if (!file_exists($filePath)) {
throw new Exception("File not found: $filePath");
}
$content = file_get_contents($filePath);
return json_decode($content, true);
}
// Main script
try {
// Read the JSON files
$currentData = readJsonFile($currentFilePath);
$availableData = readJsonFile($availableFilePath);
// Display versions
echo "Current version: " . $currentData['version'] . PHP_EOL;
echo "Available version: " . $availableData['version'] . PHP_EOL;
// Compare versions
if ($currentData['version'] !== $availableData['version']) {
echo "A new version is available. Do you want to update? (yes/no): ";
$input = trim(fgets(STDIN));
if (strtolower($input) === 'yes') {
// Execute update commands
echo "Stopping the current server..." . PHP_EOL;
exec("pkill WebRTC2RTMP", $output, $status);
if ($status !== 0) {
echo "Warning: Could not stop the server or it was not running." . PHP_EOL;
}
echo "Removing old executable..." . PHP_EOL;
unlink($executablePath);
echo "Copying new executable..." . PHP_EOL;
copy($sourceExecutablePath, $executablePath);
echo "Making new executable runnable..." . PHP_EOL;
chmod($executablePath, 0755);
echo "Starting the new server..." . PHP_EOL;
$command = "{$executablePath} --port={$obj->port} > $log ";
return execAsync($command);
echo "Update completed successfully!" . PHP_EOL;
} else {
echo "Update canceled." . PHP_EOL;
}
} else {
echo "You are already running the latest version." . PHP_EOL;
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}
WebRTC::checkAndUpdate();
WebRTC::startIfIsInactive();