wireguard-install.sh 3.5 KB

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