tun2socks 0.1.5

High performance tun2socks
Documentation
name: "Build"

on:
  push:
    branches:
      - '**'
  pull_request:
  release:
    types:
      - published
  workflow_dispatch:

jobs:
  source:
    name: Source
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Gen Source
        run: |
          REV_ID=$(git rev-parse --short HEAD)
          mkdir -p hev-socks5-tunnel-${{ github.ref_name }}
          git ls-files --recurse-submodules | tar c -O -T- | tar x -C hev-socks5-tunnel-${{ github.ref_name }}
          echo ${REV_ID} > hev-socks5-tunnel-${{ github.ref_name }}/.rev-id
          tar cJf hev-socks5-tunnel-${{ github.ref_name }}.tar.xz hev-socks5-tunnel-${{ github.ref_name }}
      - name: Upload source
        uses: actions/upload-artifact@v4
        with:
          name: hev-socks5-tunnel-${{ github.ref_name }}.tar.xz
          path: hev-socks5-tunnel-${{ github.ref_name }}.tar.xz
          if-no-files-found: error
          retention-days: 1

  linux:
    name: Linux
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        include:
          - name: arm64
            tool: aarch64-unknown-linux-musl
          - name: arm32
            tool: arm-unknown-linux-musleabi
          - name: arm32hf
            tool: arm-unknown-linux-musleabihf
          - name: arm32v7
            tool: armv7-unknown-linux-musleabi
          - name: arm32v7hf
            tool: armv7-unknown-linux-musleabihf
          - name: i586
            tool: i586-unknown-linux-musl
          - name: i686
            tool: i686-unknown-linux-musl
          - name: loong64
            tool: loongarch64-unknown-linux-musl
          - name: m68k
            tool: m68k-unknown-linux-musl
          - name: microblazeel
            tool: microblazeel-xilinx-linux-musl
          - name: microblaze
            tool: microblaze-xilinx-linux-musl
          - name: mips64el
            tool: mips64el-unknown-linux-musl
          - name: mips64
            tool: mips64-unknown-linux-musl
          - name: mips32el
            tool: mipsel-unknown-linux-musl
          - name: mips32elsf
            tool: mipsel-unknown-linux-muslsf
          - name: mips32
            tool: mips-unknown-linux-musl
          - name: mips32sf
            tool: mips-unknown-linux-muslsf
          - name: powerpc64
            tool: powerpc64-unknown-linux-musl
          - name: powerpc
            tool: powerpc-unknown-linux-musl
          - name: riscv32
            tool: riscv32-unknown-linux-musl
          - name: riscv64
            tool: riscv64-unknown-linux-musl
          - name: s390x
            tool: s390x-ibm-linux-musl
          - name: sh
            tool: sh-multilib-linux-musl
          - name: sheb
            tool: sh-multilib-linux-musl
            env:
              CFLAGS: "-mb"
          - name: x86_64
            tool: x86_64-unknown-linux-musl
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Build ${{ matrix.name }}
        run: |
          sudo mkdir -p /opt/x-tools
          wget https://github.com/musl-cross/musl-cross/releases/download/20241103/${{ matrix.tool }}.tar.xz
          sudo tar xf ${{ matrix.tool }}.tar.xz -C /opt/x-tools
          make CROSS_PREFIX=/opt/x-tools/${{ matrix.tool }}/bin/${{ matrix.tool }}- CFLAGS=${{ matrix.env.CFLAGS }} ENABLE_STATIC=1 -j`nproc`
          cp bin/hev-socks5-tunnel hev-socks5-tunnel-linux-${{ matrix.name }}
      - name: Upload ${{ matrix.name }}
        uses: actions/upload-artifact@v4
        with:
          name: hev-socks5-tunnel-linux-${{ matrix.name }}
          path: hev-socks5-tunnel-linux-${{ matrix.name }}
          if-no-files-found: error
          retention-days: 1

  release:
    name: Release
    runs-on: ubuntu-22.04
    needs:
      - source
      - linux
    if: github.event_name == 'release'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: release
          pattern: "hev-*"
          merge-multiple: true
      - name: Upload artifacts
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          for i in release/hev-*; do
            gh release upload ${{ github.event.release.tag_name }} $i
          done

  macos:
    name: macOS
    runs-on: macos-14
    if: github.event_name != 'release'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Build native
        run: |
          make
          make clean
      - name: Build cross
        run: |
          ./build-apple.sh

  android:
    name: Android
    runs-on: ubuntu-22.04
    if: github.event_name != 'release'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Prepare
        run: |
          wget https://dl.google.com/android/repository/android-ndk-r27b-linux.zip
          unzip android-ndk-r27b-linux.zip
          ln -sf . jni
      - name: Build
        run: |
          ./android-ndk-r27b/ndk-build

  llvm:
    name: LLVM
    runs-on: ubuntu-22.04
    if: github.event_name != 'release'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Prepare
        run: |
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
          sudo add-apt-repository -y 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy main'
          sudo apt install -y clang
      - name: Build
        run: |
          make CC=clang ENABLE_STATIC=1 -j`nproc`

  docker:
    name: Build Docker Image
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        platform:
          - linux/amd64
          - linux/386
          - linux/arm64/v8
          - linux/arm/v7
          - linux/arm/v6
          - linux/riscv64
    permissions:
      packages: write
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          submodules: true
      - name: Prepare QEMU
        uses: docker/setup-qemu-action@v3
      - name: Prepare Buildx
        uses: docker/setup-buildx-action@v3
      - name: Prepare Repo Name
        id: repo
        run: |
          echo "repository=${GITHUB_REPOSITORY@L}" >> $GITHUB_OUTPUT
      - name: Prepare Digest
        run: |
          platform=${{ matrix.platform }}
          echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
      - name: Login GitHub Packages Docker Image Repository
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and Push Docker Image
        id: build
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          platforms: ${{ matrix.platform }}
          provenance: false
          outputs: type=image,name=ghcr.io/${{ steps.repo.outputs.repository }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }}
          cache-from: type=gha,scope=${{ matrix.platform }}
          cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
      - name: Export Digest
        if: github.event_name != 'pull_request'
        run: |
          mkdir -p /tmp/digests
          digest="${{ steps.build.outputs.digest }}"
          touch "/tmp/digests/${digest#sha256:}"
      - name: Upload Digest
        uses: actions/upload-artifact@v4
        if: github.event_name != 'pull_request'
        with:
          name: digests-${{ env.PLATFORM_PAIR }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  docker-merge:
    name: Merge Docker Image Tags
    runs-on: ubuntu-22.04
    if: github.event_name != 'pull_request'
    needs:
      - docker
    permissions:
      packages: write
      contents: read
    steps:
      - name: Download Digests
        uses: actions/download-artifact@v4
        with:
          path: /tmp/digests
          pattern: digests-*
          merge-multiple: true
      - name: Prepare Buildx
        uses: docker/setup-buildx-action@v3
      - name: Prepare Repo Name
        id: repo
        run: |
          echo "repository=${GITHUB_REPOSITORY@L}" >> $GITHUB_OUTPUT
      - name: Login GitHub Packages Docker Image Repository
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Format Docker Image Meta
        uses: docker/metadata-action@v5
        id: docker_meta
        with:
          images: ghcr.io/${{ steps.repo.outputs.repository }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=nightly,enable={{is_default_branch}}
      - name: Create Manifest List and Push
        working-directory: /tmp/digests
        run: |
          docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
            $(printf 'ghcr.io/${{ steps.repo.outputs.repository }}@sha256:%s ' *)
      - name: Inspect image
        run: |
          docker buildx imagetools inspect ghcr.io/${{ steps.repo.outputs.repository }}:${{ steps.docker_meta.outputs.version }}