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
+139
@@ -3,12 +3,15 @@
|
||||
library;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'api/client.dart';
|
||||
import 'api/models.dart';
|
||||
import 'api/pairing.dart';
|
||||
import 'api/relay_transport.dart';
|
||||
import 'storage.dart';
|
||||
import 'terminal_state.dart';
|
||||
|
||||
@@ -190,12 +193,143 @@ class AppState extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Client _buildClient(Connection c) {
|
||||
if (c.isRelay) {
|
||||
// The relay has no reachable origin: every request rides the tunnel.
|
||||
final transport = RelayTransport(
|
||||
relayUrl: c.relayUrl,
|
||||
serverId: c.serverId,
|
||||
hostKey: jsonDecode(c.hostKey) as Map<String, dynamic>,
|
||||
grant: c.grant.isEmpty ? null : c.grant,
|
||||
);
|
||||
return Client(Uri.parse('http://relay.invalid'), transport: transport);
|
||||
}
|
||||
return Client(Uri.parse(c.url),
|
||||
proxy: c.proxy.isEmpty ? null : c.proxy,
|
||||
insecure: c.insecure,
|
||||
fingerprint: c.fingerprint.isEmpty ? null : c.fingerprint);
|
||||
}
|
||||
|
||||
/// Ends a failed pairing attempt's client without touching the app's state.
|
||||
void _dropClient(Client cl) {
|
||||
try {
|
||||
cl.close();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// Pairs a device from an `openchamber://connect` link: the one-time secret
|
||||
/// is redeemed over the first reachable candidate (LAN first, then relay),
|
||||
/// and the resulting device token is stored for later connections.
|
||||
Future<bool> connectWithPairing(String link) async {
|
||||
if (busy) return false;
|
||||
final payload = parsePairingLink(link);
|
||||
if (payload == null) {
|
||||
debugPrint('pair: invalid link (${link.length} chars)');
|
||||
_lastError = '配对链接无效或已过期';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
busy = true;
|
||||
error = null;
|
||||
_lastError = null;
|
||||
notifyListeners();
|
||||
final installId = await storage.installId();
|
||||
Object? failure;
|
||||
for (final candidate in payload.candidates) {
|
||||
final Client cl;
|
||||
try {
|
||||
cl = candidate.isRelay
|
||||
? Client(Uri.parse('http://relay.invalid'),
|
||||
transport: RelayTransport(
|
||||
relayUrl: candidate.relayUrl,
|
||||
serverId: candidate.serverId,
|
||||
hostKey: candidate.hostKey!,
|
||||
grant: candidate.grant,
|
||||
))
|
||||
: Client(Uri.parse(candidate.url));
|
||||
} catch (e) {
|
||||
failure = e;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
final redeemed = await cl
|
||||
.redeemPairing(
|
||||
pairingId: payload.pairingId,
|
||||
secret: payload.secret,
|
||||
installId: installId,
|
||||
)
|
||||
.timeout(candidate.isRelay
|
||||
? const Duration(seconds: 30)
|
||||
: const Duration(seconds: 15));
|
||||
if (redeemed.token.isEmpty) {
|
||||
throw ApiError(401, '服务器没有返回设备令牌');
|
||||
}
|
||||
final connection = _pairingConnection(candidate, payload, redeemed.response);
|
||||
await storage.saveToken(connection.id, redeemed.token);
|
||||
cl.token = redeemed.token;
|
||||
// Pairing while another server is live switches this app to the paired
|
||||
// one, exactly like picking it in the server list: close the old
|
||||
// client, shells and event stream first.
|
||||
if (client != null) {
|
||||
await disconnect();
|
||||
}
|
||||
busy = false;
|
||||
_connected(cl, connection);
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (e) {
|
||||
failure = e;
|
||||
_dropClient(cl);
|
||||
}
|
||||
}
|
||||
busy = false;
|
||||
final message = friendlyConnectError(failure ?? '配对失败');
|
||||
debugPrint('pair: failed: $message');
|
||||
error = message;
|
||||
_lastError = message;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
Connection _pairingConnection(
|
||||
PairingCandidate candidate, PairingPayload payload, Map<String, dynamic> response) {
|
||||
final server = response['server'];
|
||||
final serverLabel =
|
||||
server is Map ? server['label'] as String? : null;
|
||||
final label = (payload.label ?? serverLabel ?? '').trim();
|
||||
Connection? existing;
|
||||
for (final c in connections) {
|
||||
if (candidate.isRelay) {
|
||||
if (c.isRelay &&
|
||||
c.serverId == candidate.serverId &&
|
||||
c.relayUrl == candidate.relayUrl) {
|
||||
existing = c;
|
||||
break;
|
||||
}
|
||||
} else if (!c.isRelay && c.url == candidate.url) {
|
||||
existing = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (candidate.isRelay) {
|
||||
return Connection(
|
||||
id: existing?.id ?? Storage.newId(),
|
||||
label: label.isNotEmpty ? label : (existing?.label ?? 'Relay 服务器'),
|
||||
url: 'relay://${candidate.serverId}',
|
||||
relayUrl: candidate.relayUrl,
|
||||
serverId: candidate.serverId,
|
||||
hostKey: jsonEncode(candidate.hostKey),
|
||||
grant: candidate.grant ?? '',
|
||||
);
|
||||
}
|
||||
return Connection(
|
||||
id: existing?.id ?? Storage.newId(),
|
||||
label: label.isNotEmpty
|
||||
? label
|
||||
: (existing?.label ?? Uri.parse(candidate.url).host),
|
||||
url: candidate.url,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> connect(Connection c, {String password = '', bool silent = false}) async {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
@@ -205,6 +339,11 @@ class AppState extends ChangeNotifier {
|
||||
try {
|
||||
var token = await storage.loadToken(c.id);
|
||||
if (token == null || token.isEmpty) {
|
||||
if (c.isRelay) {
|
||||
// A relay connection is created by pairing; a missing token means it
|
||||
// was never stored or was revoked.
|
||||
throw ApiError(401, '配对已失效,请重新配对');
|
||||
}
|
||||
// No saved token: a passwordless server needs none.
|
||||
try {
|
||||
final st = await cl.sessionStatus();
|
||||
|
||||
Reference in new issue
Block a user