-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_r
executable file
·134 lines (113 loc) · 4.46 KB
/
menu_r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
# only run as root
if [[ $EUID -gt 0 ]]; then
echo "Please run as root"
exit
fi
ptb_service_user=$(cat "$HOME"/.config/ptb-service-user)
# Set Github repository details
GITHUB_USER="BlurryFlurry"
REPO_NAME="tg-vps-manager"
LOCAL_REPO_DIR="/home/$ptb_service_user/bot"
VERSION_CACHE_FILE=/home/"$ptb_service_user"/.config/ptb-service-version.txt
# Go to the local directory
cd "$LOCAL_REPO_DIR" || exit
mkdir -p /home/"$ptb_service_user"/.config/
touch $VERSION_CACHE_FILE
# Get the latest tag from Github
LATEST_TAG=$(git ls-remote --tags "https://github.com/$GITHUB_USER/$REPO_NAME.git" | awk '{print $2}' | grep -v '{}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | sort -t. -k1,2 -g -k3 -g | tail -n1)
# set new update flag
set_new_update_flag() {
if [ ! -f "$VERSION_CACHE_FILE" ] || [ "$(cat "$VERSION_CACHE_FILE")" != "$LATEST_TAG" ]; then
new_update_flag=" | New update: ${LATEST_TAG}"
else
new_update_flag=""
fi
}
start_telegram_bot() {
if systemctl is-active --quiet ptb@"$ptb_service_user".service; then
echo "Telegram bot is running, Are you sure you want to stop it? [y/n]"
read -r choice
case $choice in
y | Y) sudo systemctl stop ptb@"$ptb_service_user".service ;;
*)
echo "Aborted."
return
;;
esac
else
echo "Starting Telegram bot..."
sudo systemctl start ptb@"$ptb_service_user".service
fi
}
update_telegram_bot() {
# Check if the latest tag is different from the one stored in the local directory
if [ ! -f "$VERSION_CACHE_FILE" ] || [ "$(cat "$VERSION_CACHE_FILE")" != "$LATEST_TAG" ]; then
# backup current release ID notes for trackers in update hooks
sudo -u $ptb_service_user cp release-id.txt ~/.release-id.old
# Pull the latest tag to the local repository
sudo -u $ptb_service_user git config --global --add safe.directory "$LOCAL_REPO_DIR"
sudo -u $ptb_service_user git reset --hard origin/main && git clean -f -d
sudo -u $ptb_service_user git checkout main -f
sudo -u $ptb_service_user git reset --hard origin/main && git clean -f -d
sudo -u $ptb_service_user git pull --tags "https://github.com/$GITHUB_USER/$REPO_NAME.git" "$LATEST_TAG"
chown $ptb_service_user:$ptb_service_user -R .
# Store the latest tag in a file in the local directory
echo "$LATEST_TAG" >"$VERSION_CACHE_FILE"
LATEST_TAG=$(git ls-remote --tags "https://github.com/$GITHUB_USER/$REPO_NAME.git" | awk '{print $2}' | grep -v '{}' | sort -V | tail -n1 | sed 's|refs/tags/||')
username=$ptb_service_user
curl -sSL -H "Cache-Control: no-cache, no-store, must-revalidate" -H "Expires: 0" -H "Pragma: no-cache" https://raw.githubusercontent.com/BlurryFlurry/dropbear_squid_stunnel_nodejs_proxy_badvpn_install/main/perm_fixer.sh?token="$(date +%s)" | sh -s -- $username
new_update_flag=""
fi
}
create_ssh_user() {
read -p "Username: " username
read -p "Password: " password
# Set default values for max_sessions and days
default_max_sessions=5
default_days=30
read -p "Maximum concurrent SSH sessions [default: $default_max_sessions]: " max_sessions
max_sessions=${max_sessions:-$default_max_sessions}
read -p "How many days you need this account from today [default: $default_days]: " days
days=${days:-$default_days}
useradd -M -s /usr/sbin/nologin -e $(date -d "+$days days" +%Y-%m-%d) "$username"
echo "$username:$password" | chpasswd
# Add limit for maximum concurrent SSH sessions
echo "$username hard maxlogins $max_sessions" | sudo tee -a /etc/security/limits.conf >/dev/null
}
change_timezone() {
dpkg-reconfigure tzdata
timedatectl set-ntp true
systemctl enable --now systemd-timesyncd.service
}
show_menu() {
while true; do
set_new_update_flag
ptb_status=$(systemctl is-active ptb@"$ptb_service_user".service)
ptb_action="Start"
if [ "$ptb_status" = "active" ]; then
ptb_action="Stop"
fi
clear
echo "----------------------------------"
echo " MENU "
echo "----------------------------------"
echo "1. $ptb_action Telegram bot"
echo "2. Update Telegram bot${new_update_flag}"
echo "3. Create SSH User"
echo "4. Change server timezone"
echo "5. Exit"
echo "----------------------------------"
echo "Enter a number:"
read -r choice
case $choice in
1) start_telegram_bot ;;
2) update_telegram_bot ;;
3) create_ssh_user ;;
4) change_timezone ;;
5) break ;;
*) echo "Invalid option, please choose again." ;;
esac
done
}
show_menu