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
@@ -43,7 +43,11 @@ class ChatScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChatScreenState extends State<ChatScreen> {
|
||||
ChatState get chat => widget.app.chat!;
|
||||
/// The chat this page shows. Held rather than read from `app.chat` each
|
||||
/// time: switching servers clears `app.chat` while this widget is still on
|
||||
/// screen for one frame, and its timers and dispose must not trip on that.
|
||||
late final ChatState _chat;
|
||||
ChatState get chat => _chat;
|
||||
final TextEditingController _draftCtrl = TextEditingController();
|
||||
final FocusNode _inputFocus = FocusNode();
|
||||
final ScrollController _scroll = ScrollController();
|
||||
@@ -71,6 +75,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_chat = widget.app.chat!;
|
||||
chat.addListener(_onChat);
|
||||
_inputFocus.addListener(_onFocusChanged);
|
||||
if (chat.running) _startClock();
|
||||
|
||||
+215
-59
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../state.dart';
|
||||
import '../storage.dart';
|
||||
@@ -90,10 +91,24 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
children: [
|
||||
for (final c in conns) _serverCard(c),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton.icon(
|
||||
onPressed: () => _startEdit(null),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('添加服务器'),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () => _startEdit(null),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('添加服务器'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: FilledButton.tonalIcon(
|
||||
onPressed: _pair,
|
||||
icon: const Icon(Icons.qr_code_2, size: 18),
|
||||
label: const Text('配对链接'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -114,8 +129,9 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(c.url, maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
if (c.proxy.isNotEmpty)
|
||||
Text(c.isRelay ? '经由 Relay · ${c.serverId}' : c.url,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
if (!c.isRelay && c.proxy.isNotEmpty)
|
||||
Text('经由 ${c.proxy}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
@@ -216,48 +232,57 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
decoration: const InputDecoration(hintText: '如 我的电脑', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_labelText('服务器地址'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _url),
|
||||
onChanged: (v) => _url = v,
|
||||
keyboardType: TextInputType.url,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'http://192.168.1.10:3000', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_labelText('代理(可选)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _proxy),
|
||||
onChanged: (v) => _proxy = v,
|
||||
keyboardType: TextInputType.url,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'http://… 或 socks5://…', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_labelText('自签证书指纹(可选)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _fingerprint),
|
||||
onChanged: (v) => _fingerprint = v,
|
||||
decoration: const InputDecoration(hintText: 'SHA-256 十六进制', border: OutlineInputBorder()),
|
||||
),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: const Text('信任自签名证书'),
|
||||
value: _insecure,
|
||||
onChanged: (v) => setState(() => _insecure = v),
|
||||
),
|
||||
// Shown for new servers too: a password-protected instance rejects
|
||||
// the first attempt, and this is where the password goes.
|
||||
_labelText('密码(如果需要)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _password),
|
||||
focusNode: _passwordFocus,
|
||||
onChanged: (v) => _password = v,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '服务器密码;服务器未设置可留空',
|
||||
border: OutlineInputBorder()),
|
||||
),
|
||||
if (_edit?.isRelay == true) ...[
|
||||
const ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(Icons.lock_outline, size: 20),
|
||||
title: Text('通过 Relay 连接'),
|
||||
subtitle: Text('端到端加密;服务器与设备令牌保持不变'),
|
||||
),
|
||||
] else ...[
|
||||
_labelText('服务器地址'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _url),
|
||||
onChanged: (v) => _url = v,
|
||||
keyboardType: TextInputType.url,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'http://192.168.1.10:3000', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_labelText('代理(可选)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _proxy),
|
||||
onChanged: (v) => _proxy = v,
|
||||
keyboardType: TextInputType.url,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'http://… 或 socks5://…', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_labelText('自签证书指纹(可选)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _fingerprint),
|
||||
onChanged: (v) => _fingerprint = v,
|
||||
decoration: const InputDecoration(hintText: 'SHA-256 十六进制', border: OutlineInputBorder()),
|
||||
),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: const Text('信任自签名证书'),
|
||||
value: _insecure,
|
||||
onChanged: (v) => setState(() => _insecure = v),
|
||||
),
|
||||
// Shown for new servers too: a password-protected instance rejects
|
||||
// the first attempt, and this is where the password goes.
|
||||
_labelText('密码(如果需要)'),
|
||||
TextField(
|
||||
controller: TextEditingController(text: _password),
|
||||
focusNode: _passwordFocus,
|
||||
onChanged: (v) => _password = v,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '服务器密码;服务器未设置可留空',
|
||||
border: OutlineInputBorder()),
|
||||
),
|
||||
],
|
||||
if (_formError != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
@@ -287,14 +312,20 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
_connecting = true;
|
||||
_formError = null;
|
||||
});
|
||||
final c = Connection(
|
||||
id: _edit?.id ?? _newId(),
|
||||
label: _label.isEmpty ? _hostOf(_url) : _label,
|
||||
url: _url,
|
||||
proxy: _proxy,
|
||||
insecure: _insecure,
|
||||
fingerprint: _fingerprint,
|
||||
);
|
||||
final relay = _edit?.isRelay == true ? _edit : null;
|
||||
final c = relay != null
|
||||
? relay.copyWith(
|
||||
label: _label.isEmpty ? relay.label : _label,
|
||||
lastUsedAt: DateTime.now().millisecondsSinceEpoch,
|
||||
)
|
||||
: Connection(
|
||||
id: _edit?.id ?? Storage.newId(),
|
||||
label: _label.isEmpty ? _hostOf(_url) : _label,
|
||||
url: _url,
|
||||
proxy: _proxy,
|
||||
insecure: _insecure,
|
||||
fingerprint: _fingerprint,
|
||||
);
|
||||
// Only a connection that works is saved (AppState saves it on success),
|
||||
// so failed attempts do not pile up in the server list.
|
||||
await widget.app.connect(c, password: _password);
|
||||
@@ -322,6 +353,12 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
});
|
||||
}
|
||||
|
||||
void _pair() {
|
||||
widget.app.error = null;
|
||||
widget.app.clearLastError();
|
||||
showPairingDialog(context, widget.app);
|
||||
}
|
||||
|
||||
void _confirmDelete(Connection c) {
|
||||
showDialog(
|
||||
context: context,
|
||||
@@ -342,9 +379,6 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
static String _newId() =>
|
||||
'srv_${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}';
|
||||
|
||||
static String _hostOf(String raw) {
|
||||
try {
|
||||
return Uri.parse(raw).host;
|
||||
@@ -353,3 +387,125 @@ class _ServersScreenState extends State<ServersScreen> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the paste-a-pairing-link dialog (also reachable from the connected
|
||||
/// sidebar's server sheet).
|
||||
Future<void> showPairingDialog(BuildContext context, AppState app) {
|
||||
app.error = null;
|
||||
app.clearLastError();
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (_) => _PairingDialog(app: app),
|
||||
);
|
||||
}
|
||||
|
||||
/// Paste an `openchamber://connect` link and pair. The relay candidate in the
|
||||
/// link is used when no direct address answers.
|
||||
class _PairingDialog extends StatefulWidget {
|
||||
final AppState app;
|
||||
const _PairingDialog({required this.app});
|
||||
|
||||
@override
|
||||
State<_PairingDialog> createState() => _PairingDialogState();
|
||||
}
|
||||
|
||||
class _PairingDialogState extends State<_PairingDialog> {
|
||||
final TextEditingController _text = TextEditingController();
|
||||
bool _busy = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _paste() async {
|
||||
final data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
final text = data?.text?.trim() ?? '';
|
||||
if (text.isEmpty || !mounted) return;
|
||||
setState(() => _text.text = text);
|
||||
}
|
||||
|
||||
Future<void> _connect() async {
|
||||
final link = _text.text.trim();
|
||||
if (link.isEmpty || _busy) return;
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_error = null;
|
||||
});
|
||||
final paired = await widget.app.connectWithPairing(link);
|
||||
if (!mounted) return;
|
||||
if (paired && widget.app.client != null) {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_error = widget.app.lastError ?? '配对失败';
|
||||
});
|
||||
widget.app.clearLastError();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AlertDialog(
|
||||
title: const Text('配对链接'),
|
||||
content: SizedBox(
|
||||
width: 420,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
'在 OpenChamber 网页端打开「连接设备」,把生成的链接粘贴到这里。'
|
||||
'链接里带一次性密钥,以及直连地址或私有 Relay。',
|
||||
style: TextStyle(fontSize: 12.5, color: theme.hintColor),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _text,
|
||||
autofocus: true,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
keyboardType: TextInputType.multiline,
|
||||
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'openchamber://connect?v=2&p=…',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(_error!,
|
||||
style: TextStyle(color: theme.colorScheme.error, fontSize: 12.5)),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: TextButton.icon(
|
||||
onPressed: _busy ? null : _paste,
|
||||
icon: const Icon(Icons.content_paste, size: 18),
|
||||
label: const Text('从剪贴板粘贴'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _busy ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _busy ? null : _connect,
|
||||
child: _busy
|
||||
? const SizedBox(
|
||||
width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Text('配对并连接'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -761,8 +761,9 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
for (final c in app.connections) ...[
|
||||
ListTile(
|
||||
title: Text(c.label),
|
||||
subtitle:
|
||||
Text(c.proxy.isEmpty ? c.url : '${c.url}\n经由 ${c.proxy}'),
|
||||
subtitle: Text(c.isRelay
|
||||
? '经由 Relay · ${c.serverId}'
|
||||
: (c.proxy.isEmpty ? c.url : '${c.url}\n经由 ${c.proxy}')),
|
||||
trailing: app.conn?.id == c.id
|
||||
? Text('已连接',
|
||||
style: TextStyle(
|
||||
@@ -784,6 +785,15 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
));
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.qr_code_2),
|
||||
title: const Text('配对链接'),
|
||||
subtitle: const Text('直连地址或 OpenChamber Relay'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
showPairingDialog(context, app);
|
||||
},
|
||||
),
|
||||
if (app.client != null)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.link_off),
|
||||
|
||||
Reference in new issue
Block a user