openlist/alist使用yourls来添加短链接分享服务

openlist是一个用于多网盘聚合的工具,其受到非常多人的喜爱。但是openlist的url分享链接是又臭又长的,并且存在中文路径的话还会经历url编码,这会让单文件url分享显的非常不美观。

yourls是一款开源的短链接分享应用,我们可以利用其API来给我们的alist实现短链接分享。

一、环境配置

我们需要使用Nginx + PHP-FPM来解析php代码,帮我们实现openlist的url包装,因此需要具备Nginx + PHP-FPM的运行环境

sudo apt install nginx php php-fpm php-mysql php-cli php-curl php-mbstring php-xml mariadb-server mariadb-client
ShellScript
sudo systemctl list-units --type=service | grep fpm

#启动
sudo systemctl start php8.2-fpm
ShellScript

这里为php8.2-fpm

sudo systemctl enable mariadb
sudo systemctl start mariadb
ShellScript

一、搭建数据库

#登入数据库
sudo mariadb -u root -p

#创建数据库
CREATE DATABASE yourls CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

#设置用户名并授权
CREATE USER 'yourls用户名'@'localhost' IDENTIFIED BY 'yourl密码';
GRANT ALL PRIVILEGES ON yourls.* TO 'yourls用户名'@'localhost';
FLUSH PRIVILEGES;
EXIT;
ShellScript

二、搭建yourls

https://github.com/YOURLS/YOURLS/releases/latest

下载完成后解压到/var/www/htm/下,并且重命名文件夹为yourls,进入yourls,将user/config-sample.php复制一份为user/config.php,编辑user/config.php文件,将如下行修改为你的数据库配置

/** MySQL database username */
define( 'YOURLS_DB_USER', 'yourls用户名' );

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'yourls密码' );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', 'yourls' );

/** MySQL hostname.
 ** If using a non standard port, specify it like 'hostname:port', e.g. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', 'localhost' );

/** YOURLS installation URL */
define( 'YOURLS_SITE', 'https://你的yourls域名' );
PHP
#进入nginx配置文件夹
cd /etc/nginx/

#创建一个新配置
sv /etc/nginx/sites-available/yourls

#粘贴如下配置
server {
    listen 443;
    server_name 你的yourls域名;

    root /var/www/htm/yourls;

    location / {
        try_files $uri $uri/ /yourls-loader.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;#修改成你自己的版本
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\. {
        deny all;
    }

    client_max_body_size 100M;

    access_log /var/log/nginx/yourls.access.log;
    error_log /var/log/nginx/yourls.error.log;
}

#保存后退出并启用
sudo ln -s /etc/nginx/sites-available/yourls /etc/nginx/sites-enabled/

#重启nginx
sudo nginx -t && sudo systemctl reload nginx
ShellScript

注意,这里是没有配置ssl的,如果需要ssl的话请自行添加

不出意外的话就可以正常使用yourls了,如果存在问题的话请检查是否是权限问题

三、创建openlist包装站点

在/www/var/html/yourls下添加一个文件short.php,填入下列代码

<?php
// YOURLS 配置
$api       = "https://s.ytca.top/yourls-api.php";
$signature = "705e463d8f";
$safeDomain= "alist.ytca.top";

// 参数检查
if (empty($_GET['url'])) die("缺少参数 url");
$rawUrl = urldecode($_GET['url']);
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL)) die("非法 URL 参数");

$parsed = parse_url($rawUrl);
if (empty($parsed['scheme']) || !in_array(strtolower($parsed['scheme']), ['http','https']))
    die("仅支持 http/https 协议");

// 路径 & 参数(去掉 openlist_ts时间戳)
$path  = $parsed['path'] ?? '';
$query = '';
if (!empty($parsed['query'])) {
    parse_str($parsed['query'], $params);
    unset($params['openlist_ts']);
    if ($params) $query = '?' . http_build_query($params);
}

// 拼接安全URL
$safeUrl = "https://{$safeDomain}{$path}{$query}";

// 使用curl调用 YOURLS API
$req = "$api?signature=$signature&action=shorturl&format=simple&url=" . urlencode($safeUrl);
$ch  = curl_init($req);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT      => 'Mozilla/5.0'
]);
$shortUrl = curl_exec($ch);
curl_close($ch);

