# Linux - Internet Checker Script V1.0
# Function to check network interface
check_interface()
    local INTERFACE=$(ip route | grep default | awk '{print $5}')
    if [[ -n "$INTERFACE" ]]; then
        echo -e "\e[32mNetwork interface ($INTERFACE) is up.\e[0m"
    else
        echo -e "\e[31mNo network interface found or it is down!\e[0m"
        return 1
    fi
}

# Function to check default gateway
check_gateway() {
    local GATEWAY=$(ip route | grep default | awk '{print $3}')
    if ping -c 2 "$GATEWAY" &> /dev/null; then
        echo -e "\e[32mSuccessfully connected to gateway ($GATEWAY).\e[0m"
    else
        echo -e "\e[31mFailed to connect to the gateway ($GATEWAY).\e[0m"
        return 1
    fi
}

# Function to check DNS resolution
check_dns() {
    if nslookup google.com &> /dev/null; then
        echo -e "\e[32mDNS resolution is working.\e[0m"
    else
        echo -e "\e[31mDNS resolution failed!\e[0m"
        return 1
    fi
}

# Function to check connectivity to external servers via ping
check_ping() {
    local SERVERS=("8.8.8.8" "1.1.1.1" "google.com")
    for server in "${SERVERS[@]}"; do
        if ping -c 2 "$server" &> /dev/null; then
            echo -e "\e[32mSuccessfully pinged $server.\e[0m"
        else
            echo -e "\e[31mFailed to ping $server.\e[0m"
            return 1
        fi
    done
}

# Function to check if specific ports are open
check_ports() {
    local PORTS=(80 443)
    for port in "${PORTS[@]}"; do
        if nc -zv google.com $port &> /dev/null; then
            echo -e "\e[32mPort $port is accessible.\e[0m"
        else
            echo -e "\e[31mPort $port is not accessible!\e[0m"
            return 1
        fi
    done
}

# Function to check traceroute to Google DNS
check_traceroute() {
    if command -v traceroute &> /dev/null; then
        echo -e "\e[32mTraceroute to Google DNS (8.8.8.8) is possible:\e[0m"
        traceroute -m 5 8.8.8.8
    else
        echo -e "\e[31mTraceroute command not available on this system.\e[0m"
    fi
}

# Function to check HTTP request and handle redirects
check_http_request() {
    if curl -s -o /dev/null -w "%{http_code}" https://google.com | grep -q "200\|301\|302"; then
        echo -e "\e[32mHTTP request successful or redirected.\e[0m"
    else
        echo -e "\e[31mHTTP request failed!\e[0m"
        return 1
    fi
}

# Run all checks
echo "Starting detailed internet connection check..."
check_interface
check_gateway
check_dns
check_ping
check_ports
check_traceroute
check_http_request

echo "Internet connection check complete."