Skip to content

Commit

Permalink
feat: 增加一件脚本来关闭 https 重定向
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Jun 27, 2023
1 parent 3fbca1c commit ea41ea8
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ RUN pnpm config set network-timeout 600000 -g
RUN pnpm config set registry https://registry.npmmirror.com -g
RUN pnpm config set fetch-retries 20 -g
RUN pnpm config set fetch-timeout 600000 -g
# 复制 cli 工具
WORKDIR /app/cli
COPY ./packages/cli/ ./
RUN pnpm i
# 安装 waline
WORKDIR /app/waline
COPY ./packages/waline/ ./
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ curl -L https://vanblog.mereith.com/vanblog.sh -o vanblog.sh && chmod +x vanblog
>
> [更新后后台报错||一直加载中](https://vanblog.mereith.com/faq/update.html#升级后后台报错或持续加载)
>
> [开启了 https 重定向后关不掉](https://vanblog.mereith.com/faq/usage.html#开启了-https-重定向后关不掉)
>
> [更多常见问题](https://vanblog.mereith.com/faq/)
## 关于更新
Expand Down Expand Up @@ -156,6 +158,7 @@ curl -L https://vanblog.mereith.com/vanblog.sh -o vanblog.sh && chmod +x vanblog
- [敲代码的小盆友](https://code-child.cn)
- [Tim's Life](https://zouchanglin.cn)
- [吾・子风](https://5z5f.com/tag)
- [LinKinHan'Blog](https://www.linkinhan.top)
<!-- - [耿老师](https://transfer.teachergeng.space) -->


Expand Down
26 changes: 24 additions & 2 deletions docs/.vuepress/public/vanblog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
VANBLOG_BASE_PATH="/var/vanblog"
VANBLOG_DATA_PATH="${VANBLOG_BASE_PATH}/data"
VANBLOG_DATA_PATH_RAW="\/var\/vanblog\/data"
VANBLOG_SCRIPT_VERSION="v0.1.5"
VANBLOG_SCRIPT_VERSION="v0.1.6"

COMPOSE_URL="https://vanblog.mereith.com/docker-compose-template.yml"
SCRIPT_URL="https://vanblog.mereith.com/vanblog.sh"
Expand Down Expand Up @@ -283,6 +283,12 @@ update() {

}

reset_https() {
echo -e "> 重置 https 设置(需要先启动 vanblog)"
cd $VANBLOG_BASE_PATH && docker-compose exec vanblog node /app/cli/resetHttps.js
before_show_menu
}

start_vanblog() {
echo -e "> 启动 VanBlog"

Expand Down Expand Up @@ -369,6 +375,7 @@ show_usage() {
echo "./vanblog.sh update - 更新 VanBlog"
echo "./vanblog.sh log - 查看 VanBlog 日志"
echo "./vanblog.sh uninstall - 卸载 VanBlog"
echo "./vanblog.sh reset_https - 重置 https 设置"
echo "--------------------------------------------------------"
echo "./vanblog.sh update_script - 更新此脚本"
echo "--------------------------------------------------------"
Expand All @@ -386,10 +393,13 @@ show_menu() {
${green}6.${plain} 更新
${green}7.${plain} 查看日志
${green}8.${plain} 卸载
${green}9.${plain} 重置 https 设置
————————————————-
${green}20.${plain} 更新此脚本
${green}30.${plain} 查看脚本使用说明
${green}0.${plain} 退出脚本
"
echo && read -ep "请输入选择 [0-8]: " num
echo && read -ep "请输入选择 [0-30]: " num

case "${num}" in
0)
Expand Down Expand Up @@ -419,6 +429,15 @@ show_menu() {
8)
uninstall_vanblog
;;
9)
reset_https
;;
20)
update_script
;;
30)
show_usage
;;
*)
echo -e "${red}请输入正确的数字 [0-8]${plain}"
;;
Expand Down Expand Up @@ -456,6 +475,9 @@ if [[ $# > 0 ]]; then
"uninstall")
uninstall_vanblog 0
;;
"reset_https")
reset_https 0
;;
*) show_usage ;;
esac
else
Expand Down
13 changes: 13 additions & 0 deletions docs/faq/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ VanBlog 自 `v0.42.0` 已舍弃 `VAN_BLOG_ALLOW_DOMAINS` 环境变量,如果
默认粘贴的格式可能带有一些额外信息,你可以鼠标右键选择复制为纯文本,或者使用快捷键 <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>V</kbd>。

![粘贴示例](https://pic.mereith.com/img/88b29bad4ad0ef7d6e411e43f80ec1bc.clipboard-2022-08-22.png)

## 开启了 https 重定向后关不掉
现在有脚本可以一键重置 https 设置啦!

如果你是用的一件脚本安装的,那么重新加载一遍最新版脚本,在里面选择重置 https 设置即可。
```bash
curl -L https://vanblog.mereith.com/vanblog.sh -o vanblog.sh && chmod +x vanblog.sh && ./vanblog.sh
```

如果你是自己用 docker 部署的运行下面的命令即可:
```bash
docker exec -it <vanblog容器名> node /app/cli/resetHttps.js
```
1 change: 1 addition & 0 deletions packages/admin/src/pages/SystemConfig/tabs/Caddy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default function (props) {
</p>
<p>稳定后可打开 https 自动重定向功能,开启通过 http 访问将自动跳转至 https </p>
<p>如果你用了 80 端口反代,请不要开启 https 自动重定向!否则你的反代可能会失效。</p>
<p>如果不小心开启了此选项后关不掉,可以参考:<a href="https://vanblog.mereith.com/faq/usage.html#开启了-https-重定向后关不掉" target='_blank'>开启了 https 重定向后关不掉</a></p>
</div>
}
style={{ marginBottom: 20 }}
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# VanBlog CLI

这个包存放了一些命令行工具。
15 changes: 15 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "vanblog-cli",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^5.1.0"
}
}
67 changes: 67 additions & 0 deletions packages/cli/resetHttps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env node

const {MongoClient} = require('mongodb');


const uri = "mongodb://mongo:27017/vanBlog?authSource=admin";

const readString = (prompt) => {
process.stdout.write(prompt);
return new Promise((resolve, reject) => {
process.stdin.once('data', (data) => {
resolve(data.toString().trim());
});
});
}

const parseDBfromURI = (uri) => {
const obj = new URL(uri);
return obj.pathname.slice(1);

}


const main = async () => {
const uriFromUser = await readString('输入 MongoDB 连接 URL(如果看不懂或者使用的默认配置,请直接按回车): \n ');
const uriToUse = uriFromUser || uri;
const db = parseDBfromURI(uriToUse)
console.log("使用的 MongoDB 连接 URL: ", uriToUse, "数据库:", db);

const client = new MongoClient(uriToUse);
console.log("尝试连接数据库...")
try {
await tryConnectDB(client);
console.log("连接数据库成功")
}catch(err) {
console.log("连接数据库失败:", err);
process.exit(1);
}
await resetHttps(client,db);
await client.close();
process.exit(0);
};

const tryConnectDB = (client) => {
return new Promise((resolve, reject) => {
setTimeout(()=>{
reject(new Error("连接数据库超时"))
},5000)
client.connect().then(resolve).catch(err => reject(err))
})
}

const resetHttps = async (client,dbName) => {
try {
await client.connect();

const db = client.db(dbName);
const col = db.collection("settings");
const result = await col.deleteOne({type:"https"})
console.log("删除 HTTPS 设置成功,删除的条目数:", result.deletedCount)
console.log("重启 vanblog 后生效")
}catch (err) {
console.log("重制 HTTPS 出错:", err);
}
}

main();
25 changes: 11 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions scripts/vanblog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
VANBLOG_BASE_PATH="/var/vanblog"
VANBLOG_DATA_PATH="${VANBLOG_BASE_PATH}/data"
VANBLOG_DATA_PATH_RAW="\/var\/vanblog\/data"
VANBLOG_SCRIPT_VERSION="v0.1.5"
VANBLOG_SCRIPT_VERSION="v0.1.6"

COMPOSE_URL="https://vanblog.mereith.com/docker-compose-template.yml"
SCRIPT_URL="https://vanblog.mereith.com/vanblog.sh"
Expand Down Expand Up @@ -283,6 +283,12 @@ update() {

}

reset_https() {
echo -e "> 重置 https 设置(需要先启动 vanblog)"
cd $VANBLOG_BASE_PATH && docker-compose exec vanblog node /app/cli/resetHttps.js
before_show_menu
}

start_vanblog() {
echo -e "> 启动 VanBlog"

Expand Down Expand Up @@ -369,6 +375,7 @@ show_usage() {
echo "./vanblog.sh update - 更新 VanBlog"
echo "./vanblog.sh log - 查看 VanBlog 日志"
echo "./vanblog.sh uninstall - 卸载 VanBlog"
echo "./vanblog.sh reset_https - 重置 https 设置"
echo "--------------------------------------------------------"
echo "./vanblog.sh update_script - 更新此脚本"
echo "--------------------------------------------------------"
Expand All @@ -386,10 +393,13 @@ show_menu() {
${green}6.${plain} 更新
${green}7.${plain} 查看日志
${green}8.${plain} 卸载
${green}9.${plain} 重置 https 设置
————————————————-
${green}20.${plain} 更新此脚本
${green}30.${plain} 查看脚本使用说明
${green}0.${plain} 退出脚本
"
echo && read -ep "请输入选择 [0-8]: " num
echo && read -ep "请输入选择 [0-30]: " num

case "${num}" in
0)
Expand Down Expand Up @@ -419,6 +429,15 @@ show_menu() {
8)
uninstall_vanblog
;;
9)
reset_https
;;
20)
update_script
;;
30)
show_usage
;;
*)
echo -e "${red}请输入正确的数字 [0-8]${plain}"
;;
Expand Down Expand Up @@ -456,6 +475,9 @@ if [[ $# > 0 ]]; then
"uninstall")
uninstall_vanblog 0
;;
"reset_https")
reset_https 0
;;
*) show_usage ;;
esac
else
Expand Down

0 comments on commit ea41ea8

Please sign in to comment.