wireguard.sh 6.1 KB

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