TelePic是一个基于 Telegraph 网站的图床项目。它使用 PHP 和 jQuery 编写,可以将图片上传到 Telegraph 服务器并获取代理后的链接。
通过Nginx Basic HTTP authentication实现了只有特定用户才能上传图片,通过SQLite3实现只有被从本域名上传的图片才能被访问,从而避免了被滥用和被偷盗域名链接。
原始程序https://github.com/Fangsongs/TelePic,使用了Nginx Basic HTTP authentication和SQLite的修改版https://github.com/uselibrary/TelePic,暂时不确定后续是否会合并到主线中。
https://telegra.ph/file/731ee38f95a6eb0f69833.jpg # 这是直接上传到telegra.ph的图片
https://image.196629.xyz/file/731ee38f95a6eb0f69833.jpg # 修改域名即可访问,域名被盗窃
/file/
路径映射到 proxy.php
文件。php-curl
)是否开启。index.html
文件即可使用。以下教程基于Debian和Nginx为例。注意备份数据库,由于采用了sqlite3,数据库为单文件,直接复制到安全位置进行保存即可。
首先,安装sqlite3和php-sqlite3
apt install sqlite3
apt install php-sqlite3
新建一个sqlite数据库
sqlite3 uploads.db # 新建并进入数据库
.exit # 在数据库内输入以退出
设置权限,示例路径为/path/to
,需要更改为实际路径
chmod 664 /path/to/uploads.db
chmod 775 /path/to
chown www-data:www-data /path/to/uploads.db
chown www-data:www-data /path/to
修改upload.php
和proxy.php
中的数据库路径。此两个文件中,文件靠前位置包含如下,需将path/to/
修改为实际位置。
// Connect to the SQLite database
$db = new SQLite3('/path/to/uploads.db');
如果需要查看数据库内的内容,可以使用SELECT * FROM uploads;
查看。
安装htpasswd软件
apt install apache2-utils
生成访问密码
htpasswd -c /etc/nginx/password username # 此处的username可任意设置
首次生成需要添加-c,此后如果想添加更多的用户,则直接:
htpasswd /etc/nginx/password another_username
编辑网站的Nginx配置文件
个人习惯将网站的Nginx配置文件放到/etc/nginx/sites-available
中,并将其软链接到/etc/nginx/sites-enabled
中,此处以默认配置文件/etc/nginx/sites-available/default
为例。
找到location /
位置,添加auth_basic "Restricted Content";
和auth_basic_user_file /etc/nginx/.htpasswd;
两行配置,实际如下:
location / {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/password;
try_files $uri $uri/ =404;
}
修改upload.php
和proxy.php
中的权限,用户需要为www-data
,权限需要为755
,即
chown -R www-data:www-data /path/to # 这里的/path/to一般是/var/ww/html,必须按照实际情况进行配置
chmod -R 755 /path/to # 注意/path/to
在 Nginx 的配置文件中添加以下代码:
location /file/ {
rewrite ^/file/(.*)$ /proxy.php?$query_string;
}
其中 $query_string
是 GET 请求参数部分,保证在代理时能够正确传递参数。