99-custom.sh 6.36 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
96

    # LAN静态IP设置
wukongdaily's avatar
wukongdaily committed
97
    uci set network.lan.proto='static'
wukongdaily's avatar
wukongdaily committed
98
    uci set network.lan.ipaddr='192.168.100.1'
wukongdaily's avatar
wukongdaily committed
99
    uci set network.lan.netmask='255.255.255.0'
wukongdaily's avatar
wukongdaily committed
100
    echo "Set LAN IP to 192.168.100.1" >>$LOGFILE
101

wukongdaily's avatar
wukongdaily committed
102
103
    # PPPoE设置
    echo "enable_pppoe value: $enable_pppoe" >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
104
    if [ "$enable_pppoe" = "yes" ]; then
wukongdaily's avatar
wukongdaily committed
105
        echo "PPPoE enabled, configuring..." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
106
        uci set network.wan.proto='pppoe'
wukongdaily's avatar
wukongdaily committed
107
108
        uci set network.wan.username="$pppoe_account"
        uci set network.wan.password="$pppoe_password"
wukongdaily's avatar
wukongdaily committed
109
110
111
        uci set network.wan.peerdns='1'
        uci set network.wan.auto='1'
        uci set network.wan6.proto='none'
wukongdaily's avatar
wukongdaily committed
112
        echo "PPPoE config done." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
113
    else
wukongdaily's avatar
wukongdaily committed
114
        echo "PPPoE not enabled." >>$LOGFILE
wukongdaily's avatar
wukongdaily committed
115
    fi
wukongdaily's avatar
wukongdaily committed
116
117

    uci commit network
118
119
fi

wukongdaily's avatar
wukongdaily committed
120
121
122
123
124
125
126
127
128
129
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
# 若安装了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
168

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

wukongdaily's avatar
wukongdaily committed
172
173
174
# 设置所有网口可连接 SSH
uci set dropbear.@dropbear[0].Interface=''
uci commit
wukongdaily's avatar
wukongdaily committed
175

176
177
# 设置编译作者信息
FILE_PATH="/etc/openwrt_release"
178
NEW_DESCRIPTION="Packaged by wukongdaily"
179
180
sed -i "s/DISTRIB_DESCRIPTION='[^']*'/DISTRIB_DESCRIPTION='$NEW_DESCRIPTION'/" "$FILE_PATH"

181
182
183
184
185
186
187
# 若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

188
exit 0