|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright (c) 2020-2024, Windscribe Limited. All rights reserved. |
| 4 | +# Manages /etc/gai.conf to prioritize IPv4 when VPN blocks IPv6 |
| 5 | +# Usage: gai-ipv4-priority up|down |
| 6 | + |
| 7 | +PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin" |
| 8 | + |
| 9 | +GAI_CONF="/etc/gai.conf" |
| 10 | +WINDSCRIBE_RUN_DIR="/var/run/windscribe" |
| 11 | +GAI_BACKUP="$WINDSCRIBE_RUN_DIR/gai.conf.bak" |
| 12 | +GAI_MODIFIED_FLAG="$WINDSCRIBE_RUN_DIR/gai_modified" |
| 13 | + |
| 14 | +# Enable IPv4 priority by modifying gai.conf |
| 15 | +gai_ipv4_priority_up() |
| 16 | +{ |
| 17 | + # Create run directory if it doesn't exist |
| 18 | + if [ ! -d "$WINDSCRIBE_RUN_DIR" ]; then |
| 19 | + mkdir -p "$WINDSCRIBE_RUN_DIR" |
| 20 | + fi |
| 21 | + |
| 22 | + # Check if IPv4 priority is already configured |
| 23 | + if [ -f "$GAI_CONF" ] && grep -q "^precedence ::ffff:0:0/96 100" "$GAI_CONF" 2>/dev/null; then |
| 24 | + echo "IPv4 priority already configured in $GAI_CONF" |
| 25 | + return 0 |
| 26 | + fi |
| 27 | + |
| 28 | + # Backup original gai.conf if it exists and not already backed up |
| 29 | + if [ -f "$GAI_CONF" ] && [ ! -f "$GAI_BACKUP" ]; then |
| 30 | + cp "$GAI_CONF" "$GAI_BACKUP" |
| 31 | + fi |
| 32 | + |
| 33 | + # If gai.conf doesn't exist, create it |
| 34 | + if [ ! -f "$GAI_CONF" ]; then |
| 35 | + echo "# gai.conf - getaddrinfo configuration" > "$GAI_CONF" |
| 36 | + fi |
| 37 | + |
| 38 | + # Add IPv4 priority configuration |
| 39 | + # This gives IPv4-mapped addresses higher precedence than native IPv6 |
| 40 | + echo "" >> "$GAI_CONF" |
| 41 | + echo "# Added by Windscribe to prioritize IPv4 when IPv6 is blocked" >> "$GAI_CONF" |
| 42 | + echo "precedence ::ffff:0:0/96 100" >> "$GAI_CONF" |
| 43 | + |
| 44 | + # Mark that we modified the file |
| 45 | + touch "$GAI_MODIFIED_FLAG" |
| 46 | + |
| 47 | + echo "IPv4 priority configured in $GAI_CONF" |
| 48 | +} |
| 49 | + |
| 50 | +# Restore original gai.conf |
| 51 | +gai_ipv4_priority_down() |
| 52 | +{ |
| 53 | + # Check if we modified the file |
| 54 | + if [ ! -f "$GAI_MODIFIED_FLAG" ]; then |
| 55 | + echo "No gai.conf modifications to restore" |
| 56 | + return 0 |
| 57 | + fi |
| 58 | + |
| 59 | + # Restore backup if it exists |
| 60 | + if [ -f "$GAI_BACKUP" ]; then |
| 61 | + cp "$GAI_BACKUP" "$GAI_CONF" |
| 62 | + rm -f "$GAI_BACKUP" |
| 63 | + echo "Restored original $GAI_CONF" |
| 64 | + else |
| 65 | + # No backup exists, we created the file, so delete it |
| 66 | + if [ -f "$GAI_CONF" ]; then |
| 67 | + rm -f "$GAI_CONF" |
| 68 | + echo "Removed $GAI_CONF created by Windscribe" |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + # Remove the flag |
| 73 | + rm -f "$GAI_MODIFIED_FLAG" |
| 74 | +} |
| 75 | + |
| 76 | +main() |
| 77 | +{ |
| 78 | + local action="$1" |
| 79 | + |
| 80 | + if [[ $action == "up" ]]; then |
| 81 | + gai_ipv4_priority_up |
| 82 | + elif [[ $action == "down" ]]; then |
| 83 | + gai_ipv4_priority_down |
| 84 | + else |
| 85 | + echo "Usage: gai-ipv4-priority up|down" |
| 86 | + return 1 |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +main "$@" |
0 commit comments