wireguard-install.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. OPTIONS_PATH='/options.conf'
  53. cat >$OPTIONS_PATH <<'EOF'
  54. IPv4dev=eth0
  55. install_user=root
  56. VPN=wireguard
  57. pivpnNET=10.6.0.0
  58. subnetClass=24
  59. ALLOWED_IPS="0.0.0.0/0, ::0/0"
  60. pivpnMTU=1420
  61. pivpnPORT=51820
  62. pivpnDNS1=1.1.1.1
  63. pivpnDNS2=8.8.8.8
  64. pivpnHOST=
  65. pivpnPERSISTENTKEEPALIVE=25
  66. UNATTUPG=1
  67. EOF
  68. msg_info "Updating Container OS"
  69. apt update &>/dev/null
  70. apt-get -qqy upgrade &>/dev/null
  71. msg_ok "Updated Container OS"
  72. msg_info "Installing Dependencies"
  73. apt-get install -y curl &>/dev/null
  74. apt-get install -y sudo &>/dev/null
  75. apt-get install -y gunicorn &>/dev/null
  76. msg_ok "Installed Dependencies"
  77. msg_info "Installing WireGuard (using pivpn.io)"
  78. curl -s -L https://install.pivpn.io > install.sh
  79. chmod +x install.sh
  80. ./install.sh --unattended options.conf &>/dev/null
  81. msg_ok "Installed WireGuard"
  82. msg_info "Installing pip3"
  83. apt-get install python3-pip -y &>/dev/null
  84. pip install flask &>/dev/null
  85. pip install ifcfg &>/dev/null
  86. pip install flask_qrcode &>/dev/null
  87. pip install icmplib &>/dev/null
  88. msg_ok "Installed pip3"
  89. msg_info "Installing WGDashboard"
  90. WGDREL=$(curl -s https://api.github.com/repos/donaldzou/WGDashboard/releases/latest \
  91. | grep "tag_name" \
  92. | awk '{print substr($2, 2, length($2)-3) }') \
  93. git clone -b ${WGDREL} https://github.com/donaldzou/WGDashboard.git /etc/wgdashboard &>/dev/null
  94. cd /etc/wgdashboard/src
  95. sudo chmod u+x wgd.sh
  96. sudo ./wgd.sh install &>/dev/null
  97. sudo chmod -R 755 /etc/wireguard
  98. msg_ok "Installed WGDashboard"
  99. msg_info "Creating Service"
  100. service_path="/etc/systemd/system/wg-dashboard.service"
  101. echo "[Unit]
  102. After=netword.service
  103. [Service]
  104. WorkingDirectory=/etc/wgdashboard/src
  105. ExecStart=/usr/bin/python3 /etc/wgdashboard/src/dashboard.py
  106. Restart=always
  107. [Install]
  108. WantedBy=default.target" > $service_path
  109. sudo chmod 664 /etc/systemd/system/wg-dashboard.service
  110. sudo systemctl daemon-reload
  111. sudo systemctl enable wg-dashboard.service &>/dev/null
  112. sudo systemctl start wg-dashboard.service
  113. msg_ok "Created Service"
  114. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  115. if [[ $PASS != $ ]]; then
  116. msg_info "Customizing Container"
  117. rm /etc/motd
  118. rm /etc/update-motd.d/10-uname
  119. touch ~/.hushlogin
  120. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  121. mkdir -p $(dirname $GETTY_OVERRIDE)
  122. cat << EOF > $GETTY_OVERRIDE
  123. [Service]
  124. ExecStart=
  125. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  126. EOF
  127. systemctl daemon-reload
  128. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  129. msg_ok "Customized Container"
  130. fi
  131. msg_info "Cleaning up"
  132. apt-get autoremove >/dev/null
  133. apt-get autoclean >/dev/null
  134. rm -rf /var/{cache,log}/* /var/lib/apt/lists/*
  135. msg_ok "Cleaned"