Files
bubble/android/app/src/main/java/me/teafry/voipmessages/Config.java
T
trav 950b8ce067 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.
2026-11-06 17:35:00 -08:00

189 lines
6.9 KiB
Java

package me.teafry.voipmessages;
import android.content.Context;
import android.content.SharedPreferences;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Accounts, copied over by the desktop's "Install to Rabbit" into the app's
* private files dir (files/config.json, which no other app can read):
*
* {"voipms": {"username", "password"} | null,
* "gvoice": {"number", "cookies": {name: value}} | null,
* "dids": [{"did", "description"}], "defaultDid": "..."}
*
* Google rotates some cookies as we use them; those go back into the file.
*/
final class Config {
static final class Did {
final String did, description;
final boolean gv, sim;
Did(String did, String description, boolean gv) {
this(did, description, gv, false);
}
Did(String did, String description, boolean gv, boolean sim) {
this.did = did;
this.description = description;
this.gv = gv;
this.sim = sim;
}
}
String voipUser, voipPass;
String gvNumber;
final Map<String, String> gvCookies = new LinkedHashMap<>();
// Changes while screens read it: the SIM comes and goes with the default-SMS-app role.
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;
private JSONObject raw;
private Config(File file) {
this.file = file;
}
private static Config cached;
static synchronized Config get(Context c) {
File f = new File(c.getFilesDir(), "config.json");
if (cached == null || cached.stale()) cached = load(f);
cached.applySim(c);
return cached;
}
/** The SIM is a number to text from while bubble is the default SMS app (never in the sandbox). */
private void applySim(Context c) {
boolean sim = !sandbox && Sim.isDefault(c);
if (sim == hasSim) return;
hasSim = sim;
if (sim) {
dids.add(new Did(Sim.DID, "SIM card", false, true));
if (defaultDid == null) defaultDid = Sim.DID;
} else {
dids.removeIf(d -> d.sim);
if (Sim.DID.equals(defaultDid)) defaultDid = dids.isEmpty() ? null : dids.get(0).did;
}
}
private long loadedAt;
private boolean stale() {
return file.lastModified() > loadedAt;
}
private static Config load(File f) {
Config cfg = new Config(f);
cfg.loadedAt = f.lastModified();
try {
JSONObject o = new JSONObject(new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8));
cfg.raw = o;
JSONObject v = o.optJSONObject("voipms");
if (v != null) {
cfg.voipUser = v.optString("username", null);
cfg.voipPass = v.optString("password", null);
}
JSONObject g = o.optJSONObject("gvoice");
if (g != null) {
cfg.gvNumber = Phone.normalize(g.optString("number"));
JSONObject ck = g.optJSONObject("cookies");
if (ck != null)
for (Iterator<String> it = ck.keys(); it.hasNext(); ) {
String k = it.next();
cfg.gvCookies.put(k, ck.getString(k));
}
}
JSONArray ds = o.optJSONArray("dids");
if (ds != null)
for (int i = 0; i < ds.length(); i++) {
JSONObject d = ds.getJSONObject(i);
cfg.dids.add(new Did(Phone.normalize(d.getString("did")), d.optString("description"), false));
}
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();
}
if (cfg.defaultDid == null && !cfg.dids.isEmpty()) cfg.defaultDid = cfg.dids.get(0).did;
return cfg;
}
boolean configured() {
return voipUser != null || gvNumber != null || hasSim;
}
/** Group chats: Google Voice and the SIM (as MMS); voip.ms numbers can't. */
boolean canGroup(String did) {
return isGv(did) || Sim.isSim(did) && hasSim;
}
boolean isGv(String did) {
return gvNumber != null && gvNumber.equals(did);
}
/** Persist cookies Google rotated. */
synchronized void saveCookies() {
try {
JSONObject g = raw.optJSONObject("gvoice");
if (g == null) return;
JSONObject ck = new JSONObject();
for (Map.Entry<String, String> e : gvCookies.entrySet()) ck.put(e.getKey(), e.getValue());
g.put("cookies", ck);
File tmp = new File(file.getPath() + ".tmp");
try (FileOutputStream out = new FileOutputStream(tmp)) {
out.write(raw.toString().getBytes(StandardCharsets.UTF_8));
}
if (tmp.renameTo(file)) loadedAt = file.lastModified();
} catch (IOException | JSONException ignored) {
}
}
// ---- settings that belong to this device ----
static SharedPreferences prefs(Context c) {
return c.getSharedPreferences("settings", Context.MODE_PRIVATE);
}
/** Minutes between background checks; 0 = never (pull to refresh only). */
static int pollMinutes(Context c) {
return prefs(c).getInt("pollMinutes", 15);
}
static boolean notifications(Context c) {
return prefs(c).getBoolean("notifications", true);
}
}