wireguard-install.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. RD=`echo "\033[01;31m"`
  4. BL=`echo "\033[36m"`
  5. GN=`echo "\033[1;92m"`
  6. CL=`echo "\033[m"`
  7. RETRY_NUM=10
  8. RETRY_EVERY=3
  9. NUM=$RETRY_NUM
  10. CM="${GN}✓${CL}"
  11. CROSS="${RD}✗${CL}"
  12. BFR="\\r\\033[K"
  13. HOLD="-"
  14. set -o errexit
  15. set -o errtrace
  16. set -o nounset
  17. set -o pipefail
  18. shopt -s expand_aliases
  19. alias die='EXIT=$? LINE=$LINENO error_exit'
  20. trap die ERR
  21. function error_exit() {
  22. trap - ERR
  23. local reason="Unknown failure occured."
  24. local msg="${1:-$reason}"
  25. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  26. echo -e "$flag $msg" 1>&2
  27. exit $EXIT
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. msg_info "Setting up Container OS "
  38. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  39. locale-gen >/dev/null
  40. while [ "$(hostname -I)" = "" ]; do
  41. 1>&2 echo -en "${CROSS}${RD} No Network! "
  42. sleep $RETRY_EVERY
  43. ((NUM--))
  44. if [ $NUM -eq 0 ]
  45. then
  46. 1>&2 echo -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  47. exit 1
  48. fi
  49. done
  50. msg_ok "Set up Container OS"
  51. msg_ok "Network Connected: ${BL}$(hostname -I)"
  52. msg_info "Checking Internet Status"
  53. INTER=$(wget -q --tries=10 --timeout=5 --spider https://google.com)
  54. sleep 2
  55. if [[ "$INTER" != "0" ]]; then
  56. msg_ok "Internet Online"
  57. else
  58. echo -e "${BFR} ${CROSS}${RD} Internet Offline${CL}"
  59. fi
  60. OPTIONS_PATH='/options.conf'
  61. cat >$OPTIONS_PATH <<'EOF'
  62. IPv4dev=eth0
  63. install_user=root
  64. VPN=wireguard
  65. pivpnNET=10.6.0.0
  66. subnetClass=24
  67. ALLOWED_IPS="0.0.0.0/0, ::0/0"
  68. pivpnMTU=1420
  69. pivpnPORT=51820
  70. pivpnDNS1=1.1.1.1
  71. pivpnDNS2=8.8.8.8
  72. pivpnHOST=
  73. pivpnPERSISTENTKEEPALIVE=25
  74. UNATTUPG=1
  75. EOF
  76. msg_info "Updating Container OS"
  77. apt update &>/dev/null
  78. apt-get -qqy upgrade &>/dev/null
  79. msg_ok "Updated Container OS"
  80. msg_info "Installing Dependencies"
  81. apt-get install -y curl &>/dev/null
  82. apt-get install -y sudo &>/dev/null
  83. apt-get install -y gunicorn &>/dev/null
  84. msg_ok "Installed Dependencies"
  85. msg_info "Installing WireGuard (using pivpn.io)"
  86. curl -s -L https://install.pivpn.io > install.sh
  87. chmod +x install.sh
  88. ./install.sh --unattended options.conf &>/dev/null
  89. msg_ok "Installed WireGuard"
  90. msg_info "Installing pip3"
  91. apt-get install python3-pip -y &>/dev/null
  92. pip install flask &>/dev/null
  93. pip install ifcfg &>/dev/null
  94. pip install flask_qrcode &>/dev/null
  95. pip install icmplib &>/dev/null
  96. msg_ok "Installed pip3"
  97. msg_info "Installing WGDashboard"
  98. WGDREL=$(curl -s https://api.github.com/repos/donaldzou/WGDashboard/releases/latest \
  99. | grep "tag_name" \
  100. | awk '{print substr($2, 2, length($2)-3) }') \
  101. git clone -b ${WGDREL} https://github.com/donaldzou/WGDashboard.git /etc/wgdashboard &>/dev/null
  102. cd /etc/wgdashboard/src
  103. sudo chmod u+x wgd.sh
  104. sudo ./wgd.sh install &>/dev/null
  105. sudo chmod -R 755 /etc/wireguard
  106. msg_ok "Installed WGDashboard"
  107. msg_info "Creating Service"
  108. service_path="/etc/systemd/system/wg-dashboard.service"
  109. echo "[Unit]
  110. After=netword.service
  111. [Service]
  112. WorkingDirectory=/etc/wgdashboard/src
  113. ExecStart=/usr/bin/python3 /etc/wgdashboard/src/dashboard.py
  114. Restart=always
  115. [Install]
  116. WantedBy=default.target" > $service_path
  117. sudo chmod 664 /etc/systemd/system/wg-dashboard.service
  118. sudo systemctl daemon-reload
  119. sudo systemctl enable wg-dashboard.service &>/dev/null
  120. sudo systemctl start wg-dashboard.service
  121. msg_ok "Created Service"
  122. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  123. if [[ $PASS != $ ]]; then
  124. msg_info "Customizing Container"
  125. rm /etc/motd
  126. rm /etc/update-motd.d/10-uname
  127. touch ~/.hushlogin
  128. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  129. mkdir -p $(dirname $GETTY_OVERRIDE)
  130. cat << EOF > $GETTY_OVERRIDE
  131. [Service]
  132. ExecStart=
  133. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  134. EOF
  135. systemctl daemon-reload
  136. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  137. msg_ok "Customized Container"
  138. fi
  139. msg_info "Cleaning up"
  140. apt-get autoremove >/dev/null
  141. apt-get autoclean >/dev/null
  142. rm -rf /var/{cache,log}/* /var/lib/apt/lists/*
  143. msg_ok "Cleaned"