diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4770423..d09767d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,19 +1,63 @@ name: release on: push: + branches: + - master + - main # Sequence of patterns matched against refs/tags tags: - "v*" - - "test-release" + - "test-release" env: GITHUB_TOKEN: ${{ github.token }} RUST_BACKTRACE: 1 permissions: contents: write + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true jobs: + daily: + if: startsWith(github.ref, 'refs/heads/') + name: ${{ matrix.platform.os_name }} with rust ${{ matrix.toolchain }} + runs-on: ${{ matrix.platform.os }} + strategy: + fail-fast: false + matrix: + platform: + - os_name: Linux-x86_64 + os: ubuntu-latest + target: x86_64-unknown-linux-musl + bin: hust-network-login + - os_name: Windows-x86_64 + os: windows-latest + target: x86_64-pc-windows-msvc + bin: hust-network-login.exe + toolchain: + - stable + steps: + - uses: actions/checkout@v4 + - name: Cache cargo & target directories + uses: Swatinem/rust-cache@v2 + with: + key: "v2" + - name: Install musl-tools on Linux + run: sudo apt-get update --yes && sudo apt-get install --yes musl-tools + if: contains(matrix.platform.target, 'musl') + - name: Build binary + uses: houseabsolute/actions-rust-cross@v0 + with: + command: "build" + target: ${{ matrix.platform.target }} + toolchain: ${{ matrix.toolchain }} + args: "--locked --release" + strip: true + test: + if: startsWith(github.ref, 'refs/tags/') name: ${{ matrix.platform.os_name }} with rust ${{ matrix.toolchain }} runs-on: ${{ matrix.platform.os }} strategy: @@ -21,74 +65,51 @@ jobs: matrix: platform: - os_name: FreeBSD-x86_64 - os: ubuntu-20.04 + os: ubuntu-latest target: x86_64-unknown-freebsd bin: hust-network-login skip_tests: true - os_name: Linux-x86_64 - os: ubuntu-20.04 + os: ubuntu-latest target: x86_64-unknown-linux-musl bin: hust-network-login - os_name: Linux-aarch64 - os: ubuntu-20.04 + os: ubuntu-latest target: aarch64-unknown-linux-musl bin: hust-network-login - os_name: Linux-arm - os: ubuntu-20.04 + os: ubuntu-latest target: arm-unknown-linux-musleabi bin: hust-network-login - os_name: Linux-i686 - os: ubuntu-20.04 + os: ubuntu-latest target: i686-unknown-linux-musl bin: hust-network-login skip_tests: true - - os_name: Linux-mips - os: ubuntu-20.04 - target: mips-unknown-linux-musl - bin: hust-network-login - - os_name: Linux-mipsel - os: ubuntu-20.04 - target: mipsel-unknown-linux-musl - bin: hust-network-login - - os_name: Linux-mips64 - os: ubuntu-20.04 - target: mips64-unknown-linux-muslabi64 - bin: hust-network-login - skip_tests: true - - os_name: Linux-mips64el - os: ubuntu-20.04 - target: mips64el-unknown-linux-muslabi64 - bin: hust-network-login - skip_tests: true - os_name: Linux-powerpc - os: ubuntu-20.04 + os: ubuntu-latest target: powerpc-unknown-linux-gnu bin: hust-network-login skip_tests: true - os_name: Linux-powerpc64 - os: ubuntu-20.04 + os: ubuntu-latest target: powerpc64-unknown-linux-gnu bin: hust-network-login skip_tests: true - os_name: Linux-powerpc64le - os: ubuntu-20.04 + os: ubuntu-latest target: powerpc64le-unknown-linux-gnu bin: hust-network-login skip_tests: true - os_name: Linux-riscv64 - os: ubuntu-20.04 + os: ubuntu-latest target: riscv64gc-unknown-linux-gnu bin: hust-network-login - os_name: Linux-s390x - os: ubuntu-20.04 + os: ubuntu-latest target: s390x-unknown-linux-gnu bin: hust-network-login skip_tests: true - - os_name: NetBSD-x86_64 - os: ubuntu-20.04 - target: x86_64-unknown-netbsd - bin: hust-network-login - skip_tests: true - os_name: Windows-aarch64 os: windows-latest target: aarch64-pc-windows-msvc @@ -117,7 +138,7 @@ jobs: # - beta # - nightly steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Cache cargo & target directories uses: Swatinem/rust-cache@v2 with: @@ -160,23 +181,37 @@ jobs: ( startsWith( github.ref, 'refs/tags/v' ) || github.ref == 'refs/tags/test-release' ) - name: Publish release artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: hust-network-login-${{ matrix.platform.os_name }} - path: "hust-network-login-*" - if: matrix.toolchain == 'stable' && github.ref == 'refs/tags/test-release' + path: "*.zip" + if: | + matrix.toolchain == 'stable' && + ( startsWith( github.ref, 'refs/tags/v' ) || + github.ref == 'refs/tags/test-release' ) - name: Generate SHA-256 - run: shasum -a 256 ${{ matrix.platform.name }} + run: shasum -a 256 ${{ matrix.platform.target }}.zip if: | matrix.toolchain == 'stable' && matrix.platform.os == 'macOS-latest' && ( startsWith( github.ref, 'refs/tags/v' ) || github.ref == 'refs/tags/test-release' ) + + publish_release: + needs: test + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + steps: + - name: Download all release artifacts + uses: actions/download-artifact@v4 + with: + pattern: hust-network-login-* + merge-multiple: true + path: dist + - name: List release files + run: ls -lh dist - name: Publish GitHub release uses: softprops/action-gh-release@v1 with: - # draft: true - files: "*.zip" - # body_path: Changes.md + files: "dist/*.zip" token: ${{ secrets.GITHUB_TOKEN }} - if: matrix.toolchain == 'stable' && startsWith( github.ref, 'refs/tags/v' ) diff --git a/src/main.rs b/src/main.rs index f707031..4d9a907 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod encrypt; use std::fs; +use std::process::Command; +use std::process::Stdio; use std::time::Duration; use std::{io, thread}; @@ -15,11 +17,29 @@ fn extract<'a>(text: &'a str, prefix: &'a str, suffix: &'a str) -> io::Result<&' } fn login(username: &str, password: &str) -> io::Result<()> { - let resp = minreq::get("http://www.baidu.com") + // If baidu is directly reachable, network is already available. + let mut ping_cmd = Command::new("ping"); + if cfg!(target_os = "windows") { + ping_cmd.args(["-n", "1", "-w", "1000", "www.baidu.com"]); + } else { + ping_cmd.args(["-c", "1", "-W", "1", "www.baidu.com"]); + } + + let ping_ok = ping_cmd + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|status| status.success()) + .unwrap_or(false); + if ping_ok { + return Ok(()); + } + + let resp = minreq::get("http://172.18.18.60:8080") .with_timeout(10) .send() .map_err(|e| { - println!("baidu boom! {}", e); + println!("probe endpoint boom! {}", e); io::ErrorKind::ConnectionRefused })?; let resp = resp.as_str().map_err(|e| {