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:
1 parent
318276d3b9
commit
695d27cdd7
12 files changed
+545
-39
No files matched your search
+232
-33
@@ -1,17 +1,29 @@
|
||||
// The right sidebar's web browser: an address bar over an embedded WebView,
|
||||
// with history navigation, reload/stop and opening the page in the system
|
||||
// browser. The panel keeps its page while another tab is shown (the tab stack
|
||||
// browser. A loopback URL (the host's dev server) is carried over the
|
||||
// OpenChamber dev tunnel through a local TCP listener, so a dev server running
|
||||
// on the host can be previewed from the phone; other addresses use the phone's
|
||||
// network. The panel keeps its page while another tab is shown (the tab stack
|
||||
// keeps it alive).
|
||||
import 'dart:async';
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import '../api/dev_tunnel.dart';
|
||||
import '../api/models.dart';
|
||||
import '../l10n.dart';
|
||||
import '../state.dart';
|
||||
|
||||
class BrowserPanel extends StatefulWidget {
|
||||
const BrowserPanel({super.key});
|
||||
final AppState app;
|
||||
|
||||
/// The session/project whose dev servers the empty state lists.
|
||||
final String directory;
|
||||
|
||||
const BrowserPanel({super.key, required this.app, required this.directory});
|
||||
|
||||
@override
|
||||
State<BrowserPanel> createState() => _BrowserPanelState();
|
||||
@@ -19,9 +31,16 @@ class BrowserPanel extends StatefulWidget {
|
||||
|
||||
class _BrowserPanelState extends State<BrowserPanel> {
|
||||
static const MethodChannel _channel = MethodChannel('openchamber/browser');
|
||||
|
||||
final TextEditingController _url = TextEditingController();
|
||||
final List<String> _history = [];
|
||||
WebViewController? _web;
|
||||
DevTunnel? _tunnel;
|
||||
int? _tunnelPort;
|
||||
String? _displayOrigin;
|
||||
List<DevServerInfo> _servers = const [];
|
||||
bool _serversLoading = false;
|
||||
String? _serversError;
|
||||
bool _loading = false;
|
||||
bool _hasPage = false;
|
||||
bool _canBack = false;
|
||||
@@ -46,7 +65,7 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
_hasPage = true;
|
||||
_progress = 0;
|
||||
_error = null;
|
||||
_setUrl(url);
|
||||
_setUrl(_displayFor(url));
|
||||
});
|
||||
_syncNavigation();
|
||||
},
|
||||
@@ -55,9 +74,9 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
setState(() {
|
||||
_loading = false;
|
||||
_progress = 100;
|
||||
_setUrl(url);
|
||||
_setUrl(_displayFor(url));
|
||||
});
|
||||
_remember(url);
|
||||
_remember(_displayFor(url));
|
||||
_syncNavigation();
|
||||
},
|
||||
onProgress: (value) {
|
||||
@@ -65,7 +84,16 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
},
|
||||
onUrlChange: (change) {
|
||||
final url = change.url;
|
||||
if (url != null && mounted) setState(() => _setUrl(url));
|
||||
if (url != null && mounted) setState(() => _setUrl(_displayFor(url)));
|
||||
},
|
||||
onNavigationRequest: (request) {
|
||||
final rewritten = _rewriteNavigation(request.url);
|
||||
if (rewritten == null) return NavigationDecision.prevent;
|
||||
if (rewritten != request.url) {
|
||||
_web?.loadRequest(Uri.parse(rewritten));
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
onWebResourceError: (error) {
|
||||
if ((error.isForMainFrame ?? true) && mounted) {
|
||||
@@ -73,14 +101,48 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
}
|
||||
},
|
||||
));
|
||||
_loadServers();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
unawaited(_tunnel?.close());
|
||||
_url.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String _displayFor(String url) {
|
||||
final tunnel = _tunnel;
|
||||
final origin = _displayOrigin;
|
||||
if (tunnel == null || origin == null) return url;
|
||||
return displayUrlFor(url, tunnel.localPort, origin);
|
||||
}
|
||||
|
||||
String? _rewriteNavigation(String url) {
|
||||
final tunnel = _tunnel;
|
||||
final origin = _displayOrigin;
|
||||
if (tunnel == null || origin == null) return url;
|
||||
final local = 'http://127.0.0.1:${tunnel.localPort}';
|
||||
if (url == local || url.startsWith('$local/')) return url;
|
||||
final uri = Uri.tryParse(url);
|
||||
if (uri == null) return url;
|
||||
final originOfUrl =
|
||||
'${uri.scheme}://${uri.host}${uri.hasPort ? ':${uri.port}' : ''}';
|
||||
if (originOfUrl == origin) {
|
||||
return localTunnelUri(uri, tunnel.localPort).toString();
|
||||
}
|
||||
final port = devServerPort(uri);
|
||||
if (port != null) {
|
||||
if (port == _tunnelPort) {
|
||||
return localTunnelUri(uri, tunnel.localPort).toString();
|
||||
}
|
||||
// A loopback link to another dev port means another server on the host.
|
||||
unawaited(_navigate(url));
|
||||
return null;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<void> _syncNavigation() async {
|
||||
final web = _web;
|
||||
if (web == null) return;
|
||||
@@ -109,7 +171,63 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
if (_history.length > 20) _history.removeLast();
|
||||
}
|
||||
|
||||
void _navigate(String raw) {
|
||||
Future<void> _loadServers() async {
|
||||
final cl = widget.app.client;
|
||||
if (cl == null) return;
|
||||
setState(() {
|
||||
_serversLoading = true;
|
||||
_serversError = null;
|
||||
});
|
||||
try {
|
||||
final list = await cl.devServers(directory: widget.directory);
|
||||
if (!mounted) return;
|
||||
final dir = slashPath(widget.directory);
|
||||
final filtered = [
|
||||
for (final server in list)
|
||||
// A discovered port that answered with a page is a web dev server;
|
||||
// adb/emulator/native ports with no page would only be noise.
|
||||
if (server.title.isNotEmpty &&
|
||||
(server.directory.isEmpty ||
|
||||
slashPath(server.directory) == dir ||
|
||||
isUnderDirectory(server.directory, dir)))
|
||||
server,
|
||||
]..sort((a, b) => a.port.compareTo(b.port));
|
||||
setState(() {
|
||||
_servers = filtered;
|
||||
_serversLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_serversError = '$e';
|
||||
_serversLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<DevTunnel?> _ensureTunnel(Uri uri) async {
|
||||
final port = devServerPort(uri);
|
||||
if (port == null) {
|
||||
await _tunnel?.close();
|
||||
_tunnel = null;
|
||||
_tunnelPort = null;
|
||||
_displayOrigin = null;
|
||||
return null;
|
||||
}
|
||||
if (_tunnel != null && _tunnelPort == port) return _tunnel;
|
||||
await _tunnel?.close();
|
||||
_tunnel = null;
|
||||
_tunnelPort = null;
|
||||
final tunnel = DevTunnel(widget.app, port);
|
||||
await tunnel.start();
|
||||
_tunnel = tunnel;
|
||||
_tunnelPort = port;
|
||||
_displayOrigin =
|
||||
'${uri.scheme}://${uri.host}${uri.hasPort ? ':${uri.port}' : ''}';
|
||||
return tunnel;
|
||||
}
|
||||
|
||||
Future<void> _navigate(String raw) async {
|
||||
var text = raw.trim();
|
||||
if (text.isEmpty) return;
|
||||
if (!text.contains('://')) {
|
||||
@@ -127,7 +245,23 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
}
|
||||
FocusScope.of(context).unfocus();
|
||||
_setUrl(text);
|
||||
_web?.loadRequest(uri);
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final tunnel = await _ensureTunnel(uri);
|
||||
if (!mounted) return;
|
||||
final target = tunnel == null ? uri : localTunnelUri(uri, tunnel.localPort);
|
||||
setState(() => _hasPage = true);
|
||||
await _web?.loadRequest(target);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_loading = false;
|
||||
_error = trArgs('Could not open the dev server: {0}', [e]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _openExternal() async {
|
||||
@@ -257,6 +391,14 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
onPressed: _openExternal,
|
||||
child: const AppText('Open in system browser'),
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.dns_outlined, size: 18),
|
||||
onPressed: () {
|
||||
setState(() => _hasPage = false);
|
||||
_loadServers();
|
||||
},
|
||||
child: const AppText('Dev servers'),
|
||||
),
|
||||
SubmenuButton(
|
||||
leadingIcon: const Icon(Icons.history, size: 18),
|
||||
menuChildren: _history.isEmpty
|
||||
@@ -314,32 +456,89 @@ class _BrowserPanelState extends State<BrowserPanel> {
|
||||
}
|
||||
|
||||
Widget _empty(ThemeData theme) {
|
||||
return Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.public, size: 48, color: theme.hintColor),
|
||||
const SizedBox(height: 12),
|
||||
const AppText(
|
||||
'Web browser',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Icon(Icons.public, size: 40, color: theme.hintColor),
|
||||
const SizedBox(height: 8),
|
||||
const AppText(
|
||||
'Web browser',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
const AppText(
|
||||
'Enter a URL above, or open a dev server found on the OpenChamber host',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
Row(
|
||||
children: [
|
||||
const Expanded(
|
||||
child: AppText(
|
||||
'Dev servers',
|
||||
style: TextStyle(fontSize: 13.5, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh, size: 18),
|
||||
tooltip: tr('Refresh'),
|
||||
onPressed: _serversLoading ? null : _loadServers,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_serversLoading)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2)),
|
||||
),
|
||||
)
|
||||
else if (_servers.isNotEmpty)
|
||||
for (final server in _servers)
|
||||
ListTile(
|
||||
dense: true,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
leading: Icon(Icons.lan_outlined,
|
||||
size: 20, color: theme.hintColor),
|
||||
title: Text(server.label,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
subtitle: Text(
|
||||
[
|
||||
if (server.url.isNotEmpty) server.url,
|
||||
if (server.command.isNotEmpty) server.command,
|
||||
].join(' · '),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: const Icon(Icons.chevron_right, size: 18),
|
||||
onTap: () => _navigate(server.url.isEmpty
|
||||
? 'http://localhost:${server.port}'
|
||||
: server.url),
|
||||
)
|
||||
else
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: AppText(
|
||||
_serversError != null
|
||||
? trArgs('Could not list dev servers: {0}', [_serversError!])
|
||||
: 'No dev servers found. Start one in the terminal, then refresh.',
|
||||
style: TextStyle(fontSize: 12, color: theme.hintColor),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const AppText(
|
||||
'Enter a URL above to start browsing',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12.5),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
AppText(
|
||||
'The browser runs on this device; for a dev server on the OpenChamber host, enter the host\'s LAN address.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 11.5, color: theme.hintColor),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const AppText(
|
||||
'Loopback dev servers (localhost) are previewed through the OpenChamber host; other addresses use this device\'s network.',
|
||||
style: TextStyle(fontSize: 11.5),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user