nm-quick.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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" )
  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="$(echo $RANDOM | md5sum | head -c 32)@email.com"
  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 mosquitto.conf..."
  103. wget -q -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf
  104. echo "setting docker-compose..."
  105. mkdir -p /etc/netmaker
  106. wget -q -O /root/docker-compose.yml https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.yml
  107. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  108. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  109. sed -i "s/COREDNS_IP/$COREDNS_IP/g" /root/docker-compose.yml
  110. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  111. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/docker-compose.yml
  112. echo "starting containers..."
  113. docker-compose -f /root/docker-compose.yml up -d
  114. cat << "EOF"
  115. __ __ ______ ______ __ __ ______ __ __ ______ ______
  116. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  117. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  118. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  119. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  120. EOF
  121. echo "visit https://dashboard.$NETMAKER_BASE_DOMAIN to log in"
  122. sleep 7
  123. setup_mesh() {
  124. echo "creating netmaker network (10.101.0.0/16)"
  125. 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
  126. sleep 5
  127. echo "creating netmaker access key"
  128. 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)
  129. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  130. sleep 5
  131. echo "configuring netmaker server as ingress gateway"
  132. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  133. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  134. 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
  135. sleep 5
  136. echo "finished configuring server and network. You can now add clients."
  137. echo ""
  138. echo "For Linux, Mac, Windows, and FreeBSD:"
  139. echo " 1. Install the netclient: https://docs.netmaker.org/netclient.html#installation"
  140. echo " 2. Join the network: netclient join -t $ACCESS_TOKEN"
  141. echo ""
  142. echo "For Android and iOS clients, perform the following steps:"
  143. echo " 1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  144. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  145. echo " 3. Select the gateway and create clients"
  146. echo " 4. Scan the QR Code from WireGuard app in iOS or Android"
  147. echo ""
  148. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  149. }
  150. setup_vpn() {
  151. echo "creating vpn network (10.201.0.0/16)"
  152. 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
  153. sleep 5
  154. echo "configuring netmaker server as vpn inlet..."
  155. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  156. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  157. 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
  158. echo "waiting 10 seconds for server to apply configuration..."
  159. sleep 10
  160. echo "configuring netmaker server vpn gateway..."
  161. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  162. echo "gateway iface: $GATEWAY_IFACE"
  163. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  164. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  165. EGRESS_JSON=$( jq -n \
  166. --arg gw "$GATEWAY_IFACE" \
  167. '{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}' )
  168. echo "egress json: $EGRESS_JSON"
  169. 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
  170. echo "creating client configs..."
  171. for ((a=1; a <= $NUM_CLIENTS; a++))
  172. do
  173. CLIENT_JSON=$( jq -n \
  174. --arg clientid "vpnclient-$a" \
  175. '{clientid: $clientid}' )
  176. 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
  177. done
  178. echo "finished configuring vpn server."
  179. echo ""
  180. echo "To configure clients, perform the following steps:"
  181. echo " 1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  182. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  183. echo " 3. Download or scan a client config (vpnclient-x) to the appropriate device"
  184. echo " 4. Follow the steps for your system to configure WireGuard on the appropriate device"
  185. echo " 5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  186. }
  187. if [ "${MESH_SETUP}" != "false" ]; then
  188. setup_mesh
  189. fi
  190. if [ "${VPN_SETUP}" == "true" ]; then
  191. setup_vpn
  192. fi
  193. echo ""
  194. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."