wireguard-v2.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. BL=`echo "\033[36m"`
  4. RD=`echo "\033[01;31m"`
  5. CM='\xE2\x9C\x94\033'
  6. GN=`echo "\033[1;92m"`
  7. CL=`echo "\033[m"`
  8. while true; do
  9. read -p "This will create a New WireGuard LXC. Proceed(y/n)?" yn
  10. case $yn in
  11. [Yy]* ) break;;
  12. [Nn]* ) exit;;
  13. * ) echo "Please answer yes or no.";;
  14. esac
  15. done
  16. clear
  17. function header_info {
  18. echo -e "${RD}
  19. __ ___ _____ _
  20. \ \ / (_) / ____| | |
  21. \ \ /\ / / _ _ __ ___| | __ _ _ __ _ _ __ __| |
  22. \ \/ \/ / | | __/ _ \ | |_ | | | |/ _ | __/ _ |
  23. \ /\ / | | | | __/ |__| | |_| | (_| | | | (_| |
  24. \/ \/ |_|_| \___|\_____|\__,_|\__,_|_| \__,_|
  25. ${YW}With WGDashboard
  26. ${CL}"
  27. }
  28. header_info
  29. show_menu(){
  30. printf " ${YW} 1)${YW} Privileged ${CL}\n"
  31. printf " ${YW} 2)${GN} Unprivileged ${CL}\n"
  32. printf "Please choose a Install Method and hit enter or ${RD}x${CL} to exit."
  33. read opt
  34. }
  35. option_picked(){
  36. message1=${@:-"${CL}Error: No message passed"}
  37. printf " ${YW}${message1}${CL}\n"
  38. }
  39. show_menu
  40. while [ "$opt" != " " ]
  41. do
  42. case $opt in
  43. 1) clear;
  44. header_info;
  45. option_picked "Using Privileged Install";
  46. IM=0
  47. break;
  48. ;;
  49. 2) clear;
  50. header_info;
  51. option_picked "Using Unprivileged Install";
  52. IM=1
  53. break;
  54. ;;
  55. x)exit;
  56. ;;
  57. \n)exit;
  58. ;;
  59. *)clear;
  60. option_picked "Please choose a Install Method from the menu";
  61. show_menu;
  62. ;;
  63. esac
  64. done
  65. show_menu2(){
  66. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  67. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  68. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  69. read opt
  70. }
  71. option_picked(){
  72. message2=${@:-"${CL}Error: No message passed"}
  73. printf " ${YW}${message1}${CL}\n"
  74. printf " ${YW}${message2}${CL}\n"
  75. }
  76. show_menu2
  77. while [ "$opt" != " " ]
  78. do
  79. case $opt in
  80. 1) clear;
  81. header_info;
  82. option_picked "Using Automatic Login";
  83. PW=" "
  84. break;
  85. ;;
  86. 2) clear;
  87. header_info;
  88. option_picked "Using Password (changeme)";
  89. PW="-password changeme"
  90. break;
  91. ;;
  92. x)exit;
  93. ;;
  94. \n)exit;
  95. ;;
  96. *)clear;
  97. option_picked "Please choose a Password Type from the menu";
  98. show_menu2;
  99. ;;
  100. esac
  101. done
  102. show_menu3(){
  103. printf " ${YW} 1)${GN} Automatic DHCP ${CL}\n"
  104. printf " ${YW} 2)${GN} Manual DHCP ${CL}\n"
  105. printf "Please choose a DHCP Type and hit enter or ${RD}x${CL} to exit."
  106. read opt
  107. }
  108. option_picked(){
  109. message3=${@:-"${CL}Error: No message passed"}
  110. printf " ${YW}${message1}${CL}\n"
  111. printf " ${YW}${message2}${CL}\n"
  112. printf " ${YW}${message3}${CL}\n"
  113. }
  114. show_menu3
  115. while [ "$opt" != " " ]
  116. do
  117. case $opt in
  118. 1) clear;
  119. header_info;
  120. option_picked "Using Automatic DHCP";
  121. DHCP=" "
  122. break;
  123. ;;
  124. 2) clear;
  125. header_info;
  126. option_picked "Using Manual DHCP";
  127. DHCP="1"
  128. break;
  129. ;;
  130. x)exit;
  131. ;;
  132. \n)exit;
  133. ;;
  134. *)clear;
  135. option_picked "Please choose a DHCP Type from the menu";
  136. show_menu3;
  137. ;;
  138. esac
  139. done
  140. set -o errexit
  141. set -o errtrace
  142. set -o nounset
  143. set -o pipefail
  144. shopt -s expand_aliases
  145. alias die='EXIT=$? LINE=$LINENO error_exit'
  146. trap die ERR
  147. trap cleanup EXIT
  148. function error_exit() {
  149. trap - ERR
  150. local DEFAULT='Unknown failure occured.'
  151. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  152. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  153. msg "$FLAG $REASON"
  154. [ ! -z ${CTID-} ] && cleanup_ctid
  155. exit $EXIT
  156. }
  157. function warn() {
  158. local REASON="\e[97m$1\e[39m"
  159. local FLAG="\e[93m[WARNING]\e[39m"
  160. msg "$FLAG $REASON"
  161. }
  162. function info() {
  163. local REASON="$1"
  164. local FLAG="\e[36m[INFO]\e[39m"
  165. msg "$FLAG $REASON"
  166. }
  167. function msg() {
  168. local TEXT="$1"
  169. echo -e "$TEXT"
  170. }
  171. function cleanup_ctid() {
  172. if $(pct status $CTID &>/dev/null); then
  173. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  174. pct stop $CTID
  175. fi
  176. pct destroy $CTID
  177. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  178. pvesm free $ROOTFS
  179. fi
  180. }
  181. function cleanup() {
  182. popd >/dev/null
  183. rm -rf $TEMP_DIR
  184. }
  185. if [ "$IM" == "1" ]; then
  186. FEATURES="nesting=1,keyctl=1"
  187. else
  188. FEATURES="nesting=1"
  189. fi
  190. TEMP_DIR=$(mktemp -d)
  191. pushd $TEMP_DIR >/dev/null
  192. export CTID=$(pvesh get /cluster/nextid)
  193. export PCT_OSTYPE=debian
  194. export PCT_OSVERSION=11
  195. export PCT_DISK_SIZE=2
  196. export PCT_OPTIONS="
  197. -features $FEATURES
  198. -hostname wireguard
  199. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  200. -onboot 1
  201. -cores 1
  202. -memory 512
  203. -unprivileged ${IM}
  204. ${PW}
  205. "
  206. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  207. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  208. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  209. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  210. fi
  211. if [ "$DHCP" == "1" ]; then
  212. MAC=$(pct config $CTID \
  213. | grep -i hwaddr \
  214. | awk '{print substr($2, 31, length($3) 17 ) }') \
  215. echo -e "MAC Address ${BL}$MAC${CL}"
  216. dhcp_reservation(){
  217. printf "Please set DHCP reservation and press Enter."
  218. read
  219. }
  220. dhcp_reservation
  221. fi
  222. echo -en "${GN} Starting LXC Container... "
  223. pct start $CTID
  224. echo -e "${CM}${CL} \r"
  225. alias lxc-cmd="lxc-attach -n $CTID --"
  226. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/wireguard-install.sh)" || exit
  227. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  228. echo -e "${GN}Successfully created WireGuard LXC to${CL} ${BL}$CTID${CL}.
  229. ${BL}WGDashboard${CL} should be reachable by going to the following URL.
  230. ${BL}http://${IP}:10086${CL} \n"