nm-quick.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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="[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 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.traefik.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. __ __ ______ ______ __ __ ______ __ __ ______ ______
  121. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  122. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  123. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  124. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  125. EOF
  126. echo "visit https://dashboard.$NETMAKER_BASE_DOMAIN to log in"
  127. sleep 7
  128. setup_mesh() {
  129. echo "creating netmaker network (10.101.0.0/16)"
  130. 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
  131. sleep 5
  132. echo "creating netmaker access key"
  133. 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)
  134. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  135. sleep 5
  136. echo "configuring netmaker server as ingress gateway"
  137. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  138. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  139. 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
  140. sleep 5
  141. echo "finished configuring server and network. You can now add clients."
  142. echo ""
  143. echo "For Linux, Mac, Windows, and FreeBSD:"
  144. echo " 1. Install the netclient: https://docs.netmaker.org/netclient.html#installation"
  145. echo " 2. Join the network: netclient join -t $ACCESS_TOKEN"
  146. echo ""
  147. echo "For Android and iOS clients, perform the following steps:"
  148. echo " 1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  149. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  150. echo " 3. Select the gateway and create clients"
  151. echo " 4. Scan the QR Code from WireGuard app in iOS or Android"
  152. echo ""
  153. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  154. }
  155. setup_vpn() {
  156. echo "creating vpn network (10.201.0.0/16)"
  157. 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
  158. sleep 5
  159. echo "configuring netmaker server as vpn inlet..."
  160. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  161. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  162. 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
  163. echo "waiting 10 seconds for server to apply configuration..."
  164. sleep 10
  165. echo "configuring netmaker server vpn gateway..."
  166. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  167. echo "gateway iface: $GATEWAY_IFACE"
  168. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  169. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  170. EGRESS_JSON=$( jq -n \
  171. --arg gw "$GATEWAY_IFACE" \
  172. '{ranges: ["0.0.0.0/0"], interface: $gw}' )
  173. echo "egress json: $EGRESS_JSON"
  174. 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
  175. echo "creating client configs..."
  176. for ((a=1; a <= $NUM_CLIENTS; a++))
  177. do
  178. CLIENT_JSON=$( jq -n \
  179. --arg clientid "vpnclient-$a" \
  180. '{clientid: $clientid}' )
  181. 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
  182. done
  183. echo "finished configuring vpn server."
  184. echo ""
  185. echo "To configure clients, perform the following steps:"
  186. echo " 1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  187. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  188. echo " 3. Download or scan a client config (vpnclient-x) to the appropriate device"
  189. echo " 4. Follow the steps for your system to configure WireGuard on the appropriate device"
  190. echo " 5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  191. }
  192. if [ "${MESH_SETUP}" != "false" ]; then
  193. setup_mesh
  194. fi
  195. if [ "${VPN_SETUP}" == "true" ]; then
  196. setup_vpn
  197. fi
  198. echo ""
  199. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."