Skip to content

Commit 210423e

Browse files
authored
Merge pull request #7 from MDHaarith/feature/signed-appimage-updates
Package Linux builds as signed self-updating AppImages
2 parents c29344f + a156f6f commit 210423e

12 files changed

Lines changed: 417 additions & 14 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ jobs:
3636
# The drop zone's pure logic and its offscreen renders (snapshots are uploaded).
3737
- run: "cargo test --locked --features edge edge::"
3838
- run: cargo build --release --locked --features webview,edge
39+
env:
40+
# Lets the in-app updater tell which main build this is.
41+
AIRBRIDGE_BUILD: ${{ github.run_number }}
42+
- run: packaging/appimage.sh target/release/airbridge
3943
- uses: actions/upload-artifact@v6
4044
with:
4145
name: edge-snapshots
4246
path: linux/target/edge-snapshots
4347
- uses: actions/upload-artifact@v6
4448
with:
4549
name: airbridge-linux-x86_64
46-
path: linux/target/release/airbridge
50+
path: linux/airbridge-x86_64.AppImage
4751

4852
android:
4953
name: Android app + interop with Linux
@@ -104,14 +108,24 @@ jobs:
104108
with:
105109
name: airbridge-android-apk
106110
path: release
107-
- name: Package release assets
111+
- name: Package and sign release assets
112+
env:
113+
UPDATE_SIGNING_KEY: ${{ secrets.UPDATE_SIGNING_KEY }}
114+
TAG: main-${{ github.run_number }}
108115
run: |
109-
test -s release/airbridge
116+
test -s release/airbridge-x86_64.AppImage
110117
test -s release/app-release.apk
118+
[ -n "$UPDATE_SIGNING_KEY" ] || { echo "UPDATE_SIGNING_KEY secret is missing; refusing to publish an unsigned build"; exit 1; }
111119
# Artifact downloads do not preserve the Linux executable bit.
112-
chmod +x release/airbridge
113-
tar -C release -czf airbridge-linux-x86_64.tar.gz airbridge
120+
install -m755 release/airbridge-x86_64.AppImage airbridge-x86_64.AppImage
114121
cp release/app-release.apk airbridge-android.apk
122+
# The in-app updater checks this: the AppImage, a newline, then its tag.
123+
umask 077
124+
printf '%s\n' "$UPDATE_SIGNING_KEY" > "$RUNNER_TEMP/key.pem"
125+
{ cat airbridge-x86_64.AppImage; printf '\n%s' "$TAG"; } > "$RUNNER_TEMP/message"
126+
openssl pkeyutl -sign -rawin -inkey "$RUNNER_TEMP/key.pem" -in "$RUNNER_TEMP/message" -out airbridge-x86_64.AppImage.sig
127+
rm -f "$RUNNER_TEMP/key.pem" "$RUNNER_TEMP/message"
128+
test "$(stat -c %s airbridge-x86_64.AppImage.sig)" -eq 64
115129
- uses: softprops/action-gh-release@v3
116130
with:
117131
tag_name: main-${{ github.run_number }}
@@ -126,9 +140,10 @@ jobs:
126140
127141
**Downloads**
128142
- `airbridge-android.apk`: Android 10 or newer.
129-
- `airbridge-linux-x86_64.tar.gz`: unpack and run `./airbridge`.
143+
- `airbridge-x86_64.AppImage`: make it executable and run it. It updates itself from the app's Update button.
130144
131145
The APK uses a debug signing key; Android may require uninstalling the previous build before installation.
132146
files: |
133-
airbridge-linux-x86_64.tar.gz
147+
airbridge-x86_64.AppImage
148+
airbridge-x86_64.AppImage.sig
134149
airbridge-android.apk

