wireguard-install.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 occurred."
  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. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. while [ "$(hostname -I)" = "" ]; do
  45. echo 1>&2 -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. ((NUM--))
  48. if [ $NUM -eq 0 ]; then
  49. echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  50. exit 1
  51. fi
  52. done
  53. msg_ok "Set up Container OS"
  54. msg_ok "Network Connected: ${BL}$(hostname -I)"
  55. set +e
  56. alias die=''
  57. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  58. msg_error "Internet NOT Connected"
  59. read -r -p "Would you like to continue anyway? <y/N> " prompt
  60. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  61. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  62. else
  63. echo -e " 🖧 Check Network Settings"
  64. exit 1
  65. fi
  66. fi
  67. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  68. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  69. alias die='EXIT=$? LINE=$LINENO error_exit'
  70. set -e
  71. OPTIONS_PATH='/options.conf'
  72. cat >$OPTIONS_PATH <<'EOF'
  73. IPv4dev=eth0
  74. install_user=root
  75. VPN=wireguard
  76. pivpnNET=10.6.0.0
  77. subnetClass=24
  78. ALLOWED_IPS="0.0.0.0/0, ::0/0"
  79. pivpnMTU=1420
  80. pivpnPORT=51820
  81. pivpnDNS1=1.1.1.1
  82. pivpnDNS2=8.8.8.8
  83. pivpnHOST=
  84. pivpnPERSISTENTKEEPALIVE=25
  85. UNATTUPG=1
  86. EOF
  87. msg_info "Updating Container OS"
  88. apt-get update &>/dev/null
  89. apt-get -y upgrade &>/dev/null
  90. msg_ok "Updated Container OS"
  91. msg_info "Installing Dependencies"
  92. apt-get install -y curl &>/dev/null
  93. apt-get install -y sudo &>/dev/null
  94. apt-get install -y gunicorn &>/dev/null
  95. msg_ok "Installed Dependencies"
  96. msg_info "Installing WireGuard (using pivpn.io)"
  97. curl -s -L https://install.pivpn.io >install.sh
  98. chmod +x install.sh
  99. ./install.sh --unattended options.conf &>/dev/null
  100. msg_ok "Installed WireGuard"
  101. msg_info "Installing pip3"
  102. apt-get install python3-pip -y &>/dev/null
  103. pip install flask &>/dev/null
  104. pip install ifcfg &>/dev/null
  105. pip install flask_qrcode &>/dev/null
  106. pip install icmplib &>/dev/null
  107. msg_ok "Installed pip3"
  108. msg_info "Installing WGDashboard"
  109. WGDREL=$(curl -s https://api.github.com/repos/donaldzou/WGDashboard/releases/latest |
  110. grep "tag_name" |
  111. awk '{print substr($2, 2, length($2)-3) }')
  112. git clone -b ${WGDREL} https://github.com/donaldzou/WGDashboard.git /etc/wgdashboard &>/dev/null
  113. cd /etc/wgdashboard/src
  114. sudo chmod u+x wgd.sh
  115. sudo ./wgd.sh install &>/dev/null
  116. sudo chmod -R 755 /etc/wireguard
  117. msg_ok "Installed WGDashboard"
  118. msg_info "Creating Service"
  119. service_path="/etc/systemd/system/wg-dashboard.service"
  120. echo "[Unit]
  121. After=netword.service
  122. [Service]
  123. WorkingDirectory=/etc/wgdashboard/src
  124. ExecStart=/usr/bin/python3 /etc/wgdashboard/src/dashboard.py
  125. Restart=always
  126. [Install]
  127. WantedBy=default.target" >$service_path
  128. sudo chmod 664 /etc/systemd/system/wg-dashboard.service
  129. sudo systemctl daemon-reload
  130. sudo systemctl enable wg-dashboard.service &>/dev/null
  131. sudo systemctl start wg-dashboard.service
  132. msg_ok "Created Service"
  133. PASS=$(grep -w "root" /etc/shadow | cut -b6)
  134. if [[ $PASS != $ ]]; then
  135. msg_info "Customizing Container"
  136. rm /etc/motd
  137. rm /etc/update-motd.d/10-uname
  138. touch ~/.hushlogin
  139. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  140. mkdir -p $(dirname $GETTY_OVERRIDE)
  141. cat <<EOF >$GETTY_OVERRIDE
  142. [Service]
  143. ExecStart=
  144. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  145. EOF
  146. systemctl daemon-reload
  147. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  148. msg_ok "Customized Container"
  149. fi
  150. if [[ "${SSH_ROOT}" == "yes" ]]; then
  151. sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
  152. systemctl restart sshd
  153. fi
  154. msg_info "Cleaning up"
  155. apt-get autoremove >/dev/null
  156. apt-get autoclean >/dev/null
  157. msg_ok "Cleaned"