From 34b73d5919f2d0cd8f2ae51e559c66bf678f3808 Mon Sep 17 00:00:00 2001 From: Nathaniel Varona <194283+nathanielvarona@users.noreply.github.com> Date: Wed, 8 May 2024 21:54:35 +0800 Subject: [PATCH] Improve Code and Code Commenting --- .github/workflows/connection-tests-arm64.yml | 30 ++++++++-- .github/workflows/connection-tests-basic.yml | 33 +++++++--- .../workflows/connection-tests-complete.yml | 60 +++++++++++++++---- ...connection-tests-manual-readme-example.yml | 58 +++++++++++------- .../connection-tests-multi-server-profile.yml | 37 ++++++++---- README.md | 55 ++++------------- pritunl-client.sh | 4 +- 7 files changed, 172 insertions(+), 105 deletions(-) diff --git a/.github/workflows/connection-tests-arm64.yml b/.github/workflows/connection-tests-arm64.yml index 79509ee..0b57ac1 100644 --- a/.github/workflows/connection-tests-arm64.yml +++ b/.github/workflows/connection-tests-arm64.yml @@ -24,26 +24,40 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + # Checkout the code to run tests - name: Setup Pritunl Profile id: pritunl-connection - uses: ./ # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. + uses: ./ + # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. with: profile-file: ${{ secrets.PRITUNL_PROFILE_FILE_BASIC }} vpn-mode: ${{ matrix.vpn-mode }} client-version: ${{ matrix.client-version }} + + # Example of Our CI/CD Core Logic + # This step demonstrates our core CI/CD logic, which includes: + # 1. Installing IP Calculator + # 2. Pinging the VPN gateway + # This is a simple example of how to test VPN gateway connectivity + - name: Install IP Tooling (IP Calculator) shell: bash run: | # Install IP Calculator - + # Check the runner OS and install IP Calculator accordingly + # Linux: use apt-get + # macOS: use brew + # Windows: download from URL with retry logic (up to 3 attempts) if [ "$RUNNER_OS" == "Linux" ]; then + # Install IP Calculator on Linux sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc elif [ "$RUNNER_OS" == "macOS" ]; then + # Install IP Calculator on macOS brew install -q ipcalc elif [ "$RUNNER_OS" == "Windows" ]; then - # Retry up to 3 times in case of failure + # Install IP Calculator on Windows with retry logic for attempt in $(seq 3); do if curl -sSL "https://raw.githubusercontent.com/kjokjo/ipcalc/0.51/ipcalc" \ -o $HOME/bin/ipcalc && chmod +x $HOME/bin/ipcalc; then @@ -58,7 +72,7 @@ jobs: done fi - # Validate the IP Calculator Installation + # Verify IP Calculator installation echo "ipcalc version $(ipcalc --version)" - name: VPN Gateway Reachability Test @@ -66,11 +80,17 @@ jobs: run: | # VPN Gateway Reachability Test + # Set the ping count ping_count_number=5 + + # Get the profile IP profile_ip=$(pritunl-client list -j | jq ". | sort_by(.name)" | jq ".[0]" | jq -r ".client_address") + # Get the VPN gateway IP vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" + + # Set the ping flags based on the runner OS ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - # Ping VPN Gateway + # Ping the VPN gateway ping $vpn_gateway $ping_flags diff --git a/.github/workflows/connection-tests-basic.yml b/.github/workflows/connection-tests-basic.yml index 20fba1b..90b936f 100644 --- a/.github/workflows/connection-tests-basic.yml +++ b/.github/workflows/connection-tests-basic.yml @@ -20,25 +20,38 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v4 # Checkout the code to run tests - name: Setup Pritunl Profile id: pritunl-connection - uses: ./ # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. + uses: ./ + # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. with: profile-file: ${{ secrets.PRITUNL_PROFILE_FILE_BASIC }} + + # Example of Our CI/CD Core Logic + # This step demonstrates our core CI/CD logic, which includes: + # 1. Installing IP Calculator + # 2. Pinging the VPN gateway + # This is a simple example of how to test VPN gateway connectivity + - name: Install IP Tooling (IP Calculator) shell: bash run: | # Install IP Calculator - + # Check the runner OS and install IP Calculator accordingly + # Linux: use apt-get + # macOS: use brew + # Windows: download from URL with retry logic (up to 3 attempts) if [ "$RUNNER_OS" == "Linux" ]; then + # Install IP Calculator on Linux sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc elif [ "$RUNNER_OS" == "macOS" ]; then + # Install IP Calculator on macOS brew install -q ipcalc elif [ "$RUNNER_OS" == "Windows" ]; then - # Retry up to 3 times in case of failure + # Install IP Calculator on Windows with retry logic for attempt in $(seq 3); do if curl -sSL "https://raw.githubusercontent.com/kjokjo/ipcalc/0.51/ipcalc" \ -o $HOME/bin/ipcalc && chmod +x $HOME/bin/ipcalc; then @@ -53,19 +66,23 @@ jobs: done fi - # Validate the IP Calculator Installation + # Verify IP Calculator installation echo "ipcalc version $(ipcalc --version)" - name: VPN Gateway Reachability Test shell: bash run: | - # VPN Gateway Reachability Test - + # Set the ping count ping_count_number=5 + + # Get the profile IP profile_ip=$(pritunl-client list -j | jq ". | sort_by(.name)" | jq ".[0]" | jq -r ".client_address") + # Get the VPN gateway IP vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" + + # Set the ping flags based on the runner OS ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - # Ping VPN Gateway + # Ping the VPN gateway ping $vpn_gateway $ping_flags diff --git a/.github/workflows/connection-tests-complete.yml b/.github/workflows/connection-tests-complete.yml index 664baaf..8374184 100644 --- a/.github/workflows/connection-tests-complete.yml +++ b/.github/workflows/connection-tests-complete.yml @@ -10,7 +10,7 @@ jobs: matrix: os: - ubuntu-22.04 - - macos-13 # See the '.github/workflows/connection-tests-complete.yml' file for macOS Arm64 connection test examples. + - macos-13 # macOS Arm64 connection test examples in '.github/workflows/connection-tests-arm64.yml' - windows-2022 profile-server: - dev-team @@ -37,11 +37,12 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v4 # Checkout the code to run tests - name: Setup Pritunl Profile id: pritunl-connection - uses: ./ # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. + uses: ./ + # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. with: profile-file: ${{ secrets.PRITUNL_PROFILE_FILE }} profile-pin: ${{ secrets.PRITUNL_PROFILE_PIN }} @@ -53,27 +54,43 @@ jobs: established-connection-timeout: ${{ matrix.established-connection-timeout }} concealed-outputs: ${{ matrix.concealed-outputs }} + + # Example of Our CI/CD Core Logic + # This step demonstrates our core CI/CD logic, which includes: + # 1. Starting a VPN connection manually (if matrix.start-connection == false) + # 2. Showing the VPN connection status (if matrix.start-connection == false) + # 3. Installing IP Calculator + # 4. Pinging the VPN gateway + # 5. Stopping the VPN connection manually (if matrix.start-connection == false) + # This is a comprehensive example of how to test VPN gateway connectivity, with conditional steps for starting and stopping the VPN connection + - name: Starting a VPN Connection Manually if: matrix.start-connection == false shell: bash run: | - # Start the VPN Connection Manually + # Start the VPN connection manually + # Get the client IDs from the previous step profile_server_ids_json='${{ steps.pritunl-connection.outputs.client-ids }}' + # Loop through each client ID while read -r line; do profile_server_ids_array+=("$line") done < <(echo "$profile_server_ids_json" | jq -c '.[]') + # Start the VPN connection for each profile server for profile_server_item in "${profile_server_ids_array[@]}"; do echo "Starting connection for '$(echo "$profile_server_item" | jq -r ".name")' profile server." + # Get the ID of the profile server profile_id="$(echo "$profile_server_item" | jq -r ".id")" + # Start the VPN connection using the pritunl-client command pritunl-client start $profile_id \ --password ${{ secrets.PRITUNL_PROFILE_PIN || '' }} \ --mode ${{ matrix.vpn-mode }} + # Wait for 2 seconds sleep 2 done @@ -81,28 +98,34 @@ jobs: if: matrix.start-connection == false shell: bash run: | - # Show VPN Connection Status Manually + # Show VPN connection status manually + # Wait for 10 seconds sleep 10 profile_server_ids_json='${{ steps.pritunl-connection.outputs.client-ids }}' profile_server_ids_array=() + # Loop through each client ID while read -r line; do profile_server_ids_array+=("$line") done < <(echo "$profile_server_ids_json" | jq -c '.[]') + # Show the VPN connection status for each profile server for profile_server_item in "${profile_server_ids_array[@]}"; do echo "Establish connection for '$(echo "$profile_server_item" | jq -r ".name")' profile server." + # Get the ID of the profile server profile_id="$(echo "$profile_server_item" | jq -r ".id")" profile_name="$(echo "$profile_server_item" | jq -r ".name")" + # Get the VPN connection status profile_server=$(pritunl-client list -j) profile_ip="$(echo "$profile_server" | jq --arg profile_id "$profile_id" '.[] | select(.id == $profile_id)' | jq -r '.client_address')" + # Print the VPN connection status echo "Connected as '$profile_name' with a private client address of '$profile_ip'." - # Print new line + # Print a new line echo -n -e "\n" done @@ -111,6 +134,7 @@ jobs: run: | # Install IP Calculator + # Install IP Calculator based on the runner OS if [ "$RUNNER_OS" == "Linux" ]; then sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc elif [ "$RUNNER_OS" == "macOS" ]; then @@ -131,7 +155,7 @@ jobs: done fi - # Validate the IP Calculator Installation + # Validate the IP Calculator installation echo "ipcalc version $(ipcalc --version)" - name: VPN Gateway Reachability Test @@ -139,50 +163,62 @@ jobs: run: | # VPN Gateway Reachability Test + # Set the ping count ping_count_number=5 + + # Get the client IDs from the previous step profile_server_ids_json='${{ steps.pritunl-connection.outputs.client-ids }}' profile_server_ids_array=() + # Loop through each client ID while read -r line; do profile_server_ids_array+=("$line") done < <(echo "$profile_server_ids_json" | jq -c '.[]') + # Ping the VPN gateway for each profile server for profile_server_item in "${profile_server_ids_array[@]}"; do echo "Pinging '$(echo "$profile_server_item" | jq -r ".name")' Gateway." + # Get the ID of the profile server profile_id="$(echo "$profile_server_item" | jq -r ".id")" profile_ip="$(pritunl-client list -j | jq --arg profile_id "$profile_id" '.[] | select(.id == $profile_id)' | jq -r '.client_address')" + # Get the VPN gateway IP vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - # Ping VPN Gateway + # Ping the VPN gateway ping $vpn_gateway $ping_flags - # Print new line + # Print a new line echo -n -e "\n" done - - name: Stop VPN Connection Manually if: matrix.start-connection == false shell: bash run: | - # Stop Connection Manually + # Stop VPN Connection Manually + # Get the client IDs from the previous step profile_server_ids_json='${{ steps.pritunl-connection.outputs.client-ids }}' profile_server_ids_array=() + # Loop through each client ID while read -r line; do - profile_server_ids_array+=("$line") + profile_server_ids_array+=("$line") done < <(echo "$profile_server_ids_json" | jq -c '.[]') + # Stop the VPN connection for each profile server for profile_server_item in "${profile_server_ids_array[@]}"; do echo "Stopping connection for '$(echo "$profile_server_item" | jq -r ".name")' profile server." + # Get the ID of the profile server profile_id="$(echo "$profile_server_item" | jq -r ".id")" + # Stop the VPN connection using the pritunl-client command pritunl-client stop $profile_id + # Wait for 2 seconds sleep 2 done diff --git a/.github/workflows/connection-tests-manual-readme-example.yml b/.github/workflows/connection-tests-manual-readme-example.yml index 72dabf4..7754f92 100644 --- a/.github/workflows/connection-tests-manual-readme-example.yml +++ b/.github/workflows/connection-tests-manual-readme-example.yml @@ -17,41 +17,50 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v4 # Checkout the code to run tests - name: Setup Pritunl Profile - id: pritunl-connection # A `Setup Step ID` has been added as a reference identifier for the output `client-id`. - uses: ./ # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. + id: pritunl-connection + uses: ./ + # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. with: profile-file: ${{ secrets.PRITUNL_PROFILE_FILE }} - start-connection: false # Do not establish a connection in this step. + start-connection: false + + + # Example of Our CI/CD Core Logic + # This step demonstrates our core CI/CD logic, which includes: + # 1. Installing IP Calculator + # 2. Starting a VPN connection manually + # 3. Showing the VPN connection status + # 4. Pinging the VPN gateway + # 5. Stopping the VPN connection manually + # This is a simple example of how to test VPN gateway connectivity - name: Starting a VPN Connection Manually shell: bash run: | - # Starting a VPN Connection Manually - + # Start VPN connection manually + # Start the VPN connection using the client ID and password pritunl-client start ${{ steps.pritunl-connection.outputs.client-id }} \ --password ${{ secrets.PRITUNL_PROFILE_PIN || '' }} + # Sleep for a while to simulate establish connection test + # Wait for 10 seconds to allow the connection to establish + sleep 10 + - name: Show VPN Connection Status Manually shell: bash run: | - # Show VPN Connection Status Manually - - sleep 10 + # Show VPN connection status + # List the VPN connections and show the profile name and client address pritunl-client list -j | jq 'sort_by(.name) | .[0] | { "Profile Name": .name, "Client Address": .client_address }' - - name: Your CI/CD Core Logic + - name: Install IP Calculator shell: bash run: | - # Your CI/CD Core Logic - - ## - # Below is our simple example for VPN connectivity test. - ## - # Install IP Calculator + # Install IP Calculator based on the runner OS if [ "$RUNNER_OS" == "Linux" ]; then sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc elif [ "$RUNNER_OS" == "macOS" ]; then @@ -71,24 +80,29 @@ jobs: fi done fi - # Validate the IP Calculator Installation echo "ipcalc version $(ipcalc --version)" - # VPN Gateway Reachability Test + - name: Ping VPN Gateway + shell: bash + run: | + # Set the ping count ping_count_number=5 - profile_ip=$(pritunl-client list -j | jq -r 'sort_by(.name) | .[0].client_address') + # Get the Profile ID Details + profile_ip=$(pritunl-client list -j | jq -r 'sort_by(.name) | .[0].client_address') vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" + + # Construct the Ping Flags ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - # Ping VPN Gateway + # Ping the VPN gateway ping $vpn_gateway $ping_flags - name: Stop VPN Connection Manually if: ${{ always() }} shell: bash run: | - # Stop VPN Connection Manually - + # Stop VPN connection manually + # Stop the VPN connection using the client ID pritunl-client stop ${{ steps.pritunl-connection.outputs.client-id }} diff --git a/.github/workflows/connection-tests-multi-server-profile.yml b/.github/workflows/connection-tests-multi-server-profile.yml index 41ab1cd..654476a 100644 --- a/.github/workflows/connection-tests-multi-server-profile.yml +++ b/.github/workflows/connection-tests-multi-server-profile.yml @@ -21,20 +21,29 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v4 # Checkout the code to run tests - name: Setup Pritunl Profile id: pritunl-connection - uses: ./ # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. + uses: ./ + # Use `nathanielvarona/pritunl-client-github-action@v1` for your GitHub Action workflow. with: profile-file: ${{ secrets.PRITUNL_PROFILE_FILE_MULTI_SERVER }} profile-server: ${{ matrix.profile-server }} - - name: Install IP Tooling (IP Calculator) + + # Example of Our CI/CD Core Logic + # This step demonstrates our core CI/CD logic, which includes: + # 1. Installing IP Calculator + # 2. Pinging the VPN Gateway for reachability + # 3. Testing VPN connectivity with multiple profile servers + # This is a simple example of how to test VPN gateway connectivity + + - name: Install IP Calculator shell: bash run: | # Install IP Calculator - + # Install IP Calculator based on the runner OS if [ "$RUNNER_OS" == "Linux" ]; then sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc elif [ "$RUNNER_OS" == "macOS" ]; then @@ -54,35 +63,41 @@ jobs: fi done fi - # Validate the IP Calculator Installation echo "ipcalc version $(ipcalc --version)" - - name: VPN Gateway Reachability Test + - name: VPN Gateway Reachability Test shell: bash run: | - # VPN Gateway Reachability Test - + # Set the ping count ping_count_number=5 + + # Get the Client IDs profile_server_ids_json='${{ steps.pritunl-connection.outputs.client-ids }}' profile_server_ids_array=() + # Parse the JSON output from the Pritunl connection step while read -r line; do - profile_server_ids_array+=("$line") + profile_server_ids_array+=("$line") done < <(echo "$profile_server_ids_json" | jq -c '.[]') + # Iterate over the profile server IDs for profile_server_item in "${profile_server_ids_array[@]}"; do echo "Pinging '$(echo "$profile_server_item" | jq -r ".name")' Gateway." + # Extract the profile ID and IP from the JSON object profile_id="$(echo "$profile_server_item" | jq -r ".id")" profile_ip="$(pritunl-client list -j | jq --arg profile_id "$profile_id" '.[] | select(.id == $profile_id)' | jq -r '.client_address')" + # Calculate the VPN gateway IP using ipcalc vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" + + # Construct the Ping Flags ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - # Ping VPN Gateway + # Ping the VPN gateway ping $vpn_gateway $ping_flags - # Print new line + # Print a new line echo -n -e "\n" done diff --git a/README.md b/README.md index 6559960..5765b5e 100644 --- a/README.md +++ b/README.md @@ -229,61 +229,26 @@ Demonstrates manual control over the VPN connection, including starting, stoppin - name: Starting a VPN Connection Manually shell: bash run: | - # Starting a VPN Connection Manually - + # Start VPN connection manually + # Start the VPN connection using the client ID and password pritunl-client start ${{ steps.pritunl-connection.outputs.client-id }} \ --password ${{ secrets.PRITUNL_PROFILE_PIN || '' }} + # Sleep for a while to simulate establish connection test + # Wait for 10 seconds to allow the connection to establish + sleep 10 + - name: Show VPN Connection Status Manually shell: bash run: | - # Show VPN Connection Status Manually - - sleep 10 + # Show VPN connection status + # List the VPN connections and show the profile name and client address pritunl-client list -j | jq 'sort_by(.name) | .[0] | { "Profile Name": .name, "Client Address": .client_address }' -- name: Your CI/CD Core Logic +- name: Then Our CI/CD Core Logic shell: bash run: | - # Your CI/CD Core Logic - - ## - # Below is our simple example for VPN connectivity test. - ## - - # Install IP Calculator - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get install -qq -o=Dpkg::Use-Pty=0 -y ipcalc - elif [ "$RUNNER_OS" == "macOS" ]; then - brew install -q ipcalc - elif [ "$RUNNER_OS" == "Windows" ]; then - # Retry up to 3 times in case of failure - for attempt in $(seq 3); do - if curl -sSL "https://raw.githubusercontent.com/kjokjo/ipcalc/0.51/ipcalc" \ - -o $HOME/bin/ipcalc && chmod +x $HOME/bin/ipcalc; then - break - else - echo "Attempt $attempt failed. Retrying..." && sleep 1 - # If all retries fail, exit with an error - if [ $attempt -eq 3 ]; then - echo "Failed to install ipcalc after 3 attempts." && exit 1 - fi - fi - done - fi - - # Validate the IP Calculator Installation - echo "ipcalc version $(ipcalc --version)" - - # VPN Gateway Reachability Test - ping_count_number=5 - profile_ip=$(pritunl-client list -j | jq -r 'sort_by(.name) | .[0].client_address') - - vpn_gateway="$(ipcalc $profile_ip | awk 'NR==6{print $2}')" - ping_flags="$([[ "$RUNNER_OS" == "Windows" ]] && echo "-n $ping_count_number" || echo "-c $ping_count_number")" - - # Ping VPN Gateway - ping $vpn_gateway $ping_flags + # Then Our CI/CD Core Logic - name: Stop VPN Connection Manually if: ${{ always() }} diff --git a/pritunl-client.sh b/pritunl-client.sh index 9e8dd4b..6e20e32 100755 --- a/pritunl-client.sh +++ b/pritunl-client.sh @@ -1,13 +1,13 @@ #!/usr/bin/env bash # GitHub Project Information -# ============================ +# ========================== # Name: Pritunl Client GitHub Action # Description: Establish automated secure Pritunl VPN connections with Pritunl Client in GitHub Actions, supporting OpenVPN and WireGuard. # Repository: https://github.com/nathanielvarona/pritunl-client-github-action # Pritunl Client Resources -# ======================= +# ======================== # Installation and Setup Guides: # - https://client.pritunl.com/#install # - https://docs.pritunl.com/docs/installation-client