feat: connect through the OpenChamber private relay
A server can now be added by pasting an openchamber://connect pairing link. The one-time secret is redeemed over the first reachable candidate (direct addresses first, then the relay), and the device token is stored with the connection, so later launches reconnect without the link. The relay candidate carries the relay WebSocket URL, the host's routing id and its P-256 public key. The new transport opens one encrypted tunnel to the relay: P-256 ECDH + HKDF-SHA256 derive one AES-256-GCM key per direction; frames carry a version, a 12-byte IV and the GCM tag; a small mux carries HTTP, SSE and WebSocket sub-streams with fragmentation and ping/pong. Every request and the terminal socket ride it; WebSocket upgrades first mint the short-lived oc_url_token over the tunnel, since upgrades cannot send headers. Only the host's legacy path is negotiated (no batching, downstream flow control or body gzip). Pairing while connected switches servers like picking one in the list, which exposed and fixes a ChatScreen crash when app.chat is cleared during the switch. 0.4.4 (404).
This commit is contained in:
1 parent
a7bc380ac0
commit
902f1e18bb
20 files changed
+1918
-75
No files matched your search
+40
-4
@@ -42,6 +42,14 @@ class TransportResponse {
|
||||
const TransportResponse(this.status, this.headers, this.body, {this.abort});
|
||||
}
|
||||
|
||||
/// A binary socket: a direct WebSocket, or one sub-stream of the relay tunnel.
|
||||
/// The terminal speaks binary control frames, so text frames are not surfaced.
|
||||
abstract class TransportSocket {
|
||||
Stream<List<int>> get messages;
|
||||
void add(List<int> data);
|
||||
Future<void> close([int code = 1000, String reason = '']);
|
||||
}
|
||||
|
||||
/// The one seam every request goes through.
|
||||
abstract class Transport {
|
||||
/// Sends [request], completing when the reply head arrives.
|
||||
@@ -49,9 +57,14 @@ abstract class Transport {
|
||||
|
||||
/// Opens a WebSocket on [path] (the terminal stream). Through the same
|
||||
/// sockets as [send], so proxies and certificates keep working.
|
||||
Future<WebSocket> webSocket(String path,
|
||||
Future<TransportSocket> webSocket(String path,
|
||||
{String? rawQuery, Map<String, String> headers = const {}});
|
||||
|
||||
/// The bearer the client authenticates with. A direct transport ignores it;
|
||||
/// a relay must mint URL-scoped tokens from it for WebSocket upgrades, which
|
||||
/// cannot carry headers.
|
||||
void setBearerToken(String? token) {}
|
||||
|
||||
/// Releases whatever sockets the transport holds.
|
||||
void close();
|
||||
}
|
||||
@@ -210,21 +223,44 @@ class DirectTransport implements Transport {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<WebSocket> webSocket(String path,
|
||||
{String? rawQuery, Map<String, String> headers = const {}}) {
|
||||
Future<TransportSocket> webSocket(String path,
|
||||
{String? rawQuery, Map<String, String> headers = const {}}) async {
|
||||
// ws:// is http:// with the upgrade; wss:// checks the same certificates,
|
||||
// so the configured client is reused as it stands.
|
||||
final base = this.base.replace(
|
||||
scheme: this.base.scheme == 'https' ? 'wss' : 'ws');
|
||||
final query = rawQuery ?? '';
|
||||
final uri = base.resolve(path).replace(query: query.isEmpty ? null : query);
|
||||
return WebSocket.connect(uri.toString(), headers: headers, customClient: _http);
|
||||
final socket = await WebSocket.connect(uri.toString(),
|
||||
headers: headers, customClient: _http);
|
||||
return _DirectSocket(socket);
|
||||
}
|
||||
|
||||
@override
|
||||
void setBearerToken(String? token) {}
|
||||
|
||||
@override
|
||||
void close() => _http.close();
|
||||
}
|
||||
|
||||
/// Wraps a direct WebSocket in the transport-agnostic socket shape. The
|
||||
/// terminal only speaks binary frames; text frames are ignored.
|
||||
class _DirectSocket implements TransportSocket {
|
||||
final WebSocket _socket;
|
||||
_DirectSocket(this._socket);
|
||||
|
||||
@override
|
||||
Stream<List<int>> get messages =>
|
||||
_socket.where((event) => event is List<int>).cast<List<int>>();
|
||||
|
||||
@override
|
||||
void add(List<int> data) => _socket.add(data);
|
||||
|
||||
@override
|
||||
Future<void> close([int code = 1000, String reason = '']) =>
|
||||
_socket.close(code, reason);
|
||||
}
|
||||
|
||||
String sha256Hex(List<int> bytes) => _Digest.sha256Hex(bytes);
|
||||
|
||||
// Minimal SHA-256, so the client has no native dependencies for pinning.
|
||||
|
||||
Reference in new issue
Block a user