一个自动下载最新版本frp打包成deb的bash脚本

frp作者不仅不提供任何发行版的包,甚至把systemd文件给删了1,我每次安装都费劲拖目录写配置,今天花了点时间在gpt帮助下写了个bash脚本,自动从github release下载最新版本frp并打包成deb,这下舒服了。注意替换github api token。需要的依赖:curl,jq 好像没了

#!/bin/bash

# GitHub repository information
repo_owner="fatedier"
repo_name="frp"
binary_name="frp"
os="linux"
arch="amd64"

# GitHub API endpoint for the latest release
github_api_url="https://api.github.com/repos/$repo_owner/$repo_name/releases/latest"

# File to store the current version locally
version_file="current_version.txt"

# Personal access token
github_token="<GITHUB_API_TOKEN 换成你自己的>"

function get_latest_release() {
    # Send a GET request to the GitHub API
    curl -s -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer $github_token" "$github_api_url" |
        jq -r .tag_name
}

function download_release() {
    local version=$1
    version="${version#v}"
    # GitHub release asset URL for the binary
    asset_url="https://mirror.ghproxy.com/github.com/$repo_owner/$repo_name/releases/download/v$version/${binary_name}_${version}_${os}_${arch}.tar.gz"

    # Check if the file already exists
    if [[ ! -e "${binary_name}_${version}_${os}_${arch}.tar.gz" ]]; then
        # Download the release binary
        wget "$asset_url"

        echo "Downloaded frp $version for ${os}_${arch}."

    else
        echo "File ${binary_name}_${version}_${os}_${arch}.tar.gz already exists."
    fi

    # Build Debian package
    build_deb_package "$version"
}

function build_deb_package() {
    local version=$1

    # Extract the tar.gz file
    tar -zxf "${binary_name}_${version}_${os}_${arch}.tar.gz"

    work_dir="${binary_name}_${version}_${os}_${arch}"
    # Create Debian directory structure
    debian_dir="${work_dir}/DEBIAN"

    binary_dir="${work_dir}/usr/bin"
    conf_dir="${work_dir}/etc/frp"
    systemd_dir="${work_dir}/etc/systemd/system/"
    doc_dir="${work_dir}/usr/share/doc/frp"

    mkdir -p "$debian_dir" "$binary_dir" "$systemd_dir" "$doc_dir"

    chown -R root:root *
    chmod 755 ${work_dir}/frpc
    chmod 755 ${work_dir}/frps
    chmod 644 ${work_dir}/frpc.toml
    chmod 644 ${work_dir}/frps.toml
    mv ${work_dir}/frpc ${binary_dir}/
    mv ${work_dir}/frps ${binary_dir}/
    mv ${work_dir}/frpc.toml ${conf_dir}/
    mv ${work_dir}/frps.toml ${conf_dir}/
    mv ${work_dir}/LICENSE ${doc_dir}/

    # Create control file
    cat >"$debian_dir/control" <<EOL
Package: $binary_name
Version: $version
Architecture: $arch
Maintainer: YaCHEN Wang <yachen4ever@yahoo.com>
Description: Frp package
Depends: libc6 (>= 2.3.6-6)
Homepage: https://github.com/fatedier/frp
Section: net
Priority: optional
Installed-Size: $(du -sk "${binary_name}_${version}_${os}_${arch}" | cut -f1)
EOL

    cat >"$systemd_dir/frpc.service" <<EOL
[Unit]
Description=Frp Client Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/frpc.ini

[Install]
WantedBy=multi-user.target
EOL

    cat >"$systemd_dir/frpc@.service" <<EOL
[Unit]
Description=Frp Client Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/%i.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/%i.ini

[Install]
WantedBy=multi-user.target
EOL

    cat >"$systemd_dir/frps.service" <<EOL
[Unit]
Description=Frp Server Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini

[Install]
WantedBy=multi-user.target
EOL

    cat >"$systemd_dir/frps@.service" <<EOL
[Unit]
Description=Frp Server Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/%i.ini

[Install]
WantedBy=multi-user.target
EOL

    # Build .deb package
    dpkg-deb --build "${work_dir}"
}

function main() {
    # Check if the version file exists
    if [[ -e $version_file ]]; then
        # Read the current version from the file
        current_version=$(cat "$version_file")

        # Get the latest release version from GitHub
        latest_version=$(get_latest_release)

        if [[ "$latest_version" && "$latest_version" != "$current_version" ]]; then
            echo "New version available: $latest_version"
            download_release "$latest_version"

            # Update the version file
            echo "$latest_version" >"$version_file"
        elif [[ "$latest_version" == "$current_version" ]]; then
            echo "Already up to date."
            download_release "$current_version"
        else
            echo "Error retrieving latest release information."
        fi
    else
        echo "Error: $version_file not found."
    fi

    # Clean up
    # rm -rf debian
    # rm -rf "${binary_name}_${version}_${os}_${arch}.tar.gz"
}

main

标签: 无

发表评论: