How Lustro works¶
Lustro is a small HTTP server inside the process of an app's debug build. The browser console, the
lustro CLI, and agents on the computer reach it through an adb port forward. Each tab of the
console is a Kotlin class in the app and a set of JSON routes under /api/v1/<id>/. This page
explains how these parts fit together. The pages that it links give the details.
The parts¶
flowchart LR
subgraph computer["Computer"]
browser["Browser"]
cli["lustro CLI,<br>agents, scripts"]
forward["adb forward<br>tcp:8080 tcp:8080"]
end
subgraph app["App process on the device, debug build"]
server["Lustro server<br>127.0.0.1:8080"]
tabs["Tabs"]
appcode["App code<br>and data"]
end
browser --> forward
cli --> forward
forward --> server
server --> tabs
tabs -. read .-> appcode
- The server runs in the app's process, so a tab reads the app's objects directly: its OkHttp traffic, its databases, its preferences, its windows. There is no agent process on the device and no instrumentation.
- The address is
127.0.0.1:8080by default. Only the device itself can connect to a loopback address, so the computer connects throughadb forward.lustro openmakes the forward for you. See Accessing the UI, and LAN exposure and port forwarding for the opt-in to other addresses. - The token is in the
LustroTokenline of logcat, and every client needs it: see the flow of the token. - The clients are the browser console and anything that sends HTTP: the
lustroCLI,curl, and coding agents. They use the same routes. The console is one more client of the JSON API.
When the server listens¶
Lustro.start() does not open the socket itself. It adds an observer to the process lifecycle,
and the server listens only while the app is in the foreground:
- When the app comes to the foreground, the server binds its port and logs the
LustroTokenline again. - When the app goes to the background, the server closes its listening socket at once, so that a new connection is refused and another app can bind the port. It answers the requests in progress on their own connections, waits up to 5 s for them, and then closes those connections.
Lustro.stop()removes the observer and closes the socket at once.
While the app is in the background, a request through the forward gets no response: adb closes
the connection. When the port is taken, the server tries again for 10 s, or binds another port with bindFallback: see
Troubleshooting.
The flow of a request¶
Every request to /api/v1/ passes the same checks, in this order. The first check that fails
answers with the error envelope:
- A request whose request line and headers are larger than 8 KiB gets a
400with the messageRequest headers too large, and the server closes the connection. This limit is fixed, and it applies to every route,_authtoo. A browser usually gets there with large cookies that other local servers set for the same host name. - A request with a
Transfer-Encodingheader, such as a chunked body, or with aContent-Lengththat is not valid, gets a400. - A body larger than
maxRequestBodyBytesgets a413, before the server reads it. - A request without a valid token gets a
401. - A browser request from another origin gets a
403: see Security model. - The server reads the body. All the bodies in memory at one time stay under
maxConcurrentRequests×maxRequestBodyBytes, so a request can wait for room first. - The request waits for one of the
maxConcurrentRequestsslots. When the slots and the queue are full, or when a wait for room or for a slot is longer thanrequestTimeoutMs, it gets a503. - The server routes the request. It answers the framework routes
_metaand_schemaitself. For/api/v1/<id>/<path>, it finds the tab with that id and calls itshandle(request). - When
handlereturnsnull, the client gets a404, or a405with anAllowheader when the tab's OpenAPI document declares the path with other methods. When it throws anything, the client gets a500, and the app does not crash. When it runs longer thanrequestTimeoutMs, the client gets a504, and the server cancels the request.
POST /api/v1/_auth is the only route that needs no token, and it has its own checks: another
method gets a 405, a request from another origin a 403, a body larger than a small limit a
413, and a wrong token a 401.
Every response, the error responses too, has the Content Security Policy and
X-Content-Type-Options: nosniff.
The flow of the token¶
Every API request needs the token. The app makes it once, and the clients read it on the computer:
sequenceDiagram
participant App as Lustro in the app
participant Log as logcat
participant CLI as CLI or agent
participant Browser
App->>App: Make the token at the first bind
App->>Log: Lustro ready endpoint=... token=...
CLI->>Log: Read the endpoint and the token
CLI->>App: Request with Authorization: Bearer
Browser->>App: POST /api/v1/_auth with the token
App-->>Browser: lustro_token cookie
Browser->>App: Requests with the cookie
- On the first bind, the server makes a random 256-bit token and saves it in the app's private
lustro_debugSharedPreferences file, with the host and the port. The token stays the same until the app's data is cleared or the app is installed again. With LAN exposure, each process of the app makes a new one. - After each bind, the server logs one line at the
LustroTokentag:Lustro ready endpoint=http://127.0.0.1:8080 token=<token>. - The CLI and agents read the line, or the prefs file through
adb shell run-aswhen the device drops Info logs. With--package, the CLI reads the prefs file first, because onerun-asread is faster than a dump of a full log, and reads the line only when the prefs can't give the token and the port. They send the token in theAuthorization: Bearer <token>header of each request. - The browser gets the token in the URL fragment
#lustro_token=<token>. The console's script sends it toPOST /api/v1/_auth, which sets thelustro_tokencookie,HttpOnlyandSameSite=Strict. The script then removes the fragment from the address bar.
The server removes the Authorization and Cookie headers before it calls a tab, so a tab never
sees the token. The details of token discovery are in
Driving Lustro from agents and automation.
The anatomy of a tab¶
A tab is a DebugTab subclass. The built-in Network tab, the plugins, and your own tabs use the
same class. The server builds the console's tab bar and the tab's routes from it:
flowchart LR
subgraph code["DebugTab subclass"]
props["id, title, icon, order"]
handle["handle(request)"]
content["renderContent()"]
end
subgraph assets["assets/lustro/"]
js["{id}.js"]
css["{id}.css"]
oas["{id}.openapi.json"]
end
subgraph routes["Server"]
bar["Tab bar link to /tab/{id}"]
view["/api/v1/{id}/_view, _view.js, _view.css"]
api["/api/v1/{id}/{path}"]
schema["/api/v1/{id}/_schema"]
meta["/api/v1/_meta"]
end
props --> bar
content --> view
js --> view
css --> view
handle --> api
oas --> schema
oas --> meta
- The tab bar lists the tabs by
order, lower first, and then by id. The Network tab is10, the plugins are20to60, and the default is100. A tab withshowInTabBar = falsehas no link and no page, but its routes work. - The page
/tab/<id>needs no token, and it has only the tab bar and an empty content area. After the browser has the cookie,shared.jsloads the tab's HTML from_view, then its CSS and JS from_view.cssand_view.js. The tab's own content is never served before the token is checked. - The routes under
/api/v1/<id>/go tohandle(request), withrequest.pathset to the part after the id. A tab that declares its routes inroutes(routes)gets ahandle()that routes the requests and checks their parameters, and aschema()that returns the document of the routes. - The OpenAPI document is served at
/api/v1/<id>/_schema. Only a tab with a document is inGET /api/v1/_meta, so agents find only those tabs. Theversionin_metais the document'sinfo.version. - The assets are the files
assets/lustro/<id>.js,<id>.css, and<id>.openapi.json.renderScript(),renderStyles(), andschema()can return them from code instead.
Write a tab builds a tab step by step, and Custom tabs gives the rules for the code and the assets.
The threads of a tab¶
| Code | Thread |
|---|---|
Lustro.start() and Lustro.stop() |
The main thread. start() from another thread returns DISABLED. |
DebugTab.onStart() and onStop() |
The thread that calls Lustro.start() or Lustro.stop(), once each. They do not run when the app goes to the background. |
DebugTab.handle(request), renderContent(), renderScript(), renderStyles(), schema() |
A lustro-request-worker thread, never the main thread. Calls can run at the same time on different threads. |
Actions from request.onCancel { } |
A server thread, when the request times out or the server stops. Such an action can run after handle has returned. |
| Reading the request and writing the response | A lustro-http-<n> thread, one for each open connection. |
| The bind when the app comes to the foreground, and the close of the listening socket when it goes to the background | The main thread, in Lustro.start() and in the process lifecycle's callbacks. |
The retries of a bind to a taken port, and the LustroToken line |
The lustro-worker thread. |
| The wait for the requests in progress when the app goes to the background, and the close of their connections | The lustro-drain thread. |
Thus a tab with mutable state must make it thread-safe. A tab can block on I/O in handle,
because the per-request timeout limits the wait. Custom tabs gives the rules
for cancellation.