v0.5.0: bubble for Android 0.5.0 — text from the Rabbit's SIM on the desktop, and chat names and read status sync, through jai

A relay on jai (server/bubble_relay.py, stdlib + SQLite, tailscale address
only, bearer token) carries sealed blobs between the desktop and the Rabbit:
AES-256-GCM with a key the desktop makes and hands over USB (Sync to Rabbit),
so jai can neither read nor forge anything. It deletes what's been picked up,
and anything after 14 days.

- Desktop (src/main/relay.ts): "Rabbit SIM" is a number to text from, groups
  included; the SIM's history and new texts arrive; older photos are fetched
  when shown; names and read/unread/archived go both ways. Settings → Rabbit
  relay connects it.
- Rabbit (Relay.java, RelayJob): no timer of its own for the battery; it rides
  along with the background checks, plus one network job after a text, a send
  or a swipe. Each send from the desktop goes out at most once and not after a
  day. Plain http only to jai and localhost (network_security_config).
- scripts/deploy-jai.sh; tests: server unit tests, an end-to-end relay test,
  and DEVICE=1 relay.device.test.ts against the dev app.
This commit is contained in:
2026-11-06 17:35:00 -08:00
parent e1bceb06a3
commit 96c368dfd1
39 changed files with 1638 additions and 53 deletions
+9 -1
View File
@@ -13,7 +13,8 @@ A Signal Desktop–style SMS client for [voip.ms](https://voip.ms). It keeps you
- **MMS**: photos, video and audio appear inline in the chat. Click a photo to view it full size or save it. You can attach up to 3 files with the paperclip, by drag-and-drop, or by pasting. Photos too big for voip.ms (the budget is 900 KB each) are shrunk automatically before sending.
- **Emoji shortcodes**: type `:` and at least two letters, for example `:jo`, to get matching emoji. Pick one with ↑/↓ and Tab/Enter or by clicking it; Esc closes the list. A finished `:joy:` converts on its own. The shortcodes are Signal's and Slack's, from [emoji-datasource](https://github.com/iamcal/emoji-data); `npm run gen:emoji` regenerates them.
- Alt+↑/↓ moves between conversations.
- Ctrl +/− zooms and Ctrl 0 resets. Choose Light, Dark, or System under Settings → Appearance.
- Ctrl +/− zooms and Ctrl 0 resets. Choose Light, Dark, or System under Settings → Appearance, and the dark or inverted (lighter) icon.
- **The Rabbit's SIM, from here** (Settings → Rabbit relay): with bubble as the Rabbit R1's texting app, its SIM is a third number to text from ("Rabbit SIM"), its texts show up here, and chat names and read/unread/archived stay in step with the Rabbit, no cable needed. It goes through a small relay on jai (`server/bubble_relay.py`, deployed with `scripts/deploy-jai.sh`), which only ever holds AES-256-GCM-sealed messages: the key lives on this computer and the Rabbit, never on jai. A text sent from here goes out at the Rabbit's next background check, so it can take a while; older photos come over when you open them.
## Where your data lives
@@ -25,6 +26,7 @@ A Signal Desktop–style SMS client for [voip.ms](https://voip.ms). It keeps you
| MMS photos and attachments (cached on arrival) | `~/Documents/voipms-desktop-data/media/` | ✅ |
| API password | GNOME Keyring (via Electron `safeStorage`) → `~/.config/voipms-desktop/credentials.bin` | ❌ on purpose; you re-enter it after a restore |
| Contact photo cache | `~/.config/voipms-desktop/photo-cache/` | ❌ not needed |
| Rabbit relay address, token and key | GNOME Keyring → `~/.config/voip.ms Messages/relay.bin` | ❌ on purpose; reconnect and Sync to Rabbit after a restore |
The snapshots are consistent SQLite copies, made daily and whenever you quit. So even if a backup catches the live database halfway through a write, the backup still holds a clean, restorable copy. To move the data folder, go to Settings → Storage → Move…. If you point it at a folder that already holds a `messages.db`, for example one restored from backup, the app uses that history.
@@ -38,6 +40,12 @@ In the voip.ms portal, go to **Main Menu → SOAP & REST/JSON API** and:
Then launch the app and enter your account email and API password.
### The Rabbit relay (optional)
1. `scripts/deploy-jai.sh` puts the relay on jai (its own folder, port 8080 on the tailscale address only); `scripts/deploy-jai.sh --token` prints its token.
2. Settings → Rabbit relay: paste the token, Connect.
3. Plug in the Rabbit and press Sync to Rabbit once: that hands it the address, token and the sealing key.
### Apple-style emoji (optional)
To look like Signal, the app uses an `Apple Color Emoji` font when one is installed, and falls back to Noto Color Emoji otherwise. To install it for your user only (about 116 MB):
+11
View File
@@ -87,5 +87,16 @@ Install/Sync buttons then target the dev app with the sandbox's made-up data.
side, through `MmsFiles`. Long texts go as multipart SMS; photos, voice notes
and group chats as MMS (needs mobile data on). The dev app's sandbox never
uses the SIM.
- Relay (`Relay`, `RelayJob`): the desktop's link through jai (its
`src/main/relay.ts`, server `server/bubble_relay.py`). config.json's `relay`
(address, token, AES-256-GCM key) comes from Sync to Rabbit. The SIM's
messages go to the desktop, sends typed there go out here (each one at most
once, and not after a day), chat names and read status go both ways, and
older photos are sent when the desktop asks. For the battery it has no timer
of its own: it rides along with `PollJob`'s checks, plus one network job a few
seconds after something happens here (a text in, a send done, a swipe or
rename), and a catch-up when the app is opened (at most once a minute). Plain
http is allowed only to jai's tailscale address and localhost
(`res/xml/network_security_config.xml`).
- The scroll wheel is left alone: it stays the volume control (and volume
down + the side button still takes a screenshot).
+4 -1
View File
@@ -29,7 +29,7 @@
android:icon="@drawable/ic_launcher"
android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
android:allowBackup="false"
android:usesCleartextTraffic="false">
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name="me.teafry.voipmessages.MainActivity"
@@ -118,6 +118,9 @@
<service android:name="me.teafry.voipmessages.PollJob"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />
<service android:name="me.teafry.voipmessages.RelayJob"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />
<receiver android:name="me.teafry.voipmessages.Alarms" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
@@ -53,6 +53,9 @@ final class Config {
final List<Did> dids = new CopyOnWriteArrayList<>();
private boolean hasSim;
String defaultDid;
/** The relay through jai (Relay.java): address, bearer token, and the 32-byte key that seals everything. */
String relayUrl, relayToken;
byte[] relayKey;
/** Made-up data for testing (android/tools/push_sandbox.py): never goes online. */
boolean sandbox;
private final File file;
@@ -120,6 +123,17 @@ final class Config {
}
if (cfg.gvNumber != null && !cfg.gvNumber.isEmpty()) cfg.dids.add(new Did(cfg.gvNumber, "Google Voice", true));
cfg.defaultDid = o.optString("defaultDid", null);
JSONObject r = o.optJSONObject("relay");
if (r != null) {
String url = r.optString("url", ""), token = r.optString("token", "");
byte[] key = android.util.Base64.decode(r.optString("key", ""), android.util.Base64.DEFAULT);
// Only the addresses network_security_config lets through, and a real key.
if (url.matches("http://(100\\.\\d+\\.\\d+\\.\\d+|127\\.0\\.0\\.1)(:\\d+)?|https://[^/]+") && !token.isEmpty() && key.length == 32) {
cfg.relayUrl = url;
cfg.relayToken = token;
cfg.relayKey = key;
}
}
cfg.sandbox = o.optBoolean("sandbox", false);
} catch (IOException | JSONException e) {
cfg.raw = new JSONObject();
@@ -148,6 +148,8 @@ public final class MainActivity extends BaseActivity {
refresh();
}
private static long lastRelay;
private void refresh() {
bg.execute(() -> {
Sync.poll(this);
@@ -269,6 +271,10 @@ public final class MainActivity extends BaseActivity {
bg.execute(() -> {
StateSync.importPending(this);
Sim.syncSoon(this);
if (System.currentTimeMillis() - lastRelay > 60_000) {
lastRelay = System.currentTimeMillis();
Relay.soon(this);
}
List<Store.Conv> fresh = Store.get(this).conversations(null);
Contacts.all(this); // load names here, not on the main thread while drawing rows
runOnUiThread(() -> {
@@ -0,0 +1,399 @@
package me.teafry.voipmessages;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* The relay through jai (the desktop's src/main/relay.ts; server in
* server/bubble_relay.py): the SIM's texts go to the desktop, texts typed
* there go out over the SIM here, and chat names and read/unread/archived
* stay in step without a cable.
*
* Everything is AES-256-GCM sealed with the key the desktop handed over USB
* (config.json "relay"); jai only passes blobs on. It runs with the checks
* the Rabbit already makes (PollJob) plus one network job after something
* happens here (a text, a send, a swipe), never on a timer of its own: the
* battery pays for nothing new while nothing's going on.
*/
final class Relay {
private Relay() {}
private static final String TAG = "Relay";
private static final int JOB_ID = 2;
private static final Object LOCK = new Object();
private static final SecureRandom random = new SecureRandom();
/** A send from the desktop that waited longer than this isn't sent any more. */
private static final long MAX_SEND_AGE = 24 * 3600_000L;
private static final int MSG_CHUNK = 300;
private static final int MAX_PUSH = 7 * 1024 * 1024;
/** New messages carry their photos; older ones (the first copy of the history) send them when asked. */
private static final long INLINE_RECENT = 2 * 86_400_000L;
static boolean configured(Config cfg) {
return cfg.relayUrl != null && cfg.relayToken != null && cfg.relayKey != null;
}
private static SharedPreferences prefs(Context c) {
return c.getSharedPreferences("relay", Context.MODE_PRIVATE);
}
// ---- sealing (relay.ts seal/unseal) ----
static String seal(byte[] key, String author, JSONObject payload) throws Exception {
byte[] nonce = new byte[12];
random.nextBytes(nonce);
Cipher ci = Cipher.getInstance("AES/GCM/NoPadding");
ci.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, nonce));
ci.updateAAD(("bubble-relay/1/" + author).getBytes(StandardCharsets.UTF_8));
byte[] ct = ci.doFinal(payload.toString().getBytes(StandardCharsets.UTF_8));
byte[] out = new byte[1 + 12 + ct.length];
out[0] = 1;
System.arraycopy(nonce, 0, out, 1, 12);
System.arraycopy(ct, 0, out, 13, ct.length);
return Base64.encodeToString(out, Base64.NO_WRAP);
}
/** Null for anything not sealed with our key by `author`. */
static JSONObject unseal(byte[] key, String author, String blob) {
try {
byte[] b = Base64.decode(blob, Base64.DEFAULT);
if (b.length < 1 + 12 + 16 || b[0] != 1) return null;
Cipher ci = Cipher.getInstance("AES/GCM/NoPadding");
ci.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, b, 1, 12));
ci.updateAAD(("bubble-relay/1/" + author).getBytes(StandardCharsets.UTF_8));
return new JSONObject(new String(ci.doFinal(b, 13, b.length - 13), StandardCharsets.UTF_8));
} catch (Exception e) {
return null;
}
}
// ---- what's waiting to go ----
/** Names or read/unread/archived changed here (StateSync): tell the desktop soon. */
static void stateChanged(Context c) {
if (!configured(Config.get(c))) return;
prefs(c).edit().putBoolean("stateDirty", true).apply();
soon(c);
}
/** Something to tell the desktop: one network job, a few seconds out so a burst shares it. */
static void soon(Context c) {
if (!configured(Config.get(c))) return;
JobInfo job = new JobInfo.Builder(JOB_ID, new ComponentName(c, RelayJob.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setMinimumLatency(5000)
.build();
c.getSystemService(JobScheduler.class).schedule(job);
}
private static synchronized void addToList(Context c, String key, JSONObject item) {
try {
JSONArray a = new JSONArray(prefs(c).getString(key, "[]"));
a.put(item);
prefs(c).edit().putString(key, a.toString()).commit();
} catch (Exception ignored) {
}
}
private static synchronized JSONArray takeList(Context c, String key, int count) {
try {
JSONArray a = new JSONArray(prefs(c).getString(key, "[]"));
JSONArray rest = new JSONArray();
for (int i = count; i < a.length(); i++) rest.put(a.get(i));
prefs(c).edit().putString(key, rest.toString()).commit();
return a;
} catch (Exception e) {
return new JSONArray();
}
}
private static JSONArray peekList(Context c, String key) {
try {
return new JSONArray(prefs(c).getString(key, "[]"));
} catch (Exception e) {
return new JSONArray();
}
}
// ---- one sync ----
/** One round with jai (more while there's more). Off the main thread; quiet when jai's unreachable. */
static void sync(Context c) {
Config cfg = Config.get(c);
if (!configured(cfg)) return;
synchronized (LOCK) {
try {
for (int round = 0; round < 20; round++)
if (!syncOnce(c, cfg)) break;
prefs(c).edit().putLong("lastSync", System.currentTimeMillis()).remove("error").apply();
} catch (Exception e) {
Log.w(TAG, "relay sync failed: " + e.getMessage());
prefs(c).edit().putString("error", e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()).apply();
}
}
}
static String status(Context c) {
SharedPreferences p = prefs(c);
String err = p.getString("error", null);
long last = p.getLong("lastSync", 0);
if (err != null) return "⚠ " + err;
return last == 0 ? "Not yet" : "Last check " + Ui.shortTime(c, last).toLowerCase();
}
/** Returns true if there's more to do right away. */
private static boolean syncOnce(Context c, Config cfg) throws Exception {
SharedPreferences p = prefs(c);
Store store = Store.get(c);
List<String> push = new ArrayList<>();
int size = 0;
// 1. How the desktop's sends went.
JSONArray results = peekList(c, "results");
for (int i = 0; i < results.length(); i++) {
String b = seal(cfg.relayKey, "rabbit", results.getJSONObject(i));
push.add(b);
size += b.length();
}
int resultsPushed = results.length();
// 2. The SIM's messages we haven't passed on (both id ranges: SMS, MMS).
long smsLast = p.getLong("smsLast", Sim.SMS_BASE), mmsLast = p.getLong("mmsLast", Sim.MMS_BASE);
long[] newSms = {smsLast}, newMms = {mmsLast};
boolean moreMsgs = false;
long now = System.currentTimeMillis();
int[] inlineBudget = {3 * 1024 * 1024};
for (int range = 0; range < 2; range++) {
long from = range == 0 ? smsLast : mmsLast, to = range == 0 ? Sim.MMS_BASE : Sim.FALLBACK_BASE;
JSONArray msgs = new JSONArray();
long max = from;
int n = 0;
try (Cursor cur = store.db.rawQuery("SELECT id FROM messages WHERE did = ? AND id > ? AND id < ? ORDER BY id LIMIT " + MSG_CHUNK,
new String[] {Sim.DID, String.valueOf(from), String.valueOf(to)})) {
while (cur.moveToNext()) {
Store.Msg m = store.getMessage(cur.getLong(0));
n++;
if (m == null) continue;
max = Math.max(max, m.id);
msgs.put(toJson(c, m, now - m.ts < INLINE_RECENT, inlineBudget));
}
}
if (n == MSG_CHUNK) moreMsgs = true;
if (msgs.length() > 0) {
String b = seal(cfg.relayKey, "rabbit", new JSONObject().put("t", "msgs").put("msgs", msgs));
push.add(b);
size += b.length();
}
if (range == 0) newSms[0] = max;
else newMms[0] = max;
}
// 3. Names and read status, if they changed here.
boolean stateDirty = p.getBoolean("stateDirty", false);
if (stateDirty) {
String b = seal(cfg.relayKey, "rabbit", new JSONObject().put("t", "state")
.put("names", store.namesExport()).put("read", store.readStateExport()));
push.add(b);
size += b.length();
}
// 4. Photos the desktop asked for (a few MB per round).
JSONArray wants = peekList(c, "wants");
int wantsDone = 0;
for (int i = 0; i < wants.length() && size < MAX_PUSH; i++) {
JSONObject w = wants.getJSONObject(i);
String b = seal(cfg.relayKey, "rabbit", mediaReply(c, w.optLong("id"), w.optInt("index")));
push.add(b);
size += b.length();
wantsDone++;
}
// Out, and in.
long since = p.getLong("cursor", 0);
JSONArray arr = new JSONArray();
for (String b : push) arr.put(b);
byte[] body = new JSONObject().put("device", "rabbit").put("since", since).put("push", arr).toString().getBytes(StandardCharsets.UTF_8);
Map<String, String> h = new HashMap<>();
h.put("Authorization", "Bearer " + cfg.relayToken);
h.put("Content-Type", "application/json");
Http.Response r = Http.request("POST", cfg.relayUrl + "/sync", h, body, 120_000);
if (r.status == 401) throw new Exception("jai turned down the token (Sync to Rabbit from the desktop again)");
if (r.status != 200) throw new Exception("jai answered " + r.status);
JSONObject res = new JSONObject(new String(r.body, StandardCharsets.UTF_8));
// jai has them now.
if (resultsPushed > 0) takeList(c, "results", resultsPushed);
if (wantsDone > 0) takeList(c, "wants", wantsDone);
SharedPreferences.Editor ed = p.edit().putLong("smsLast", newSms[0]).putLong("mmsLast", newMms[0]);
if (stateDirty) ed.putBoolean("stateDirty", false);
ed.commit();
JSONArray events = res.getJSONArray("events");
for (int i = 0; i < events.length(); i++) handle(c, unseal(cfg.relayKey, "desktop", events.getString(i)));
p.edit().putLong("cursor", res.getLong("cursor")).commit();
return res.optBoolean("more") || moreMsgs || peekList(c, "wants").length() > 0 && wantsDone > 0;
}
private static JSONObject toJson(Context c, Store.Msg m, boolean inline, int[] budget) throws Exception {
JSONObject o = new JSONObject().put("id", m.id).put("did", m.did).put("contact", m.contact).put("direction", m.direction)
.put("body", m.body == null ? "" : m.body).put("ts", m.ts).put("kind", m.kind);
if (m.sender != null) o.put("sender", m.sender);
List<JSONObject> refs = m.media();
if (!refs.isEmpty()) {
JSONArray media = new JSONArray();
for (int i = 0; i < refs.size(); i++) {
JSONObject ref = refs.get(i);
JSONObject out = new JSONObject().put("mime", ref.optString("mime", "application/octet-stream"));
String url = ref.optString("url", "");
File f = null;
if (inline || !url.startsWith("mmspart:")) f = Media.resolve(c, m.id, i);
if (f != null && (f.length() <= budget[0] || !url.startsWith("mmspart:"))) {
budget[0] -= (int) f.length();
out.put("data", Base64.encodeToString(Media.read(f), Base64.NO_WRAP));
if (ref.optInt("width") > 0) out.put("width", ref.optInt("width")).put("height", ref.optInt("height"));
} else if (url.startsWith("mmspart:")) {
out.put("url", url);
} else continue;
media.put(out);
}
if (media.length() > 0) o.put("media", media);
}
return o;
}
private static JSONObject mediaReply(Context c, long id, int index) throws Exception {
JSONObject out = new JSONObject().put("t", "media").put("id", id).put("index", index);
Store.Msg m = Store.get(c).getMessage(id);
// Only the SIM's own attachments go this way.
File f = m != null && Sim.isSim(m.did) ? Media.resolve(c, id, index) : null;
if (f == null) return out.put("missing", true);
JSONObject ref = m.media().get(index);
out.put("mime", ref.optString("mime", "application/octet-stream")).put("data", Base64.encodeToString(Media.read(f), Base64.NO_WRAP));
if (ref.optInt("width") > 0) out.put("width", ref.optInt("width")).put("height", ref.optInt("height"));
return out;
}
// ---- what the desktop sent ----
private static void handle(Context c, JSONObject p) {
if (p == null) return;
try {
switch (p.optString("t")) {
case "send": onSend(c, p); break;
case "state": onState(c, p); break;
case "want": {
JSONArray items = p.optJSONArray("items");
for (int i = 0; items != null && i < items.length(); i++) addToList(c, "wants", items.getJSONObject(i));
break;
}
default: break;
}
} catch (Exception e) {
Log.w(TAG, "skipped a message from the desktop: " + e.getMessage());
}
}
private static void onState(Context c, JSONObject p) throws Exception {
Store store = Store.get(c);
JSONArray names = p.optJSONArray("names"), read = p.optJSONArray("read");
if (names != null) store.namesImport(names);
if (read != null) store.readStateImport(read);
StateSync.export(c);
Notifier.sweep(c);
Sync.changed();
}
/** Each send from the desktop goes out at most once, ever: remembered before it's tried. */
private static synchronized boolean firstTime(Context c, String id) {
SharedPreferences p = prefs(c);
Set<String> done = new HashSet<>(p.getStringSet("sent", new HashSet<>()));
long now = System.currentTimeMillis();
for (String e : done) if (e.startsWith(id + "\t")) return false;
// Forget ones old enough to be refused by age anyway.
done.removeIf(e -> {
try {
return now - Long.parseLong(e.substring(e.indexOf('\t') + 1)) > 2 * MAX_SEND_AGE;
} catch (Exception x) {
return true;
}
});
done.add(id + "\t" + now);
return p.edit().putStringSet("sent", done).commit();
}
private static void result(Context c, String id, long msgId, String error) {
try {
JSONObject o = new JSONObject().put("t", "sent").put("id", id);
o.put("msgId", msgId != 0 ? msgId : JSONObject.NULL);
o.put("error", error != null ? error : JSONObject.NULL);
addToList(c, "results", o);
} catch (Exception ignored) {
}
soon(c);
}
private static void onSend(Context c, JSONObject p) throws Exception {
String id = p.getString("id");
if (!id.matches("[0-9a-fA-F-]{36}") || !firstTime(c, id)) return;
String contact = p.optString("contact", "");
String text = p.optString("text", "");
if (contact.isEmpty() || !contact.matches("[0-9,]+|[^,]+")) {
result(c, id, 0, "No one to send it to.");
return;
}
if (System.currentTimeMillis() - p.optLong("ts") > MAX_SEND_AGE) {
result(c, id, 0, "Waited over a day for the Rabbit, so it wasn't sent. Send it again?");
return;
}
if (!Sim.isDefault(c)) {
result(c, id, 0, "bubble isn't the Rabbit's texting app, so it can't use the SIM.");
return;
}
List<Sender.Photo> photos = new ArrayList<>();
JSONArray media = p.optJSONArray("media");
for (int i = 0; media != null && i < media.length(); i++) {
JSONObject a = media.getJSONObject(i);
byte[] data = Base64.decode(a.getString("data"), Base64.DEFAULT);
String mime = a.optString("mime", "application/octet-stream");
String file = Media.save(c, data, mime);
int w = a.optInt("width"), hgt = a.optInt("height");
if (w == 0 && mime.startsWith("image/")) {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, o);
w = o.outWidth;
hgt = o.outHeight;
}
photos.add(new Sender.Photo(file, mime, Math.max(0, w), Math.max(0, hgt)));
}
if (text.isEmpty() && photos.isEmpty()) {
result(c, id, 0, "It was empty.");
return;
}
Sender.send(c, Sim.DID, contact, text, photos, (msgId, error) -> result(c, id, msgId, error));
}
}
@@ -0,0 +1,21 @@
package me.teafry.voipmessages;
import android.app.job.JobParameters;
import android.app.job.JobService;
/** One round with the relay, after something happened here (Relay.soon). */
public final class RelayJob extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
new Thread(() -> {
Relay.sync(this);
jobFinished(params, false);
}, "relay").start();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
@@ -55,7 +55,16 @@ final class Sender {
long run() throws Exception;
}
/** How a send ended: the message's id, or 0 and why not (Relay reports it to the desktop). */
interface Done {
void done(long msgId, String error);
}
static void send(Context ctx, String did, String contact, String text, List<Photo> photos) {
send(ctx, did, contact, text, photos, null);
}
static void send(Context ctx, String did, String contact, String text, List<Photo> photos, Done done) {
Context c = ctx.getApplicationContext();
Config cfg = Config.get(c);
if (cfg.sandbox) {
@@ -63,13 +72,15 @@ final class Sender {
Store.Msg m = pending(did, contact, text, photos);
Store store = Store.get(c);
long id = store.insertPending(m);
store.confirmPending(id, 9_000_000_000L + (System.currentTimeMillis() % 1_000_000_000L));
long real = 9_000_000_000L + (System.currentTimeMillis() % 1_000_000_000L);
store.confirmPending(id, real);
Sync.changed();
if (done != null) done.done(real, null);
return;
}
if (cfg.isGv(did)) {
if (photos.isEmpty()) {
enqueue(c, pending(did, contact, text, null), () -> gvSend(c, did, contact, text, null), true);
enqueue(c, pending(did, contact, text, null), () -> gvSend(c, did, contact, text, null), true, done);
} else {
// Google Voice carries one photo per message: the text rides with the first.
for (int i = 0; i < photos.size(); i++) {
@@ -79,7 +90,7 @@ final class Sender {
one.add(p);
Store.Msg m = pending(did, contact, body, one);
m.ts += i;
enqueue(c, m, () -> gvSend(c, did, contact, body, p), true);
enqueue(c, m, () -> gvSend(c, did, contact, body, p), true, i == 0 ? done : null);
}
}
return;
@@ -89,7 +100,10 @@ final class Sender {
boolean mms = !photos.isEmpty() || Phone.isGroup(contact);
Store.Msg m = pending(did, contact, text, photos);
if (mms) m.kind = "mms";
enqueue(c, m, () -> mms ? Sim.sendMms(c, contact, text, photos) : Sim.sendSms(c, contact, text), false);
enqueue(c, m, () -> mms ? Sim.sendMms(c, contact, text, photos) : Sim.sendSms(c, contact, text), false, (id, err) -> {
if (done != null) done.done(id, err);
Relay.soon(c); // the desktop hears about it on the next round
});
return;
}
boolean mms = !photos.isEmpty() || Phone.charCount(text) > Phone.SMS_TEXT_LIMIT;
@@ -102,7 +116,7 @@ final class Sender {
for (Photo p : photos)
media.add("data:" + p.mime + ";base64," + Base64.encodeToString(Media.read(new File(Media.dir(c), p.file)), Base64.NO_WRAP));
return api.sendMms(did, contact, text, media);
}, false);
}, false, done);
}
private static Store.Msg pending(String did, String contact, String text, List<Photo> photos) {
@@ -121,34 +135,38 @@ final class Sender {
return m;
}
private static void enqueue(Context c, Store.Msg m, Attempt attempt, boolean gv) {
private static void enqueue(Context c, Store.Msg m, Attempt attempt, boolean gv, Done done) {
Store store = Store.get(c);
long tempId = store.insertPending(m);
Sync.changed();
queue.execute(() -> {
deliver(c, store, tempId, attempt, gv);
long[] id = {0};
String error = deliver(c, store, tempId, attempt, gv, id);
Sync.changed();
if (done != null) done.done(id[0], error);
});
}
private static void deliver(Context c, Store store, long tempId, Attempt attempt, boolean gv) {
/** Null when it went out (its id in id[0]), else why not. */
private static String deliver(Context c, Store store, long tempId, Attempt attempt, boolean gv, long[] id) {
for (int n = 1; ; n++) {
try {
store.confirmPending(tempId, attempt.run());
return;
id[0] = attempt.run();
store.confirmPending(tempId, id[0]);
return null;
} catch (Exception e) {
if (store.getMessage(tempId) == null) return; // a check found it already sent
if (store.getMessage(tempId) == null) return null; // a check found it already sent
String kind = classify(e, gv);
if (kind.equals("unknown")) {
for (long ms : CHECK_DELAYS) {
sleep(ms);
Sync.poll(c);
if (store.getMessage(tempId) == null) return;
if (store.getMessage(tempId) == null) return null;
}
}
if (kind.equals("fatal") || n >= MAX_ATTEMPTS) {
store.markFailed(tempId, e.getMessage());
return;
return e.getMessage() == null ? "It didn't go out." : e.getMessage();
}
if (kind.equals("safe")) sleep(RETRY_DELAYS[Math.min(n - 1, RETRY_DELAYS.length - 1)]);
}
@@ -122,6 +122,13 @@ public final class SettingsActivity extends BaseActivity {
+ " the texts already on the phone come over too.");
}
if (Relay.configured(cfg)) {
section("Relay (jai)");
kv("Desktop link", Relay.status(c));
note("Texts typed on the desktop for \"via SIM\" go out at this Rabbit's background check, and chat names and read status"
+ " go both ways. Sealed with a key only the desktop and this Rabbit have.");
}
section("About");
String version = "?";
try {
@@ -389,6 +389,7 @@ final class Sim {
List<Store.Msg> added = sync(c);
Sync.changed();
Notifier.notifyNew(c, added);
if (!added.isEmpty()) Relay.soon(c);
}
/** A carrier's "you have an MMS" (MmsReceiver): fetch it. */
@@ -54,6 +54,7 @@ final class StateSync {
static void rename(Context c, String did, String contact, String name) {
Store.get(c).setName(did, contact, name, System.currentTimeMillis());
export(c);
Relay.stateChanged(c);
Sync.changed();
}
@@ -61,16 +62,19 @@ final class StateSync {
Store.get(c).markRead(did, contact);
if (Sim.isSim(did)) Sim.markRead(c, contact);
export(c);
Relay.stateChanged(c);
Notifier.sweep(c);
}
static void markUnread(Context c, String did, String contact) {
Store.get(c).markUnread(did, contact);
export(c);
Relay.stateChanged(c);
}
static void setArchived(Context c, String did, String contact, boolean archived) {
Store.get(c).setArchived(did, contact, archived);
export(c);
Relay.stateChanged(c);
}
}
@@ -93,6 +93,8 @@ final class Sync {
}
}
store.set("last_poll", String.valueOf(System.currentTimeMillis()));
// The relay rides along with this check: no wakeups of its own.
Relay.sync(c);
if (!added.isEmpty()) StateSync.export(c);
// Threads read on the phone or the web came back read (see storeThread).
Notifier.sweep(c);
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Everything is https, except the relay on jai over tailscale (WireGuard
already encrypts that hop, and what it carries is sealed: Relay.java) and
localhost for testing it with adb reverse. -->
<network-security-config>
<base-config cleartextTrafficPermitted="false" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">100.69.139.40</domain>
<domain includeSubdomains="false">127.0.0.1</domain>
</domain-config>
</network-security-config>
+2 -2
View File
@@ -26,8 +26,8 @@ import sys
import zipfile
from pathlib import Path
VERSION_NAME = "0.4.0"
VERSION_CODE = 4
VERSION_NAME = "0.5.0"
VERSION_CODE = 5
PACKAGE = "me.teafry.voipmessages"
DEV_PACKAGE = PACKAGE + ".dev"
+16 -1
View File
@@ -5,10 +5,16 @@ Its config is marked sandbox, so the app never goes online.
python3 android/build.py --dev --install && python3 android/tools/push_sandbox.py
With --relay URL (and RELAY_TOKEN / RELAY_KEY in the environment) the dev app
also gets relay settings, e.g. a throwaway local server/bubble_relay.py reached
through `adb reverse tcp:18080 tcp:<port>` with --relay http://127.0.0.1:18080.
Uses the same copy-in mechanism as the desktop installer: files are piped
through `adb shell run-as` into the app's private storage.
"""
import argparse
import json
import os
import sqlite3
import subprocess
import tempfile
@@ -26,8 +32,12 @@ def push(local: Path, name: str):
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--relay", help="relay URL as the Rabbit reaches it (token and key from RELAY_TOKEN / RELAY_KEY)")
args = ap.parse_args()
subprocess.run(["adb", "shell", "am", "force-stop", PKG], check=True)
subprocess.run(["adb", "shell", f"run-as {PKG} sh -c 'mkdir -p files && rm -f files/messages.db*'"], check=True)
# Fresh: no history, and no relay cursors left from an earlier run.
subprocess.run(["adb", "shell", f"run-as {PKG} sh -c 'mkdir -p files && rm -f files/messages.db* shared_prefs/relay.xml'"], check=True)
with tempfile.TemporaryDirectory() as d:
db = Path(d) / "messages.db"
con = sqlite3.connect(db)
@@ -65,6 +75,10 @@ def main():
NAMED = "5415550101,5415550140"
rows.append((2**52 + 20, GV, NAMED, "in", "Booked the cabin!", now - 600 * m, "5415550140"))
con.execute("INSERT INTO read_state (did, contact, last_read_ts, name, name_ts) VALUES (?, ?, 0, 'Cabin trip', ?)", (GV, NAMED, now))
# The Rabbit's SIM (did 'sim', ids from 2^51 like Sim.java's).
SIM_BASE = 2**51
rows.append((SIM_BASE + 1, "sim", "5415550150", "in", "Texting your SIM number now", now - 20 * m, None))
rows.append((SIM_BASE + 2, "sim", "5415550150", "out", "Got it, this is the SIM", now - 19 * m, None))
con.executemany("INSERT INTO messages (id, did, contact, direction, body, ts, sender) VALUES (?,?,?,?,?,?,?)", rows)
con.execute("INSERT INTO kv VALUES ('unread_floor', '0')")
con.execute("INSERT INTO gv_threads VALUES (?, ?, 'g.demo')", (GV, GROUP))
@@ -77,6 +91,7 @@ def main():
"gvoice": {"number": GV, "cookies": {}},
"dids": [{"did": VOIP, "description": "Demo"}],
"defaultDid": VOIP,
**({"relay": {"url": args.relay, "token": os.environ["RELAY_TOKEN"], "key": os.environ["RELAY_KEY"]}} if args.relay else {}),
}))
push(db, "messages.db")
push(cfg, "config.json")
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "voipms-desktop",
"productName": "bubble",
"version": "0.4.0",
"version": "0.5.0",
"description": "Signal-style desktop SMS client for voip.ms",
"author": "trav <trav@teafry.me>",
"private": true,
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
{"version_name": "0.4.0", "version_code": 4, "package": "me.teafry.voipmessages"}
{"version_name": "0.5.0", "version_code": 5, "package": "me.teafry.voipmessages"}
+33
View File
@@ -0,0 +1,33 @@
#!/bin/bash
# Deploy bubble-relay to jai (see ~/Documents/shared-resources/jai.html#run).
# jai is production: this only ever touches ~/Documents/bubble-server and its
# own screen session, and installs nothing.
#
# scripts/deploy-jai.sh copy the code and (re)start the service
# scripts/deploy-jai.sh --stop stop it
# scripts/deploy-jai.sh --token print the bearer token, for Settings → Rabbit relay on the desktop
#
# What runs is server/run.sh, also started by jai's ~/startup.sh (re-run by its
# watchdog every 5 minutes), so it comes back after a crash or a reboot.
set -euo pipefail
HOST=${JAI_HOST:-jai-ext}
DIR=/home/jai/Documents/bubble-server
NAME=bubble-relay
cd "$(dirname "$0")/.."
case "${1:-}" in
--token) ssh "$HOST" "cat $DIR/data/token"; echo; exit 0 ;;
--stop) ssh "$HOST" "screen -S $NAME -X quit || true"; echo "stopped"; exit 0 ;;
esac
ssh "$HOST" "mkdir -p $DIR/app $DIR/data ~/service-logs && chmod 700 $DIR $DIR/data"
rsync -a server/bubble_relay.py server/run.sh "$HOST:$DIR/app/"
ssh "$HOST" bash -s <<REMOTE
set -e
screen -S $NAME -X quit 2>/dev/null || true
sleep 1
screen -dmS $NAME bash -c 'bash $DIR/app/run.sh 2>&1 | tee ~/service-logs/$NAME.log'
sleep 2
for pid in \$(pgrep -f bubble_relay.py); do echo 1000 > /proc/\$pid/oom_score_adj; done
curl -s -m 5 http://100.69.139.40:8080/health; echo
REMOTE
+178
View File
@@ -0,0 +1,178 @@
#!/usr/bin/env python3
"""bubble-relay: a mailbox between the desktop app and the Rabbit R1. Runs on jai.
It never sees a message: every blob is AES-256-GCM sealed by the apps with a
key jai doesn't have (the desktop makes it and hands it to the Rabbit over
USB). This only stores opaque base64 strings, hands each device what the
other one posted, and forgets them once picked up (or after KEEP_DAYS).
Standard library only. Bind it to the tailscale address; every request but
/health needs "Authorization: Bearer <token>" (made on first start in
<data>/token, mode 600).
GET /health -> {"ok": true}
POST /sync {"device": "desktop"|"rabbit", "since": <cursor>, "push": ["<blob>", ...]}
-> {"cursor": <n>, "events": ["<blob>", ...], "more": bool}
`since` is the cursor the last answer gave; it also acknowledges everything up
to it, which the other side posted, so those are deleted. `more` = there's
more than fits in one answer: call again with the new cursor. A cursor past
anything this server ever issued (a wiped server) means "from the start".
python3 bubble_relay.py --host 100.69.139.40 --port 8080 --data DIR
"""
import argparse
import json
import os
import re
import secrets
import sqlite3
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
DEVICES = ("desktop", "rabbit")
KEEP_DAYS = 14
MAX_BODY = 24 * 1024 * 1024 # one request
MAX_BLOB = 12 * 1024 * 1024 # one sealed blob (a few photos)
MAX_PUSH = 2000 # blobs in one request
MAX_ANSWER = 8 * 1024 * 1024 # roughly, per answer (`more` past it)
MAX_STORED = 512 * 1024 * 1024 # everything waiting; pushes past it are refused
BLOB_RE = re.compile(r"^[A-Za-z0-9+/]+={0,2}$")
LOCK = threading.Lock()
class Store:
def __init__(self, path):
self.db = sqlite3.connect(path, check_same_thread=False)
self.db.execute("PRAGMA journal_mode=WAL")
self.db.execute("CREATE TABLE IF NOT EXISTS events (seq INTEGER PRIMARY KEY AUTOINCREMENT,"
" author TEXT NOT NULL, blob TEXT NOT NULL, size INTEGER NOT NULL, created INTEGER NOT NULL)")
self.db.commit()
def issued(self):
"""The highest cursor ever handed out (survives deletions)."""
row = self.db.execute("SELECT seq FROM sqlite_sequence WHERE name = 'events'").fetchone()
return row[0] if row else 0
def stored(self):
return self.db.execute("SELECT COALESCE(SUM(size), 0) FROM events").fetchone()[0]
def sync(self, device, since, pushed):
with LOCK:
now = int(time.time())
issued = self.issued()
if since > issued:
since = 0
# Picked up by this device: gone. Too old for anyone: gone.
self.db.execute("DELETE FROM events WHERE author != ? AND seq <= ?", (device, since))
self.db.execute("DELETE FROM events WHERE created < ?", (now - KEEP_DAYS * 86400,))
if pushed:
incoming = sum(len(b) for b in pushed)
if self.stored() + incoming > MAX_STORED:
self.db.commit()
return None
self.db.executemany("INSERT INTO events (author, blob, size, created) VALUES (?, ?, ?, ?)",
[(device, b, len(b), now) for b in pushed])
self.db.commit()
events, size, cursor, more = [], 0, since, False
for seq, blob in self.db.execute("SELECT seq, blob FROM events WHERE author != ? AND seq > ? ORDER BY seq",
(device, since)):
if events and size + len(blob) > MAX_ANSWER:
more = True
break
events.append(blob)
size += len(blob)
cursor = seq
if not more:
# Nothing left for this device: skip past the other device's own posts too.
cursor = max(cursor, self.issued())
return {"cursor": cursor, "events": events, "more": more}
class Handler(BaseHTTPRequestHandler):
server_version = "bubble-relay"
sys_version = ""
timeout = 60 # a stalled client doesn't hold a thread forever
def _send(self, code, obj):
body = json.dumps(obj, separators=(",", ":")).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(body)
def _authed(self):
got = self.headers.get("Authorization", "")
return secrets.compare_digest(got.encode(), ("Bearer " + self.server.token).encode())
def do_GET(self):
if self.path == "/health":
return self._send(200, {"ok": True})
self._send(404, {"error": "not found"})
def do_POST(self):
if self.path != "/sync":
return self._send(404, {"error": "not found"})
if not self._authed():
return self._send(401, {"error": "unauthorized"})
try:
n = int(self.headers.get("Content-Length") or 0)
except ValueError:
return self._send(400, {"error": "bad length"})
if n < 0 or n > MAX_BODY:
return self._send(413, {"error": "too large"})
try:
req = json.loads(self.rfile.read(n) or b"{}")
device = req.get("device")
since = int(req.get("since", 0))
push = req.get("push", [])
if device not in DEVICES:
raise ValueError("device")
if since < 0:
raise ValueError("since")
if not isinstance(push, list) or len(push) > MAX_PUSH:
raise ValueError("push")
for b in push:
if not isinstance(b, str) or not b or len(b) > MAX_BLOB or not BLOB_RE.match(b):
raise ValueError("blob")
except (ValueError, TypeError, AttributeError) as e:
return self._send(400, {"error": "bad request: %s" % e})
out = self.server.store.sync(device, since, push)
if out is None:
return self._send(507, {"error": "relay full"})
self._send(200, out)
def log_message(self, fmt, *args):
# Method, path and status only: nothing a request carries.
sys.stderr.write("%s %s\n" % (self.log_date_time_string(), fmt % args))
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8080)
ap.add_argument("--data", required=True)
a = ap.parse_args()
os.makedirs(a.data, mode=0o700, exist_ok=True)
os.chmod(a.data, 0o700)
os.umask(0o077)
tok_path = os.path.join(a.data, "token")
if not os.path.exists(tok_path):
fd = os.open(tok_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w") as f:
f.write(secrets.token_urlsafe(32))
srv = ThreadingHTTPServer((a.host, a.port), Handler)
srv.daemon_threads = True
srv.token = open(tok_path).read().strip()
srv.store = Store(os.path.join(a.data, "relay.db"))
print("bubble-relay on %s:%d" % (a.host, a.port), flush=True)
srv.serve_forever()
if __name__ == "__main__":
main()
Executable
+4
View File
@@ -0,0 +1,4 @@
#!/bin/bash
# The one real command line for bubble-relay on jai (~/startup.sh and scripts/deploy-jai.sh both run this).
cd /home/jai/Documents/bubble-server/app || exit 1
exec python3 bubble_relay.py --host 100.69.139.40 --port 8080 --data /home/jai/Documents/bubble-server/data
+80
View File
@@ -0,0 +1,80 @@
"""python3 -m unittest server/test_bubble_relay.py (a real server on localhost, made-up blobs)"""
import json
import os
import socket
import subprocess
import sys
import tempfile
import time
import unittest
import urllib.error
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
class RelayTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dir = tempfile.mkdtemp()
s = socket.socket()
s.bind(("127.0.0.1", 0))
cls.port = s.getsockname()[1]
s.close()
cls.proc = subprocess.Popen([sys.executable, os.path.join(HERE, "bubble_relay.py"), "--port", str(cls.port),
"--data", cls.dir], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
for _ in range(50):
try:
urllib.request.urlopen("http://127.0.0.1:%d/health" % cls.port, timeout=1)
break
except OSError:
time.sleep(0.1)
cls.token = open(os.path.join(cls.dir, "token")).read().strip()
@classmethod
def tearDownClass(cls):
cls.proc.terminate()
cls.proc.wait()
def call(self, body, token=None, raw=None):
data = raw if raw is not None else json.dumps(body).encode()
req = urllib.request.Request("http://127.0.0.1:%d/sync" % self.port, data=data, method="POST",
headers={"Authorization": "Bearer " + (token or self.token)})
try:
with urllib.request.urlopen(req, timeout=5) as r:
return r.status, json.loads(r.read())
except urllib.error.HTTPError as e:
return e.code, json.loads(e.read())
def test_flow(self):
st, d = self.call({"device": "desktop", "since": 0, "push": ["QUJD", "REVG"]})
self.assertEqual(st, 200)
self.assertEqual(d["events"], []) # never your own
st, r = self.call({"device": "rabbit", "since": 0, "push": ["R0hJ"]})
self.assertEqual(r["events"], ["QUJD", "REVG"])
st, d2 = self.call({"device": "desktop", "since": d["cursor"]})
self.assertEqual(d2["events"], ["R0hJ"])
# Acknowledged by the rabbit: deleted, so nobody gets them again.
self.call({"device": "rabbit", "since": r["cursor"]})
st, again = self.call({"device": "rabbit", "since": 0})
self.assertEqual(again["events"], [])
# A cursor from a wiped server means "from the start".
st, w = self.call({"device": "desktop", "since": 10 ** 9})
self.assertEqual(w["events"], ["R0hJ"])
def test_rejects(self):
self.assertEqual(self.call({"device": "desktop", "since": 0}, token="wrong")[0], 401)
self.assertEqual(self.call({"device": "laptop", "since": 0})[0], 400)
self.assertEqual(self.call({"device": "desktop", "since": 0, "push": ["not base64!"]})[0], 400)
self.assertEqual(self.call({"device": "desktop", "since": 0, "push": [5]})[0], 400)
self.assertEqual(self.call(None, raw=b"{nope")[0], 400)
with urllib.request.urlopen("http://127.0.0.1:%d/health" % self.port) as r:
self.assertEqual(json.loads(r.read()), {"ok": True})
def test_token_file_private(self):
self.assertEqual(os.stat(os.path.join(self.dir, "token")).st_mode & 0o777, 0o600)
self.assertEqual(os.stat(self.dir).st_mode & 0o777, 0o700)
if __name__ == "__main__":
unittest.main()
+77 -5
View File
@@ -27,6 +27,7 @@ import { gvMessageId, GvSyncEngine } from './gvoice/sync'
import { WaaSigner } from './gvoice/waa'
import { clearNotifications, notifyNew } from './notify'
import * as rabbit from './rabbit'
import { checkUrl, clearRelayConfig, loadRelayConfig, newKey, RelayEngine, saveRelayConfig, SIM_DID } from './relay'
protocol.registerSchemesAsPrivileged([
{ scheme: 'contact-photo', privileges: { standard: true, secure: true, supportFetchAPI: true } },
@@ -47,6 +48,8 @@ let dids: Did[] = []
let gv: GVoice | undefined
let gvSync: GvSyncEngine | undefined
let waa: WaaSigner | undefined
// The relay through jai: the Rabbit's SIM, names and read state (relay.ts).
let relay: RelayEngine | undefined
const send = (channel: string, ...args: unknown[]): void => {
if (win && !win.isDestroyed()) win.webContents.send(channel, ...args)
@@ -107,13 +110,38 @@ function openFromNotification(key: { did: string; contact: string }): void {
const gvNumber = (): string | undefined => db.get('gv_number') || undefined
// Every number we can text from: voip.ms's DIDs, then Google Voice's.
// Every number we can text from: voip.ms's DIDs, then Google Voice's, then
// the Rabbit's SIM (through jai).
function allDids(): Did[] {
const n = gvNumber()
return n ? [...dids, { did: n, description: 'Google Voice', smsEnabled: true, provider: 'gvoice' }] : dids
const out: Did[] = n ? [...dids, { did: n, description: 'Google Voice', smsEnabled: true, provider: 'gvoice' }] : [...dids]
if (relay) out.push({ did: SIM_DID, description: 'Rabbit SIM', smsEnabled: true, provider: 'sim' })
return out
}
const providerOf = (did: string): Provider => (did === gvNumber() ? 'gvoice' : 'voipms')
const providerOf = (did: string): Provider => (did === SIM_DID ? 'sim' : did === gvNumber() ? 'gvoice' : 'voipms')
function startRelay(): void {
relay?.stop()
relay = undefined
const cfg = loadRelayConfig()
if (!cfg) return
relay = new RelayEngine(db, media, cfg, settings.pollSeconds)
relay.on('messages', onNewMessages)
relay.on('changed', () => send('conversationsChanged'))
relay.on('status', () => send('relayStatus', relay?.status()))
relay.start()
}
// A text over the Rabbit's SIM: pending here until the Rabbit's next check
// sends it and says how it went (relay.ts).
function relaySend(contact: string, text: string, refs: MediaRef[]): void {
if (!relay) throw new Error('The Rabbit relay is off: connect it in Settings → Rabbit.')
const kind = refs.length || isGroup(contact) ? 'mms' : 'sms'
const tempId = db.insertPending({ did: SIM_DID, contact, direction: 'out', body: text, ts: Date.now(), kind, ...(refs.length ? { media: refs } : {}) })
relay.send(tempId, contact, text, refs)
send('conversationsChanged')
}
function syncStatus(): SyncStatus {
const base: SyncStatus = sync?.getStatus() ?? { phase: 'unconfigured', backfillDone: false, total: db.count() }
@@ -170,6 +198,10 @@ async function rabbitConfig(): Promise<rabbit.RabbitConfig> {
...(SANDBOX ? { sandbox: true } : {}),
voipms: SANDBOX ? { username: 'sandbox', password: 'sandbox' } : creds ? { username: creds.username, password: creds.password } : null,
gvoice: gv ? { number: gv, cookies: SANDBOX ? {} : await exportCookies() } : null,
relay: (() => {
const r = loadRelayConfig()
return r ? { url: r.url, token: r.token, key: r.key } : null
})(),
dids: voipDids.map((d) => ({ did: d.did, description: d.description })),
defaultDid: settings.defaultDid ?? voipDids[0]?.did ?? gv
}
@@ -393,6 +425,7 @@ async function gvSend(did: string, contact: string, text: string, ref?: MediaRef
// Anything longer than one SMS goes out as a single text-only MMS (voip.ms;
// Google Voice has no such limit).
async function sendText(did: string, contact: string, text: string): Promise<void> {
if (did === SIM_DID) return relaySend(contact, text, [])
const from = normalizeNumber(did)
const m: Omit<NewMessage, 'id'> = { did: from, contact, direction: 'out', body: text, ts: Date.now() }
if (providerOf(from) === 'gvoice') return deliver(m, () => gvSend(from, contact, text))
@@ -410,6 +443,7 @@ async function sendMms(did: string, contact: string, text: string, attachments:
}
async function sendMmsRefs(did: string, contact: string, text: string, refs: MediaRef[]): Promise<void> {
if (did === SIM_DID) return relaySend(contact, text, refs)
const from = normalizeNumber(did)
if (providerOf(from) === 'gvoice') {
if (refs.some((r) => !SendMediaType[r.mime ?? ''])) throw new Error('Google Voice can only send photos.')
@@ -537,20 +571,25 @@ function registerIpc(): void {
// Clear the unread badge on the phone too.
const thread = providerOf(did) === 'gvoice' && db.hasUnread(did, contact) ? db.getGvThread(did, contact) : undefined
if (thread) gv?.markRead(thread).catch((e: Error) => console.warn('[gvoice] mark read failed:', e.message))
const had = db.hasUnread(did, contact)
db.markRead(did, contact)
clearNotifications(did, contact)
if (had) relay?.stateChanged()
send('conversationsChanged')
})
ipcMain.handle('markUnread', (_e, did: string, contact: string) => {
db.markUnread(did, contact)
relay?.stateChanged()
send('conversationsChanged')
})
ipcMain.handle('setArchived', (_e, did: string, contact: string, archived: boolean) => {
db.setArchived(did, contact, archived)
relay?.stateChanged()
send('conversationsChanged')
})
ipcMain.handle('setName', (_e, did: string, contact: string, name: string | null) => {
db.setName(did, contact, name)
relay?.stateChanged()
send('conversationsChanged')
})
ipcMain.handle('getDrafts', () => db.getDrafts())
@@ -577,8 +616,34 @@ function registerIpc(): void {
clipboard.write([new ClipboardItem({ 'image/png': new Blob([png], { type: 'image/png' }) })])
)
ipcMain.handle('relayStatus', () => relay?.status() ?? { connected: false, waiting: 0, url: loadRelayConfig()?.url })
ipcMain.handle('relayConnect', async (_e, url: string, token: string) => {
const origin = checkUrl(url)
const tok = token.trim()
if (!/^[A-Za-z0-9_-]{20,200}$/.test(tok)) throw new Error("That doesn't look like the relay's token.")
// A harmless call that needs the token: acknowledges nothing, posts nothing.
const res = await fetch(`${origin}/sync`, {
method: 'POST',
headers: { Authorization: `Bearer ${tok}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ device: 'desktop', since: Number(db.get('relay_cursor') ?? 0), push: [] }),
signal: AbortSignal.timeout(15_000)
}).catch(() => undefined)
if (!res) throw new Error(`Couldn't reach ${origin}. Is tailscale up?`)
if (res.status === 401) throw new Error('jai turned down that token.')
if (!res.ok) throw new Error(`jai answered ${res.status}.`)
// Keep the key across reconnects so the Rabbit's copy stays good.
saveRelayConfig({ url: origin, token: tok, key: loadRelayConfig()?.key ?? newKey() })
startRelay()
send('stateChanged')
})
ipcMain.handle('relayDisconnect', () => {
relay?.stop()
relay = undefined
clearRelayConfig()
send('stateChanged')
})
ipcMain.handle('syncStatus', () => syncStatus())
ipcMain.handle('pollNow', () => Promise.all([sync?.pollNow(), gvSync?.pollNow()]))
ipcMain.handle('pollNow', () => Promise.all([sync?.pollNow(), gvSync?.pollNow(), relay?.syncNow()]))
ipcMain.handle('openDataFolder', () => shell.openPath(getDataDir()))
ipcMain.handle('snapshotNow', () => db.snapshot(snapshotDir()))
ipcMain.handle('chooseDataDir', async () => {
@@ -679,7 +744,12 @@ app.whenReady().then(async () => {
db = new MessageDb(path.join(getDataDir(), 'messages.db'))
if (SANDBOX) seedDemo(db)
dids = JSON.parse(db.get('dids') ?? '[]') as Did[]
media = new MediaStore(path.join(getDataDir(), 'media'), db, (url) => {
media = new MediaStore(path.join(getDataDir(), 'media'), db, (url, at) => {
if (url.startsWith('mmspart:')) {
// On the Rabbit: ask for it; it shows up after the Rabbit's next check.
if (at) relay?.want(at.id, at.index, url)
return Promise.reject(new Error('on the Rabbit'))
}
if (!url.startsWith('gv:')) return net.fetch(url)
if (!gv) return Promise.reject(new Error('Google Voice is not connected'))
return gv.downloadMedia(url.slice(3))
@@ -698,6 +768,7 @@ app.whenReady().then(async () => {
const creds = loadCredentials()
if (creds) startSync(creds)
if (gvNumber() && (await hasGvSession())) startGvSync()
startRelay()
void dailySnapshot()
setInterval(() => void dailySnapshot(), 60 * 60 * 1000)
@@ -710,6 +781,7 @@ app.on('before-quit', (e) => {
quitting = true
sync?.stop()
stopGv()
relay?.stop()
contacts?.close()
db.snapshot(snapshotDir())
.catch((err) => console.error('[snapshot] on quit failed:', err))
+2 -2
View File
@@ -16,7 +16,7 @@ export class MediaStore {
readonly dir: string,
private db: MessageDb,
// How to fetch a MediaRef.url; Google Voice's gv:<id> refs need its session.
private fetchUrl: (url: string) => Promise<Response> = (url) => net.fetch(url)
private fetchUrl: (url: string, at?: { id: number; index: number }) => Promise<Response> = (url) => net.fetch(url)
) {}
filePath(file: string): string {
@@ -49,7 +49,7 @@ export class MediaStore {
private async download(id: number, index: number, url: string): Promise<MediaRef | undefined> {
try {
const res = await this.fetchUrl(url)
const res = await this.fetchUrl(url, { id, index })
const header = res.headers.get('content-type')?.split(';')[0].trim()
// voip.ms answers expired/unknown media with a 200 HTML page.
if (!res.ok || header === 'text/html') return undefined
+4
View File
@@ -22,6 +22,8 @@ export interface RabbitConfig {
gvoice: { number: string; cookies: Record<string, string> } | null
dids: Array<{ did: string; description: string }>
defaultDid?: string
// The relay through jai (relay.ts): address, token, and the key that seals everything.
relay?: { url: string; token: string; key: string } | null
}
export interface RabbitTarget {
@@ -146,6 +148,8 @@ async function historyCopy(db: MessageDb): Promise<string> {
copy.exec(`
DROP TRIGGER IF EXISTS messages_ai; DROP TRIGGER IF EXISTS messages_ad; DROP TRIGGER IF EXISTS messages_au;
DROP TABLE IF EXISTS messages_fts;
DROP TABLE IF EXISTS relay_outbox;
DELETE FROM kv WHERE key LIKE 'relay%';
`)
copy.pragma('journal_mode = DELETE')
copy.exec('VACUUM')
+316
View File
@@ -0,0 +1,316 @@
// The relay through jai: the Rabbit's SIM texts reach this computer, texts
// typed here go out over the Rabbit's SIM, and chat names and read/unread/
// archived stay in step between the two, all without a USB cable.
//
// jai only ever holds sealed blobs: AES-256-GCM with a key made here and handed
// to the Rabbit over USB (Install/Sync to Rabbit puts it in the app's private
// config.json). The bearer token only lets a device talk to the mailbox; the
// key is what keeps jai (and anyone else on it) from reading or forging
// anything. Server: server/bubble_relay.py. Android side: Relay.java.
//
// What travels (inside the seal; `t` is the kind):
// rabbit → desktop msgs {msgs: NewMessage[] (did 'sim'), media inline as {data} or lazy {url: 'mmspart:N'}}
// sent {id, msgId | null, error | null} a send from here, done
// media {id, index, mime, data} | {.., missing: true}
// desktop → rabbit send {id (uuid), ts, contact, text, media: [{mime, data, width, height}]}
// want {items: [{id, index, url}]} a lazy photo, please
// both ways state {names, read} merged newest-wins (db.namesImport/readStateImport)
import { app, safeStorage } from 'electron'
import crypto from 'node:crypto'
import { EventEmitter } from 'node:events'
import fs from 'node:fs'
import path from 'node:path'
import type { RelayStatus } from '@shared/api'
import { SIM_DID } from '@shared/phone'
import type { MediaRef } from '@shared/types'
import type { MessageDb, NameChange, NewMessage, ReadState } from './db'
import type { MediaStore } from './media'
export { SIM_DID } from '@shared/phone'
export const DEFAULT_URL = 'http://100.69.139.40:8080'
export interface RelayConfig {
url: string
token: string
key: string // base64, 32 bytes
}
// ---- the config, encrypted with the system keyring like the voip.ms login ----
const file = (): string => path.join(app.getPath('userData'), 'relay.bin')
export function loadRelayConfig(): RelayConfig | undefined {
try {
return JSON.parse(safeStorage.decryptString(fs.readFileSync(file()))) as RelayConfig
} catch {
return undefined
}
}
export function saveRelayConfig(c: RelayConfig): void {
if (!safeStorage.isEncryptionAvailable()) throw new Error('No system keyring available to keep the relay key safe.')
fs.mkdirSync(path.dirname(file()), { recursive: true })
fs.writeFileSync(file(), safeStorage.encryptString(JSON.stringify(c)), { mode: 0o600 })
}
export function clearRelayConfig(): void {
fs.rmSync(file(), { force: true })
}
export function newKey(): string {
return crypto.randomBytes(32).toString('base64')
}
// Plain http is fine only because tailscale encrypts the hop and the payload is
// sealed anyway; still, don't send the token just anywhere.
export function checkUrl(url: string): string {
const u = new URL(url.trim())
if (u.protocol !== 'http:' && u.protocol !== 'https:') throw new Error('The relay address should start with http:// or https://')
const host = u.hostname
const tailnet = /^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.\d+\.\d+$/.test(host) || host.endsWith('.ts.net')
const local = host === '127.0.0.1' || host === 'localhost'
if (u.protocol === 'http:' && !tailnet && !local) throw new Error('Plain http only to a tailscale (100.x) address; use https otherwise.')
return u.origin
}
// ---- sealing ----
type Device = 'desktop' | 'rabbit'
const aad = (author: Device): Buffer => Buffer.from(`bubble-relay/1/${author}`)
export function seal(key: Buffer, author: Device, payload: unknown): string {
const nonce = crypto.randomBytes(12)
const c = crypto.createCipheriv('aes-256-gcm', key, nonce)
c.setAAD(aad(author))
const ct = Buffer.concat([c.update(JSON.stringify(payload), 'utf8'), c.final()])
return Buffer.concat([Buffer.from([1]), nonce, ct, c.getAuthTag()]).toString('base64')
}
// Undefined for anything not sealed with our key by `author` (tampered, foreign, garbage).
export function unseal(key: Buffer, author: Device, blob: string): unknown {
try {
const b = Buffer.from(blob, 'base64')
if (b.length < 1 + 12 + 16 || b[0] !== 1) return undefined
const d = crypto.createDecipheriv('aes-256-gcm', key, b.subarray(1, 13))
d.setAAD(aad(author))
d.setAuthTag(b.subarray(b.length - 16))
return JSON.parse(Buffer.concat([d.update(b.subarray(13, b.length - 16)), d.final()]).toString('utf8'))
} catch {
return undefined
}
}
// ---- payloads ----
interface RelayMedia {
url?: string
mime?: string
data?: string
width?: number
height?: number
}
type Incoming =
| { t: 'msgs'; msgs: Array<NewMessage & { media?: RelayMedia[] }> }
| { t: 'sent'; id: string; msgId: number | null; error: string | null }
| { t: 'media'; id: number; index: number; mime?: string; data?: string; width?: number; height?: number; missing?: boolean }
| { t: 'state'; names: NameChange[]; read: ReadState[] }
const MAX_PUSH_BYTES = 8 * 1024 * 1024
const isInt = (n: unknown): n is number => typeof n === 'number' && Number.isSafeInteger(n)
export class RelayEngine extends EventEmitter {
private key: Buffer
private timer?: NodeJS.Timeout
private kickTimer?: NodeJS.Timeout
private running = false
private syncing?: Promise<void>
private stateDirty = false
private wanted = new Set<string>()
private lastSync?: number
private error?: string
constructor(
private db: MessageDb,
private media: MediaStore,
private cfg: RelayConfig,
private pollSeconds: number
) {
super()
this.key = Buffer.from(cfg.key, 'base64')
if (this.key.length !== 32) throw new Error('bad relay key')
db.db.exec('CREATE TABLE IF NOT EXISTS relay_outbox (seq INTEGER PRIMARY KEY AUTOINCREMENT, blob TEXT NOT NULL)')
}
start(): void {
if (this.running) return
this.running = true
// The Rabbit should know where things stand here as soon as it can.
this.stateDirty = true
this.loop(1000)
}
stop(): void {
this.running = false
clearTimeout(this.timer)
clearTimeout(this.kickTimer)
}
status(): RelayStatus {
const waiting = (this.db.db.prepare(`SELECT COUNT(*) AS n FROM kv WHERE key LIKE 'relay_send:%'`).get() as { n: number }).n
return { connected: true, url: this.cfg.url, lastSync: this.lastSync, error: this.error, waiting }
}
private loop(delay: number): void {
clearTimeout(this.timer)
if (!this.running) return
this.timer = setTimeout(() => void this.syncNow().finally(() => this.loop(Math.max(15, this.pollSeconds) * 1000)), delay)
}
// Something to send: go soon, but gather a burst of changes into one call.
kick(): void {
clearTimeout(this.kickTimer)
this.kickTimer = setTimeout(() => void this.syncNow(), 1500)
}
syncNow(): Promise<void> {
this.syncing ??= this.syncLoop().finally(() => (this.syncing = undefined))
return this.syncing
}
// Names or read/unread/archived changed here.
stateChanged(): void {
this.stateDirty = true
this.kick()
}
private queue(payload: unknown): void {
this.db.db.prepare('INSERT INTO relay_outbox (blob) VALUES (?)').run(seal(this.key, 'desktop', payload))
}
// A pending message (already in the db as `tempId`) to go out over the Rabbit's SIM.
send(tempId: number, contact: string, text: string, refs: MediaRef[]): void {
const id = crypto.randomUUID()
const media = refs.map((r) => ({
mime: r.mime ?? 'application/octet-stream',
data: fs.readFileSync(this.media.filePath(r.file!)).toString('base64'),
...(r.width ? { width: r.width, height: r.height } : {})
}))
this.db.set(`relay_send:${id}`, String(tempId))
this.queue({ t: 'send', id, ts: Date.now(), contact, text, media })
this.kick()
}
// A photo the Rabbit has but we don't (history comes over without them).
want(id: number, index: number, url: string): void {
const k = `${id}/${index}`
if (this.wanted.has(k)) return
this.wanted.add(k)
this.queue({ t: 'want', items: [{ id, index, url }] })
this.kick()
}
private async syncLoop(): Promise<void> {
try {
if (this.stateDirty) {
this.stateDirty = false
this.queue({ t: 'state', names: this.db.namesExport(), read: this.db.readStateExport() })
}
for (let round = 0; round < 50 && this.running; round++) {
const rows = this.db.db.prepare('SELECT seq, blob FROM relay_outbox ORDER BY seq').all() as Array<{ seq: number; blob: string }>
const push: typeof rows = []
let size = 0
for (const r of rows) {
if (push.length && size + r.blob.length > MAX_PUSH_BYTES) break
push.push(r)
size += r.blob.length
}
const since = Number(this.db.get('relay_cursor') ?? 0)
const res = await fetch(`${this.cfg.url}/sync`, {
method: 'POST',
headers: { Authorization: `Bearer ${this.cfg.token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ device: 'desktop', since, push: push.map((r) => r.blob) }),
signal: AbortSignal.timeout(120_000)
})
if (res.status === 401) throw new Error('jai turned down the token. Check it in Settings → Rabbit.')
if (!res.ok) throw new Error(`jai answered ${res.status}`)
const body = (await res.json()) as { cursor: number; events: string[]; more: boolean }
// Only now that jai has them: out of the outbox.
if (push.length) this.db.db.prepare('DELETE FROM relay_outbox WHERE seq <= ?').run(push.at(-1)!.seq)
for (const blob of body.events) this.handle(unseal(this.key, 'rabbit', blob) as Incoming | undefined)
this.db.set('relay_cursor', String(body.cursor))
if (!body.more && rows.length === push.length) break
}
this.lastSync = Date.now()
this.error = undefined
} catch (e) {
// Can't reach jai: an ordinary, quiet outcome; try again next round.
this.error = (e as Error).name === 'TimeoutError' ? "jai didn't answer" : (e as Error).message
}
this.emit('status', this.status())
}
private handle(p: Incoming | undefined): void {
if (!p || typeof p !== 'object') return
try {
if (p.t === 'msgs' && Array.isArray(p.msgs)) this.onMsgs(p.msgs)
else if (p.t === 'sent') this.onSent(p)
else if (p.t === 'media') this.onMedia(p)
else if (p.t === 'state') {
const n = this.db.namesImport(Array.isArray(p.names) ? p.names : []) + this.db.readStateImport(Array.isArray(p.read) ? p.read : [])
if (n) this.emit('changed', 'state')
}
} catch (e) {
console.warn('[relay] skipped a message from the Rabbit:', (e as Error).message)
}
}
private onMsgs(raw: Array<NewMessage & { media?: RelayMedia[] }>): void {
const msgs: NewMessage[] = []
for (const m of raw) {
// Only the SIM's own conversations come this way, whatever a message claims.
if (!isInt(m.id) || m.did !== SIM_DID || typeof m.contact !== 'string' || !m.contact || !isInt(m.ts)) continue
if (m.direction !== 'in' && m.direction !== 'out') continue
const media: MediaRef[] = []
for (const r of Array.isArray(m.media) ? m.media : []) {
const mime = typeof r.mime === 'string' ? r.mime : 'application/octet-stream'
if (typeof r.data === 'string') {
const ref: MediaRef = { file: this.media.save(Buffer.from(r.data, 'base64'), mime), mime }
if (isInt(r.width) && isInt(r.height)) Object.assign(ref, { width: r.width, height: r.height })
media.push(ref)
} else if (typeof r.url === 'string' && r.url.startsWith('mmspart:')) media.push({ url: r.url, mime })
}
msgs.push({
id: m.id,
did: SIM_DID,
contact: m.contact,
direction: m.direction,
body: typeof m.body === 'string' ? m.body : '',
ts: m.ts,
kind: m.kind === 'mms' ? 'mms' : 'sms',
...(media.length ? { media } : {}),
...(typeof m.sender === 'string' ? { sender: m.sender } : {})
})
}
const added = this.db.upsertMessages(msgs)
if (added.length) this.emit('messages', added)
}
private onSent(p: { id: string; msgId: number | null; error: string | null }): void {
const key = `relay_send:${p.id}`
const temp = this.db.get(key)
if (temp === undefined || temp === null) return
const tempId = Number(temp)
if (isInt(p.msgId)) this.db.confirmPending(tempId, p.msgId)
else this.db.markFailed(tempId, typeof p.error === 'string' ? `Rabbit: ${p.error}` : "The Rabbit couldn't send it.")
this.db.db.prepare('DELETE FROM kv WHERE key = ?').run(key)
this.emit('changed', 'sent')
}
private onMedia(p: { id: number; index: number; mime?: string; data?: string; width?: number; height?: number; missing?: boolean }): void {
if (!isInt(p.id) || !isInt(p.index)) return
this.wanted.delete(`${p.id}/${p.index}`)
if (typeof p.data !== 'string') return
const mime = typeof p.mime === 'string' ? p.mime : 'application/octet-stream'
const patch: MediaRef = { file: this.media.save(Buffer.from(p.data, 'base64'), mime), mime }
if (isInt(p.width) && isInt(p.height)) Object.assign(patch, { width: p.width, height: p.height })
this.db.setMediaFile(p.id, p.index, patch)
this.emit('changed', 'media')
}
}
+4
View File
@@ -22,6 +22,10 @@ const api: Api = {
rabbitInstall: (mode) => ipcRenderer.invoke('rabbitInstall', mode),
rabbitSync: () => ipcRenderer.invoke('rabbitSync'),
onRabbitProgress: (cb) => on('rabbitProgress', cb),
relayStatus: () => ipcRenderer.invoke('relayStatus'),
relayConnect: (url, token) => ipcRenderer.invoke('relayConnect', url, token),
relayDisconnect: () => ipcRenderer.invoke('relayDisconnect'),
onRelayStatus: (cb) => on('relayStatus', cb),
listContacts: () => ipcRenderer.invoke('listContacts'),
listAddressBooks: () => ipcRenderer.invoke('listAddressBooks'),
+1 -1
View File
@@ -72,7 +72,7 @@ export function App() {
const all = state?.dids.filter((d) => d.smsEnabled) ?? []
const chosen = state?.settings.dids ?? []
// The sync list is voip.ms's; Google Voice is on whenever it's connected.
return chosen.length ? all.filter((d) => d.provider === 'gvoice' || chosen.includes(d.did)) : all
return chosen.length ? all.filter((d) => d.provider === 'gvoice' || d.provider === 'sim' || chosen.includes(d.did)) : all
}, [state])
const open = useCallback((key: ConvKey, jumpTo?: number) => {
+2 -2
View File
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Virtuoso, type VirtuosoHandle } from 'react-virtuoso'
import { formatNumber, groupMembers, isGroup } from '@shared/phone'
import { formatNumber, groupMembers, isGroup, didName } from '@shared/phone'
import type { ContactInfo, Conversation, Did, Message, SearchHit } from '@shared/types'
import type { ConvKey, Jump } from '../App'
import { chatsWith, convName, dayLabel, sameDay, senderName, senderShortName } from '../format'
@@ -70,7 +70,7 @@ export function ChatView({ conv, contact, contactMap, conversations, onOpen, nam
if (!chats.length) return [{ label: `No other chats with ${senderShortName(number, contactMap)}`, onSelect: () => undefined, disabled: true }]
return chats.map((c) => ({
label:
(isGroup(c.contact) ? convName(c.contact, contactMap, c.name) : `${senderName(c.contact, contactMap)} (just you two, from ${formatNumber(c.did)})`) +
(isGroup(c.contact) ? convName(c.contact, contactMap, c.name) : `${senderName(c.contact, contactMap)} (just you two, from ${didName(c.did)})`) +
(c.archived ? ' — archived' : ''),
onSelect: () => onOpen({ did: c.did, contact: c.contact })
}))
+5 -5
View File
@@ -1,5 +1,5 @@
import { useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, type ChangeEvent, type Ref } from 'react'
import { charCount, formatNumber, isGroup, SMS_TEXT_LIMIT } from '@shared/phone'
import { charCount, formatNumber, isGroup, SMS_TEXT_LIMIT, didName, groupsFrom, SIM_DID } from '@shared/phone'
import type { Did } from '@shared/types'
import type { ConvKey } from '../App'
import { emojiQuery, exactEmoji, searchEmoji } from '../emoji'
@@ -37,8 +37,8 @@ export function Composer({ conv, isNew, smsDids, gvNumber, onChangeDid, ref }: P
const key = conv.contact
// Google Voice: no SMS/MMS split to count toward, and photos only.
const gv = conv.did === gvNumber
// A group thread lives on Google Voice; it can't switch numbers.
const fromDids = isGroup(conv.contact) ? smsDids.filter((d) => d.did === gvNumber) : smsDids
// A group lives on Google Voice or the Rabbit's SIM; only those can carry it.
const fromDids = isGroup(conv.contact) ? smsDids.filter((d) => groupsFrom(d.did, gvNumber)) : smsDids
const [text, setText] = useState(() => drafts.get(key) ?? '')
const [attachments, setAttachments] = useState<Attachment[]>(() => attachmentDrafts.get(key) ?? [])
const [preparing, setPreparing] = useState(0)
@@ -187,8 +187,8 @@ export function Composer({ conv, isNew, smsDids, gvNumber, onChangeDid, ref }: P
<select value={conv.did} onChange={(e) => onChangeDid(e.target.value)}>
{fromDids.map((d) => (
<option key={d.did} value={d.did}>
{formatNumber(d.did)}
{d.provider === 'gvoice' ? ' — Google Voice' : d.description ? ` — ${d.description}` : ''}
{didName(d.did)}
{d.provider === 'gvoice' ? ' — Google Voice' : d.provider !== 'sim' && d.description ? ` — ${d.description}` : ''}
</option>
))}
</select>
@@ -42,6 +42,8 @@ function Attachment({ id, index, media, onOpen, onMenu, onLoad }: { id: number;
const [broken, setBroken] = useState(false)
const src = mediaSrc(id, index)
const kind = mediaKind(media)
// Still on the Rabbit (relayed history): asked for, it arrives after the Rabbit's next check.
if (broken && media.url?.startsWith('mmspart:') && !media.file) return <div className="media-missing">On the Rabbit — it'll appear after its next check</div>
if (broken) return <div className="media-missing">Media unavailable</div>
if (kind === 'image')
return (
@@ -107,7 +109,7 @@ export const MessageBubble = memo(function MessageBubble({ m, sender, onSenderMe
{media.length > 0 && (
<div className="media">
{media.map((ref, i) => (
<Attachment key={i} id={m.id} index={i} media={ref} onOpen={() => onOpenMedia(m.id, i)} onMenu={(e) => onMediaMenu(m.id, i, e)} onLoad={onMediaLoad} />
<Attachment key={`${i}:${ref.file ?? ''}`} id={m.id} index={i} media={ref} onOpen={() => onOpenMedia(m.id, i)} onMenu={(e) => onMediaMenu(m.id, i, e)} onLoad={onMediaLoad} />
))}
</div>
)}
+13 -11
View File
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import { formatNumber, groupKey, normalizeNumber } from '@shared/phone'
import { formatNumber, groupKey, normalizeNumber, didName, groupsFrom, SIM_DID } from '@shared/phone'
import type { ContactInfo, Conversation, Did } from '@shared/types'
import type { ConvKey } from '../App'
import { Avatar } from './Avatar'
@@ -26,7 +26,9 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
const [members, setMembers] = useState<string[]>([])
const inputRef = useRef<HTMLInputElement>(null)
const listRef = useRef<HTMLDivElement>(null)
const canGroup = !!from && from === gvNumber
const canGroup = groupsFrom(from, gvNumber)
// Where a new group goes from: Google Voice if it's connected, else the Rabbit's SIM.
const groupDid = gvNumber ?? smsDids.find((d) => d.did === SIM_DID)?.did
useEffect(() => inputRef.current?.focus(), [])
@@ -66,10 +68,10 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
onPick({ did: existing?.did ?? from, contact })
}
// Groups only exist on Google Voice, so starting one sends from there.
// Groups go from Google Voice or the Rabbit's SIM (voip.ms has none).
const startGroup = (): void => {
if (!gvNumber) return
setFrom(gvNumber)
if (!groupDid) return
if (!groupsFrom(from, gvNumber)) setFrom(groupDid)
setFromChosen(true)
setGrouping(true)
setQ('')
@@ -111,7 +113,7 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
onChange={(e) => {
setFrom(e.target.value)
setFromChosen(true)
if (e.target.value !== gvNumber) {
if (!groupsFrom(e.target.value, gvNumber)) {
setMembers([])
setGrouping(false)
}
@@ -119,8 +121,8 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
>
{smsDids.map((d) => (
<option key={d.did} value={d.did}>
{formatNumber(d.did)}
{d.provider === 'gvoice' ? ' — Google Voice' : d.description ? ` — ${d.description}` : ''}
{didName(d.did)}
{d.provider === 'gvoice' ? ' — Google Voice' : d.provider !== 'sim' && d.description ? ` — ${d.description}` : ''}
</option>
))}
</select>
@@ -164,8 +166,8 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
{!grouping && !q.trim() && (
<button
className="row group-row"
disabled={!gvNumber}
title={gvNumber ? undefined : 'Group chats need Google Voice — connect it in Settings.'}
disabled={!groupDid}
title={groupDid ? undefined : 'Group chats need Google Voice or the Rabbit relay — connect one in Settings.'}
onClick={startGroup}
>
<span className="group-row-icon">
@@ -176,7 +178,7 @@ export function NewChat({ contacts, conversations, smsDids, gvNumber, defaultDid
<span className="row-name">Group chat</span>
</div>
<div className="row-bottom">
<span className="row-preview">{gvNumber ? 'Pick several people, then Done' : 'Needs Google Voice (Settings)'}</span>
<span className="row-preview">{groupDid ? 'Pick several people, then Done' : 'Needs Google Voice or the Rabbit relay (Settings)'}</span>
</div>
</div>
</button>
+103 -4
View File
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react'
import type { AppState, RabbitStatus } from '@shared/api'
import { formatNumber } from '@shared/phone'
import type { AppState, RabbitStatus, RelayStatus } from '@shared/api'
import { didName, formatNumber } from '@shared/phone'
import type { AddressBook, GvStatus, IconStyle, Settings, SyncStatus, Theme } from '@shared/types'
import { CloseIcon } from './Icons'
@@ -35,7 +35,7 @@ export function SettingsDialog({ state, sync, onClose, onChanged }: Props) {
}
const synced = s.dids.length ? s.dids : smsDids.map((d) => d.did)
const sendFrom = state.gvoiceNumber ? [...synced, state.gvoiceNumber] : synced
const sendFrom = [...synced, ...(state.gvoiceNumber ? [state.gvoiceNumber] : []), ...state.dids.filter((d) => d.provider === 'sim').map((d) => d.did)]
return (
<div className="modal-backdrop" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
@@ -129,6 +129,7 @@ export function SettingsDialog({ state, sync, onClose, onChanged }: Props) {
<ContactsSection value={s.contactBook} onChange={(contactBook) => setS({ ...s, contactBook })} />
<RabbitSection onChanged={onChanged} />
<RelaySection onChanged={onChanged} />
{smsDids.length > 0 && (
<section>
@@ -153,7 +154,7 @@ export function SettingsDialog({ state, sync, onClose, onChanged }: Props) {
<select value={s.defaultDid ?? sendFrom[0]} onChange={(e) => setS({ ...s, defaultDid: e.target.value })}>
{sendFrom.map((d) => (
<option key={d} value={d}>
{formatNumber(d)}
{didName(d)}
{d === state.gvoiceNumber ? ' — Google Voice' : ''}
</option>
))}
@@ -414,3 +415,101 @@ function RabbitSection({ onChanged }: { onChanged: () => void }) {
</section>
)
}
// The Rabbit's SIM (and names + read status) through jai, no cable needed.
function RelaySection({ onChanged }: { onChanged: () => void }) {
const [st, setSt] = useState<RelayStatus>()
const [url, setUrl] = useState('http://100.69.139.40:8080')
const [token, setToken] = useState('')
const [busy, setBusy] = useState(false)
const [error, setError] = useState<string>()
const [done, setDone] = useState<string>()
const refresh = (): void =>
void window.api.relayStatus().then((s) => {
setSt(s)
if (s.url) setUrl(s.url)
})
useEffect(() => {
refresh()
return window.api.onRelayStatus(setSt)
}, [])
const connect = (): void => {
setBusy(true)
setError(undefined)
setDone(undefined)
window.api
.relayConnect(url, token)
.then(() => {
setToken('')
setDone('Connected. Now use Sync to Rabbit (above) once, with the Rabbit plugged in, so it gets the key.')
refresh()
onChanged()
})
.catch((e: Error) => setError(cleanError(e)))
.finally(() => setBusy(false))
}
const disconnect = (): void => {
void window.api.relayDisconnect().then(() => {
setDone(undefined)
refresh()
onChanged()
})
}
const last = st?.lastSync ? new Date(st.lastSync).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }) : undefined
return (
<section>
<h3>Rabbit relay (jai)</h3>
<p className="muted small">
Texts the Rabbit's SIM card sends and gets show up here, you can text from it here (as "Rabbit SIM"), and chat names
and read/unread/archived stay in step with the Rabbit without plugging it in. It goes through jai, which only passes on
sealed messages it can't read: the key stays on this computer and the Rabbit. A text sent from here goes out at the
Rabbit's next background check (Settings there: how often), so it can take a while.
</p>
{st?.connected ? (
<>
<div className="kv">
<span>Relay</span>
<span>{st.url}</span>
</div>
<div className="kv">
<span>Last check</span>
<span>{st.error ? `⚠ ${st.error}` : last ?? 'Not yet'}</span>
</div>
{st.waiting > 0 && (
<div className="kv">
<span>Waiting for the Rabbit</span>
<span>
{st.waiting} text{st.waiting === 1 ? '' : 's'}
</span>
</div>
)}
<div className="btn-row">
<button onClick={disconnect}>Disconnect</button>
</div>
</>
) : (
<>
<label className="field">
Address
<input value={url} onChange={(e) => setUrl(e.target.value)} spellCheck={false} />
</label>
<label className="field">
Token
<input type="password" value={token} onChange={(e) => setToken(e.target.value)} placeholder="from scripts/deploy-jai.sh --token" autoComplete="off" />
</label>
<div className="btn-row">
<button className="primary" disabled={busy || !token.trim()} onClick={connect}>
Connect
</button>
</div>
</>
)}
{done && <p className="muted small">{done}</p>}
{error && <p className="form-error">{error}</p>}
</section>
)
}
+7 -1
View File
@@ -1,7 +1,13 @@
import { formatNumber } from '@shared/phone'
import { formatNumber, SIM_DID } from '@shared/phone'
// "via <number>", with a GV tag when it's the Google Voice number.
export function Via({ did, gvNumber }: { did: string; gvNumber?: string }) {
if (did === SIM_DID)
return (
<>
via <span className="gv-tag">SIM</span>Rabbit
</>
)
return (
<>
via {did === gvNumber && <span className="gv-tag">GV</span>}
+12
View File
@@ -17,6 +17,14 @@ export interface RabbitStatus {
device?: { serial: string; model: string; installedVersion?: string }
}
export interface RelayStatus {
connected: boolean
url?: string
lastSync?: number
error?: string
waiting: number // texts sent here via the Rabbit's SIM, not yet done
}
export interface Api {
getState(): Promise<AppState>
testCredentials(creds: Credentials): Promise<Did[]>
@@ -32,6 +40,10 @@ export interface Api {
rabbitStatus(): Promise<RabbitStatus>
rabbitInstall(mode?: 'full' | 'update'): Promise<void> // update: app only, keeps its data
rabbitSync(): Promise<void>
relayStatus(): Promise<RelayStatus>
relayConnect(url: string, token: string): Promise<void>
relayDisconnect(): Promise<void>
onRelayStatus(cb: (s: RelayStatus) => void): () => void
onRabbitProgress(cb: (step: string) => void): () => void
listContacts(): Promise<ContactInfo[]>
+14
View File
@@ -8,6 +8,20 @@ export function normalizeNumber(raw: string): string {
return digits
}
// The Rabbit's SIM, reached through jai (main/relay.ts): a number we text
// from that isn't a phone number here.
export const SIM_DID = 'sim'
// A number to send from, as pickers and "via" show it.
export function didName(did: string): string {
return did === SIM_DID ? 'Rabbit SIM' : formatNumber(did)
}
// Group chats: Google Voice threads, or MMS over the Rabbit's SIM.
export function groupsFrom(did: string | undefined, gvNumber: string | undefined): boolean {
return !!did && (did === gvNumber || did === SIM_DID)
}
export function formatNumber(n: string): string {
const d = normalizeNumber(n)
if (d.length === 10) return `(${d.slice(0, 3)}) ${d.slice(3, 6)}-${d.slice(6)}`
+1 -1
View File
@@ -96,7 +96,7 @@ export interface SearchHit {
snippet: string // contains  /  markers around matched text
}
export type Provider = 'voipms' | 'gvoice'
export type Provider = 'voipms' | 'gvoice' | 'sim'
export interface Did {
did: string
+88
View File
@@ -0,0 +1,88 @@
// The relay against the dev app on a plugged-in Rabbit (DEVICE=1 npm test --
// test/relay.device.test.ts). A throwaway local relay, reached from the Rabbit
// through adb reverse; the dev app's made-up data (tools/push_sandbox.py).
// Never the real app, never jai.
import { execFileSync, spawn, type ChildProcess } from 'node:child_process'
import fs from 'node:fs'
import net from 'node:net'
import os from 'node:os'
import path from 'node:path'
import { afterAll, describe, expect, it, vi } from 'vitest'
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'relay-device-'))
vi.mock('electron', () => ({
app: { getPath: () => tmp },
safeStorage: { isEncryptionAvailable: () => true, encryptString: (s: string) => Buffer.from(s), decryptString: (b: Buffer) => b.toString() },
nativeImage: { createFromBuffer: () => ({ getSize: () => ({ width: 0, height: 0 }) }) },
net: { fetch: () => Promise.reject(new Error('no network in tests')) }
}))
const { MessageDb } = await import('../src/main/db')
const { MediaStore } = await import('../src/main/media')
const { newKey, RelayEngine } = await import('../src/main/relay')
const PKG = 'me.teafry.voipmessages.dev'
const DEVICE_PORT = 18080
let server: ChildProcess | undefined
afterAll(() => {
server?.kill()
try {
execFileSync('adb', ['reverse', '--remove', `tcp:${DEVICE_PORT}`])
} catch {
/* gone already */
}
})
const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms))
describe.skipIf(!process.env['DEVICE'])('relay with the dev app on the Rabbit', () => {
it('seals in Java and opens in Node, and back', { timeout: 240_000 }, async () => {
const port = await new Promise<number>((resolve) => {
const s = net.createServer().listen(0, '127.0.0.1', () => {
const p = (s.address() as net.AddressInfo).port
s.close(() => resolve(p))
})
})
const data = path.join(tmp, 'server')
server = spawn('python3', [path.join(__dirname, '../server/bubble_relay.py'), '--port', String(port), '--data', data], { stdio: 'ignore' })
const url = `http://127.0.0.1:${port}`
for (let i = 0; i < 50 && !(await fetch(`${url}/health`).then((r) => r.ok).catch(() => false)); i++) await sleep(100)
const token = fs.readFileSync(path.join(data, 'token'), 'utf8').trim()
const key = newKey()
execFileSync('adb', ['reverse', `tcp:${DEVICE_PORT}`, `tcp:${port}`])
execFileSync('python3', [path.join(__dirname, '../android/tools/push_sandbox.py'), '--relay', `http://127.0.0.1:${DEVICE_PORT}`], {
env: { ...process.env, RELAY_TOKEN: token, RELAY_KEY: key },
stdio: 'ignore'
})
const db = new MessageDb(path.join(tmp, 'messages.db'))
const media = new MediaStore(path.join(tmp, 'media'), db)
const engine = new RelayEngine(db, media, { url, token, key }, 30)
;(engine as unknown as { running: boolean }).running = true
db.setName('sim', '5415550150', 'Made-up SIM friend')
engine.stateChanged()
const tempId = db.insertPending({ did: 'sim', contact: '5415550150', direction: 'out', body: 'from the desk', ts: Date.now() })
engine.send(tempId, '5415550150', 'from the desk', [])
await engine.syncNow()
// Opening the app has it check the relay (a job a few seconds later).
execFileSync('adb', ['shell', 'am', 'start', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`])
let simIds: number[] = []
for (let i = 0; i < 40; i++) {
await sleep(4000)
await engine.syncNow()
simIds = db.getConversationMessages('sim', '5415550150').map((m) => m.id)
if (simIds.includes(2 ** 51 + 1) && db.getMessage(tempId)?.status === 'failed') break
// The send's answer comes on the Rabbit's next round: nudge it.
if (i % 5 === 4) execFileSync('adb', ['shell', 'am', 'start', '-S', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`])
}
// The made-up SIM history came over, decrypted.
expect(simIds).toEqual(expect.arrayContaining([2 ** 51 + 1, 2 ** 51 + 2]))
expect(db.getConversationMessages('sim', '5415550150').find((m) => m.id === 2 ** 51 + 1)?.body).toBe('Texting your SIM number now')
// The send was read and answered (the dev app isn't the default SMS app, so: a reason).
expect(db.getMessage(tempId)?.status).toBe('failed')
expect(db.getMessage(tempId)?.error).toMatch(/texting app/)
engine.stop()
db.close()
})
})
+151
View File
@@ -0,0 +1,151 @@
// The relay end to end: the real server (server/bubble_relay.py) on localhost,
// the desktop's RelayEngine on a scratch database, and the Rabbit's side played
// here with the same sealing. Made-up numbers and texts only.
import { spawn, type ChildProcess } from 'node:child_process'
import fs from 'node:fs'
import net from 'node:net'
import os from 'node:os'
import path from 'node:path'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'relay-test-'))
vi.mock('electron', () => ({
app: { getPath: () => tmp },
safeStorage: { isEncryptionAvailable: () => true, encryptString: (s: string) => Buffer.from(s), decryptString: (b: Buffer) => b.toString() },
nativeImage: { createFromBuffer: () => ({ getSize: () => ({ width: 0, height: 0 }) }) },
net: { fetch: () => Promise.reject(new Error('no network in tests')) }
}))
const { MessageDb } = await import('../src/main/db')
const { MediaStore } = await import('../src/main/media')
const { checkUrl, newKey, RelayEngine, seal, unseal } = await import('../src/main/relay')
let server: ChildProcess
let url = ''
let token = ''
async function freePort(): Promise<number> {
return new Promise((resolve) => {
const s = net.createServer().listen(0, '127.0.0.1', () => {
const p = (s.address() as net.AddressInfo).port
s.close(() => resolve(p))
})
})
}
beforeAll(async () => {
const port = await freePort()
const data = path.join(tmp, 'server')
server = spawn('python3', [path.join(__dirname, '../server/bubble_relay.py'), '--port', String(port), '--data', data], { stdio: 'ignore' })
url = `http://127.0.0.1:${port}`
for (let i = 0; i < 50; i++) {
const ok = await fetch(`${url}/health`).then((r) => r.ok).catch(() => false)
if (ok) break
await new Promise((r) => setTimeout(r, 100))
}
token = fs.readFileSync(path.join(data, 'token'), 'utf8').trim()
})
afterAll(() => server?.kill())
// The Rabbit's half: one sync call with its sealed posts; returns what it got.
let rabbitCursor = 0
async function rabbit(key: Buffer, push: unknown[] = [], raw: string[] = []): Promise<Array<Record<string, unknown>>> {
const res = await fetch(`${url}/sync`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body: JSON.stringify({ device: 'rabbit', since: rabbitCursor, push: [...push.map((p) => seal(key, 'rabbit', p)), ...raw] })
})
const body = (await res.json()) as { cursor: number; events: string[] }
rabbitCursor = body.cursor
return body.events.map((b) => unseal(key, 'desktop', b) as Record<string, unknown>)
}
describe('relay', () => {
it('seals so that only the right key and author open it', () => {
const k = Buffer.from(newKey(), 'base64')
const blob = seal(k, 'desktop', { t: 'x', n: 1 })
expect(unseal(k, 'desktop', blob)).toEqual({ t: 'x', n: 1 })
expect(unseal(k, 'rabbit', blob)).toBeUndefined() // a reflected blob isn't the other side's
expect(unseal(Buffer.from(newKey(), 'base64'), 'desktop', blob)).toBeUndefined()
const b = Buffer.from(blob, 'base64')
b[20] ^= 1
expect(unseal(k, 'desktop', b.toString('base64'))).toBeUndefined()
})
it('only sends the token to tailscale or https', () => {
expect(checkUrl('http://100.69.139.40:8080/')).toBe('http://100.69.139.40:8080')
expect(() => checkUrl('http://example.com')).toThrow()
expect(checkUrl('https://example.com')).toBe('https://example.com')
})
it('carries texts, photos, sends and chat names both ways', async () => {
const db = new MessageDb(path.join(tmp, 'messages.db'))
const media = new MediaStore(path.join(tmp, 'media'), db, (u, at) => {
if (u.startsWith('mmspart:') && at) engine.want(at.id, at.index, u)
return Promise.reject(new Error('on the Rabbit'))
})
const cfg = { url, token, key: newKey() }
const key = Buffer.from(cfg.key, 'base64')
const engine = new RelayEngine(db, media, cfg, 30)
const got: number[] = []
engine.on('messages', (m: Array<{ id: number }>) => got.push(...m.map((x) => x.id)))
;(engine as unknown as { running: boolean }).running = true
// Desktop → Rabbit: a text and a photo over the SIM; the state goes too.
engine.stateChanged()
const photo = media.save(Buffer.from('fake jpeg'), 'image/jpeg')
const tempId = db.insertPending({ did: 'sim', contact: '5415550101', direction: 'out', body: 'hi from the desk', ts: Date.now(), kind: 'mms', media: [{ file: photo, mime: 'image/jpeg' }] })
engine.send(tempId, '5415550101', 'hi from the desk', [{ file: photo, mime: 'image/jpeg' }])
await engine.syncNow()
const toRabbit = await rabbit(key)
const sendReq = toRabbit.find((p) => p.t === 'send')!
expect(sendReq).toMatchObject({ contact: '5415550101', text: 'hi from the desk' })
expect(Buffer.from((sendReq.media as Array<{ data: string }>)[0].data, 'base64').toString()).toBe('fake jpeg')
expect(toRabbit.some((p) => p.t === 'state')).toBe(true)
// Rabbit → desktop: it went out (as message 2^51+7), a text came in, an old photo stays on the Rabbit, a group got a name.
const sentId = 2 ** 51 + 7
const inId = 2 ** 51 + 8
const oldMms = 2 ** 51 + 2 ** 49 + 3
await rabbit(
key,
[
{ t: 'sent', id: sendReq.id, msgId: sentId, error: null },
{
t: 'msgs',
msgs: [
{ id: sentId, did: 'sim', contact: '5415550101', direction: 'out', body: 'hi from the desk', ts: Date.now(), kind: 'mms' },
{ id: inId, did: 'sim', contact: '5415550101', direction: 'in', body: 'hey!', ts: Date.now() + 1, kind: 'sms' },
{ id: oldMms, did: 'sim', contact: '5415550102', direction: 'in', body: '', ts: Date.now() - 86_400_000, kind: 'mms', media: [{ url: 'mmspart:44', mime: 'image/png' }] },
{ id: 99, did: '5415550100', contact: '5415550103', direction: 'in', body: 'not a SIM message', ts: Date.now() }
]
},
{ t: 'state', names: [{ did: 'sim', contact: '5415550101,5415550102', name: 'Made-up group', ts: Date.now() }], read: [] }
],
['AAAA'] // garbage from a stranger: ignored
)
await engine.syncNow()
expect(db.getMessage(tempId)).toBeUndefined()
expect(db.getMessage(sentId)?.status).toBe('delivered')
expect(db.getMessage(sentId)?.media?.[0].file).toBe(photo) // our copy kept
expect(db.getConversationMessages('sim', '5415550101').map((m) => m.id)).toEqual([sentId, inId])
expect(db.getMessage(99)).toBeUndefined() // only did 'sim' comes this way
expect(got).toContain(inId)
expect(db.getName('sim', '5415550101,5415550102')).toBe('Made-up group')
// The old photo: asked for when shown, delivered by the Rabbit's next check.
expect(await media.resolve(oldMms, 0)).toBeUndefined()
await engine.syncNow()
const want = (await rabbit(key)).find((p) => p.t === 'want')!
expect(want.items).toEqual([{ id: oldMms, index: 0, url: 'mmspart:44' }])
await rabbit(key, [{ t: 'media', id: oldMms, index: 0, mime: 'image/png', data: Buffer.from('fake png').toString('base64') }])
await engine.syncNow()
const ref = await media.resolve(oldMms, 0)
expect(fs.readFileSync(media.filePath(ref!.file!)).toString()).toBe('fake png')
// Picked up = gone from jai.
expect(await rabbit(key)).toEqual([])
engine.stop()
db.close()
})
})