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
@@ -0,0 +1,88 @@
|
||||
// Exercises the private relay end to end: parse a pairing link, open the E2EE
|
||||
// tunnel, redeem the one-time secret, then use the client like the app does
|
||||
// (HTTP, SSE, terminal WebSocket).
|
||||
// dart run tool/relay_probe.dart 'openchamber://connect?v=2&p=...'
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:openchamber/api/client.dart';
|
||||
import 'package:openchamber/api/pairing.dart';
|
||||
import 'package:openchamber/api/relay_transport.dart';
|
||||
import 'package:openchamber/api/terminal.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
if (args.isEmpty) {
|
||||
print('usage: dart run tool/relay_probe.dart <pairing link>');
|
||||
return;
|
||||
}
|
||||
final payload = parsePairingLink(args.first);
|
||||
if (payload == null) {
|
||||
print('invalid pairing link');
|
||||
return;
|
||||
}
|
||||
PairingCandidate? relay;
|
||||
for (final candidate in payload.candidates) {
|
||||
if (candidate.isRelay) {
|
||||
relay = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (relay == null) {
|
||||
print('link has no relay candidate');
|
||||
return;
|
||||
}
|
||||
print('relay ${relay.relayUrl} server ${relay.serverId}');
|
||||
final transport = RelayTransport(
|
||||
relayUrl: relay.relayUrl,
|
||||
serverId: relay.serverId,
|
||||
hostKey: relay.hostKey!,
|
||||
grant: relay.grant,
|
||||
version: 'probe',
|
||||
);
|
||||
final client = Client(Uri.parse('http://relay.invalid'), transport: transport);
|
||||
try {
|
||||
final redeemed = await client.redeemPairing(
|
||||
pairingId: payload.pairingId,
|
||||
secret: payload.secret,
|
||||
installId: 'relay-probe',
|
||||
clientLabel: 'relay probe',
|
||||
);
|
||||
print('redeem ${redeemed.token.isEmpty ? 'no token' : 'ok'}: '
|
||||
'${redeemed.response['server']}');
|
||||
if (redeemed.token.isEmpty) return;
|
||||
client.token = redeemed.token;
|
||||
|
||||
final status = await client.sessionStatus();
|
||||
print('session ${status['authenticated']}');
|
||||
final page = await client.listSessions(limit: 1);
|
||||
print('sessions ${page.data.length}'
|
||||
'${page.data.isEmpty ? '' : ' first=${page.data.first.id}'}');
|
||||
|
||||
final sse = <String>[];
|
||||
final sub = client.streamEvents(types: const ['session.created']).listen(
|
||||
(event) => sse.add('${event['type']}'),
|
||||
onError: (Object error) => sse.add('error: $error'),
|
||||
);
|
||||
final dir = page.data.isNotEmpty
|
||||
? page.data.first.directory
|
||||
: Directory.current.path;
|
||||
final info = await client.createTerminal(cwd: dir, cols: 80, rows: 24);
|
||||
print('terminal ${info.sessionId}');
|
||||
final terminal = TerminalSocket(
|
||||
transport: transport,
|
||||
headers: client.eventHeaders,
|
||||
sessionId: info.sessionId,
|
||||
onEvent: (event) =>
|
||||
print('terminal event ${event.type} ${event.data.length}'),
|
||||
onState: (state, {detail}) => print('terminal state $state $detail'),
|
||||
);
|
||||
await terminal.connect();
|
||||
await Future<void>.delayed(const Duration(seconds: 3));
|
||||
await sub.cancel();
|
||||
await terminal.dispose();
|
||||
await client.closeTerminal(info.sessionId, directory: dir);
|
||||
print('sse ${sse.take(3).toList()}');
|
||||
} finally {
|
||||
transport.close();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user