Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
frigate-glance: trust Frigate's new certificate from Overview; the card says why it has no picture
NickLDaddb66dc50f5
diff --git a/addons/frigate-glance/README.md b/addons/frigate-glance/README.mdindex 1bafcc456..f63a9599f 100644--- a/addons/frigate-glance/README.md+++ b/addons/frigate-glance/README.md@@ -64,7 +64,7 @@ and `TestCardReceiver` shows a test card over whatever is playing.|---|---|---|| `mqtt` | required | `mqtt://user:password@host:1883`; percent-encode `@ : /` in the user or password || `frigateUrl` | required | `https://host:8971` (authenticated port) or `http://host:5000` |-| `frigateUser`, `frigatePassword` | empty | Frigate login; the certificate is trusted on first contact and pinned |+| `frigateUser`, `frigatePassword` | empty | Frigate login; the certificate is trusted on first contact and pinned. Frigate makes a new self-signed one whenever its container is recreated (an update) unless `/etc/letsencrypt` is a volume; Overview then shows **New certificate** with both fingerprints, and OK on the Frigate row trusts it || `topicPrefix` | `frigate` | Frigate's MQTT topic prefix || `cameras` | `{}` (all) | camera name to the name shown on the card; listed cameras only || `newCameras` | `false` | with `cameras` listed: cameras Frigate gains later open cards too |diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardText.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardText.javaindex f018af428..58e3f4ea7 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardText.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/CardText.java@@ -36,6 +36,14 @@ final class CardText {return m;}+ /** Shown over an empty picture when Frigate sends no frame: what went wrong, and where to fix it. */+ static String noPicture(boolean newCertificate, String error) {+ if (newCertificate) return "No picture: Frigate has a new certificate.\nTrust it in Frigate Glance › Overview › Frigate.";+ if (error == null || error.isEmpty()) return "No picture: Frigate not reached.";+ // An SSL or socket error arrives as "java.net.Foo: why"; the class name means nothing on a TV.+ return "No picture: Frigate not reached.\n" + error.replaceFirst("^([a-z]+\\.)+[A-Za-z]+(Exception|Error): ", "");+ }+static String duration(int minutes) {return minutes % 60 == 0 ? (minutes / 60) + "h" : minutes + " min";}diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/FrigateApi.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/FrigateApi.javaindex db9e2f234..48a7fc2dd 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/FrigateApi.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/FrigateApi.java@@ -30,7 +30,8 @@ import org.json.JSONObject;/*** Frigate's HTTP API, as Peek uses it: log in on the authenticated port (8971) for a frigate_token cookie,* read /api/config, fetch a camera's latest frame. Frigate's certificate is usually self-signed, so it is- * trusted on first contact and pinned (SHA-256 of the leaf certificate) from then on.+ * trusted on first contact and pinned (SHA-256 of the leaf certificate) from then on. Frigate makes itself a new+ * one when its container is recreated; that one is refused until it is trusted from the settings screen.*/public final class FrigateApi {public interface PinListener {@@ -42,6 +43,8 @@ public final class FrigateApi {private final String baseUrl, user, password;private final PinListener pinListener;private volatile String pin;+ /** A certificate seen in place of the pinned one, or "": what trustOffered() would pin. */+ private volatile String offered = "";private volatile String token;public FrigateApi(String baseUrl, String user, String password, String pinnedSha256, PinListener onFirstPin) {@@ -189,13 +192,7 @@ public final class FrigateApi {@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {if (chain == null || chain.length == 0) throw new CertificateException("no certificate");- String seen = sha256(chain[0].getEncoded());- if (pin.isEmpty()) {- pin = seen;- pinListener.onFirstPin(seen);- } else if (!pin.equals(seen)) {- throw new CertificateException("Frigate's certificate changed (pinned " + pin.substring(0, 12) + ")");- }+ checkLeaf(sha256(chain[0].getEncoded()));}@Override@@ -212,6 +209,39 @@ public final class FrigateApi {}}+ /** Trusts on first contact, then only the pinned certificate; a different one is kept as the offer. */+ void checkLeaf(String seen) throws CertificateException {+ if (pin.isEmpty()) {+ pin = seen;+ pinListener.onFirstPin(seen);+ } else if (!pin.equals(seen)) {+ offered = seen;+ throw new CertificateException("New certificate");+ } else {+ offered = "";+ }+ }++ /** The pinned certificate's SHA-256, or "" before first contact. */+ public String pin() {+ return pin;+ }++ /** The certificate Frigate presented in place of the pinned one, or "" when there is none. */+ public String offeredPin() {+ return offered;+ }++ /** Pins the certificate Frigate now presents; false when none was offered. */+ public boolean trustOffered() {+ String o = offered;+ if (o.isEmpty()) return false;+ pin = o;+ offered = "";+ pinListener.onFirstPin(o);+ return true;+ }+static String sha256(byte[] der) {try {byte[] d = MessageDigest.getInstance("SHA-256").digest(der);diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.javaindex 11463377e..fa1c74e06 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/GlanceService.java@@ -202,10 +202,23 @@ public class GlanceService extends Service {static final class Overview {boolean running, mqttUp, frigateUp;String mqttHost = "", frigateHost = "", mqttError = "", frigateError = "";+ /** SHA-256 of the pinned certificate and of a different one Frigate now presents ("" for none). */+ String pin = "", offeredPin = "";long mqttSince, frigateSince, lastMessageAt;int cameras;}+ /** Reads Frigate's config again, first trusting the certificate it now presents if asked; the Overview's Frigate row. */+ public static void checkFrigate(boolean trustNewCertificate) {+ GlanceService s = instance;+ FrigateApi f = s == null ? null : s.frigate;+ if (f == null) return;+ s.refetcher.execute(() -> {+ if (trustNewCertificate && f.trustOffered()) Log.i(TAG, "trusted Frigate's new certificate " + f.pin().substring(0, 12));+ s.fetchFrigateConfig();+ });+ }+static Overview overview() {Overview o = new Overview();GlanceService s = instance;@@ -219,6 +232,11 @@ public class GlanceService extends Service {o.mqttError = s.mqttError;o.frigateError = s.frigateError;o.cameras = s.cameraNames.size();+ FrigateApi f = s.frigate;+ if (f != null) {+ o.pin = f.pin();+ o.offeredPin = f.offeredPin();+ }GlanceConfig c = s.config;if (c != null) {o.mqttHost = hostPort(c.mqttUrl, 1883);@@ -401,6 +419,12 @@ public class GlanceService extends Service {GlanceConfig cfg = config;return cfg == null || !cfg.liveVideo ? null : GlanceService.liveUrl(camera);}++ @Override+ public boolean newCertificate() {+ FrigateApi f = frigate;+ return f != null && !f.offeredPin().isEmpty();+ }}, KeyService::setFiltering);} else {card.setConfig(c);diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/OverlayCard.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/OverlayCard.javaindex 128b8aa2d..0023c41bc 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/OverlayCard.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/OverlayCard.java@@ -40,6 +40,9 @@ final class OverlayCard implements CardView {/** RTSP address for live video, or null when live video is off. */String liveUrl(String camera);++ /** True while Frigate presents a certificate other than the trusted one. */+ boolean newCertificate();}/** Told when a card appears and leaves, so the key service filters keys only meanwhile. */@@ -63,6 +66,8 @@ final class OverlayCard implements CardView {private GlanceConfig config;private LinearLayout root;private FrameLayout picture;+ /** Over an empty picture: why there is no frame yet. */+ private TextView problem;private ImageView image;private TextureView video;private LiveVideo live;@@ -144,6 +149,7 @@ final class OverlayCard implements CardView {@Overridepublic void onPlaying() {if (badge != null) badge.setLive(true);+ showProblem(null);if (video != null) video.animate().alpha(1f).setDuration(300).start();}@@ -257,6 +263,13 @@ final class OverlayCard implements CardView {picture.addView(image, new FrameLayout.LayoutParams(-1, -1));picture.addView(video, new FrameLayout.LayoutParams(-1, -1));picture.addView(boxes, new FrameLayout.LayoutParams(-1, -1));+ problem = text(12, 0xFFFFFFFF, Typeface.DEFAULT);+ problem.setSingleLine(false);+ problem.setMaxLines(4);+ problem.setGravity(Gravity.CENTER);+ problem.setPadding(px(20), 0, px(20), 0);+ problem.setVisibility(View.GONE);+ picture.addView(problem, new FrameLayout.LayoutParams(-1, -2, Gravity.CENTER));timerBar = new View(context);timerBar.setPivotX(0f);picture.addView(timerBar, new FrameLayout.LayoutParams(-1, px(3), Gravity.TOP));@@ -281,6 +294,7 @@ final class OverlayCard implements CardView {// At most 320 dp wide and 180 dp tall (a 16:9 frame fills both), in the camera's own shape so boxes line up.sizePicture(source.aspect(m.camera));image.setImageDrawable(null);+ showProblem(null);countingDown = false;}SpannableStringBuilder t = new SpannableStringBuilder(m.title);@@ -353,16 +367,29 @@ final class OverlayCard implements CardView {// The frame's own shape, in case Frigate's camera sizes were not known when the card opened.sizePicture((float) bmp.getWidth() / bmp.getHeight());image.setImageBitmap(bmp);+ showProblem(null);});} catch (IOException | RuntimeException e) {if (!snapshotLogged) Log.w(ConfigStore.TAG, "frigate error: snapshot " + camera + ": " + e.getMessage());snapshotLogged = true;+ String why = CardText.noPicture(source.newCertificate(), e.getMessage());+ main.post(() -> {+ // A frame from a moment ago beats a message; only an empty picture says why it is empty.+ if (root != null && id.equals(reviewId) && image.getDrawable() == null) showProblem(why);+ });} finally {if (done != null) main.post(done);}});}+ /** null hides the message. */+ private void showProblem(String why) {+ if (problem == null) return;+ problem.setText(why);+ problem.setVisibility(why == null ? View.GONE : View.VISIBLE);+ }+/** Current review id on screen, or null. */String reviewId() {return reviewId;diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingRow.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingRow.javaindex 5dab10990..2af2c43cd 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingRow.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingRow.java@@ -43,9 +43,9 @@ final class SettingRow extends LinearLayout {this.value = new TextView(context);this.value.setTextSize(17);- this.value.setTextColor(0xFFC9D1D9);this.value.setText(value);addView(this.value, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));+ setValue(value);}void setDetail(String d) {@@ -55,6 +55,8 @@ final class SettingRow extends LinearLayout {void setValue(String v) {value.setText(v);+ value.setTextColor(0xFFC9D1D9);+ value.setTypeface(android.graphics.Typeface.DEFAULT);}/** A value that needs to stand out, such as the snooze state: coloured and bold. */diff --git a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.javaindex 5b19ec7a7..7c430a01e 100644--- a/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java+++ b/addons/frigate-glance/app/src/main/java/io/github/nickld/frigateglance/SettingsActivity.java@@ -27,7 +27,7 @@ import org.json.JSONObject;*/public class SettingsActivity extends Activity {private static final long REFRESH_MS = 2000;- private static final int SNOOZED = 0xFFFFB454, CARDS_ON = 0xFF3DDC84;+ private static final int SNOOZED = 0xFFFFB454, CARDS_ON = 0xFF3DDC84, WARNING = 0xFFFFB454;private static final String[] SECTIONS = {"Overview", "Cameras", "Alerts", "Card", "Remote"};private static final Map<String, String> CORNER_NAMES = Map.of("top-right", "Top right", "top-left", "Top left", "bottom-left", "Bottom left", "bottom-right", "Bottom right");@@ -139,7 +139,10 @@ public class SettingsActivity extends Activity {});divider();mqttRow = info("MQTT broker", "");- frigateRow = info("Frigate", "");+ frigateRow = row("Frigate", "", "", () -> {+ GlanceService.checkFrigate(!GlanceService.overview().offeredPin.isEmpty());+ main.postDelayed(this::updateLive, 1500);+ });messagesRow = info("Last message from Frigate", "");lastCardRow = info("Last card", "");tvRow = info("On this TV", "");@@ -347,12 +350,24 @@ public class SettingsActivity extends Activity {status.setText(GlanceService.statusLine());if (mqttRow != null) {GlanceService.Overview o = GlanceService.overview();- mqttRow.setDetail(o.mqttHost);- mqttRow.setValue(!o.running ? "Not running" : o.mqttUp ? "Connected " + ago(o.mqttSince)- : "Not connected" + (o.mqttError.isEmpty() ? "" : ": " + o.mqttError));- frigateRow.setDetail(o.frigateHost + (o.cameras > 0 ? " · " + o.cameras + " cameras" : ""));- frigateRow.setValue(!o.running ? "Not running" : o.frigateUp ? "Reachable"- : "Not reached" + (o.frigateError.isEmpty() ? "" : ": " + o.frigateError));+ // Errors go under the address: in the value they squeeze the address into a narrow column.+ mqttRow.setDetail(dots(o.mqttHost, o.running && !o.mqttUp ? o.mqttError : ""));+ mqttRow.setValue(!o.running ? "Not running" : o.mqttUp ? "Connected " + ago(o.mqttSince) : "Not connected");+ if (!o.running) {+ frigateRow.setDetail(o.frigateHost);+ frigateRow.setValue("Not running");+ } else if (!o.offeredPin.isEmpty()) {+ frigateRow.setDetail(dots(o.frigateHost, "Frigate shows a new certificate (" + o.offeredPin.substring(0, 12)+ + ", trusted was " + o.pin.substring(0, 12) + "). Frigate makes a new one when it is"+ + " reinstalled or updated; if that just happened, OK trusts it."));+ frigateRow.setValue("New certificate", WARNING);+ } else if (o.frigateUp) {+ frigateRow.setDetail(dots(o.frigateHost, o.cameras > 0 ? o.cameras + " cameras" : "", "OK reads the cameras again."));+ frigateRow.setValue("Reachable");+ } else {+ frigateRow.setDetail(dots(o.frigateHost, o.frigateError, "OK tries again."));+ frigateRow.setValue("Not reached");+ }messagesRow.setDetail("An alert or a tracked object. Quiet is normal while nothing moves.");messagesRow.setValue(o.lastMessageAt == 0 ? "None yet" : ago(o.lastMessageAt));lastCardRow.setDetail(OverlayCard.lastTitle);@@ -411,6 +426,17 @@ public class SettingsActivity extends Activity {return Math.round(dp * getResources().getDisplayMetrics().density);}+ /** The non-empty parts, joined with " · ": the address is empty until the service has read the config. */+ private static String dots(String... parts) {+ StringBuilder s = new StringBuilder();+ for (String p : parts) {+ if (p == null || p.isEmpty()) continue;+ if (s.length() > 0) s.append(" · ");+ s.append(p);+ }+ return s.toString();+ }+private static String onOff(boolean on) {return on ? "On" : "Off";}diff --git a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardTextTest.java b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardTextTest.javaindex 378c88fa1..155b3a349 100644--- a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardTextTest.java+++ b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/CardTextTest.java@@ -55,4 +55,13 @@ public class CardTextTest {assertEquals("▶ Snooze 1h", p.get(KeyRules.SNOOZE));assertEquals(2, CardText.parts("KEYCODE_DPAD_LEFT", "KEYCODE_DPAD_RIGHT", 60, null).size());}++ @Test+ public void noPictureSaysWhy() {+ assertEquals("No picture: Frigate has a new certificate.\nTrust it in Frigate Glance › Overview › Frigate.",+ CardText.noPicture(true, "javax.net.ssl.SSLHandshakeException: whatever"));+ assertEquals("No picture: Frigate not reached.\nfailed to connect to /192.168.1.11 (port 8971)",+ CardText.noPicture(false, "java.net.ConnectException: failed to connect to /192.168.1.11 (port 8971)"));+ assertEquals("No picture: Frigate not reached.", CardText.noPicture(false, null));+ }}diff --git a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/FrigateApiTest.java b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/FrigateApiTest.javaindex ce9271dca..ab48367c5 100644--- a/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/FrigateApiTest.java+++ b/addons/frigate-glance/app/src/test/java/io/github/nickld/frigateglance/FrigateApiTest.java@@ -114,4 +114,33 @@ public class FrigateApiTest {assertEquals("h=360", query[0]);assertEquals(0, logins.get());}++ @Test+ public void newCertificateIsRefusedUntilTrusted() throws Exception {+ List<String> saved = new java.util.ArrayList<>();+ FrigateApi api = new FrigateApi("https://frigate:8971", "", "", "old", saved::add);+ try {+ api.checkLeaf("new");+ fail("a different certificate was accepted");+ } catch (java.security.cert.CertificateException expected) {+ // refused, and offered for trusting+ }+ assertEquals("new", api.offeredPin());+ assertTrue(api.trustOffered());+ assertEquals("new", api.pin());+ assertEquals("", api.offeredPin());+ assertEquals(List.of("new"), saved);+ api.checkLeaf("new");+ assertEquals(false, api.trustOffered());+ }++ @Test+ public void firstCertificateIsPinned() throws Exception {+ List<String> saved = new java.util.ArrayList<>();+ FrigateApi api = new FrigateApi("https://frigate:8971", "", "", "", saved::add);+ api.checkLeaf("first");+ assertEquals("first", api.pin());+ assertEquals("", api.offeredPin());+ assertEquals(List.of("first"), saved);+ }}