nm-quick-interactive.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 16)@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 "Email for LetsEncrypt (Hit 'enter' to use $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. if [ -f "/root/docker-compose.yml" ]; then
  85. echo "Using existing docker compose"
  86. else
  87. echo "Pulling docker compose"
  88. wget -q -O /root/docker-compose.yml https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.yml
  89. fi
  90. if [ -f "/root/mosquitto.conf" ]; then
  91. echo "Using existing mosquitto config"
  92. else
  93. echo "Pulling mosquitto config"
  94. wget -q -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf
  95. fi
  96. mkdir -p /etc/netmaker
  97. echo "Setting docker-compose..."
  98. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  99. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  100. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  101. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/docker-compose.yml
  102. echo "Starting containers..."
  103. docker-compose -f /root/docker-compose.yml up -d
  104. sleep 2
  105. test_connection() {
  106. echo "Testing Traefik setup (please be patient, this may take 1-2 minutes)"
  107. for i in 1 2 3 4 5 6
  108. do
  109. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  110. if [[ "$i" == 6 ]]; then
  111. echo " Traefik is having an issue setting up certificates, please investigate (docker logs traefik)"
  112. echo " Exiting..."
  113. exit 1
  114. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  115. echo " Certificates not yet configured, retrying..."
  116. elif [[ "$curlresponse" == *"left intact"* ]]; then
  117. echo " Certificates ok"
  118. break
  119. else
  120. secs=$(($i*5+10))
  121. echo " Issue establishing connection...retrying in $secs seconds..."
  122. fi
  123. sleep $secs
  124. done
  125. }
  126. setup_mesh() {( set -e
  127. sleep 5
  128. echo "Creating netmaker network (10.101.0.0/16)"
  129. 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
  130. sleep 5
  131. echo "Creating netmaker access key"
  132. 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)
  133. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  134. sleep 5
  135. echo "Configuring netmaker server as ingress gateway"
  136. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  137. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  138. 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
  139. sleep 5
  140. )}
  141. mesh_connect_logs() {
  142. sleep 5
  143. echo "-----------------------------------------------------------------"
  144. echo "-----------------------------------------------------------------"
  145. echo "DEFAULT NETWORK CLIENT INSTALL INSTRUCTIONS:"
  146. echo "-----------------------------------------------------------------"
  147. echo "-----------------------------------------------------------------"
  148. sleep 5
  149. echo "For Linux and Mac clients, install with the following command:"
  150. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  151. echo "curl -sfL https://raw.githubusercontent.com/gravitl/netmaker/develop/scripts/netclient-install.sh | sudo KEY=$VPN_ACCESS_TOKEN sh -"
  152. sleep 5
  153. echo "-----------------------------------------------------------------"
  154. echo "-----------------------------------------------------------------"
  155. echo "For Windows clients, perform the following from powershell, as administrator:"
  156. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  157. echo "1. Make sure WireGuardNT is installed - https://download.wireguard.com/windows-client/wireguard-installer.exe"
  158. echo "2. Download netclient.exe - wget https://github.com/gravitl/netmaker/releases/download/latest/netclient.exe"
  159. echo "3. Install Netclient - powershell.exe .\\netclient.exe join -t $VPN_ACCESS_TOKEN"
  160. echo "4. Whitelist C:\ProgramData\Netclient in Windows Defender"
  161. sleep 5
  162. echo "-----------------------------------------------------------------"
  163. echo "-----------------------------------------------------------------"
  164. echo "For Android and iOS clients, perform the following steps:"
  165. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  166. echo "1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  167. echo "2. Navigate to \"EXTERNAL CLIENTS\" tab"
  168. echo "3. Select the gateway and create clients"
  169. echo "4. Scan the QR Code from WireGuard app in iOS or Android"
  170. echo "-----------------------------------------------------------------"
  171. echo "-----------------------------------------------------------------"
  172. sleep 5
  173. }
  174. setup_vpn() {( set -e
  175. echo "Creating vpn network (10.201.0.0/16)"
  176. sleep 5
  177. 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
  178. sleep 5
  179. echo "Configuring netmaker server as vpn inlet..."
  180. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  181. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  182. 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
  183. echo "Waiting 10 seconds for server to apply configuration..."
  184. sleep 10
  185. echo "Configuring netmaker server vpn gateway..."
  186. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  187. echo "Gateway iface: $GATEWAY_IFACE"
  188. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  189. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  190. EGRESS_JSON=$( jq -n \
  191. --arg gw "$GATEWAY_IFACE" \
  192. '{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}' )
  193. echo "Egress json: $EGRESS_JSON"
  194. 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
  195. echo "Creating client configs..."
  196. for ((a=1; a <= $NUM_CLIENTS; a++))
  197. do
  198. CLIENT_JSON=$( jq -n \
  199. --arg clientid "vpnclient-$a" \
  200. '{clientid: $clientid}' )
  201. 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
  202. done
  203. sleep 5
  204. )}
  205. vpn_connect_logs() {
  206. sleep 5
  207. echo "-----------------------------------------------------------------"
  208. echo "-----------------------------------------------------------------"
  209. echo "VPN GATEWAY CLIENT INSTALL INSTRUCTIONS:"
  210. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  211. echo "1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  212. echo "2. Navigate to \"EXTERNAL CLIENTS\" tab"
  213. echo "3. Download or scan a client config (vpnclient-x) to the appropriate device"
  214. echo "4. Follow the steps for your system to configure WireGuard on the appropriate device"
  215. echo "5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  216. echo "-----------------------------------------------------------------"
  217. echo "-----------------------------------------------------------------"
  218. sleep 5
  219. }
  220. set +e
  221. test_connection
  222. if [ "${MESH_SETUP}" != "false" ]; then
  223. setup_mesh
  224. fi
  225. if [ "${VPN_SETUP}" == "true" ]; then
  226. setup_vpn
  227. fi
  228. echo "-----------------------------------------------------------------"
  229. echo "-----------------------------------------------------------------"
  230. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  231. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  232. echo "-----------------------------------------------------------------"
  233. echo "-----------------------------------------------------------------"
  234. # cp -f /etc/skel/.bashrc /root/.bashrc