‎.github/workflows/release.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ jobs:
6262
working-directory: linux
6363
run: |
6464
cargo build --release --locked --features webview,edge
65-
tar -C target/release -czf ../airbridge-linux-x86_64.tar.gz airbridge
65+
packaging/appimage.sh target/release/airbridge ..
6666
- uses: actions/upload-artifact@v6
6767
with:
6868
name: release-linux-x86_64
69-
path: airbridge-linux-x86_64.tar.gz
69+
path: airbridge-x86_64.AppImage
7070
if-no-files-found: error
7171

7272
android:
@@ -106,7 +106,8 @@ jobs:
106106
merge-multiple: true
107107
- name: Check release assets
108108
run: |
109-
test -s airbridge-linux-x86_64.tar.gz
109+
test -s airbridge-x86_64.AppImage
110+
chmod +x airbridge-x86_64.AppImage
110111
test -s airbridge-android.apk
111112
- uses: softprops/action-gh-release@v3
112113
with:
@@ -116,10 +117,10 @@ jobs:
116117
body: |
117118
**Downloads**
118119
- `airbridge-android.apk`: the Android app (Android 10 or newer). Allow installs from your browser or file manager when asked.
119-
- `airbridge-linux-x86_64.tar.gz`: the Linux app. Unpack it and run `./airbridge`, or install it with `packaging/install.sh` from the source.
120+
- `airbridge-x86_64.AppImage`: the Linux app. Make it executable and run it (needs GTK 3 and WebKitGTK 4.1, present on most desktops).
120121
121122
The APK is signed with a debug key made for this build, not a private release key, so a newer release may need the old app uninstalled first. See [SECURITY.md](https://github.com/${{ github.repository }}/blob/${{ env.VERSION }}/docs/SECURITY.md).
122123
files: |
123-
airbridge-linux-x86_64.tar.gz
124+
airbridge-x86_64.AppImage
124125
airbridge-android.apk
125126
generate_release_notes: true

‎README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ Received photos show up as a preview in the notification.
127127

128128
### Linux
129129

130+
Download `airbridge-x86_64.AppImage` from the newest
131+
[release](https://github.com/MDHaarith/Linux-Android-AirDrop/releases), then:
132+
133+
```bash
134+
chmod +x airbridge-x86_64.AppImage && ./airbridge-x86_64.AppImage
135+
```
136+
137+
It uses the system's GTK 3, WebKitGTK 4.1 and gtk-layer-shell, which most desktops already have.
138+
When a newer build of `main` passes CI, an **Update** button appears in the toolbar. It
139+
downloads the new AppImage, checks its Ed25519 signature against the key built into the app,
140+
swaps the file and restarts. Unsigned or older builds are refused.
141+
142+
#### From source
143+
130144
You need [Rust](https://rustup.rs) 1.89 or newer and the Wayland client headers
131145
(`libwayland-dev` / `wayland-devel`, used by the file picker). For a native app window, also
132146
install WebKitGTK. Without it, AirBridge opens in a Chromium app window or your default browser.

‎docs/SECURITY.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ to your own verified devices. Devices at the phone's own address are refused
5959
transfers, received text is marked sensitive on the clipboard, and malformed
6060
requests cannot crash the app.
6161

62+
## Updates
63+
64+
The Linux AppImage updates itself only from `main-N` prereleases on GitHub, over
65+
HTTPS (system CA store, HTTPS-only redirects). CI signs each AppImage with an
66+
Ed25519 key kept in the `UPDATE_SIGNING_KEY` secret. The signature covers the
67+
file and its tag, and the public key is built into the app. A download with a
68+
bad signature, an older or equal build number, or one that is not an ELF file
69+
is rejected before anything is written. The swap is atomic (temp file, fsync,
70+
rename). Only the token-protected local UI can start an update.
71+
6272
## Known limits
6373

6474
- The official LocalSend apps do not present client certificates. They work,

‎linux/packaging/appimage.sh‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
# Packs a release binary into airbridge-x86_64.AppImage.
3+
# ./appimage.sh path/to/airbridge [output-dir]
4+
# GTK, WebKitGTK 4.1 and gtk-layer-shell come from the system, as on any desktop.
5+
set -euo pipefail
6+
7+
bin="$(realpath "$1")"
8+
out="$(realpath "${2:-.}")"
9+
here="$(cd "$(dirname "$0")" && pwd)"
10+
work="$(mktemp -d)"
11+
trap 'rm -rf "$work"' EXIT
12+
13+
# Pinned tools, checked before use.
14+
fetch() { # url sha256 dest
15+
curl -fsSL --proto '=https' --retry 3 -o "$3" "$1"
16+
echo "$2 $3" | sha256sum -c --quiet
17+
}
18+
fetch https://github.com/AppImage/appimagetool/releases/download/1.9.1/appimagetool-x86_64.AppImage \
19+
ed4ce84f0d9caff66f50bcca6ff6f35aae54ce8135408b3fa33abfc3cb384eb0 "$work/appimagetool"
20+
fetch https://github.com/AppImage/type2-runtime/releases/download/20251108/runtime-x86_64 \
21+
2fca8b443c92510f1483a883f60061ad09b46b978b2631c807cd873a47ec260d "$work/runtime"
22+
chmod +x "$work/appimagetool"
23+
24+
app="$work/AirBridge.AppDir"
25+
install -Dm755 "$bin" "$app/usr/bin/airbridge"
26+
install -Dm644 "$here/../ui/icon.svg" "$app/airbridge.svg"
27+
install -Dm644 "$here/../ui/icon.svg" "$app/usr/share/icons/hicolor/scalable/apps/airbridge.svg"
28+
install -Dm644 "$here/airbridge.desktop" "$app/airbridge.desktop"
29+
cat > "$app/AppRun" <<'EOF'
30+
#!/bin/sh
31+
exec "$(dirname "$(readlink -f "$0")")/usr/bin/airbridge" "$@"
32+
EOF
33+
chmod +x "$app/AppRun"
34+
35+
# No FUSE on CI runners.
36+
APPIMAGE_EXTRACT_AND_RUN=1 ARCH=x86_64 "$work/appimagetool" --no-appstream \
37+
--runtime-file "$work/runtime" "$app" "$out/airbridge-x86_64.AppImage"

‎linux/src/fetch.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,43 @@ async fn stage(name: &str, data: &[u8]) -> Result<SendItem> {
210210
})
211211
}
212212

213+
/// Downloads `url` into memory over HTTPS only (redirects included), up to `max` bytes.
214+
pub async fn download(url: &str, max: u64) -> Result<Vec<u8>> {
215+
let mut uri: Uri = url.parse().context("not a valid link")?;
216+
for _ in 0..=MAX_REDIRECTS {
217+
if uri.scheme_str() != Some("https") {
218+
bail!("refusing a non-HTTPS download");
219+
}
220+
let response = get(&uri).await?;
221+
let status = response.status();
222+
if status.is_redirection() {
223+
let location = response
224+
.headers()
225+
.get(hyper::header::LOCATION)
226+
.and_then(|v| v.to_str().ok())
227+
.ok_or_else(|| anyhow!("redirect without a location"))?;
228+
uri = resolve(&uri, location)?;
229+
continue;
230+
}
231+
if status != StatusCode::OK {
232+
bail!("the server answered {status}");
233+
}
234+
let mut stream = response.into_body().into_data_stream();
235+
let mut data = Vec::new();
236+
while let Some(chunk) = timeout(IO_TIMEOUT, stream.next())
237+
.await
238+
.map_err(|_| anyhow!("the download stalled"))?
239+
{
240+
data.extend_from_slice(&chunk?);
241+
if data.len() as u64 > max {
242+
bail!("the download is too large");
243+
}
244+
}
245+
return Ok(data);
246+
}
247+
bail!("too many redirects")
248+
}
249+
213250
async fn stage_stream(name: &str, body: hyper::body::Incoming) -> Result<SendItem> {
214251
let path = staging_path(name);
215252
tokio::fs::create_dir_all(path.parent().unwrap()).await?;

‎linux/src/main.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod transfers;
2727
#[cfg(feature = "tray")]
2828
mod tray;
2929
mod ui;
30+
mod update;
3031
#[cfg(feature = "webview")]
3132
mod window;
3233

@@ -157,13 +158,27 @@ fn main() -> ExitCode {
157158
};
158159
}
159160
let runtime = tokio::runtime::Runtime::new().expect("tokio runtime");
160-
match runtime.block_on(run(cli)) {
161+
let code = match runtime.block_on(run(cli)) {
161162
Ok(code) => code,
162163
Err(err) => {
163164
eprintln!("airbridge: {err:#}");
164165
ExitCode::FAILURE
165166
}
167+
};
168+
if update::RESTART.load(std::sync::atomic::Ordering::Relaxed) {
169+
// Close every socket first so the new version can take the ports.
170+
runtime.shutdown_timeout(Duration::from_secs(3));
171+
if let Some(appimage) = update::appimage() {
172+
use std::os::unix::process::CommandExt;
173+
if let Err(err) = std::process::Command::new(appimage)
174+
.process_group(0)
175+
.spawn()
176+
{
177+
eprintln!("airbridge: could not restart after the update: {err}");
178+
}
179+
}
166180
}
181+
code
167182
}
168183

169184
async fn run(cli: Cli) -> Result<ExitCode> {

‎linux/src/ui.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::config;
2525
use crate::desktop;
2626
use crate::receiver::constant_time_eq;
2727
use crate::sender::{expand_paths, SendItem};
28+
use crate::update;
2829

2930
const COOKIE: &str = "airbridge_token";
3031

@@ -64,6 +65,7 @@ pub fn router(app: Arc<App>) -> Router {
6465
.route("/open-downloads", post(open_downloads))
6566
.route("/show", post(show))
6667
.route("/quit", post(quit))
68+
.route("/update", get(check_update).post(apply_update))
6769
.layer(middleware::from_fn_with_state(app.clone(), auth));
6870
Router::new()
6971
.route("/", get(index))
@@ -665,3 +667,40 @@ async fn quit(State(app): State<Arc<App>>) -> StatusCode {
665667
app.shutdown.cancel();
666668
StatusCode::NO_CONTENT
667669
}
670+
671+
async fn check_update() -> Response {
672+
match update::check().await {
673+
Ok(available) => Json(json!({
674+
"current": update::current_build(),
675+
"available": available,
676+
"canInstall": update::appimage().is_some(),
677+
}))
678+
.into_response(),
679+
Err(err) => bad_request(format!("Could not check for updates: {err:#}")),
680+
}
681+
}
682+
683+
/// Installs the newest build and restarts; outside the AppImage it opens the release page.
684+
async fn apply_update(State(app): State<Arc<App>>) -> Response {
685+
let available = match update::check().await {
686+
Ok(Some(available)) => available,
687+
Ok(None) => return bad_request("AirBridge is up to date."),
688+
Err(err) => return bad_request(format!("Could not check for updates: {err:#}")),
689+
};
690+
if update::appimage().is_none() {
691+
desktop::open_url(&available.page);
692+
return Json(json!({ "restarting": false })).into_response();
693+
}
694+
if let Err(err) = update::install(&available).await {
695+
tracing::warn!("Update failed: {err:#}");
696+
return bad_request(format!("Update failed: {err:#}"));
697+
}
698+
tracing::info!("Updated to {}; restarting", available.tag);
699+
update::RESTART.store(true, Ordering::Relaxed);
700+
// Let this answer reach the window before the server goes away.
701+
tokio::spawn(async move {
702+
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
703+
app.shutdown.cancel();
704+
});
705+
Json(json!({ "restarting": true })).into_response()
706+
}

0 commit comments

Comments
 (0)