Last active
May 18, 2022 07:01
-
-
Save fengjijiao/787a009009e6fe83340323be0fd988b4 to your computer and use it in GitHub Desktop.
simply to install vtun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RELEASE_API="https://api.github.com/repos/net-byte/vtun/releases/latest" | |
BIN_DST_DIR=/usr/local/bin | |
RESOURCE_DST_DIR=/usr/local/etc/vtun | |
CIDR_V4="192.168.90.0/24" | |
CIDR_V6="fced:9999::/64" | |
SIP_V4="192.168.90.1" | |
SIP_V6="fced:9999::1" | |
RUNNING_LISTENER="127.0.0.1:8002" | |
DEFAULT_KEY="defaultkey" | |
function getReleaseInfo() { | |
response=$(curl ${RELEASE_API}) | |
if [ $? == 0 ]; then | |
echo $response | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function check_error() { | |
if [ $? == 1 ]; then | |
echo $1 | |
exit 1 | |
fi | |
} | |
function get_asset_name() { | |
echo "vtun-$1-$2" | |
} | |
function install() { | |
releaseData=$(getReleaseInfo) | |
check_error "get release data error!" | |
name=$(echo $releaseData | jq -r '.name') | |
assets=$(echo $releaseData | jq -r '.assets') | |
assetsSize=$(echo $releaseData | jq -r '.assets | length') | |
expectedAssetName=$(get_asset_name linux amd64) | |
expectedIndex=-1 | |
for i in $(seq 0 $(expr ${assetsSize} - 1)) | |
do | |
currentAsset=$(echo $assets | jq -r ".[${i}]") | |
currentAssetName=$(echo $currentAsset | jq -r '.name') | |
if [[ ${expectedAssetName} = ${currentAssetName} ]]; then | |
expectedIndex=$i | |
fi | |
done | |
assetDownloadUrl=$(echo $assets | jq -r ".[$expectedIndex].browser_download_url") | |
if [[ $assetDownloadUrl == '' ]]; then | |
echo "get download url error!" | |
exit 1 | |
fi | |
echo $assetDownloadUrl | |
wget -O ${BIN_DST_DIR}/vtun $assetDownloadUrl | |
check_error "download binary error!" | |
chmod +x ${BIN_DST_DIR}/vtun | |
mkdir $RESOURCE_DST_DIR | |
cat << EOT > /etc/systemd/system/vtun.service | |
[unit] | |
Description=vtun | |
Wants=network.target | |
After=syslog.target network-online.target | |
[Service] | |
Type=simple | |
Environment=GOGC=20 | |
ExecStart=${BIN_DST_DIR}/vtun -S -c ${CIDR_V4} -c6 ${CIDR_V6} -sip ${SIP_V4} -sip6 ${SIP_V6} -p ws -l ${RUNNING_LISTENER} -k ${DEFAULT_KEY} -dn vt9 | |
Restart=on-failure | |
RestartSec=10 | |
KillMode=process | |
LimitNOFILE=65536 | |
[Install] | |
WantedBy=multi-user.target | |
EOT | |
systemctl enable --now vtun | |
iptables -t nat -A POSTROUTING -j MASQUERADE | |
check_error "iptables execuate error!" | |
ip6tables -t nat -A POSTROUTING -j MASQUERADE | |
check_error "ip6tables execuate error!" | |
clear | |
echo "running on ${RUNNING_LISTENER}" | |
echo "key is ${DEFAULT_KEY}" | |
} | |
function remove() { | |
iptables -t nat -D POSTROUTING -j MASQUERADE | |
ip6tables -t nat -D POSTROUTING -j MASQUERADE | |
systemctl stop vtun | |
systemctl disable vtun | |
rm -rf /etc/systemd/system/vtun.service | |
rm -rf ${BIN_DST_DIR}/vtun | |
rm -rf $RESOURCE_DST_DIR | |
clear | |
echo "remove finished!" | |
} | |
function help() { | |
cat << EOF | |
vtun install helper!!! | |
usage: | |
./vtun.sh menu | |
install: install vtun | |
remove: remove vtun | |
EOF | |
} | |
function main() { | |
if [ $# -lt 1 ]; then | |
help | |
else | |
case $1 in | |
install) | |
install | |
;; | |
remove) | |
remove | |
;; | |
*) | |
echo 'command not found!' | |
help | |
;; | |
esac | |
fi | |
exit 0 | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment