esi20
July 24th, 2020, 21:10
سلام و درود
قصد دارم با استفاده از ماژول secure_link لینک هایی با مدت زمان اعتبار معلوم ایجاد کنم ولی کلا 403 میگیرم.
server {
listen 80;
server_name sub.domain.xyz;
location / {
alias /home/www;
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$secure_link_expires$uri$remote_addr MySecret";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 410;
}
}
}
کدهای فوق مربوط به کانفیگ nginx هست و لینک هم توسط کد PHP زیر ایجاد میکنم.
<?php
/**
* @param $baseUrl - non protected part of the URL including hostname, e.g. http://example.com
* @param $path - protected path to the file, e.g. /downloads/myfile.zip
* @param $secret - the shared secret with the nginx server. Keep this info secure!!!
* @param $ttl - the number of seconds until this link expires
* @param $userIp - ip of the user allowed to download
* @return string
*/
function buildSecureLink($baseUrl, $path, $secret, $ttl, $userIp)
{
$expires = time() + $ttl;
$md5 = md5("$expires$path$userIp $secret", true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, '+/', '-_');
$md5 = str_replace('=', '', $md5);
return $baseUrl . $path . '?md5=' . $md5 . '&expires=' . $expires;
}
// example usage
$secret = 'MySecret';
$baseUrl = 'http://sub.domain.xyz';
$path = '/path/to/file.zip';
$ttl = 120; //no of seconds this link is active
$userIp = '195.99.99.99'; // normally you would read this from something like $_SERVER['REMOTE_ADDR'];
echo buildSecureLink($baseUrl, $path, $secret, $ttl, $userIp);
ممنون میشم عنایت بفرمایید.
قصد دارم با استفاده از ماژول secure_link لینک هایی با مدت زمان اعتبار معلوم ایجاد کنم ولی کلا 403 میگیرم.
server {
listen 80;
server_name sub.domain.xyz;
location / {
alias /home/www;
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$secure_link_expires$uri$remote_addr MySecret";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 410;
}
}
}
کدهای فوق مربوط به کانفیگ nginx هست و لینک هم توسط کد PHP زیر ایجاد میکنم.
<?php
/**
* @param $baseUrl - non protected part of the URL including hostname, e.g. http://example.com
* @param $path - protected path to the file, e.g. /downloads/myfile.zip
* @param $secret - the shared secret with the nginx server. Keep this info secure!!!
* @param $ttl - the number of seconds until this link expires
* @param $userIp - ip of the user allowed to download
* @return string
*/
function buildSecureLink($baseUrl, $path, $secret, $ttl, $userIp)
{
$expires = time() + $ttl;
$md5 = md5("$expires$path$userIp $secret", true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, '+/', '-_');
$md5 = str_replace('=', '', $md5);
return $baseUrl . $path . '?md5=' . $md5 . '&expires=' . $expires;
}
// example usage
$secret = 'MySecret';
$baseUrl = 'http://sub.domain.xyz';
$path = '/path/to/file.zip';
$ttl = 120; //no of seconds this link is active
$userIp = '195.99.99.99'; // normally you would read this from something like $_SERVER['REMOTE_ADDR'];
echo buildSecureLink($baseUrl, $path, $secret, $ttl, $userIp);
ممنون میشم عنایت بفرمایید.