From a0797a350551304fcd522bc051c2c3c12e7e04f3 Mon Sep 17 00:00:00 2001 From: Matas Misiunas Date: Sun, 7 Apr 2024 22:56:31 +0300 Subject: [PATCH] Add OpenWRT build option --- build/foss/Cargo.lock | 36 ++++++++--------- build/foss/Cargo.toml | 2 +- build/foss/build_openwrt.sh | 25 ++++++++++++ build/openvpn/build.sh | 78 ++++++++++++++++++++++-------------- ci/compile.sh | 44 +++++++++++++++----- daemon/vpn/openvpn/config.go | 4 +- go.mod | 7 ++-- go.sum | 13 +++--- magefiles/mage.go | 78 ++++++++++++++++++++++++++++++++++-- 9 files changed, 213 insertions(+), 74 deletions(-) create mode 100755 build/foss/build_openwrt.sh diff --git a/build/foss/Cargo.lock b/build/foss/Cargo.lock index fb221424..e5e4138a 100644 --- a/build/foss/Cargo.lock +++ b/build/foss/Cargo.lock @@ -3192,7 +3192,7 @@ checksum = "b4e17d8598067a8c134af59cd33c1c263470e089924a11ab61cf61690919fe3b" [[package]] name = "telio" version = "4.3.3" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "anyhow", "async-trait", @@ -3244,7 +3244,7 @@ dependencies = [ [[package]] name = "telio-crypto" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "base64 0.13.1", "crypto_box", @@ -3260,7 +3260,7 @@ dependencies = [ [[package]] name = "telio-dns" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "base64 0.13.1", @@ -3286,7 +3286,7 @@ dependencies = [ [[package]] name = "telio-firewall" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "hashlink", "pnet_packet 0.28.0", @@ -3299,7 +3299,7 @@ dependencies = [ [[package]] name = "telio-lana" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "anyhow", "serde", @@ -3313,7 +3313,7 @@ dependencies = [ [[package]] name = "telio-model" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "ipnetwork", "itertools", @@ -3333,7 +3333,7 @@ dependencies = [ [[package]] name = "telio-nat-detect" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "nat-detect", "tokio", @@ -3342,7 +3342,7 @@ dependencies = [ [[package]] name = "telio-nurse" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "bitflags 1.3.2", @@ -3376,7 +3376,7 @@ dependencies = [ [[package]] name = "telio-pq" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "blake2", "boringtun", @@ -3400,7 +3400,7 @@ dependencies = [ [[package]] name = "telio-proto" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "bytes", "protobuf", @@ -3416,7 +3416,7 @@ dependencies = [ [[package]] name = "telio-proxy" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "crypto_box", @@ -3435,7 +3435,7 @@ dependencies = [ [[package]] name = "telio-relay" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "bytes", @@ -3471,7 +3471,7 @@ dependencies = [ [[package]] name = "telio-sockets" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "block", "boringtun", @@ -3499,7 +3499,7 @@ dependencies = [ [[package]] name = "telio-task" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "futures", @@ -3512,7 +3512,7 @@ dependencies = [ [[package]] name = "telio-traversal" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "base64 0.13.1", @@ -3549,7 +3549,7 @@ dependencies = [ [[package]] name = "telio-utils" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "futures", "hashlink", @@ -3564,7 +3564,7 @@ dependencies = [ [[package]] name = "telio-wg" version = "0.1.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "async-trait", "boringtun", @@ -4162,7 +4162,7 @@ checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] name = "wg-go-rust-wrapper" version = "1.0.0" -source = "git+https://github.com/NordSecurity/libtelio?tag=v4.3.3#e7a8f6b2018766d7b9623370f2189d1480987dea" +source = "git+https://github.com/NordSecurity/libtelio?branch=matislovas/openwrt_build#6b6e30db607cf9a14736c347c1843e9024a0ccd4" dependencies = [ "cc", "libc", diff --git a/build/foss/Cargo.toml b/build/foss/Cargo.toml index 244ad58f..769bce28 100644 --- a/build/foss/Cargo.toml +++ b/build/foss/Cargo.toml @@ -9,4 +9,4 @@ crate-type = ["staticlib"] [dependencies] norddrop = { git = "https://github.com/NordSecurity/libdrop", tag = "v5.4.0_moose_backport" } -telio = { git = "https://github.com/NordSecurity/libtelio", tag= "v4.3.3" } +telio = { git = "https://github.com/NordSecurity/libtelio", tag= "v4.3.5" } diff --git a/build/foss/build_openwrt.sh b/build/foss/build_openwrt.sh new file mode 100755 index 00000000..aaf89fca --- /dev/null +++ b/build/foss/build_openwrt.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euox + +source "${WORKDIR}/ci/archs.sh" +source "${WORKDIR}/ci/env.sh" + +declare -A targets=( + [amd64]=x86_64-unknown-linux-musl + [aarch64]=aarch64-unknown-linux-musl +) + +declare -A cc=( + [amd64]="x86_64-openwrt-linux-musl-gcc" + [aarch64]="aarch64-openwrt-linux-musl-gcc" +) + +pushd "${WORKDIR}/build/foss" +for arch in "${ARCHS[@]}"; do + target="${targets[$arch]}" + compiler="${cc[$arch]}" + TARGET_CC="${compiler}" cargo build --target "${target}" --release + mkdir -p "${WORKDIR}/bin/deps/foss/${arch}" + ln -frs "${WORKDIR}/build/foss/target/${target}/release" "${WORKDIR}/bin/deps/foss/${arch}/latest" +done +popd diff --git a/build/openvpn/build.sh b/build/openvpn/build.sh index f954e994..9d748fa1 100755 --- a/build/openvpn/build.sh +++ b/build/openvpn/build.sh @@ -68,43 +68,63 @@ declare -A cross_compiler_map=( [aarch64]=aarch64-linux-gnu-gcc ) +declare -A cross_compiler_map_openwrt=( + [amd64]="x86_64-openwrt-linux-musl-gcc" + [aarch64]="aarch64-openwrt-linux-musl-gcc" +) + pushd "${current_dir}" target="" + compiler="" openssl_cflags="" openssl_ldflags="" lzo_cflags="-g -O2" lzo_ldflags="" openvpn_cflags="-Wall -Wno-unused-parameter -Wno-unused-function -g -O2 -D_FORTIFY_SOURCE=2 -std=c99 -fstack-protector" openvpn_ldflags="-Wl,-z,relro,-z,now -Wl,--as-needed" - compiler="${cross_compiler_map[${ARCH}]}" - case "${ARCH}" in - "i386") - target="i686-linux-gnu" - prefix="$target-" - openssl_cflags+=" -m32" - openssl_ldflags+=" -m32" - lzo_cflags+=" -m32" - lzo_ldflags+=" -m32" - openvpn_cflags+=" -m32" - openvpn_ldflags+=" -m32" - ;; - "amd64") - target="x86_64-linux-gnu" - prefix="$target-" - ;; - "armel") - target="arm-linux-gnueabi" - prefix="$target-" - ;; - "armhf") - target="arm-linux-gnueabihf" - prefix="$target-" - ;; - "aarch64") - target="aarch64-linux-gnu" - prefix="$target-" - ;; - esac + + if [[ "${OS}" == "openwrt" ]]; then + compiler="${cross_compiler_map_openwrt[${ARCH}]}" + case "${ARCH}" in + "amd64") + target="x86_64-openwrt-linux-musl" + ;; + "aarch64") + target="aarch64-openwrt-linux-musl" + ;; + esac + prefix="$target-" + else + compiler="${cross_compiler_map[${ARCH}]}" + case "${ARCH}" in + "i386") + target="i686-linux-gnu" + prefix="$target-" + openssl_cflags+=" -m32" + openssl_ldflags+=" -m32" + lzo_cflags+=" -m32" + lzo_ldflags+=" -m32" + openvpn_cflags+=" -m32" + openvpn_ldflags+=" -m32" + ;; + "amd64") + target="x86_64-linux-gnu" + prefix="$target-" + ;; + "armel") + target="arm-linux-gnueabi" + prefix="$target-" + ;; + "armhf") + target="arm-linux-gnueabihf" + prefix="$target-" + ;; + "aarch64") + target="aarch64-linux-gnu" + prefix="$target-" + ;; + esac + fi pushd "${sources}/openssl-${OPENSSL_VERSION}" configure_openssl "${compiler}" diff --git a/ci/compile.sh b/ci/compile.sh index b89069a2..558b4751 100755 --- a/ci/compile.sh +++ b/ci/compile.sh @@ -4,6 +4,7 @@ set -euox source "${WORKDIR}"/ci/env.sh source "${WORKDIR}"/ci/archs.sh + # Since race detector has huge performance price and it works only on amd64 and does not # work with pie executables, its enabled only for development builds. # shellcheck disable=SC2153 @@ -30,8 +31,11 @@ declare -A names_map=( [norduser]=norduserd ) +declare -A cross_compiler_map +declare -A cross_compiler_map_openwrt + # shellcheck disable=SC2034 -declare -A cross_compiler_map=( +cross_compiler_map=( [i386]=i686-linux-gnu-gcc [amd64]=x86_64-linux-gnu-gcc [armel]=arm-linux-gnueabi-gcc @@ -39,13 +43,23 @@ declare -A cross_compiler_map=( [aarch64]=aarch64-linux-gnu-gcc ) +cross_compiler_map_openwrt=( + [amd64]="x86_64-openwrt-linux-musl-gcc" + [aarch64]="aarch64-openwrt-linux-musl-gcc" +) + # Required by Go when cross-compiling export CGO_ENABLED=1 -GOARCH="${ARCHS_GO["${ARCH}"]}" -export GOARCH="${GOARCH}" + +if [[ "${OS}" == "openwrt" ]]; then + mkdir -p "$GO_BUILD_DIR/bin" "$GO_BUILD_CACHE_DIR" "$GO_MOD_CACHE_DIR" "$GO_BUILD_BIN_DIR" +else + GOARCH="${ARCHS_GO["${ARCH}"]}" + export GOARCH +fi # C compiler flags for binary hardening. -export CGO_CFLAGS="-g -O2 -D_FORTIFY_SOURCE=2" +export CGO_CFLAGS="${CGO_CFLAGS:-""} -g -O2 -D_FORTIFY_SOURCE=2" # These C linker flags get appended to the ones specified in the source code export CGO_LDFLAGS="${CGO_LDFLAGS:-""} -Wl,-z,relro,-z,now" @@ -83,12 +97,20 @@ fi for program in ${!names_map[*]}; do # looping over keys pushd "${WORKDIR}/cmd/${program}" - # BUILDMODE can be no value and `go` does not like empty parameter '' - # this is why surrounding double quotes are removed to not cause empty parameter i.e. '' - # shellcheck disable=SC2086 - CC="${cross_compiler_map[${ARCH}]}" \ - go build ${BUILD_FLAGS:+"${BUILD_FLAGS}"} ${BUILDMODE:-} -tags "${tags}" \ - -ldflags "-linkmode=external ${ldflags}" \ - -o "${WORKDIR}/bin/${ARCH}/${names_map[${program}]}" + if [[ "${OS}" == "openwrt" ]]; then + CC="${cross_compiler_map_openwrt[${ARCH}]}" \ + go build ${BUILD_FLAGS:+"${BUILD_FLAGS}"} ${BUILDMODE:-} -tags "${tags}" \ + -ldflags "-linkmode=external ${ldflags}" \ + -o "${WORKDIR}/bin/${ARCH}/${names_map[${program}]}" + cp -r "${WORKDIR}/bin/${ARCH}/${names_map[${program}]}" "${GO_BUILD_BIN_DIR}" + else + # BUILDMODE can be no value and `go` does not like empty parameter '' + # this is why surrounding double quotes are removed to not cause empty parameter i.e. '' + # shellcheck disable=SC2086 + CC="${cross_compiler_map[${ARCH}]}" \ + go build ${BUILD_FLAGS:+"${BUILD_FLAGS}"} ${BUILDMODE:-}-tags "${tags}" \ + -ldflags "-linkmode=external ${ldflags}" \ + -o "${WORKDIR}/bin/${ARCH}/${names_map[${program}]}" + fi popd done diff --git a/daemon/vpn/openvpn/config.go b/daemon/vpn/openvpn/config.go index 6bb50400..2b549e44 100644 --- a/daemon/vpn/openvpn/config.go +++ b/daemon/vpn/openvpn/config.go @@ -13,8 +13,8 @@ import ( "github.com/NordSecurity/nordvpn-linux/config" "github.com/NordSecurity/nordvpn-linux/internal" - "github.com/jbowtie/gokogiri/xml" - "github.com/jbowtie/ratago/xslt" + "github.com/NordSecurity/gokogiri/xml" + "github.com/NordSecurity/ratago/xslt" ) const ovpnConfig = ` diff --git a/go.mod b/go.mod index 4cb3ffac..2a7d7135 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.2 require ( github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4 github.com/NordSecurity/libdrop v1.1.2-0.20240214140349-bc7d2cffb948 - github.com/NordSecurity/libtelio v0.0.0-20240412122320-157263e48f69 + github.com/NordSecurity/libtelio v0.0.0-20240408140328-b686c9a8c058 github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d github.com/coreos/go-semver v0.3.1 github.com/deckarep/golang-set/v2 v2.6.0 @@ -22,9 +22,9 @@ require ( github.com/godbus/dbus/v5 v5.1.0 github.com/google/uuid v1.6.0 github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b - github.com/jbowtie/gokogiri v0.0.0-20190301021639-37f655d3078f - github.com/jbowtie/ratago v0.0.0-20200401224626-3140c0a9b186 + github.com/kofalt/go-memoize v0.0.0-20220914132407-0b5d6a304579 github.com/magefile/mage v1.14.0 + github.com/NordSecurity/ratago v0.0.0-20240614120605-6d41449322ed github.com/mattn/go-isatty v0.0.17 github.com/milosgajdos/tenus v0.0.3 github.com/quic-go/quic-go v0.42.0 @@ -64,6 +64,7 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/NordSecurity/gokogiri v0.0.0-20240614112150-0f4833678724// indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/morikuni/aec v1.0.0 // indirect diff --git a/go.sum b/go.sum index 713fbadc..022f5f82 100644 --- a/go.sum +++ b/go.sum @@ -11,12 +11,16 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg6 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg= github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= +github.com/NordSecurity/gokogiri v0.0.0-20240614112150-0f4833678724 h1:A4cexOJNdwA260klguU9rYqD0VAX14poXG8WySL0I78= +github.com/NordSecurity/gokogiri v0.0.0-20240614112150-0f4833678724/go.mod h1:9GiVyouzyFROF6v6ep6fYuDFDsZxqNmjYC7i8hfv9iI= github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4 h1:2ozEjYEw4WzXAXe/t5rxnvcFytR8z/PA/Xrv3FpByng= github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4/go.mod h1:tguhorMSnkMcQExNIHWBX6TRhFeGYlERzbeAWZ4j9Uw= github.com/NordSecurity/libdrop v1.1.2-0.20240214140349-bc7d2cffb948 h1:stDWDEyqQwjw/o3SQvqfBXTGj4c0tgthB8f4JHTHlLs= github.com/NordSecurity/libdrop v1.1.2-0.20240214140349-bc7d2cffb948/go.mod h1:uWQP1L7e1CCtg1VYvm4/zjUNR7Oyw/EDjQfo8qO1nM4= -github.com/NordSecurity/libtelio v0.0.0-20240412122320-157263e48f69 h1:uc7IHbJTshw2R9f+8s8FBUF0tdj6TqzCUdv+DnZSfmg= -github.com/NordSecurity/libtelio v0.0.0-20240412122320-157263e48f69/go.mod h1:gWS9UWU2FSEixmSWdm1MsIoacVttKykO0mgPec2uBVc= +github.com/NordSecurity/libtelio v0.0.0-20240408140328-b686c9a8c058 h1:aTxg1g9ZgUBnHCuQ9dRyKXHaK8j1JdUMr237RvKtxZs= +github.com/NordSecurity/libtelio v0.0.0-20240408140328-b686c9a8c058/go.mod h1:gWS9UWU2FSEixmSWdm1MsIoacVttKykO0mgPec2uBVc= +github.com/NordSecurity/ratago v0.0.0-20240614120605-6d41449322ed h1:o7dWUaZcoAPhVuko8lI65KXSjaAMSgzeqrGgJAZErms= +github.com/NordSecurity/ratago v0.0.0-20240614120605-6d41449322ed/go.mod h1:Iez4INN9mrd+k+ClvNMiC4ON+CPTrhWpQ9h/sMex2Io= github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d h1:oUEFXgFRa9Svcjr+O1stzR3vEXZ5OfQxLUcDjqFcOuo= github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d/go.mod h1:PqJ9YgQXNUYUuoLvVrbMmZACufI5xM8JJ5aSuRX5ljg= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -113,14 +117,11 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4= github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jbowtie/gokogiri v0.0.0-20190301021639-37f655d3078f h1:6UIvzqlGM38lOpKP380Wbl0kUyyjutcc7KJUaDM/U4o= -github.com/jbowtie/gokogiri v0.0.0-20190301021639-37f655d3078f/go.mod h1:C3R3VzPq+DAwilxue7DiV6F2QL1rrQX0L56GyI+sBxM= -github.com/jbowtie/ratago v0.0.0-20200401224626-3140c0a9b186 h1:8N1+ik35JbbQVslv63BvyO1yv0TC5Ol/ip26fOy+MP0= -github.com/jbowtie/ratago v0.0.0-20200401224626-3140c0a9b186/go.mod h1:0ZLxKWdtG2yYN5kJTy71ALuAcl/gFhkxuGbKCMufBwI= github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kofalt/go-memoize v0.0.0-20220914132407-0b5d6a304579/go.mod h1:PifxINf6wYU0USPBk0z1Z8Pka1AqeyCJAp9ecCcNL5Q= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/magefiles/mage.go b/magefiles/mage.go index 964febf8..bdc54f30 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -308,7 +308,7 @@ func (Build) SnapDocker(ctx context.Context) error { return buildPackageDocker(ctx, "snap", "") } -func buildBinaries(buildFlags string) error { +func buildBinaries(ctx context.Context, buildFlags string, goenvstr string, rustenvstr string, openwrt bool) error { if err := installHookIfNordsec(); err != nil { return err } @@ -326,7 +326,7 @@ func buildBinaries(buildFlags string) error { } if !strings.Contains(env["FEATURES"], "internal") { - mg.Deps(Build.Rust) + mg.Deps(mg.F(Build.RustCustom, rustenvstr, openwrt)) } git, err := getGitInfo() @@ -340,13 +340,42 @@ func buildBinaries(buildFlags string) error { env["VERSION"] = git.versionTag env["ENVIRONMENT"] = string(internal.Development) env["BUILD_FLAGS"] = buildFlags + if openwrt { + env["OS"] = "openwrt" + } + + // Split the string by whitespace + goenv := strings.Split(goenvstr, " ") + + // Iterate through the environment variables and set them + for _, v := range goenv { + // Split the variable by '=' to get the key-value pair + kv := strings.SplitN(v, "=", 2) + key := kv[0] + if len(kv) > 1 { + value := kv[1] + env[key] = value + } else { + fmt.Printf("Skipping environment variable '%s' as it has no value.\n", key) + } + } return sh.RunWith(env, "ci/compile.sh") } // Binaries from cmd/* for the host architecture -func (Build) Binaries() error { - return buildBinaries("") +func (Build) Binaries(ctx context.Context, goenvstr string, rustenvstr string) error { + return buildBinaries(ctx, "", goenvstr, rustenvstr, false) +} + +// Binaries from cmd/* for the host architecture (openwrt case) +func (Build) Openwrt(ctx context.Context, goenvstr string, rustenvstr string) error { + return buildBinaries(ctx, "", goenvstr, rustenvstr, true) +} + +// Binaries from cmd/* for the host architecture (openwrt case) +func (Build) OpenwrtOpenvpn(ctx context.Context) error { + return buildOpenvpn(ctx, true) } func buildBinariesDocker(ctx context.Context, buildFlags string) error { @@ -391,6 +420,10 @@ func (Build) BinariesDocker(ctx context.Context) error { // Openvpn binaries for the host architecture func (Build) Openvpn(ctx context.Context) error { + return buildOpenvpn(ctx, false) +} + +func buildOpenvpn(ctx context.Context, openwrt bool) error { mg.Deps(DownloadOpenvpn) cwd, err := os.Getwd() @@ -404,6 +437,9 @@ func (Build) Openvpn(ctx context.Context) error { env["ARCH"] = build.Default.GOARCH env["WORKDIR"] = cwd + if openwrt { + env["OS"] = "openwrt" + } return sh.RunWith(env, "build/openvpn/build.sh") } @@ -440,6 +476,40 @@ func (Build) Rust(ctx context.Context) error { return sh.RunWith(env, "build/foss/build.sh") } +// Rust dependencies for the host architecture +func (Build) RustCustom(ctx context.Context, rustenvstr string, openwrt bool) error { + cwd, err := os.Getwd() + if err != nil { + return err + } + env := map[string]string{ + "ARCHS": build.Default.GOARCH, + "WORKDIR": cwd, + } + + // Split the string by whitespace + rustenv := strings.Split(rustenvstr, " ") + + // Iterate through the environment variables and set them + for _, v := range rustenv { + // Split the variable by '=' to get the key-value pair + kv := strings.SplitN(v, "=", 2) + key := kv[0] + if len(kv) > 1 { + value := kv[1] + env[key] = value + } else { + fmt.Printf("Skipping environment variable '%s' as it has no value.\n", key) + } + } + + if openwrt { + return sh.RunWith(env, "build/foss/build_openwrt.sh") + } + + return sh.RunWith(env, "build/foss/build.sh") +} + // Builds rust dependencies using Docker builder func (Build) RustDocker(ctx context.Context) error { env, err := getEnv()