openTunnel method

Future<TunnelHandle> openTunnel({
  1. required int targetPort,
  2. String nodeId = '',
  3. String targetHost = 'localhost',
  4. int? publicPort,
  5. bool local = false,
})

Opens a tunnel that exposes targetPort — reachable by nodeId, or this client's own machine when local is set — on a public Hub port. When publicPort is given the Hub validates it against its configured range; otherwise the Hub allocates one in range. Completes once the Hub confirms, or throws TunnelRejectedException if refused.

For a local tunnel this client must stay connected to serve the forwarded connections (it dials its own targetHost:targetPort for each).

Implementation

Future<TunnelHandle> openTunnel({
  required int targetPort,
  String nodeId = '',
  String targetHost = 'localhost',
  int? publicPort,
  bool local = false,
}) {
  _ensureConnected();
  final id = newId();
  final effectiveNode = local ? TunnelOpenRequest.localNode : nodeId;
  final completer = Completer<TunnelHandle>();
  _pendingTunnelOpens[id] = (
    completer: completer,
    nodeId: effectiveNode,
    targetPort: targetPort,
  );
  _connection!.send(
    ControlFrame(
      TunnelOpenRequest(
        requestId: id,
        nodeId: effectiveNode,
        targetHost: targetHost,
        targetPort: targetPort,
        publicPort: publicPort,
      ),
    ),
  );
  return completer.future;
}