<?php
$channel = isset($_GET['channel']) ? $_GET['channel'] : (isset($argv[1]) ? $argv[1] : null);
$clientId = $_SERVER['REMOTE_ADDR'];
if (!$channel || !$clientId) {
exit;
}
$macFile = "/volume1/stalker/89/89mac.txt";
$clientMacFile = "/volume1/stalker/89/client_macs.json";
$macAddresses = file($macFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!$macAddresses) {
exit;
}
if (!file_exists($clientMacFile)) {
file_put_contents($clientMacFile, json_encode([], JSON_PRETTY_PRINT));
}
$clientMacs = json_decode(file_get_contents($clientMacFile), true);
$usedMacs = array_values($clientMacs);
$availableMacs = array_diff($macAddresses, $usedMacs);
function generateRandomToken($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomToken = '';
for ($i = 0; $i < $length; $i++) {
$randomToken .= $characters[rand(0, $charactersLength - 1)];
}
return $randomToken;
}
$url = "http://89.45.13.220:8080";
if (substr($channel, -4) === '.mkv') {
$selectedMac = $availableMacs[array_rand($availableMacs)];
$token = generateRandomToken();
$constructedUrl = "$url/play/movie.php?mac=$selectedMac&stream=$channel&play_token=$token&type=movie";
header('Location: ' . $constructedUrl);
exit;
}
function isChannelAvailableForMac($mac, $channel) {
$macFolder = "/volume1/stalker/89/data/" . str_replace(':', '_', $mac);
$liveStreamsFile = "$macFolder/live_streams.txt";
if (!file_exists($liveStreamsFile)) {
return false;
}
$streams = file($liveStreamsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
return in_array($channel, $streams);
}
function getStreamingLink($url, $channel, $selectedMac, $token) {
$encodedChannel = urlencode("http://localhost/ch/{$channel}_");
$opts = [
"http" => [
"header" => "User-Agent: Mozilla/5.0"
]
];
$context = stream_context_create($opts);
$response = @file_get_contents("$url/portal.php?type=itv&action=create_link&cmd=$encodedChannel&JsHttpRequest=1-xml&mac=$selectedMac&token=$token", false, $context);
$http_response_header = $http_response_header ?? [];
$error_codes = [401, 405, 456];
foreach ($http_response_header as $header) {
foreach ($error_codes as $code) {
if (strpos($header, (string)$code) !== false) {
return ['error' => true, 'streamingLink' => null];
}
}
}
if ($response !== false) {
$response = json_decode($response, true);
if (isset($response['js']['cmd'])) {
$urlPart = str_replace('\/', '/', $response['js']['cmd']);
$streamingLink = substr($urlPart, strpos($urlPart, 'http://'));
return ['error' => false, 'streamingLink' => $streamingLink];
}
}
return ['error' => true, 'streamingLink' => null];
}
$selectedMac = null;
$availableMacs = array_values($availableMacs);
shuffle($availableMacs);
foreach ($availableMacs as $mac) {
if (isChannelAvailableForMac($mac, $channel)) {
$selectedMac = $mac;
break;
}
}
if (!$selectedMac) {
exit;
}
$clientMacs[$clientId] = $selectedMac;
file_put_contents($clientMacFile, json_encode($clientMacs, JSON_PRETTY_PRINT));
$streamingLink = null;
$selectedMac = trim($selectedMac);
$response = @file_get_contents("$url/portal.php?type=stb&action=handshake&JsHttpRequest=1-xml&mac=$selectedMac");
if ($response !== false) {
$response = json_decode($response, true);
$token = $response['js']['token'] ?? null;
if ($token) {
do {
$result = getStreamingLink($url, $channel, $selectedMac, $token);
if (!$result['error']) {
$streamingLink = $result['streamingLink'];
} else {
unset($clientMacs[$clientId]);
file_put_contents($clientMacFile, json_encode($clientMacs, JSON_PRETTY_PRINT));
header('Location: ' . $_SERVER['PHP_SELF'] . '?channel=' . $channel);
exit;
}
$responseHeaders = @get_headers($streamingLink, 1);
} while (empty($responseHeaders['Location']) && !empty($availableMacs));
if (empty($responseHeaders['Location'])) {
unset($clientMacs[$clientId]);
file_put_contents($clientMacFile, json_encode($clientMacs, JSON_PRETTY_PRINT));
header('Location: ' . $_SERVER['PHP_SELF'] . '?channel=' . $channel);
exit;
}
}
}
if (!$streamingLink) {
unset($clientMacs[$clientId]);
file_put_contents($clientMacFile, json_encode($clientMacs, JSON_PRETTY_PRINT));
header('Location: ' . $_SERVER['PHP_SELF'] . '?channel=' . $channel);
exit;
}
header('Location: ' . $streamingLink);
?>