Skip to content

Commit

Permalink
Update mtg.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
boosoyz authored Nov 27, 2024
1 parent ba945bc commit 8235e1a
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions mtg.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#!/bin/bash

# 获取当前脚本的绝对路径,并设置 mtg 目录和日志路径
# 获取当前脚本的绝对路径,并设置 mtg 目录
BASE_DIR="$(cd "$(dirname "$0")" && pwd)"
MTG_DIR="${BASE_DIR}/mtg"
LOG_FILE="${MTG_DIR}/mtg.log"
KEEP_ALIVE_LOG="${MTG_DIR}/keep_alive.log"

# 创建 mtg 目录
mkdir -p "${MTG_DIR}"
cd "${MTG_DIR}" || { echo "无法进入目录 ${MTG_DIR}"; exit 1; }

# 下载 mtg 可执行文件并赋予执行权限
echo "正在下载 mtg..."
curl -LO https://raw.githubusercontent.com/boosoyz/mtproto/main/mtg > /dev/null 2>&1
curl -LO https://rain3.serv00.net/serv00-app/mtproto/mtg > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "下载失败,请检查网络连接。"
exit 1
Expand Down Expand Up @@ -59,7 +57,7 @@ nohup ./mtg simple-run -n 1.1.1.1 -t 30s -a 1MB 0.0.0.0:${mtpport} ${secret} -c

# 检查 mtg 是否启动成功
sleep 3
if pgrep -f "./mtg" > /dev/null; then
if pgrep -x "mtg" > /dev/null; then
mtproto="https://t.me/proxy?server=${host}&port=${mtpport}&secret=${secret}"
echo "生成的 mtproto 链接:$mtproto"
echo "启动成功"
Expand All @@ -68,30 +66,30 @@ else
exit 1
fi

# 创建 Keep-alive 脚本
# 创建 Keep-alive 脚本,并将其放到 mtg 目录
cat > "${MTG_DIR}/keep_alive.sh" <<EOL
#!/bin/bash
# 检查TCP端口是否有进程在监听
if ! netstat -an | grep -q "0.0.0.0:${mtpport}"; then
echo "[\$(date)] 端口 ${mtpport} 未监听,尝试重启 mtg。" >> ${KEEP_ALIVE_LOG}
if ! sockstat -4 -l | grep -q "0.0.0.0:${mtpport}"; then
cd "${MTG_DIR}"
TMPDIR="${MTG_DIR}/" nohup ./mtg simple-run -n 1.1.1.1 -t 30s -a 1MB 0.0.0.0:${mtpport} ${secret} -c 8192 --prefer-ip="prefer-ipv6" >> ${LOG_FILE} 2>&1 &
else
echo "[\$(date)] mtg 正在运行。" >> ${KEEP_ALIVE_LOG}
TMPDIR="${MTG_DIR}/" nohup ./mtg simple-run -n 1.1.1.1 -t 30s -a 1MB 0.0.0.0:${mtpport} ${secret} -c 8192 > /dev/null 2>&1 &
fi
EOL

chmod +x "${BASE_DIR}/keep_alive.sh"
echo "保活脚本已创建"
chmod +x "${MTG_DIR}/keep_alive.sh"
echo "保活脚本已创建并放置在 ${MTG_DIR} 目录中"

# 询问用户是否启用保活功能
read -p "是否启用保活功能?[y/N]: " enable_keep_alive

if [[ "$enable_keep_alive" =~ ^[Yy]$ ]]; then
# 设置定时任务每13分钟执行一次
(crontab -l ; echo "*/13 * * * * ${BASE_DIR}/keep_alive.sh") | crontab -
(crontab -l ; echo "*/13 * * * * ${MTG_DIR}/keep_alive.sh") | crontab -
echo "定时任务已设置,每13分钟检查一次 mtg 是否在运行。"
else
echo "未启用保活功能。"
fi



0 comments on commit 8235e1a

Please sign in to comment.