生成ip和限时下载链接

2024-04-23

有一个用于下载文件的直接链接。用户可以在付款后下载该链接,如下所示:

http://example.com/download/webapp.rar

但我需要生成ip和时间限制的下载链接,以防止其他人窃取该文件。我想在不使用任何数据库的情况下执行此操作。像这样的东西:

http://example.com/download.php?a5fds588fgdf

or

http://example.com/download/a5fds588fgdf

有什么建议吗?


有一个非常好的 nginx 模块可以做到这一点。

URL 有两个参数 - 我们称它们为 s(安全性)和 t(时间戳)。安全性是根据时间戳、路径和盐生成的安全哈希(在您的情况下只需添加 ip)。

$ip = $_SERVER['REMOTE_ADDR'];
$salt = 'change me cause im not secure';
$path = '/download/webapp.rar';
$timestamp = time() + 3600; // one hour valid
$hash = md5($salt . $ip . $timestamp . $path); // order isn't important at all... just do the same when verifying
$url = "http://mysite.com{$path}?s={$hash}&t={$timestamp}"; // use this as DL url

核实:

$ip = $_SERVER['REMOTE_ADDR'];
$salt = 'change me cause im not secure';
$path = $_SERVER['REQUEST_URI'];
$hashGiven = $_GET['s'];
$timestamp = $_GET['t'];
$hash = md5($salt . $ip . $timestamp . $path);
if($hashGiven == $hash && $timestamp <= time()) {
    // serve file
} else {
    die('link expired or invalid');
}

现在您只需将下载重写为“中间人”脚本即可。

nginx 重写示例:

location /download {
    rewrite ^.*$ /download.php last;
    break;
}

我不太熟悉 apache 重写,所以你可以自己检查一下。

如果您使用以下模块之一,则不需要自己验证所有这些,并且它在性能方面要好得多,但请注意,它提供更多配置,有时还提供另一种生成 url 和哈希的方法(请参阅此处的模块文档)。

或者你只使用 nginx 安全链接模块:http://wiki.nginx.org/HttpSecureLinkModule http://wiki.nginx.org/HttpSecureLinkModule

还有一个 Lighty 的吊坠:http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload

或者nginx安全下载模块:http://wiki.nginx.org/HttpSecureDownload http://wiki.nginx.org/HttpSecureDownload

也许 apache 也有一些东西...也许你可以在那里重写一些东西...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

生成ip和限时下载链接 的相关文章

随机推荐