feat: preview the host's dev servers through the dev tunnel

- loopback dev-server URLs are no longer opened on the phone's network: the
  panel binds a local 127.0.0.1 TCP listener and pipes each connection over
  an authenticated WebSocket to the host's /api/dev-tunnel, which dials its
  own loopback port; the WebView keeps the original address in the bar while
  the page, absolute URLs, cookies and HMR sockets use the tunneled origin
- the empty state lists /api/dev-servers results for the project (only ports
  that answered with a web page), with title, URL and command; tapping one
  opens it through the tunnel
- relay transports mint oc_url_token themselves; a direct connection mints
  and caches /auth/url-token and also sends the bearer
- a new loopback port opened from a tunnelled page gets its own tunnel
- version 0.7.1+701; tests cover the URL mapping and tunnel helpers
This commit is contained in:
blumia committed 2026-12-04 12:21:09 +08:00
1 parent 318276d3b9
commit 695d27cdd7
12 files changed
+545 -39

No files matched your search

+37
View File
@@ -13,6 +13,7 @@ import 'api/client.dart';
import 'api/models.dart';
import 'api/pairing.dart';
import 'api/relay_transport.dart';
import 'api/transport.dart';
import 'l10n.dart';
import 'storage.dart';
import 'terminal_state.dart';
@@ -613,6 +614,8 @@ class AppState extends ChangeNotifier {
archivedLoaded = false;
_sessionsNext = null;
perms = [];
_tunnelUrlToken = '';
_tunnelUrlTokenExpiresAt = DateTime.fromMillisecondsSinceEpoch(0);
error = null;
_connectionError = false;
notifyListeners();
@@ -727,6 +730,40 @@ class AppState extends ChangeNotifier {
} catch (_) {}
}
String _tunnelUrlToken = '';
DateTime _tunnelUrlTokenExpiresAt =
DateTime.fromMillisecondsSinceEpoch(0);
/// Opens the host's dev-server tunnel for [port] as a raw WebSocket. The
/// browser panel pipes a loopback TCP listener through it, so a page served
/// by the OpenChamber host's dev server loads on the phone.
Future<TransportSocket> openDevTunnel(int port) async {
final cl = client;
if (cl == null) throw StateError(tr('Not connected to a server'));
var query = 'port=$port';
// A relay's transport mints its own URL token; a direct socket cannot
// carry the bearer in a browser, so mint one here as the web client does.
if (conn?.isRelay != true) {
final now = DateTime.now();
if (_tunnelUrlToken.isEmpty ||
!now.isBefore(_tunnelUrlTokenExpiresAt)) {
final minted = await cl.mintUrlToken();
_tunnelUrlToken = minted.token;
_tunnelUrlTokenExpiresAt = now.add(
Duration(seconds: minted.expiresIn) - const Duration(seconds: 20));
}
if (_tunnelUrlToken.isNotEmpty) {
query += '&oc_url_token=${Uri.encodeQueryComponent(_tunnelUrlToken)}';
}
}
return cl.transport.webSocket('/api/dev-tunnel',
rawQuery: query,
headers: {
if (cl.token != null && cl.token!.isNotEmpty)
'authorization': 'Bearer ${cl.token}',
});
}
/// Adds a folder to the server's project list, then refreshes the list so
/// its settings row decorates it. Returns the resolved project; the caller
/// may select it for a new session. Throws when the server refuses the path.