wireguard-v3.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #!/usr/bin/env bash
  2. NEXTID=$(pvesh get /cluster/nextid)
  3. INTEGER='^[0-9]+$'
  4. YW=`echo "\033[33m"`
  5. BL=`echo "\033[36m"`
  6. RD=`echo "\033[01;31m"`
  7. BGN=`echo "\033[4;92m"`
  8. GN=`echo "\033[1;92m"`
  9. DGN=`echo "\033[32m"`
  10. CL=`echo "\033[m"`
  11. BFR="\\r\\033[K"
  12. HOLD="-"
  13. CM="${GN}✓${CL}"
  14. APP="Wireguard"
  15. NSAPP=$(echo ${APP,,} | tr -d ' ')
  16. set -o errexit
  17. set -o errtrace
  18. set -o nounset
  19. set -o pipefail
  20. shopt -s expand_aliases
  21. alias die='EXIT=$? LINE=$LINENO error_exit'
  22. trap die ERR
  23. function error_exit() {
  24. trap - ERR
  25. local reason="Unknown failure occured."
  26. local msg="${1:-$reason}"
  27. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  28. echo -e "$flag $msg" 1>&2
  29. exit $EXIT
  30. }
  31. while true; do
  32. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  33. case $yn in
  34. [Yy]* ) break;;
  35. [Nn]* ) exit;;
  36. * ) echo "Please answer yes or no.";;
  37. esac
  38. done
  39. clear
  40. function header_info {
  41. echo -e "${RD}
  42. __ ___ _____ _
  43. \ \ / (_) / ____| | |
  44. \ \ /\ / / _ _ __ ___| | __ _ _ __ _ _ __ __| |
  45. \ \/ \/ / | | __/ _ \ | |_ | | | |/ _ | __/ _ |
  46. \ /\ / | | | | __/ |__| | |_| | (_| | | | (_| |
  47. \/ \/ v3|_|_| \___|\_____|\__,_|\__,_|_| \__,_|
  48. ${YW}With WGDashboard
  49. ${CL}"
  50. }
  51. header_info
  52. function msg_info() {
  53. local msg="$1"
  54. echo -ne " ${HOLD} ${YW}${msg}..."
  55. }
  56. function msg_ok() {
  57. local msg="$1"
  58. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  59. }
  60. function PVE_CHECK() {
  61. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  62. if [[ $PVE != 1 ]]; then
  63. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  64. echo -e "Exiting..."
  65. sleep 2
  66. exit
  67. fi
  68. }
  69. function default_settings() {
  70. clear
  71. header_info
  72. echo -e "${BL}Using Default Settings${CL}"
  73. echo -e "${DGN}Using CT Type ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  74. CT_TYPE="1"
  75. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  76. PW=" "
  77. echo -e "${DGN}Using CT ID ${BGN}$NEXTID${CL}"
  78. CT_ID=$NEXTID
  79. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  80. HN=$NSAPP
  81. echo -e "${DGN}Using Disk Size ${BGN}2${CL}${DGN}GB${CL}"
  82. DISK_SIZE="2"
  83. echo -e "${DGN}Using ${BGN}1${CL}${DGN}vCPU${CL}"
  84. CORE_COUNT="1"
  85. echo -e "${DGN}Using ${BGN}512${CL}${DGN}MiB RAM${CL}"
  86. RAM_SIZE="512"
  87. echo -e "${DGN}Using Bridge ${BGN}vmbr0${CL}"
  88. BRG="vmbr0"
  89. echo -e "${DGN}Using Static IP Address ${BGN}DHCP${CL}"
  90. NET=dhcp
  91. echo -e "${DGN}Using Gateway Address ${BGN}NONE${CL}"
  92. GATE=""
  93. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  94. VLAN=""
  95. }
  96. function advanced_settings() {
  97. clear
  98. header_info
  99. echo -e "${RD}Using Advanced Settings${CL}"
  100. echo -e "${YW}Type Privileged, or Press [ENTER] for Default: Unprivileged (${RD}NO DEVICE PASSTHROUGH${CL}${YW})"
  101. read CT_TYPE1
  102. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Unprivileged" CT_TYPE="1";
  103. echo -en "${DGN}Set CT Type ${BL}$CT_TYPE1${CL}"
  104. else
  105. CT_TYPE1="Privileged"
  106. CT_TYPE="0"
  107. echo -en "${DGN}Set CT Type ${BL}Privileged${CL}"
  108. fi;
  109. echo -e " ${CM}${CL} \r"
  110. sleep 1
  111. clear
  112. header_info
  113. echo -e "${RD}Using Advanced Settings${CL}"
  114. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  115. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  116. read PW1
  117. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  118. echo -en "${DGN}Set CT ${BL}$PW1${CL}"
  119. else
  120. PW="-password $PW1"
  121. echo -en "${DGN}Set CT Password ${BL}$PW1${CL}"
  122. fi;
  123. echo -e " ${CM}${CL} \r"
  124. sleep 1
  125. clear
  126. header_info
  127. echo -e "${RD}Using Advanced Settings${CL}"
  128. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  129. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  130. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  131. read CT_ID
  132. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  133. echo -en "${DGN}Set CT ID To ${BL}$CT_ID${CL}"
  134. echo -e " ${CM}${CL} \r"
  135. sleep 1
  136. clear
  137. header_info
  138. echo -e "${RD}Using Advanced Settings${CL}"
  139. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  140. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  141. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  142. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  143. read CT_NAME
  144. if [ -z $CT_NAME ]; then
  145. HN=$NSAPP
  146. else
  147. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  148. fi
  149. echo -en "${DGN}Set CT Name To ${BL}$HN${CL}"
  150. echo -e " ${CM}${CL} \r"
  151. sleep 1
  152. clear
  153. header_info
  154. echo -e "${RD}Using Advanced Settings${CL}"
  155. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  156. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  157. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  158. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  159. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 2 "
  160. read DISK_SIZE
  161. if [ -z $DISK_SIZE ]; then DISK_SIZE="2"; fi;
  162. if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
  163. echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${CL}${DGN}GB${CL}"
  164. echo -e " ${CM}${CL} \r"
  165. sleep 1
  166. clear
  167. header_info
  168. echo -e "${RD}Using Advanced Settings${CL}"
  169. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  170. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  171. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  172. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  173. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  174. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 1 "
  175. read CORE_COUNT
  176. if [ -z $CORE_COUNT ]; then CORE_COUNT="1"; fi;
  177. echo -en "${DGN}Set Cores To ${BL}$CORE_COUNT${CL}${DGN}vCPU${CL}"
  178. echo -e " ${CM}${CL} \r"
  179. sleep 1
  180. clear
  181. header_info
  182. echo -e "${RD}Using Advanced Settings${CL}"
  183. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  184. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  185. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  186. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  187. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  188. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  189. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 512 "
  190. read RAM_SIZE
  191. if [ -z $RAM_SIZE ]; then RAM_SIZE="512"; fi;
  192. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}${DGN}MiB RAM${CL}"
  193. echo -e " ${CM}${CL} \n"
  194. sleep 1
  195. clear
  196. header_info
  197. echo -e "${RD}Using Advanced Settings${CL}"
  198. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  199. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  200. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  201. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  202. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  203. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  204. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  205. echo -e "${YW}Enter a Bridge, or Press [ENTER] for Default: vmbr0 "
  206. read BRG
  207. if [ -z $BRG ]; then BRG="vmbr0"; fi;
  208. echo -en "${DGN}Set Bridge To ${BL}$BRG${CL}"
  209. echo -e " ${CM}${CL} \n"
  210. sleep 1
  211. clear
  212. header_info
  213. echo -e "${RD}Using Advanced Settings${CL}"
  214. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  215. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  216. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  217. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  218. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  219. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  220. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  221. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  222. echo -e "${YW}Enter a Static IP Address, or Press [ENTER] for Default: DHCP "
  223. read NET
  224. if [ -z $NET ]; then NET="dhcp"; fi;
  225. echo -en "${DGN}Set Static IP Address To ${BL}$NET${CL}"
  226. echo -e " ${CM}${CL} \n"
  227. sleep 1
  228. clear
  229. header_info
  230. echo -e "${RD}Using Advanced Settings${CL}"
  231. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  232. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  233. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  234. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  235. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  236. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  237. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  238. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  239. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  240. echo -e "${YW}Enter a Gateway IP, or Press [ENTER] for Default: NONE "
  241. read GATE1
  242. if [ -z $GATE1 ]; then GATE1="NONE" GATE="";
  243. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  244. else
  245. GATE=",gw=$GATE1"
  246. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  247. fi;
  248. echo -e " ${CM}${CL} \n"
  249. sleep 1
  250. clear
  251. header_info
  252. echo -e "${RD}Using Advanced Settings${CL}"
  253. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  254. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  255. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  256. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  257. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  258. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  259. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  260. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  261. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  262. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  263. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  264. read VLAN1
  265. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN="";
  266. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  267. else
  268. VLAN=",tag=$VLAN1"
  269. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  270. fi;
  271. echo -e " ${CM}${CL} \n"
  272. sleep 1
  273. clear
  274. header_info
  275. echo -e "${RD}Using Advanced Settings${CL}"
  276. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  277. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  278. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  279. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  280. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  281. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  282. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  283. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  284. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  285. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  286. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  287. read -p "Are these settings correct(y/n)? " -n 1 -r
  288. echo
  289. if [[ ! $REPLY =~ ^[Yy]$ ]]
  290. then
  291. advanced_settings
  292. fi
  293. }
  294. function start_script() {
  295. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  296. read SETTINGS
  297. if [ -z $SETTINGS ]; then default_settings;
  298. else
  299. advanced_settings
  300. fi;
  301. }
  302. start_script
  303. if [ "$CT_TYPE" == "1" ]; then
  304. FEATURES="nesting=1,keyctl=1"
  305. else
  306. FEATURES="nesting=1"
  307. fi
  308. TEMP_DIR=$(mktemp -d)
  309. pushd $TEMP_DIR >/dev/null
  310. export CTID=$CT_ID
  311. export PCT_OSTYPE=debian
  312. export PCT_OSVERSION=11
  313. export PCT_DISK_SIZE=$DISK_SIZE
  314. export PCT_OPTIONS="
  315. -features $FEATURES
  316. -hostname $HN
  317. -net0 name=eth0,bridge=$BRG,ip=$NET$GATE$VLAN
  318. -onboot 1
  319. -cores $CORE_COUNT
  320. -memory $RAM_SIZE
  321. -unprivileged $CT_TYPE
  322. $PW
  323. "
  324. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  325. msg_info "Starting LXC Container"
  326. pct start $CTID
  327. msg_ok "Started LXC Container"
  328. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/wireguard-install.sh)" || exit
  329. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  330. msg_ok "Completed Successfully!\n"
  331. echo -e "WGDashboard should be reachable by going to the following URL.
  332. ${BL}http://${IP}:10086${CL} \n"