Skip to content

feat: automatically match WAN interface name in OpenWRT environment#20

Open
Tamako0401 wants to merge 1 commit into
s235784:mainfrom
Tamako0401:main
Open

feat: automatically match WAN interface name in OpenWRT environment#20
Tamako0401 wants to merge 1 commit into
s235784:mainfrom
Tamako0401:main

Conversation

@Tamako0401
Copy link
Copy Markdown

因为 OpenWrt 新旧版本的网络栈命名差异,导致 WAN 接口名称不同(不同于文档中的 eth0.x,可能会显示名称为wan),容易引起误导。此更改实现了 OpenWrt 或 ImmortalWrt 环境的 WAN 接口名称的自动匹配,无需在路由器后台记住任何东西。
此更改在仙林校区测试可用。
PixPin_2026-02-27_20-04-23

Copilot AI review requested due to automatic review settings February 27, 2026 12:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds automatic WAN interface detection for OpenWrt and ImmortalWrt routers to address naming inconsistencies across different versions. The change eliminates the need for OpenWrt users to manually remember and specify the WAN interface name (which varies between versions as wan, eth0.x, etc.) by querying the uci configuration system.

Changes:

  • Added get_default_interface() function to automatically detect WAN interface on OpenWrt/ImmortalWrt systems
  • Updated OpenWrt documentation to reflect that the -i parameter is now optional
  • Simplified crontab example in OpenWrt section by removing the -i parameter

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
NJUPT-AutoLogin.sh Implements automatic WAN interface detection via new get_default_interface() function that queries uci on OpenWrt/ImmortalWrt systems
README.md Updates OpenWrt section documentation to indicate auto-detection support and removes manual interface specification instructions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread NJUPT-AutoLogin.sh
Comment on lines +45 to +64
get_default_interface() {
local def_if="eth0"
if [[ -f "/etc/os-release" ]]; then
# shellcheck source=/dev/null
. /etc/os-release
if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi
fi
printf "%s" "$def_if"
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new default interface logic breaks macOS compatibility. The function defaults to "eth0" for all non-OpenWrt systems, but macOS uses "en0" as the default Wi-Fi interface (as shown in the usage example on line 20). This will cause the script to fail on macOS unless users explicitly specify the -i parameter.

The function should check the system environment and return "en0" for macOS (Darwin) and "eth0" for Linux systems, then apply the OpenWrt-specific detection on top of that. For example:

get_default_interface() {
    local def_if="eth0"
    # Set platform-specific defaults
    if [[ "$sysenv" == "Darwin" ]]; then
        def_if="en0"
    fi
    
    # Override with OpenWrt/ImmortalWrt detection if applicable
    if [[ -f "/etc/os-release" ]]; then
        . /etc/os-release
        if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then
            if command -v uci >/dev/null 2>&1; then
                local wan_if
                wan_if=$(uci -q get network.wan.device)
                if [[ -z "$wan_if" ]]; then
                    wan_if=$(uci -q get network.wan.ifname)
                fi
                if [[ -n "$wan_if" ]]; then
                    def_if="$wan_if"
                fi
            fi
        fi
    fi
    printf "%s" "$def_if"
}

Copilot uses AI. Check for mistakes.
Comment thread NJUPT-AutoLogin.sh
Comment on lines +47 to +62
if [[ -f "/etc/os-release" ]]; then
# shellcheck source=/dev/null
. /etc/os-release
if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi
fi
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OpenWrt/ImmortalWrt detection logic in this function duplicates the existing check_openwrt() function (lines 499-508). Consider refactoring to use the existing function to avoid code duplication. This would improve maintainability and ensure consistency across the codebase.

For example, the function could be restructured as:

get_default_interface() {
    local def_if="eth0"
    if [[ "$sysenv" == "Darwin" ]]; then
        def_if="en0"
    fi
    
    if check_openwrt; then
        if command -v uci >/dev/null 2>&1; then
            local wan_if
            wan_if=$(uci -q get network.wan.device)
            if [[ -z "$wan_if" ]]; then
                wan_if=$(uci -q get network.wan.ifname)
            fi
            if [[ -n "$wan_if" ]]; then
                def_if="$wan_if"
            fi
        fi
    fi
    printf "%s" "$def_if"
}

However, note that this would require defining check_openwrt() before get_default_interface(), or moving the function definition order in the file.

Suggested change
if [[ -f "/etc/os-release" ]]; then
# shellcheck source=/dev/null
. /etc/os-release
if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi
fi
if [[ "$sysenv" == "Darwin" ]]; then
def_if="en0"
fi
if check_openwrt; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants