99-custom.sh 6.83 KB
Newer Older
1
#!/bin/sh
2
3
# 99-custom.sh 就是immortalwrt固件首次启动时运行的脚本 位于固件内的/etc/uci-defaults/99-custom.sh
# Log file for debugging
4
LOGFILE="/etc/config/uci-defaults-log.txt"
wukongdaily's avatar
wukongdaily committed
5
echo "Starting 99-custom.sh at $(date)" >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
6
7
8
9
# 设置默认防火墙规则,方便单网口虚拟机首次访问 WebUI 
# 因为本项目中 单网口模式是dhcp模式 直接就能上网并且访问web界面 避免新手每次都要修改/etc/config/network中的静态ip
# 当你刷机运行后 都调整好了 你完全可以在web页面自行关闭 wan口防火墙的入站数据
# 具体操作方法:网络——防火墙 在wan的入站数据 下拉选项里选择 拒绝 保存并应用即可。
10
11
12
13
14
15
16
uci set firewall.@zone[1].input='ACCEPT'

# 设置主机名映射,解决安卓原生 TV 无法联网的问题
uci add dhcp domain
uci set "dhcp.@domain[-1].name=time.android.com"
uci set "dhcp.@domain[-1].ip=203.107.6.88"

17
18
19
# 检查配置文件pppoe-settings是否存在 该文件由build.sh动态生成
SETTINGS_FILE="/etc/config/pppoe-settings"
if [ ! -f "$SETTINGS_FILE" ]; then
wukongdaily's avatar
wukongdaily committed
20
    echo "PPPoE settings file not found. Skipping." >>$LOGFILE
21
else
wukongdaily's avatar
wukongdaily committed
22
23
    # 读取pppoe信息($enable_pppoe、$pppoe_account、$pppoe_password)
    . "$SETTINGS_FILE"
24
fi
25

wukongdaily's avatar
wukongdaily committed
26
# 1. 先获取所有物理接口列表
27
ifnames=""
28
for iface in /sys/class/net/*; do
wukongdaily's avatar
wukongdaily committed
29
30
31
32
    iface_name=$(basename "$iface")
    if [ -e "$iface/device" ] && echo "$iface_name" | grep -Eq '^eth|^en'; then
        ifnames="$ifnames $iface_name"
    fi
33
done
34
ifnames=$(echo "$ifnames" | awk '{$1=$1};1')
35

wukongdaily's avatar
wukongdaily committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
count=$(echo "$ifnames" | wc -w)
echo "Detected physical interfaces: $ifnames" >>$LOGFILE
echo "Interface count: $count" >>$LOGFILE

# 2. 根据板子型号映射WAN和LAN接口
board_name=$(cat /tmp/sysinfo/board_name 2>/dev/null || echo "unknown")
echo "Board detected: $board_name" >>$LOGFILE

wan_ifname=""
lan_ifnames=""
# 此处特殊处理个别开发板网口顺序问题
case "$board_name" in
    "radxa,e20c"|"friendlyarm,nanopi-r5c")
        wan_ifname="eth1"
        lan_ifnames="eth0"
        echo "Using $board_name mapping: WAN=$wan_ifname LAN=$lan_ifnames" >>"$LOGFILE"
        ;;
    *)
        # 默认第一个接口为WAN,其余为LAN
        wan_ifname=$(echo "$ifnames" | awk '{print $1}')
        lan_ifnames=$(echo "$ifnames" | cut -d ' ' -f2-)
        echo "Using default mapping: WAN=$wan_ifname LAN=$lan_ifnames" >>"$LOGFILE"
        ;;
esac

# 3. 配置网络
62
if [ "$count" -eq 1 ]; then
wukongdaily's avatar
wukongdaily committed
63
    # 单网口设备,DHCP模式
wukongdaily's avatar
wukongdaily committed
64
    uci set network.lan.proto='dhcp'
65
66
    uci delete network.lan.ipaddr
    uci delete network.lan.netmask
wukongdaily's avatar
wukongdaily committed
67
68
    uci delete network.lan.gateway
    uci delete network.lan.dns
69
    uci commit network
70
elif [ "$count" -gt 1 ]; then
wukongdaily's avatar
wukongdaily committed
71
72
    # 多网口设备配置
    # 配置WAN
wukongdaily's avatar
wukongdaily committed
73
74
75
    uci set network.wan=interface
    uci set network.wan.device="$wan_ifname"
    uci set network.wan.proto='dhcp'
wukongdaily's avatar
wukongdaily committed
76
77

    # 配置WAN6
wukongdaily's avatar
wukongdaily committed
78
79
    uci set network.wan6=interface
    uci set network.wan6.device="$wan_ifname"
wukongdaily's avatar
wukongdaily committed
80
81
82
    uci set network.wan6.proto='dhcpv6'

    # 查找 br-lan 设备 section
wukongdaily's avatar
wukongdaily committed
83
84
85
86
    section=$(uci show network | awk -F '[.=]' '/\.@?device\[\d+\]\.name=.br-lan.$/ {print $2; exit}')
    if [ -z "$section" ]; then
        echo "error:cannot find device 'br-lan'." >>$LOGFILE
    else
wukongdaily's avatar
wukongdaily committed
87
        # 删除原有ports
wukongdaily's avatar
wukongdaily committed
88
        uci -q delete "network.$section.ports"
wukongdaily's avatar
wukongdaily committed
89
        # 添加LAN接口端口
wukongdaily's avatar
wukongdaily committed
90
91
92
        for port in $lan_ifnames; do
            uci add_list "network.$section.ports"="$port"
        done
wukongdaily's avatar
wukongdaily committed
93
        echo "Updated br-lan ports: $lan_ifnames" >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
94
    fi
wukongdaily's avatar
wukongdaily committed
95

wukongdaily's avatar
wukongdaily committed
96
    # LAN口设置静态IP
wukongdaily's avatar
wukongdaily committed
97
    uci set network.lan.proto='static'
wukongdaily's avatar
wukongdaily committed
98
    # 多网口设备 支持修改为别的管理后台地址 在Github Action 的UI上自行输入即可 
wukongdaily's avatar
wukongdaily committed
99
    uci set network.lan.netmask='255.255.255.0'
wukongdaily's avatar
wukongdaily committed
100
101
102
103
104
105
106
107
108
109
110
    # 设置路由器管理后台地址
    IP_VALUE_FILE="/etc/config/custom_router_ip.txt"
    if [ -f "$IP_VALUE_FILE" ]; then
        CUSTOM_IP=$(cat "$IP_VALUE_FILE")
        # 用户在UI上设置的路由器后台管理地址
        uci set network.lan.ipaddr=$CUSTOM_IP
        echo "custom router ip is $CUSTOM_IP" >> $LOGFILE
    else
        uci set network.lan.ipaddr='192.168.100.1'
        echo "default router ip is 192.168.100.1" >> $LOGFILE
    fi
111

wukongdaily's avatar
wukongdaily committed
112
113
    # PPPoE设置
    echo "enable_pppoe value: $enable_pppoe" >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
114
    if [ "$enable_pppoe" = "yes" ]; then
wukongdaily's avatar
wukongdaily committed
115
        echo "PPPoE enabled, configuring..." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
116
        uci set network.wan.proto='pppoe'
wukongdaily's avatar
wukongdaily committed
117
118
        uci set network.wan.username="$pppoe_account"
        uci set network.wan.password="$pppoe_password"
wukongdaily's avatar
wukongdaily committed
119
120
121
        uci set network.wan.peerdns='1'
        uci set network.wan.auto='1'
        uci set network.wan6.proto='none'
wukongdaily's avatar
wukongdaily committed
122
        echo "PPPoE config done." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
123
    else
wukongdaily's avatar
wukongdaily committed
124
        echo "PPPoE not enabled." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
125
    fi
wukongdaily's avatar
wukongdaily committed
126
127

    uci commit network
128
129
fi

wukongdaily's avatar
wukongdaily committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 若安装了dockerd 则设置docker的防火墙规则
# 扩大docker涵盖的子网范围 '172.16.0.0/12'
# 方便各类docker容器的端口顺利通过防火墙 
if command -v dockerd >/dev/null 2>&1; then
    echo "检测到 Docker,正在配置防火墙规则..."
    FW_FILE="/etc/config/firewall"

    # 删除所有名为 docker 的 zone
    uci delete firewall.docker

    # 先获取所有 forwarding 索引,倒序排列删除
    for idx in $(uci show firewall | grep "=forwarding" | cut -d[ -f2 | cut -d] -f1 | sort -rn); do
        src=$(uci get firewall.@forwarding[$idx].src 2>/dev/null)
        dest=$(uci get firewall.@forwarding[$idx].dest 2>/dev/null)
        echo "Checking forwarding index $idx: src=$src dest=$dest"
        if [ "$src" = "docker" ] || [ "$dest" = "docker" ]; then
            echo "Deleting forwarding @forwarding[$idx]"
            uci delete firewall.@forwarding[$idx]
        fi
    done
    # 提交删除
    uci commit firewall
    # 追加新的 zone + forwarding 配置
    cat <<EOF >>"$FW_FILE"

config zone 'docker'
  option input 'ACCEPT'
  option output 'ACCEPT'
  option forward 'ACCEPT'
  option name 'docker'
  list subnet '172.16.0.0/12'

config forwarding
  option src 'docker'
  option dest 'lan'

config forwarding
  option src 'docker'
  option dest 'wan'

config forwarding
  option src 'lan'
  option dest 'docker'
EOF

else
    echo "未检测到 Docker,跳过防火墙配置。"
fi
178

wukongdaily's avatar
wukongdaily committed
179
180
181
# 设置所有网口可访问网页终端
uci delete ttyd.@ttyd[0].interface

wukongdaily's avatar
wukongdaily committed
182
183
184
# 设置所有网口可连接 SSH
uci set dropbear.@dropbear[0].Interface=''
uci commit
wukongdaily's avatar
wukongdaily committed
185

186
187
# 设置编译作者信息
FILE_PATH="/etc/openwrt_release"
188
NEW_DESCRIPTION="Packaged by wukongdaily"
189
190
sed -i "s/DISTRIB_DESCRIPTION='[^']*'/DISTRIB_DESCRIPTION='$NEW_DESCRIPTION'/" "$FILE_PATH"

191
192
193
194
195
196
197
# 若luci-app-advancedplus (进阶设置)已安装 则去除zsh的调用 防止命令行报 /usb/bin/zsh: not found的提示
if opkg list-installed | grep -q '^luci-app-advancedplus '; then
    sed -i '/\/usr\/bin\/zsh/d' /etc/profile
    sed -i '/\/bin\/zsh/d' /etc/init.d/advancedplus
    sed -i '/\/usr\/bin\/zsh/d' /etc/init.d/advancedplus
fi

198
exit 0