153 lines
3.7 KiB
Dart
153 lines
3.7 KiB
Dart
// A loopback TCP listener that carries each accepted connection through the
|
|||
|
|
// OpenChamber host's dev-server tunnel. The WebView loads
|
||
|
|
// http://127.0.0.1:<localPort>, so a dev server running on the host (its own
|
||
|
|
// localhost) can be previewed from the phone; the original URL stays in the
|
||
|
|
// address bar.
|
||
|
|
import 'dart:async';
|
||
|
|
import 'dart:io';
|
||
|
|
|
||
|
|
import '../state.dart';
|
||
|
|
import 'transport.dart';
|
||
|
|
|
||
|
|
/// True for an address that means "the host running the dev server".
|
||
|
|
bool isLoopbackDevHost(String host) {
|
||
|
|
final h = host.trim().toLowerCase();
|
||
|
|
return h == 'localhost' ||
|
||
|
|
h == '127.0.0.1' ||
|
||
|
|
h == '::1' ||
|
||
|
|
h == '[::1]' ||
|
||
|
|
h == '0.0.0.0' ||
|
||
|
|
h == '::';
|
||
|
|
}
|
||
|
|
|
||
|
|
/// The port a dev-server URL names (80/443 when it has none), or null.
|
||
|
|
int? devServerPort(Uri uri) {
|
||
|
|
if (!isLoopbackDevHost(uri.host)) return null;
|
||
|
|
if (uri.hasPort) return uri.port > 0 ? uri.port : null;
|
||
|
|
return uri.scheme == 'https' ? 443 : 80;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// The URL the WebView loads: the same path on the tunnel's local port.
|
||
|
|
Uri localTunnelUri(Uri original, int localPort) => original.replace(
|
||
|
|
scheme: 'http',
|
||
|
|
host: '127.0.0.1',
|
||
|
|
port: localPort,
|
||
|
|
);
|
||
|
|
|
||
|
|
/// Maps a URL the WebView reports back to the original host's URL, so the
|
||
|
|
/// address bar and history show the dev server's address, not the listener's.
|
||
|
|
String displayUrlFor(String webUrl, int localPort, String displayOrigin) {
|
||
|
|
final local = 'http://127.0.0.1:$localPort';
|
||
|
|
if (webUrl == local) return displayOrigin;
|
||
|
|
if (webUrl.startsWith('$local/')) {
|
||
|
|
return '$displayOrigin${webUrl.substring(local.length)}';
|
||
|
|
}
|
||
|
|
return webUrl;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// A local TCP listener for [remotePort] on the OpenChamber host.
|
||
|
|
class DevTunnel {
|
||
|
|
final AppState app;
|
||
|
|
final int remotePort;
|
||
|
|
|
||
|
|
ServerSocket? _server;
|
||
|
|
final Set<_Pipe> _pipes = {};
|
||
|
|
bool _closed = false;
|
||
|
|
|
||
|
|
DevTunnel(this.app, this.remotePort);
|
||
|
|
|
||
|
|
int get localPort => _server?.port ?? 0;
|
||
|
|
|
||
|
|
Future<int> start() async {
|
||
|
|
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||
|
|
if (_closed) {
|
||
|
|
await server.close();
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
_server = server;
|
||
|
|
server.listen(_accept);
|
||
|
|
return server.port;
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _accept(Socket socket) async {
|
||
|
|
if (_closed) {
|
||
|
|
socket.destroy();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
TransportSocket? tunnel;
|
||
|
|
try {
|
||
|
|
tunnel = await app.openDevTunnel(remotePort);
|
||
|
|
} catch (_) {
|
||
|
|
socket.destroy();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (_closed) {
|
||
|
|
socket.destroy();
|
||
|
|
await tunnel.close().catchError((Object _) {});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
late final _Pipe pipe;
|
||
|
|
pipe = _Pipe(socket, tunnel, () => _pipes.remove(pipe));
|
||
|
|
_pipes.add(pipe);
|
||
|
|
socket.listen(
|
||
|
|
pipe.toTunnel,
|
||
|
|
onError: (_) => pipe.close(),
|
||
|
|
onDone: () => pipe.close(),
|
||
|
|
);
|
||
|
|
tunnel.messages.listen(
|
||
|
|
pipe.toSocket,
|
||
|
|
onError: (_) => pipe.close(),
|
||
|
|
onDone: () => pipe.close(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> close() async {
|
||
|
|
if (_closed) return;
|
||
|
|
_closed = true;
|
||
|
|
await _server?.close();
|
||
|
|
_server = null;
|
||
|
|
for (final pipe in [..._pipes]) {
|
||
|
|
await pipe.close();
|
||
|
|
}
|
||
|
|
_pipes.clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class _Pipe {
|
||
|
|
final Socket socket;
|
||
|
|
final TransportSocket tunnel;
|
||
|
|
final void Function() onClosed;
|
||
|
|
bool _closed = false;
|
||
|
|
|
||
|
|
_Pipe(this.socket, this.tunnel, this.onClosed);
|
||
|
|
|
||
|
|
void toTunnel(List<int> data) {
|
||
|
|
if (_closed) return;
|
||
|
|
try {
|
||
|
|
tunnel.add(data);
|
||
|
|
} catch (_) {
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void toSocket(List<int> data) {
|
||
|
|
if (_closed) return;
|
||
|
|
try {
|
||
|
|
socket.add(data);
|
||
|
|
} catch (_) {
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> close() async {
|
||
|
|
if (_closed) return;
|
||
|
|
_closed = true;
|
||
|
|
try {
|
||
|
|
socket.destroy();
|
||
|
|
} catch (_) {}
|
||
|
|
try {
|
||
|
|
await tunnel.close();
|
||
|
|
} catch (_) {}
|
||
|
|
onClosed();
|
||
|
|
}
|
||
|
|
}
|