Code tạo token
<?PHP$secret = ‘vietgithiviet’;
$id = $_GET[‘id’];
$ttl = 7200;
$ip = $_SERVER[‘REMOTE_ADDR’]; //nếu là local thì điền 127.0.0.1
$token = buildSecureLink($secret,$ttl,$ip);
function buildSecureLink($secret,$ttl,$ip){
$expires = time() + $ttl;
$md5 = md5(“$expires$ip $secret”, true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, ‘+/’, ‘-_’);
$md5 = str_replace(‘=’, ”, $md5);
return $md5.’/’.$expires;
};$m3u = ‘http://localhost/live/’.$token.’/’.$id.’.m3u8′;
echo $m3u;?>
Code bảo mật trong nginx
rewrite ^/live/(.*)/(.*)/(.*)$ /live/$3?token=$1&expires=$2 last;secure_link $arg_token,$arg_expires;
secure_link_md5 “$secure_link_expires$remote_addr vietgithiviet”;if ($secure_link = “”) { return 403; }
if ($secure_link = “0”) { return 410; }
Link xuất sẽ có dạng
http://localhost/live/’.$token.’/’.$id.’.m3u8
Tags:
PHP