Skip to content

Commit

Permalink
Refactor with mudules
Browse files Browse the repository at this point in the history
  • Loading branch information
EkkoG committed Oct 20, 2023
1 parent d621ab8 commit 6f390ed
Show file tree
Hide file tree
Showing 41 changed files with 297 additions and 237 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ jobs:
# Runs a single command using the runners shell
- name: Build image
run: |
echo "PPPOE_USERNAME=123" >> .env
echo "PPPOE_PASSWORD=123" >> .env
echo "LAN_IP=192.168.1.1" >> .env
echo "CLASH_CONFIG_URL=https://gist.github.com/EkkoG/20a52db0169c4a4769689521b1c5500e/raw/cf61a9397d0bdbd5b6bbad8fbb388ec851116470/clash_example.yaml" >> .env
echo "PPPOE_USERNAME=123" >> modules/network/.env
echo "PPPOE_PASSWORD=123" >> modules/network/.env
echo "LAN_IP=192.168.1.1" >> modules/network/.env
echo "CLASH_CONFIG_URL=https://gist.github.com/EkkoG/20a52db0169c4a4769689521b1c5500e/raw/cf61a9397d0bdbd5b6bbad8fbb388ec851116470/clash_example.yaml" >> modules/openclash/.env
echo "MODULES=openclash openclash-as-default"
./run.sh --image=openwrt/imagebuilder:x86-64-23.05.0-rc3
./run.sh --image=openwrt/imagebuilder:x86-64-22.03.3
./run.sh --image=openwrt/imagebuilder:x86-64-21.02.7
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,24 @@ files/etc/hotplug.d/iface/*
!files/etc/hotplug.d/iface/99-clash
!files/etc/hotplug.d/iface/99-odhcpd

modules/*
!modules/add-feed
!modules/add-feed-base
!modules/base
!modules/argon
!modules/daed
!modules/daed-as-default/
!modules/ib/
!modules/network/
!modules/openclash/
!modules/openclash-as-default/
!modules/opkg-mirror/
!modules/passwall/
!modules/prefer-ipv6-settings/
!modules/python/
!modules/root-password/
!modules/statistics/
!modules/system/
!modules/tools/

docker-compose.yml
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,40 @@
## 特色

- 采用官方原版 [ImageBuilder](https://openwrt.org/docs/guide-user/additional-software/imagebuilder) 构建而非从源码构建,几分钟即可构建完成
- 集成 OpenClash,构建固件时只需要提供 clash 配置链接即可在刷机完成后,直接启动 OpenClash
- 可配置默认 LAN 口 IP,PPPoE 账号密码,推荐的 IPv6 配置,刷机完成后,不用配置网络
- 集成常用代理软件及其最佳实践配置,包括 [openclsh](https://github.com/vernesong/OpenClash), [daed](https://github.com/daeuniverse/daed), [passwall](https://github.com/xiaorouji/openwrt-passwall)
- 可配置默认 LAN 口 IP,PPPoE 账号密码,推荐的 IPv6 配置,刷机完成后,无需配置网络
- 可按照官方推荐的 uci 功能进行自定义,无需代码修改,您可以通过 uci 进行几乎任何自定义
- 模块化配置,详见 [modules](https://github.com/EkkoG/OpenWrt#modules%20%E4%BB%8B%E7%BB%8D)

## modules 介绍

本项目所有的特性均通过 modules 进行配置,您可以根据自己的需求,自由选择需要的模块,或者自行添加新的模块

### modules 目录结构

```bash
packages #定义本 module 依赖的软件包
files/ #定义本 module 需要的文件,按照 [OpenWrt 的 files](https://openwrt.org/docs/guide-developer/toolchain/use-buildsystem#custom_files) 规范,放置到对应的目录下,最后会将所有 module 的 files 合并到一个目录下
post-files.sh #定义本 module 的预处理脚本,会在将当前 module 的 files 合并到最终目录后执行
.env #定义本 module 的环境变量,会将其中的变量的值替换到 files/uci-defaults/ 目录下的文件中
```

您可以通过在项目根目录下的 .env 中定义 MODULES 变量,来选择需要的 module,例如

```bash
MODULES="python -tools"
```

以减号开头的 module 会被排除,上面的例子中,会增加 python module,并排除 tools module

默认使用的 modules 参见 https://github.com/EkkoG/OpenWrt/blob/master/build.sh 开头


## 依赖

- Docker
- docker-compose v1 or v2

## 使用

本项目通过改变使用不同的 ImageBuilder 镜像,来构建不同的固件,镜像名可以从 [Docker Hub](https://hub.docker.com/r/openwrtorg/imagebuilder/tags) 上查看
Expand Down
214 changes: 47 additions & 167 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,185 +1,65 @@
#!/bin/bash -e
if [[ $PWD =~ "immortalwrt" ]]; then
PROJECT_NAME="immortalwrt"
sudo chown -R $(whoami):$(whoami) bin
else
PROJECT_NAME="openwrt"
fi


if [ -z $LAN_IP ]; then
echo "LAN IP 不可为空"
exit 1
fi

cp -r custom_files files

PACKAGES_ARCH=$(cat .config | grep CONFIG_TARGET_ARCH_PACKAGES | awk -F '=' '{print $2}' | sed 's/"//g')
OPENWRT_VERSION=$(cat ./include/version.mk | grep 'VERSION_NUMBER:=$(if' | awk -F ',' '{print $3}' | awk -F ')' '{print $1}')
BIG_VERSION=$(echo $OPENWRT_VERSION | awk -F '.' '{print $1"."$2}')

echo "PACKAGES_ARCH: $PACKAGES_ARCH OPENWRT_VERSION: $OPENWRT_VERSION BIG_VERSION: $BIG_VERSION"
DISTRIB_ARCH=$PACKAGES_ARCH
DISTRIB_RELEASE=$OPENWRT_VERSION
add_packages() {
echo "try add $1"

all_supported=$(curl https://sourceforge.net/projects/ekko-openwrt-dist/files/$1/ | grep -e "<th.*files/$1" | grep -o 'href="/projects[^"]*"' | sed 's/href="//' | sed 's/"$//' | awk -F/ '{print $6}')
echo "All supported version: "
echo "$all_supported"

version=$(echo "$DISTRIB_RELEASE" | awk -F- '{print $1}')
echo "Current version: $version"

# get the first two version number
big_version=$(echo "$version" | awk -F. '{print $1"."$2}')

if [ "$1" == "luci" ]; then
supported=$(echo "$all_supported" | grep "$big_version")
feed_version="$DISTRIB_RELEASE"
default_modules="add-feed ib argon base network opkg-mirror prefer-ipv6-settings statistics system tools"

echo "Default modules: $default_modules"

final_modules=$default_modules
for module in $MODULES; do
# check if module fisrt char is "-"
if [ "${module:0:1}" == "-" ]; then
# remove module from final_modules
temp="$(echo "$final_modules" | tr ' ' '\n')"
final_modules=""
for m in $temp; do
if [ "$m" != "${module:1}" ]; then
final_modules="$final_modules $m"
fi
done
else
supported=$(echo "$all_supported" | grep $DISTRIB_ARCH | grep $big_version)
feed_version="$DISTRIB_ARCH-v$DISTRIB_RELEASE"
# add module to final_modules
final_modules="$final_modules $module"
fi
done
final_modules="$(echo "$final_modules" | tr '\n' ' ')"
echo "Final modules: $final_modules"
cp -r modules work_modules
ls work_modules

echo "Supported version: "
echo "$supported"
for module in $final_modules; do

if [ -z "$supported" ]; then
echo "Your device is not supported"
exit 1
if [ -f "work_modules/$module/packages" ]; then
all_packages="$all_packages $(cat work_modules/$module/packages)"
fi

full_support=0
for i in $supported; do
if [ "$i" = "$feed_version" ]; then
full_support=1
break
fi
done

if [ "$full_support" = "0" ]; then
echo "Your device is not fully supported"
echo "Find the latest version that supports your device"

# 过滤掉 rc 和 SNAPSHOT 版本, 不用 grep
final_release=$(echo "$supported" | grep -v "\-rc" | grep -v "SNAPSHOT" | tail -n 1)
if [ -z "$final_release" ]; then
echo "No final release found, use the latest rc version"
feed_version=$(echo "$supported" | grep "\-rc" | tail -n 1)
else
feed_version=$final_release
fi
if [ -f "work_modules/$module/.env" ]; then
echo "Running envsubst for $module"
mv work_modules/$module/.env work_modules/$module/.env.tmp
# add export to .env, every line
sed -i 's/^/export /' work_modules/$module/.env.tmp
. work_modules/$module/.env.tmp
# envsubst
for file in $(find "work_modules/$module/files/etc/uci-defaults" -type f); do
envsubst < $file | sed -e 's/§/$/g' > $file.tmp
mv $file.tmp $file
done
fi
echo "Feed version: $feed_version"
EKKOG_FEED="src/gz ekkog_$1 https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/ekko-openwrt-dist/$1/$feed_version"
echo "$EKKOG_FEED" >> files/etc/opkg/customfeeds.conf
# 添加软件源到第一行
echo "$EKKOG_FEED" | cat - ./repositories.conf > temp && mv temp ./repositories.conf
}

add_geodata() {
FEED_URL="src/gz ekkog_geodata https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/ekko-openwrt-dist/$1"
echo "$FEED_URL" | cat - ./repositories.conf > temp && mv temp ./repositories.conf
echo "$FEED_URL" >> files/etc/opkg/customfeeds.conf
}

if [[ $OPENWRT_VERSION =~ "SNAPSHOT" ]]; then
PASSWALL_FEED=$(cat <<-END
src/gz passwall_luci https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/snapshots/packages/$PACKAGES_ARCH/passwall_luci
src/gz passwall_packages https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/snapshots/packages/$PACKAGES_ARCH/passwall_packages
src/gz passwall2 https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/snapshots/packages/$PACKAGES_ARCH/passwall2
END
)
else
PASSWALL_FEED=$(cat <<-END
src/gz passwall_luci https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/releases/packages-$BIG_VERSION/$PACKAGES_ARCH/passwall_luci
src/gz passwall_packages https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/releases/packages-$BIG_VERSION/$PACKAGES_ARCH/passwall_packages
src/gz passwall2 https://ghproxy.imciel.com/https://downloads.sourceforge.net/project/openwrt-passwall-build/releases/packages-$BIG_VERSION/$PACKAGES_ARCH/passwall2
END
)
fi


if [ $USE_MIRROR = '1' ]; then
sed -i 's/https:\/\/downloads.'"$PROJECT_NAME"'.org/https:\/\/mirrors.pku.edu.cn\/'"$PROJECT_NAME"'/g' ./repositories.conf
fi

mkdir -p files/etc/opkg/
touch files/etc/opkg/customfeeds.conf

add_packages "luci"
add_packages "packages"
# if big version great than 23.05 or snapshot
if [[ $OPENWRT_VERSION =~ "23.05" ]] || [[ $OPENWRT_VERSION =~ "SNAPSHOT" ]]; then
add_packages "dae"
fi
add_packages "clash"
add_geodata "geodata/Loyalsoldier"

if [ $PROJECT_NAME = "openwrt" ]; then
echo "$PASSWALL_FEED" >> ./repositories.conf
echo "$PASSWALL_FEED" >> files/etc/opkg/customfeeds.conf
fi

cat ./repositories.conf

# 添加签名验证的 key
cp files/etc/opkg/keys/* keys

# 添加 SSH 相关
if [ -f "files/etc/dropbear/authorized_keys" ];then
chmod 644 files/etc/dropbear/authorized_keys
fi

# 不需要的镜像
sed -i '/CONFIG_ISO_IMAGES/ c\# CONFIG_ISO_IMAGES is not set' .config
sed -i '/CONFIG_TARGET_IMAGES_PAD/ c\# CONFIG_TARGET_IMAGES_PAD is not set' .config
sed -i '/CONFIG_VDI_IMAGES/ c\# CONFIG_VDI_IMAGES is not set' .config
sed -i '/CONFIG_VMDK_IMAGES/ c\# CONFIG_VMDK_IMAGES is not set' .config
sed -i '/CONFIG_VHDX_IMAGES/ c\# CONFIG_VHDX_IMAGES is not set' .config


# # 添加本地软件源,安装自定义 ipk 使用
# if [ "$OPENWRT_VERSION" = "21.02" ]; then
# echo "src imagebuilder file:packages" >> ./repositories.conf
# fi

# base packages
all_packages="luci luci-compat luci-lib-ipkg luci-i18n-opkg-zh-cn -dnsmasq dnsmasq-full luci-i18n-base-zh-cn luci-i18n-firewall-zh-cn openssl-util"
# Make time correct at OpenWrt https://github.com/daeuniverse/dae/discussions/345
all_packages="$all_packages zoneinfo-all"

if [ "$PROXY_CLIENT" = "openclash" ] || [[ "$EXTRA_PROXY_CLIENT" =~ "openclash" ]]; then
# openclash
all_packages="$all_packages luci-app-openclash clash-meta-for-openclash"

mkdir -p files/etc/openclash/config
wget --user-agent='clash' $CLASH_CONFIG_URL -O files/etc/openclash/config/config.yaml
fi

if [ "$PROXY_CLIENT" = "passwall" ] || [[ "$EXTRA_PROXY_CLIENT" =~ "passwall" ]]; then
all_packages="$all_packages luci-i18n-passwall-zh-cn"
fi

if [ "$PROXY_CLIENT" = "daed" ] || [[ "$EXTRA_PROXY_CLIENT" =~ "daed" ]]; then
all_packages="$all_packages daed-geoip daed-geosite luci-app-daed"
fi
if [ -d "work_modules/$module/files" ]; then
mkdir -p files
cp -r work_modules/$module/files/** files/
fi

# printenv | grep 'CONFIG_', export all config
for config in $(printenv | grep '^CONFIG_'); do
config_name=$(echo $config | awk -F '=' '{print $1}')
config_value=$(echo $config | awk -F '=' '{print $2}')
sed -i "/$config_name/ c\\$config_name=$config_value" .config
if [ -f "work_modules/$module/post-files.sh" ]; then
echo "Running post-files.sh for $module"
. work_modules/$module/post-files.sh
fi
done


# theme
all_packages="$all_packages $EXTRA_PKGS luci-theme-argon"

echo "EXTRA_PKGS: $EXTRA_PKGS"
echo "All packages: $all_packages"

make info
cat ./repositories.conf
if [ -z "$PROFILE" ]; then
make image PACKAGES="$all_packages" FILES="files"
else
Expand Down
2 changes: 0 additions & 2 deletions files/etc/opkg/keys/12bd84d19e263f64

This file was deleted.

2 changes: 0 additions & 2 deletions files/etc/opkg/keys/8ead5420c4005b0d

This file was deleted.

8 changes: 0 additions & 8 deletions files/etc/uci-defaults/99-daed

This file was deleted.

Loading

0 comments on commit 6f390ed

Please sign in to comment.