ipv6_controller.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env bash
  2. # _modules/scripts/ipv6_controller.sh
  3. # THIS SCRIPT IS DESIGNED TO BE RUNNING BY MAILCOW SCRIPTS ONLY!
  4. # DO NOT, AGAIN, NOT TRY TO RUN THIS SCRIPT STANDALONE!!!!!!
  5. # 1) Check if the host supports IPv6
  6. get_ipv6_support() {
  7. # ---- helper: probe external IPv6 connectivity without DNS ----
  8. _probe_ipv6_connectivity() {
  9. # Use literal, always-on IPv6 echo responders (no DNS required)
  10. local PROBE_IPS=("2001:4860:4860::8888" "2606:4700:4700::1111")
  11. local ip rc=1
  12. for ip in "${PROBE_IPS[@]}"; do
  13. if command -v ping6 &>/dev/null; then
  14. ping6 -c1 -W2 "$ip" &>/dev/null || ping6 -c1 -w2 "$ip" &>/dev/null
  15. rc=$?
  16. elif command -v ping &>/dev/null; then
  17. ping -6 -c1 -W2 "$ip" &>/dev/null || ping -6 -c1 -w2 "$ip" &>/dev/null
  18. rc=$?
  19. else
  20. rc=1
  21. fi
  22. [[ $rc -eq 0 ]] && return 0
  23. done
  24. return 1
  25. }
  26. if [[ ! -f /proc/net/if_inet6 ]] || grep -qs '^1' /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null; then
  27. DETECTED_IPV6=false
  28. echo -e "${YELLOW}IPv6 not detected on host – ${LIGHT_RED}IPv6 is administratively disabled${YELLOW}.${NC}"
  29. return
  30. fi
  31. if ip -6 route show default 2>/dev/null | grep -qE '^default'; then
  32. echo -e "${YELLOW}Default IPv6 route found – testing external IPv6 connectivity...${NC}"
  33. if _probe_ipv6_connectivity; then
  34. DETECTED_IPV6=true
  35. echo -e "IPv6 detected on host – ${LIGHT_GREEN}leaving IPv6 support enabled${YELLOW}.${NC}"
  36. else
  37. DETECTED_IPV6=false
  38. echo -e "${YELLOW}Default IPv6 route present but external IPv6 connectivity failed – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  39. fi
  40. return
  41. fi
  42. if ip -6 addr show scope global 2>/dev/null | grep -q 'inet6'; then
  43. DETECTED_IPV6=false
  44. echo -e "${YELLOW}Global IPv6 address present but no default route – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  45. return
  46. fi
  47. if ip -6 addr show scope link 2>/dev/null | grep -q 'inet6'; then
  48. echo -e "${YELLOW}Only link-local IPv6 addresses found – testing external IPv6 connectivity...${NC}"
  49. if _probe_ipv6_connectivity; then
  50. DETECTED_IPV6=true
  51. echo -e "External IPv6 connectivity available – ${LIGHT_GREEN}leaving IPv6 support enabled${YELLOW}.${NC}"
  52. else
  53. DETECTED_IPV6=false
  54. echo -e "${YELLOW}Only link-local IPv6 present and no external connectivity – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  55. fi
  56. return
  57. fi
  58. DETECTED_IPV6=false
  59. echo -e "${YELLOW}IPv6 not detected on host – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  60. }
  61. # 2) Ensure Docker daemon.json has (or create) the required IPv6 settings
  62. docker_daemon_edit(){
  63. DOCKER_DAEMON_CONFIG="/etc/docker/daemon.json"
  64. DOCKER_MAJOR=$(docker version --format '{{.Server.Version}}' 2>/dev/null | cut -d. -f1)
  65. MISSING=()
  66. _has_kv() { grep -Eq "\"$1\"[[:space:]]*:[[:space:]]*$2" "$DOCKER_DAEMON_CONFIG" 2>/dev/null; }
  67. if [[ -f "$DOCKER_DAEMON_CONFIG" ]]; then
  68. # reject empty or whitespace-only file immediately
  69. if [[ ! -s "$DOCKER_DAEMON_CONFIG" ]] || ! grep -Eq '[{}]' "$DOCKER_DAEMON_CONFIG"; then
  70. echo -e "${RED}ERROR: $DOCKER_DAEMON_CONFIG exists but is empty or contains no JSON braces – please initialize it with valid JSON (e.g. {}).${NC}"
  71. exit 1
  72. fi
  73. # Validate JSON if jq is present
  74. if command -v jq &>/dev/null && ! jq empty "$DOCKER_DAEMON_CONFIG" &>/dev/null; then
  75. echo -e "${RED}ERROR: Invalid JSON in $DOCKER_DAEMON_CONFIG – please correct manually.${NC}"
  76. exit 1
  77. fi
  78. # Gather missing keys
  79. ! _has_kv ipv6 true && MISSING+=("ipv6: true")
  80. # For Docker < 28, keep requiring fixed-cidr-v6 (default bridge needs it on old engines)
  81. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  82. ! grep -Eq '"fixed-cidr-v6"[[:space:]]*:[[:space:]]*".+"' "$DOCKER_DAEMON_CONFIG" \
  83. && MISSING+=('fixed-cidr-v6: "fd00:dead:beef:c0::/80"')
  84. fi
  85. # For Docker < 27, ip6tables needed and was tied to experimental in older releases
  86. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  87. _has_kv ipv6 true && ! _has_kv ip6tables true && MISSING+=("ip6tables: true")
  88. ! _has_kv experimental true && MISSING+=("experimental: true")
  89. fi
  90. # Fix if needed
  91. if ((${#MISSING[@]}>0)); then
  92. echo -e "${MAGENTA}Your daemon.json is missing: ${YELLOW}${MISSING[*]}${NC}"
  93. if [[ -n "$FORCE" ]]; then
  94. ans=Y
  95. else
  96. read -p "Would you like to update $DOCKER_DAEMON_CONFIG now? [Y/n] " ans
  97. ans=${ans:-Y}
  98. fi
  99. if [[ $ans =~ ^[Yy]$ ]]; then
  100. cp "$DOCKER_DAEMON_CONFIG" "${DOCKER_DAEMON_CONFIG}.bak"
  101. if command -v jq &>/dev/null; then
  102. TMP=$(mktemp)
  103. # Base filter: ensure ipv6 = true
  104. JQ_FILTER='.ipv6 = true'
  105. # Add fixed-cidr-v6 only for Docker < 28
  106. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  107. JQ_FILTER+=' | .["fixed-cidr-v6"] = (.["fixed-cidr-v6"] // "fd00:dead:beef:c0::/80")'
  108. fi
  109. # Add ip6tables/experimental only for Docker < 27
  110. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  111. JQ_FILTER+=' | .ip6tables = true | .experimental = true'
  112. fi
  113. jq "$JQ_FILTER" "$DOCKER_DAEMON_CONFIG" >"$TMP" && mv "$TMP" "$DOCKER_DAEMON_CONFIG"
  114. echo -e "${LIGHT_GREEN}daemon.json updated. Restarting Docker...${NC}"
  115. (command -v systemctl &>/dev/null && systemctl restart docker) || service docker restart
  116. echo -e "${YELLOW}Docker restarted.${NC}"
  117. else
  118. echo -e "${RED}Please install jq or manually update daemon.json and restart Docker.${NC}"
  119. exit 1
  120. fi
  121. else
  122. echo -e "${YELLOW}User declined Docker update – please insert these changes manually:${NC}"
  123. echo "${MISSING[*]}"
  124. exit 1
  125. fi
  126. fi
  127. else
  128. # Create new daemon.json if missing
  129. if [[ -n "$FORCE" ]]; then
  130. ans=Y
  131. else
  132. read -p "$DOCKER_DAEMON_CONFIG not found. Create it with IPv6 settings? [Y/n] " ans
  133. ans=${ans:-Y}
  134. fi
  135. if [[ $ans =~ ^[Yy]$ ]]; then
  136. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  137. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  138. {
  139. "ipv6": true,
  140. "fixed-cidr-v6": "fd00:dead:beef:c0::/80",
  141. "ip6tables": true,
  142. "experimental": true
  143. }
  144. EOF
  145. elif [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  146. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  147. {
  148. "ipv6": true,
  149. "fixed-cidr-v6": "fd00:dead:beef:c0::/80"
  150. }
  151. EOF
  152. else
  153. # Docker 28+: ipv6 works without fixed-cidr-v6
  154. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  155. {
  156. "ipv6": true
  157. }
  158. EOF
  159. fi
  160. echo -e "${GREEN}Created $DOCKER_DAEMON_CONFIG with IPv6 settings.${NC}"
  161. echo "Restarting Docker..."
  162. (command -v systemctl &>/dev/null && systemctl restart docker) || service docker restart
  163. echo "Docker restarted."
  164. else
  165. echo "User declined to create daemon.json – please manually merge the docker daemon with these configs:"
  166. echo "${MISSING[*]}"
  167. exit 1
  168. fi
  169. fi
  170. }
  171. # 3) Main wrapper for generate_config.sh and update.sh
  172. configure_ipv6() {
  173. # detect manual override if mailcow.conf is present
  174. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]] && grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  175. MANUAL_SETTING=$(grep '^ENABLE_IPV6=' "$MAILCOW_CONF" | cut -d= -f2)
  176. elif [[ -z "$MAILCOW_CONF" ]] && [[ -n "${ENABLE_IPV6:-}" ]]; then
  177. MANUAL_SETTING="$ENABLE_IPV6"
  178. else
  179. MANUAL_SETTING=""
  180. fi
  181. get_ipv6_support
  182. # if user manually set it, check for mismatch
  183. if [[ "$DETECTED_IPV6" != "true" ]]; then
  184. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  185. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  186. sed -i 's/^ENABLE_IPV6=.*/ENABLE_IPV6=false/' "$MAILCOW_CONF"
  187. else
  188. echo "ENABLE_IPV6=false" >> "$MAILCOW_CONF"
  189. fi
  190. else
  191. export IPV6_BOOL=false
  192. fi
  193. echo "Skipping Docker IPv6 configuration because host does not support IPv6."
  194. echo "Make sure to check if your docker daemon.json does not include \"enable_ipv6\": true if you do not want IPv6."
  195. echo "IPv6 configuration complete: ENABLE_IPV6=false"
  196. sleep 2
  197. return
  198. fi
  199. docker_daemon_edit
  200. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  201. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  202. sed -i 's/^ENABLE_IPV6=.*/ENABLE_IPV6=true/' "$MAILCOW_CONF"
  203. else
  204. echo "ENABLE_IPV6=true" >> "$MAILCOW_CONF"
  205. fi
  206. else
  207. export IPV6_BOOL=true
  208. fi
  209. echo "IPv6 configuration complete: ENABLE_IPV6=true"
  210. }