nm-quick.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/bin/bash
  2. echo "checking for root permissions..."
  3. echo "setting flags..."
  4. while getopts d:e:m:v:c: flag
  5. do
  6. case "${flag}" in
  7. d) domain=${OPTARG};;
  8. e) email=${OPTARG};;
  9. m) addmesh=${OPTARG};;
  10. v) addvpn=${OPTARG};;
  11. c) num_clients=${OPTARG};;
  12. esac
  13. done
  14. echo "checking for root permissions..."
  15. if [ $EUID -ne 0 ]; then
  16. echo "This script must be run as root"
  17. exit 1
  18. fi
  19. echo "checking dependencies..."
  20. declare -A osInfo;
  21. osInfo[/etc/debian_version]="apt-get install -y"u
  22. osInfo[/etc/alpine-release]="apk --update add"
  23. osInfo[/etc/centos-release]="yum install -y"
  24. osInfo[/etc/fedora-release]="dnf install -y"
  25. for f in ${!osInfo[@]}
  26. do
  27. if [[ -f $f ]];then
  28. install_cmd=${osInfo[$f]}
  29. fi
  30. done
  31. if [ -f /etc/debian_version ]; then
  32. apt update
  33. elif [ -f /etc/alpine-release ]; then
  34. apk update
  35. elif [ -f /etc/centos-release ]; then
  36. yum update
  37. elif [ -f /etc/fedora-release ]; then
  38. dnf update
  39. fi
  40. dependencies=("docker.io" "docker-compose" "wireguard" "jq" "resolvectl")
  41. for dependency in ${dependencies[@]}; do
  42. is_installed=$(dpkg-query -W --showformat='${Status}\n' ${dependency} | grep "install ok installed")
  43. if [ "${is_installed}" == "install ok installed" ]; then
  44. echo " " ${dependency} is installed
  45. else
  46. echo " " ${dependency} is not installed. Attempting install.
  47. ${install_cmd} ${dependency}
  48. sleep 5
  49. is_installed=$(dpkg-query -W --showformat='${Status}\n' ${dependency} | grep "install ok installed")
  50. if [ "${is_installed}" == "install ok installed" ]; then
  51. echo " " ${dependency} is installed
  52. elif [ -x "$(command -v ${dependency})" ]; then
  53. echo " " ${dependency} is installed
  54. else
  55. echo " " failed to install ${dependency}. Exiting.
  56. exit 1
  57. fi
  58. fi
  59. done
  60. set -e
  61. NETMAKER_BASE_DOMAIN=nm.$(curl -s ifconfig.me | tr . -).nip.io
  62. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  63. SERVER_PUBLIC_IP=$(curl -s ifconfig.me)
  64. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  65. EMAIL="[email protected]"
  66. if [ -n "$domain" ]; then
  67. NETMAKER_BASE_DOMAIN=$domain
  68. fi
  69. if [ -n "$email" ]; then
  70. EMAIL=$email
  71. fi
  72. if [ -n "$addmesh" ]; then
  73. MESH_SETUP=$addmesh
  74. else
  75. MESH_SETUP="true"
  76. fi
  77. if [ -n "$addvpn" ]; then
  78. VPN_SETUP=$addvpn
  79. else
  80. VPN_SETUP="false"
  81. fi
  82. if [ -n "$num_clients" ]; then
  83. NUM_CLIENTS=$num_clients
  84. else
  85. NUM_CLIENTS=5
  86. fi
  87. echo " ----------------------------"
  88. echo " SETUP ARGUMENTS"
  89. echo " ----------------------------"
  90. echo " domain: $NETMAKER_BASE_DOMAIN"
  91. echo " email: $EMAIL"
  92. echo " coredns ip: $COREDNS_IP"
  93. echo " public ip: $SERVER_PUBLIC_IP"
  94. echo " master key: $MASTER_KEY"
  95. echo " setup mesh?: $MESH_SETUP"
  96. echo " setup vpn?: $VPN_SETUP"
  97. if [ "${VPN_SETUP}" == "true" ]; then
  98. echo " # clients: $NUM_CLIENTS"
  99. fi
  100. echo " ----------------------------"
  101. sleep 5
  102. echo "setting caddyfile..."
  103. wget -q -O /root/Caddyfile https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile
  104. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  105. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  106. echo "setting mosquitto.conf..."
  107. wget -q -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf
  108. echo "setting docker-compose..."
  109. wget -q -O /root/docker-compose.yml https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.contained.yml
  110. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  111. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  112. sed -i "s/COREDNS_IP/$COREDNS_IP/g" /root/docker-compose.yml
  113. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  114. echo "starting containers..."
  115. docker-compose -f /root/docker-compose.yml up -d
  116. cat << "EOF"
  117. ______ ______ ______ __ __ __ ______ __
  118. /\ ___\ /\ == \ /\ __ \ /\ \ / / /\ \ /\__ _\ /\ \
  119. \ \ \__ \ \ \ __< \ \ __ \ \ \ \'/ \ \ \ \/_/\ \/ \ \ \____
  120. \ \_____\ \ \_\ \_\ \ \_\ \_\ \ \__| \ \_\ \ \_\ \ \_____\
  121. \/_____/ \/_/ /_/ \/_/\/_/ \/_/ \/_/ \/_/ \/_____/
  122. __ __ ______ ______ __ __ ______ __ __ ______ ______
  123. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  124. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  125. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  126. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  127. EOF
  128. echo "visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  129. sleep 2
  130. setup_mesh() {
  131. echo "creating default network (10.101.0.0/16)"
  132. curl -s -o /dev/null -d '{"addressrange":"10.101.0.0/16","netid":"default"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks
  133. sleep 2
  134. echo "creating default key"
  135. curlresponse=$(curl -s -d '{"uses":99999,"name":"defaultkey"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks/default/keys)
  136. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  137. sleep 2
  138. echo "configuring netmaker server as ingress gateway"
  139. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/default)
  140. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  141. curl -o /dev/null -s -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/default/$SERVER_ID/createingress
  142. echo "finished configuring server and network. You can now add clients."
  143. echo ""
  144. echo "For Linux and Mac clients, install with the following command:"
  145. echo " curl -sfL https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/netclient-install.sh | sudo KEY=$ACCESS_TOKEN sh -"
  146. echo ""
  147. echo "For Windows clients, perform the following from powershell, as administrator:"
  148. echo " 1. Make sure WireGuardNT is installed - https://download.wireguard.com/windows-client/wireguard-installer.exe"
  149. echo " 2. Download netclient.exe - wget https://github.com/gravitl/netmaker/releases/download/latest/netclient.exe"
  150. echo " 3. Install Netclient - powershell.exe .\\netclient.exe join -t $ACCESS_TOKEN"
  151. echo " 4. Whitelist C:\ProgramData\Netclient in Windows Defender"
  152. echo ""
  153. echo "For Android and iOS clients, perform the following steps:"
  154. echo " 1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  155. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  156. echo " 3. Select the gateway and create clients"
  157. echo " 4. Scan the QR Code from WireGuard app in iOS or Android"
  158. echo ""
  159. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  160. }
  161. setup_vpn() {
  162. echo "creating vpn network (10.201.0.0/16)"
  163. curl -s -o /dev/null -d '{"addressrange":"10.201.0.0/16","netid":"vpn","defaultextclientdns":"8.8.8.8"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/networks
  164. sleep 2
  165. echo "configuring netmaker server as vpn inlet..."
  166. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/vpn)
  167. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  168. curl -s -o /dev/null -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/vpn/$SERVER_ID/createingress
  169. echo "waiting 10 seconds for server to apply configuration..."
  170. sleep 10
  171. echo "configuring netmaker server vpn gateway..."
  172. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  173. echo "gateway iface: $GATEWAY_IFACE"
  174. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/vpn)
  175. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  176. EGRESS_JSON=$( jq -n \
  177. --arg gw "$GATEWAY_IFACE" \
  178. '{ranges: ["0.0.0.0/0"], interface: $gw}' )
  179. echo "egress json: $EGRESS_JSON"
  180. curl -s -o /dev/null -X POST -d "$EGRESS_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/nodes/vpn/$SERVER_ID/creategateway
  181. echo "creating client configs..."
  182. for ((a=1; a <= $NUM_CLIENTS; a++))
  183. do
  184. CLIENT_JSON=$( jq -n \
  185. --arg clientid "vpnclient-$a" \
  186. '{clientid: $clientid}' )
  187. curl -s -o /dev/null -d "$CLIENT_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' localhost:8081/api/extclients/vpn/$SERVER_ID
  188. done
  189. echo "finished configuring vpn server."
  190. echo ""
  191. echo "To configure clients, perform the following steps:"
  192. echo " 1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  193. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  194. echo " 3. Download or scan a client config (vpnclient-x) to the appropriate device"
  195. echo " 4. Follow the steps for your system to configure WireGuard on the appropriate device"
  196. echo " 5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  197. }
  198. if [ "${MESH_SETUP}" != "false" ]; then
  199. setup_mesh
  200. fi
  201. if [ "${VPN_SETUP}" == "true" ]; then
  202. setup_vpn
  203. fi
  204. echo ""
  205. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."