-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathsetup.sh
executable file
·186 lines (156 loc) · 5.25 KB
/
setup.sh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bash
# Bash3 Boilerplate. Copyright (c) 2014, kvz.io
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app
red() { echo -e "$(tput setaf 1)$*$(tput setaf 9)"; }
green() { echo -e "$(tput setaf 2)$*$(tput setaf 9)"; }
yellow() { echo -e "$(tput setaf 3)$*$(tput setaf 9)"; }
if [[ ! -d /run/systemd/system ]]; then
red "> Sorry but this scripts is only for Linux Dist with systemd, eg: Ubuntu 16.04+/Centos 7+ ..."
exit 1
fi
BINEXEC=/usr/local/bin/mtg
CalUnixTsDrifft() {
local cfts=$(wget -qO- https://www.cloudflare.com/cdn-cgi/trace | grep ts= | cut -d'=' -f2 | cut -d'.' -f1)
local localts=$(date +"%s")
local drifft=$(($localts-$cfts))
# abs value
echo ${drifft#-}
}
GetBinTag() {
if [[ $(uname -s) != "Linux" ]]; then
red "unsupported OS: $(uname -s)"
exit 1
fi
case $(uname -m) in
x86_64)
echo "linux-amd64"
;;
i*86)
echo "linux-386"
;;
aarch64)
echo "linux-arm64"
;;
arm*)
echo "linux-arm"
;;
*)
red "unsupported OSARCH: $OSARCH"
exit 1
;;
esac
}
DEFINPUT () {
local DEFAULT=$1
local INPUT
read INPUT
if [[ -z $INPUT ]]; then
echo "$DEFAULT"
else
echo "$INPUT"
fi
}
DLMTG() {
DLTEMP=$(mktemp --suffix=.tar.gz)
EXTMPDIR=$(mktemp -d)
trap 'echo Signal caught, cleaning up >&2; cd /; /bin/rm -rf "$DLTEMP" "EXTMPDIR"; exit 15' 1 2 3 15
yellow "> Downloading mtg binary ..."
wget -qO- https://api.github.com/repos/9seconds/mtg/releases/latest \
| grep browser_download_url | grep $(GetBinTag) | cut -d '"' -f 4 \
| wget -q -i- -O $DLTEMP
if [[ ! -f $DLTEMP ]]; then
red "> Failed to download ..."
exit 1
fi
yellow "> Extracting ..."
tar xvf $DLTEMP --strip-components=1 -C $EXTMPDIR
systemctl stop mtg || true
yellow "> Install mtg binary ..."
install -v -m755 $EXTMPDIR/mtg $BINEXEC
yellow "> Cleanup tmp ..."
rm -rf $DLTEMP $EXTMPDIR
}
LOCALMTG() {
local mtgtar=$1
if [[ ! -f $mtgtar ]]; then mtgtar=$USER_PWD/$mtgtar; fi
if [[ ! -f $mtgtar ]]; then
red "> $mtgtar does not exists"
exit 1
fi
yellow "> Local tar found, using offline install mode"
EXTMPDIR=$(mktemp -d)
trap 'echo Signal caught, cleaning up >&2; cd /; /bin/rm -rf "EXTMPDIR"; exit 15' 1 2 3 15
yellow "> Extracting ..."
tar xvf $mtgtar --strip-components=1 -C $EXTMPDIR
systemctl stop mtg || true
yellow "> Install mtg binary ..."
install -v -m755 $EXTMPDIR/mtg $BINEXEC
yellow "> Cleanup tmp ..."
rm -rf $EXTMPDIR
}
SHOWACCESS(){
journalctl -u mtg --since today --no-pager
yellow "> Setup mtproxy in telegram with following URL: "
if command -v jq >/dev/null 2>&1; then
yellow "> IPv6 Access"
$BINEXEC access /etc/mtg.toml | jq -r .ipv6.tme_url
yellow "> IPv4 Access"
$BINEXEC access /etc/mtg.toml | jq -r .ipv4.tme_url
else
$BINEXEC access /etc/mtg.toml | grep tme_url
fi
yellow "> To checkout all available urls, run \`mtg access /etc/mtg.toml'"
green "> Bye."
}
arg1="${1:-}"
if [[ -f /etc/mtg.toml ]]; then
yellow "> Previos config (/etc/mtg.toml) exists, only upgrade the mtg binary."
if [[ ! -z $arg1 ]]; then LOCALMTG $arg1; else DLMTG; fi
yellow "> Installed mtg version: `$BINEXEC --version`"
systemctl restart mtg
yellow "> Upgrade success. Here shows the recent logs"
SHOWACCESS
exit 0
fi
PORT=$(shuf -i 2000-65000 -n1)
FAKEDOMAIN=hostupdate.vmware.com
green "=================================================="
yellow "> Input service PORT, or press Enter to use a random port"
PORT=$(DEFINPUT $PORT)
yellow "> Input a domain for FakeTLS, or press Enter to use \"$FAKEDOMAIN\""
FAKEDOMAIN=$(DEFINPUT $FAKEDOMAIN)
green "=================================================="
yellow "> Using: PORT: ${PORT}, FakeTLS DOMAIN : ${FAKEDOMAIN}"
green "=================================================="
if [[ ! -z $arg1 ]]; then LOCALMTG $arg1; else DLMTG; fi
yellow "> Installed mtg version: `$BINEXEC --version`"
SECRET=$($BINEXEC generate-secret "$FAKEDOMAIN")
sed -i "s/#PORT#/$PORT/;s/#SECRET#/$SECRET/" $__dir/conf/mtg.toml
if [[ $(CalUnixTsDrifft) -gt 5 ]]; then
skew=$(CalUnixTsDrifft)
yellow ">>>> This host has a time drift of $skew seconds, setting mtg to tolerate."
skew=$(($skew+5))
sed -i "s/#SKEW#/${skew}s/" $__dir/conf/mtg.toml
else
sed -i "s/#SKEW#/5s/" $__dir/conf/mtg.toml
fi
install -v -m644 $__dir/conf/mtg.service /etc/systemd/system/
install -v -m644 $__dir/conf/mtg.toml /etc/
systemctl daemon-reload
systemctl enable mtg
systemctl start mtg
yellow "=================================================="
green "> Installation success. Wait for service to load ..."
yellow "> Generated Secret: ${SECRET}"
yellow "> Listening Port: ${PORT}"
yellow "=================================================="
sleep 2
SHOWACCESS