$success     = !empty($shortUrl);
$errorMsg    = $success ? '' : '短链生成失败,请稍后重试。';
$downloadUrl = htmlspecialchars($rawUrl, ENT_QUOTES, 'UTF-8');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>短链生成</title>
<style>
body {
    font-family: "Segoe UI", Tahoma, sans-serif;
    background: #f4f6f8;
    display: flex; justify-content: center; align-items: center;
    min-height: 100vh; margin: 0;
}
.container {
    background: #fff; padding: 30px 20px; border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    width: 100%; max-width: 600px;
}
h2 { text-align: center; color: #333; margin-bottom: 20px; }
input[type="text"], button, a.download-btn {
    width: 100%; padding: 12px; font-size: 14px;
    border-radius: 6px; box-sizing: border-box; margin-bottom: 10px;
}
input[type="text"] {
    border: 1px solid #ccc; background: #fff; color: #333; text-align: center;
}
button, a.download-btn {
    display: block; width: 100%; padding: 12px; font-size: 16px; font-family: inherit;
    line-height: 1.2; color: #fff; background: #4CAF50; border: none; border-radius: 6px;
    cursor: pointer; text-align: center; text-decoration: none; transition: background 0.2s;
    margin-bottom: 10px; box-sizing: border-box;
}
button:hover, a.download-btn:hover { background: #45a049; }
#copy-msg, #error-msg { text-align: center; font-size: 14px; height: 20px; }
#error-msg { color: #d9534f; margin-top: 10px; }
</style>
</head>
<body>
<div class="container">
    <h2>短链生成器</h2>
    <?php if($success): ?>
        <input type="text" id="short" value="<?=htmlspecialchars($shortUrl)?>" readonly>
        <button onclick="copy()">复制短链</button>
        <a class="download-btn" href="<?=$downloadUrl?>" download>下载文件</a>
        <div id="copy-msg"></div>
    <?php else: ?>
        <div id="error-msg"><?=$errorMsg?></div>
    <?php endif; ?>
</div>
<script>
function copy(){
    let input=document.getElementById("short");
    input.select(); document.execCommand("copy");
    let msg=document.getElementById("copy-msg");
    msg.textContent="短链已复制到剪贴板 ✅";
    setTimeout(()=>msg.textContent="",3000);
}
</script>
</body>
</html>
 
PHP

访问yourls域名/short.php?url=alist域名路径即可打开这个openlist中转,我的php代码会对请求域名进行包装限制,会强制替换访问的域名,防止任意文件下载,让其只能访问alist的文件下载

四、openlist添加后缀名预览

在openlist的设置页面修改Iframe 预览为下列配置:

{
	"doc,docx,xls,xlsx,ppt,pptx": {
		"Microsoft": "https://view.officeapps.live.com/op/view.aspx?src=$e_url",
		"Google(需要科学上网)": "https://docs.google.com/gview?url=$e_url&embedded=true"
	},
	"pdf": {
		"PDF-Github": "https://alist-org.github.io/pdfjs/web/viewer.html?file=$e_url"
	},
	"epub": {
		"EPUB.js": "https://alist-org.github.io/static/epub.js/viewer.html?url=$durl"
	},
	"txt,htm,html,css,mjs,php,xml,jsp,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx,vtt,srt,ass,rs,lrc,strm": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"mp3,flac,ogg,m4a,wav,opus,wma,acc": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"mp4,mkv,avi,mov,rmvb,webm,flv,m3u8": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"jpg,tiff,jpeg,png,gif,bmp,svg,ico,swf,webp,avif": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"zip,rar,7z,tar,gz,bz2,xz,iso,dmg": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"txt,doc,docx,pdf,xls,xlsx,ppt,pptx,odt,ini,conf,cfg,env": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	},
	"exe,bat,app,jar,py,apk,deb": {
		"短链接分享(下载请点我选择Dowload)": "https://yourls域名/short.php?url=$e_durl"
	}
}
ShellScript

如果你还有更多后缀分析的话也可以手动添加上相应后缀名


📌 本文由 FishBoss_Tca 原创,转载请注明作者和原文链接。
原文链接:https://www.ytca.top/guidance/linux/2392/

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