Detailed breakdown

A project log for The Mailblocks: Physical Inbox for Virtual Alerts

This mailbox coordinates with a Raspberry Pi based router to block push notifications network-wide until a phone is placed inside.

guy-dupontGuy Dupont 12/22/2023 at 21:010 Comments

The Raspberry Pi

As a router

My original idea was to run a MITM and see if I could actually intercept the push notifications as my phone received them. I would have been fine not being able to decrypt/read them, I just wanted to know they were there and redirect them to /dev/null.  So I played around with the tools associated with mitmproxy. Specifically, I followed this tutorial from Dino Fizzotti. I had fun playing with mitmproxy, but ultimately decided this project would work well enough with the simple, DNS based approach. However, I still ended up using two of the tools that got pulled in during this phase of the project:

  1. hostapd - which allows the Pi to act as a WiFi access point.
  2. isc-dhcp-server - which acts as a DHCP server on the Pi (assigns IP addresses to devices that connect)

These two tools running together effectively turn the Pi into a wireless router. 

Interesting note - whenever the system changes state (notifications blocked or not, read below how that works), I use hostapd's "disassociate" command to briefly kick all devices off the access point. Unlike the "deauthenticate" command, this allows them to reconnect immediately. Doing this ensures that any existing TCP connections are broken and ensures that the phone either will or won't get the data it should or should not.

As a DNS server

The way the Pi blocks push notifications is by providing invalid IP addresses any time a device on the WiFi network makes a request to any of the hostnames Google lists as being used for messaging. So there is a DNS server running on the Pi that all connected devices are forced to use. When the phone is not inside the mailbox (and notifications are blocked), the DNS server redirects the targeted lookups, and when the phone goes in, the DNS server acts as a passthrough for every request. 

I am using fakedns, which is a standalone DNS server written in Python. It's great! No dependencies to install, very easy to get up and running. I was able to add a single line of code to add the functionality of toggling redirects on and off - I'm simply checking if a file named '.disabled' exists in the directory the server is run right as each request comes in.

Note - I think dnsmasq may have been a good option here, it seems to handle both DHCP and DNS.

As an HTTP server

I needed a way for the microcontroller in the mailbox to turn the DNS server redirects on and off. I set up a simple HTTP server with two endpoints: block and passthrough. When "block" is hit, I make sure that there is no file named ".disabled" is in the DNS server's local directory. When "passthrough" is hit, I write a blank file named ".disabled" is in the DNS server's local directory. Kludgy but simple! I wrote the server with fastapi/uvicorn.

The Mailbox

(Please see the video for a better description/images of the mailbox hardware)

- Contains a homemade switch consisting of two springs with different lengths and diameters. I glued the longer/slimmer spring to dangle inside the shorter/wider spring. The longer spring hangs down into the area of the mailbox that the phone slides into. When the phone is inserted, the longer spring bends and makes contact with the outer spring. One of the springs is wired to a GPIO pin on the ESP-32, and the other is wired to ground. This switch is responsible for waking the ESP from deep sleep, at which point it hits the "passthrough" endpoint on the HTTP server running on the Pi (see last section). When the phone is removed and the springs no longer touch, the ESP hits the Pi's "block" endpoint and goes back to sleep. (Sidenote - there is also a timer. If the phone is not removed before 30 seconds passes, the ESP will perform the same "bedtime" routine. This is so you can't just leave something inside the mailbox to allow notifications indefinitely)

- Contains a cheap MG90S hobby servo. When the phone goes in and the ESP wakes up, it uses the servo to move the mailbox's arm up. Before the ESP goes to sleep, it brings the servo back down. Nice visual indicator of the system state.

- Contains a 3D printed shelf that holds up the servo to the mailbox's arm, provides an anchor for the dangling spring switch, and holds the rest of the hardware out of the phone's way. The shelf is adhered to the internal mailbox wall using double sided "nano tape"