nm-quick-interactive.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/bin/bash
  2. set -e
  3. cat << "EOF"
  4. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. __ __ ______ ______ __ __ ______ __ __ ______ ______
  8. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  9. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  10. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  11. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  12. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  15. EOF
  16. NETMAKER_BASE_DOMAIN=nm.$(curl -s ifconfig.me | tr . -).nip.io
  17. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  18. SERVER_PUBLIC_IP=$(curl -s ifconfig.me)
  19. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  20. EMAIL="$(echo $RANDOM | md5sum | head -c 32)@email.com"
  21. echo "Default Base Domain: $NETMAKER_BASE_DOMAIN"
  22. echo "To Override, add a Wildcard (*.netmaker.example.com) DNS record pointing to $SERVER_PUBLIC_IP"
  23. echo "Or, add three DNS records pointing to $SERVER_PUBLIC_IP for the following (Replacing 'netmaker.example.com' with the domain of your choice):"
  24. echo " dashboard.netmaker.example.com"
  25. echo " api.netmaker.example.com"
  26. echo " grpc.netmaker.example.com"
  27. echo "-----------------------------------------------------"
  28. read -p "Domain (Hit 'enter' to use $NETMAKER_BASE_DOMAIN): " domain
  29. read -p "Contact Email: " email
  30. if [ -n "$domain" ]; then
  31. NETMAKER_BASE_DOMAIN=$domain
  32. fi
  33. if [ -n "$email" ]; then
  34. EMAIL=$email
  35. fi
  36. while true; do
  37. read -p 'Configure a default network automatically? [y/n]: ' yn
  38. case $yn in
  39. [Yy]* ) MESH_SETUP="true"; break;;
  40. [Nn]* ) MESH_SETUP="false"; break;;
  41. * ) echo "Please answer yes or no.";;
  42. esac
  43. done
  44. while true; do
  45. read -p 'Configure a VPN gateway automatically? [y/n]: ' yn
  46. case $yn in
  47. [Yy]* ) VPN_SETUP="true"; break;;
  48. [Nn]* ) VPN_SETUP="false"; break;;
  49. * ) echo "Please answer yes or no.";;
  50. esac
  51. done
  52. if [ "${VPN_SETUP}" == "true" ]; then
  53. while :; do
  54. read -ep '# of VPN clients to configure by default: ' num_clients
  55. [[ $num_clients =~ ^[[:digit:]]+$ ]] || continue
  56. (( ( (num_clients=(10#$num_clients)) <= 200 ) && num_clients >= 0 )) || continue
  57. break
  58. done
  59. fi
  60. if [ -n "$num_clients" ]; then
  61. NUM_CLIENTS=$num_clients
  62. fi
  63. echo "-----------------------------------------------------------------"
  64. echo " SETUP ARGUMENTS"
  65. echo "-----------------------------------------------------------------"
  66. echo " domain: $NETMAKER_BASE_DOMAIN"
  67. echo " email: $EMAIL"
  68. echo " public ip: $SERVER_PUBLIC_IP"
  69. echo " setup mesh?: $MESH_SETUP"
  70. echo " setup vpn?: $VPN_SETUP"
  71. if [ "${VPN_SETUP}" == "true" ]; then
  72. echo " # clients: $NUM_CLIENTS"
  73. fi
  74. while true; do
  75. read -p 'Does everything look right? [y/n]: ' yn
  76. case $yn in
  77. [Yy]* ) override="true"; break;;
  78. [Nn]* ) echo "exiting..."; exit;;
  79. * ) echo "Please answer yes or no.";;
  80. esac
  81. done
  82. echo "Beginning installation in 5 seconds..."
  83. sleep 5
  84. echo "Setting docker-compose..."
  85. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  86. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  87. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  88. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/docker-compose.yml
  89. echo "Starting containers..."
  90. docker-compose -f /root/docker-compose.yml up -d
  91. sleep 2
  92. test_connection() {
  93. echo "testing Traefik setup (please be patient, this may take 1-2 minutes)"
  94. for i in 1 2 3 4 5 6
  95. do
  96. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  97. if [[ "$i" == 6 ]]; then
  98. echo " Traefik is having an issue setting up certificates, please investigate (docker logs traefik)"
  99. echo " exiting..."
  100. exit 1
  101. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  102. echo " certificates not yet configured, retrying..."
  103. elif [[ "$curlresponse" == *"left intact"* ]]; then
  104. echo " certificates ok"
  105. break
  106. else
  107. secs=$(($i*5+10))
  108. echo " issue establishing connection...retrying in $secs seconds..."
  109. fi
  110. sleep $secs
  111. done
  112. }
  113. setup_mesh() {( set -e
  114. sleep 5
  115. echo "creating netmaker network (10.101.0.0/16)"
  116. curl -s -o /dev/null -d '{"addressrange":"10.101.0.0/16","netid":"netmaker"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks
  117. sleep 5
  118. echo "creating netmaker access key"
  119. curlresponse=$(curl -s -d '{"uses":99999,"name":"netmaker-key"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks/netmaker/keys)
  120. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  121. sleep 5
  122. echo "configuring netmaker server as ingress gateway"
  123. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  124. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  125. curl -o /dev/null -s -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker/$SERVER_ID/createingress
  126. sleep 5
  127. )}
  128. mesh_connect_logs() {
  129. sleep 5
  130. echo "-----------------------------------------------------------------"
  131. echo "-----------------------------------------------------------------"
  132. echo "DEFAULT NETWORK CLIENT INSTALL INSTRUCTIONS:"
  133. echo "-----------------------------------------------------------------"
  134. echo "-----------------------------------------------------------------"
  135. sleep 5
  136. echo "For Linux and Mac clients, install with the following command:"
  137. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  138. echo "curl -sfL https://raw.githubusercontent.com/gravitl/netmaker/develop/scripts/netclient-install.sh | sudo KEY=$VPN_ACCESS_TOKEN sh -"
  139. sleep 5
  140. echo "-----------------------------------------------------------------"
  141. echo "-----------------------------------------------------------------"
  142. echo "For Windows clients, perform the following from powershell, as administrator:"
  143. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  144. echo "1. Make sure WireGuardNT is installed - https://download.wireguard.com/windows-client/wireguard-installer.exe"
  145. echo "2. Download netclient.exe - wget https://github.com/gravitl/netmaker/releases/download/latest/netclient.exe"
  146. echo "3. Install Netclient - powershell.exe .\\netclient.exe join -t $VPN_ACCESS_TOKEN"
  147. echo "4. Whitelist C:\ProgramData\Netclient in Windows Defender"
  148. sleep 5
  149. echo "-----------------------------------------------------------------"
  150. echo "-----------------------------------------------------------------"
  151. echo "For Android and iOS clients, perform the following steps:"
  152. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  153. echo "1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  154. echo "2. Navigate to \"EXTERNAL CLIENTS\" tab"
  155. echo "3. Select the gateway and create clients"
  156. echo "4. Scan the QR Code from WireGuard app in iOS or Android"
  157. echo "-----------------------------------------------------------------"
  158. echo "-----------------------------------------------------------------"
  159. sleep 5
  160. }
  161. setup_vpn() {( set -e
  162. echo "creating vpn network (10.201.0.0/16)"
  163. sleep 5
  164. 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' https://api.${NETMAKER_BASE_DOMAIN}/api/networks
  165. sleep 5
  166. echo "configuring netmaker server as vpn inlet..."
  167. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  168. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  169. curl -s -o /dev/null -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn/$SERVER_ID/createingress
  170. echo "waiting 10 seconds for server to apply configuration..."
  171. sleep 10
  172. echo "configuring netmaker server vpn gateway..."
  173. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  174. echo "gateway iface: $GATEWAY_IFACE"
  175. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  176. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  177. EGRESS_JSON=$( jq -n \
  178. --arg gw "$GATEWAY_IFACE" \
  179. '{ranges: ["0.0.0.0/5","8.0.0.0/7","11.0.0.0/8","12.0.0.0/6","16.0.0.0/4","32.0.0.0/3","64.0.0.0/2","128.0.0.0/3","160.0.0.0/5","168.0.0.0/6","172.0.0.0/12","172.32.0.0/11","172.64.0.0/10","172.128.0.0/9","173.0.0.0/8","174.0.0.0/7","176.0.0.0/4","192.0.0.0/9","192.128.0.0/11","192.160.0.0/13","192.169.0.0/16","192.170.0.0/15","192.172.0.0/14","192.176.0.0/12","192.192.0.0/10","193.0.0.0/8","194.0.0.0/7","196.0.0.0/6","200.0.0.0/5","208.0.0.0/4"], interface: $gw}' )
  180. echo "egress json: $EGRESS_JSON"
  181. curl -s -o /dev/null -X POST -d "$EGRESS_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn/$SERVER_ID/creategateway
  182. echo "creating client configs..."
  183. for ((a=1; a <= $NUM_CLIENTS; a++))
  184. do
  185. CLIENT_JSON=$( jq -n \
  186. --arg clientid "vpnclient-$a" \
  187. '{clientid: $clientid}' )
  188. curl -s -o /dev/null -d "$CLIENT_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/extclients/vpn/$SERVER_ID
  189. done
  190. sleep 5
  191. )}
  192. vpn_connect_logs() {
  193. sleep 5
  194. echo "-----------------------------------------------------------------"
  195. echo "-----------------------------------------------------------------"
  196. echo "VPN GATEWAY CLIENT INSTALL INSTRUCTIONS:"
  197. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  198. echo "1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  199. echo "2. Navigate to \"EXTERNAL CLIENTS\" tab"
  200. echo "3. Download or scan a client config (vpnclient-x) to the appropriate device"
  201. echo "4. Follow the steps for your system to configure WireGuard on the appropriate device"
  202. echo "5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  203. echo "-----------------------------------------------------------------"
  204. echo "-----------------------------------------------------------------"
  205. sleep 5
  206. }
  207. set +e
  208. test_connection
  209. if [ "${MESH_SETUP}" != "false" ]; then
  210. setup_mesh
  211. fi
  212. if [ "${VPN_SETUP}" == "true" ]; then
  213. setup_vpn
  214. fi
  215. echo "-----------------------------------------------------------------"
  216. echo "-----------------------------------------------------------------"
  217. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  218. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  219. echo "-----------------------------------------------------------------"
  220. echo "-----------------------------------------------------------------"
  221. # cp -f /etc/skel/.bashrc /root/.bashrc