Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SSH keyboard-interactive authentication (2FA/TOTP prompts) with secure dialog.
- Opt-in OpenCode agent tunnel over SSH local forward to remote `127.0.0.1:agentPort`.
- Import OpenSSH `~/.ssh/config` from the connected host into profiles.
- Profile search, favorites pin, and duplicate in the profile manager.

## [1.3.0] - 2026-11-15

Expand Down
12 changes: 9 additions & 3 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## SSH client

Connect to remote hosts with password or private-key auth. The built-in xterm
terminal supports colors, scrollback, and a shortcut bar for Ctrl, Alt, arrows,
and custom snippets.
Connect to remote hosts with password, private-key, or keyboard-interactive
(2FA/TOTP) auth. The built-in xterm terminal supports colors, scrollback, and a
shortcut bar for Ctrl, Alt, arrows, and custom snippets. Profiles can be
searched, pinned as favorites, duplicated, or imported from the host’s
`~/.ssh/config` over an active SFTP session.

### SFTP (Client + Agents)

Expand All @@ -24,6 +26,10 @@ The **Agents** tab talks to an OpenCode HTTP API on the connected host.
Sessions are scoped by project directory; slash commands handle model and
provider configuration (`/model`, `/models`, `/connect`, `/agent`).

When OpenCode listens only on the Windows host loopback, enable **Tunnel agent
over SSH** on the profile: the app opens an SSH local forward to
`127.0.0.1:agentPort` and connects Agents through that tunnel.

## Android extras

- **Foreground service** keeps SSH and agent sessions alive in the background
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MyApp extends StatelessWidget {
update: (context, ssh, agent) {
final provider = agent ?? AgentProvider();
provider.onLog = ssh.addLog;
provider.ssh = ssh;
return provider;
},
),
Expand Down
8 changes: 8 additions & 0 deletions lib/models/agent_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:opencode_api/opencode_api.dart';
import '../models/agent_model_option.dart';
import '../models/ssh_profile.dart';
import '../services/opencode_connection_service.dart';
import '../services/ssh_local_forward.dart';

class AgentConnection {
AgentConnection({
Expand All @@ -30,11 +31,14 @@ class AgentConnection {
this.sessionInputTokens = 0,
this.sessionOutputTokens = 0,
this.sessionCost,
this.agentTunnel,
});

final String id;
final SSHProfile profile;
final OpenCodeConnectionService service;
/// Active SSH local forward when [SSHProfile.tunnelAgent] is enabled.
final SshLocalForward? agentTunnel;
List<Session> sessions;
String? activeSessionId;
List<MessageWithParts> messages;
Expand Down Expand Up @@ -74,5 +78,9 @@ class AgentConnection {
unawaited(connectionEventSub?.cancel());
eventSub = null;
connectionEventSub = null;
final tunnel = agentTunnel;
if (tunnel != null && !tunnel.isClosed) {
unawaited(tunnel.close());
}
}
}
17 changes: 17 additions & 0 deletions lib/models/ssh_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class SSHProfile {
final String? agentPassword;
final SessionManager sessionManager;

/// When true, Agents HTTP goes through an SSH local forward to
/// `127.0.0.1:agentPort` on the remote host (for OpenCode bound to loopback).
final bool tunnelAgent;

/// Pinned to the top of the profile list when true.
final bool isFavorite;

SSHProfile({
required this.name,
required this.host,
Expand All @@ -38,6 +45,8 @@ class SSHProfile {
this.agentUsername = 'opencode',
this.agentPassword,
this.sessionManager = SessionManager.none,
this.tunnelAgent = false,
this.isFavorite = false,
}) : id = id ?? const Uuid().v4();

String get agentBaseUrl =>
Expand Down Expand Up @@ -72,6 +81,8 @@ class SSHProfile {
String? agentUsername,
String? agentPassword,
SessionManager? sessionManager,
bool? tunnelAgent,
bool? isFavorite,
}) {
return SSHProfile(
id: id ?? this.id,
Expand All @@ -89,6 +100,8 @@ class SSHProfile {
agentUsername: agentUsername ?? this.agentUsername,
agentPassword: agentPassword ?? this.agentPassword,
sessionManager: sessionManager ?? this.sessionManager,
tunnelAgent: tunnelAgent ?? this.tunnelAgent,
isFavorite: isFavorite ?? this.isFavorite,
);
}

Expand All @@ -110,6 +123,8 @@ class SSHProfile {
'agentUsername': agentUsername,
if (includePassword) 'agentPassword': agentPassword,
'sessionManager': sessionManager.name,
'tunnelAgent': tunnelAgent,
'isFavorite': isFavorite,
};
}

Expand All @@ -132,6 +147,8 @@ class SSHProfile {
sessionManager: SessionManagerX.fromStorage(
json['sessionManager'] as String?,
),
tunnelAgent: json['tunnelAgent'] as bool? ?? false,
isFavorite: json['isFavorite'] as bool? ?? false,
);
}
}
66 changes: 64 additions & 2 deletions lib/providers/agent_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import '../models/agent_connection.dart';
import '../models/agent_permission_request.dart';
import '../models/agent_prompt_attachment.dart';
import '../models/ssh_profile.dart';
import '../providers/ssh_provider.dart';
import '../services/config_repository.dart';
import '../services/opencode_connection_service.dart';
import '../services/opencode_v2_mappers.dart';
import '../services/ssh_local_forward.dart';
import '../utils/agent_prompt_utils.dart';
import '../utils/agent_session_utils.dart';

Expand All @@ -19,6 +21,10 @@ class AgentProvider extends ChangeNotifier {
: _config = config ?? ConfigServiceRepository();

final ConfigRepository _config;
SSHProvider? _ssh;

/// Wired from [ChangeNotifierProxyProvider] so agent tunnels can reuse SSH.
set ssh(SSHProvider value) => _ssh = value;

final List<AgentConnection> _connections = [];
String? _activeConnectionId;
Expand Down Expand Up @@ -49,8 +55,17 @@ class AgentProvider extends ChangeNotifier {
}

Future<AgentConnection> connectFromProfile(SSHProfile profile) async {
SshLocalForward? tunnel;
var baseUrl = profile.agentBaseUrl;

if (profile.tunnelAgent) {
final prepared = await _prepareAgentTunnel(profile);
tunnel = prepared.tunnel;
baseUrl = prepared.baseUrl;
}

final service = OpenCodeConnectionService(
baseUrl: profile.agentBaseUrl,
baseUrl: baseUrl,
username: profile.effectiveAgentUsername,
password: profile.effectiveAgentPassword,
allowInsecureTls: profile.useHttps && profile.allowSelfSignedCerts,
Expand All @@ -72,25 +87,72 @@ class AgentProvider extends ChangeNotifier {
sessions: sessions,
isConnected: true,
selectedDirectory: directory,
agentTunnel: tunnel,
);

_wireConnection(connection);

_connections.add(connection);
_activeConnectionId = connection.id;
onLog?.call(
'Agent connected: ${profile.name} (${profile.agentBaseUrl})',
'Agent connected: ${profile.name} ($baseUrl)',
);
notifyListeners();
// ignore: unawaited_futures
_loadMetadata(connection);
return connection;
} catch (e) {
service.dispose();
if (tunnel != null && !tunnel.isClosed) {
await tunnel.close();
}
rethrow;
}
}

/// Ensures an SSH session exists and opens a loopback forward to the agent.
Future<({SshLocalForward tunnel, String baseUrl})> _prepareAgentTunnel(
SSHProfile profile,
) async {
final ssh = _ssh;
if (ssh == null) {
throw StateError(
'SSH provider unavailable. Cannot tunnel agent without SSH.',
);
}

var session = ssh.findConnectedSessionForProfile(profile);
if (session == null) {
onLog?.call(
'Agent tunnel: connecting SSH session for ${profile.name}...',
);
session = ssh.createSessionFromProfile(profile, name: profile.name);
await ssh.connectSession(session.id);
session = ssh.findConnectedSessionForProfile(profile);
}

final client = session?.client;
if (session == null || client == null || !session.isConnected) {
throw StateError(
'No SSH session for ${profile.host}. Connect Client first, '
'or check credentials, then retry Agents with "Tunnel agent" enabled.',
);
}

final tunnel = await SshLocalForward.start(
client: client,
remoteHost: '127.0.0.1',
remotePort: profile.agentPort,
);
final scheme = profile.useHttps ? 'https' : 'http';
final baseUrl = '$scheme://127.0.0.1:${tunnel.localPort}';
onLog?.call(
'Agent tunnel ready: $baseUrl -> '
'${profile.host} 127.0.0.1:${profile.agentPort}',
);
return (tunnel: tunnel, baseUrl: baseUrl);
}

Future<AgentConnection> connectFromUrl({
required String url,
String username = 'opencode',
Expand Down
39 changes: 38 additions & 1 deletion lib/providers/ssh_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class SSHProvider extends ChangeNotifier {
required String presentedFingerprint,
})? onHostKeyMismatch;

/// UI hook for SSH keyboard-interactive prompts (2FA/TOTP). Return null to
/// cancel / skip the method. When null, keyboard-interactive is not offered.
Future<List<String>?> Function(SSHUserInfoRequest request)?
onUserInfoRequest;

Future<void> loadConfig() async {
var profileData = await _config.getProfiles();
final migratedProfiles =
Expand Down Expand Up @@ -294,6 +299,17 @@ class SSHProvider extends ChangeNotifier {
return null;
}

/// Prefers a connected session for [profile.id], else any session to the host.
SessionEntry? findConnectedSessionForProfile(SSHProfile profile) {
for (final session in sessions) {
if (!session.isConnected || session.client == null) continue;
if (session.profile.id == profile.id) {
return session;
}
}
return findConnectedSessionForHost(profile.host);
}

bool _hostsMatch(String a, String b) {
final na = a.toLowerCase();
final nb = b.toLowerCase();
Expand All @@ -309,7 +325,10 @@ class SSHProvider extends ChangeNotifier {
try {
addLog('Connecting to ${profile.host}:${profile.port}...');
final auth = await resolveSshAuthMaterial(profile);
if (!auth.hasKeyAuth && !auth.hasPasswordAuth) {
final canKeyboardInteractive = onUserInfoRequest != null;
if (!auth.hasKeyAuth &&
!auth.hasPasswordAuth &&
!canKeyboardInteractive) {
throw StateError(
'No credentials: set a password or select an SSH key for this profile',
);
Expand All @@ -325,6 +344,24 @@ class SSHProvider extends ChangeNotifier {
username: profile.username,
identities: auth.identities,
onPasswordRequest: auth.hasPasswordAuth ? () => auth.password! : null,
onUserInfoRequest: canKeyboardInteractive
? (request) async {
final hook = onUserInfoRequest;
if (hook == null) {
return null;
}
addLog(
'Keyboard-interactive auth for ${profile.host}:${profile.port}',
);
return hook(request);
}
: null,
onUserauthBanner: (banner) {
final trimmed = banner.trim();
if (trimmed.isNotEmpty) {
addLog('SSH banner: $trimmed');
}
},
keepAliveInterval: const Duration(seconds: 15),
onVerifyHostKey: (type, fingerprintBytes) async {
final fingerprint = String.fromCharCodes(fingerprintBytes);
Expand Down
7 changes: 7 additions & 0 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../providers/settings_provider.dart';
import '../providers/ssh_provider.dart';
import '../widgets/log_viewer.dart';
import '../widgets/host_key_dialog.dart';
import '../widgets/keyboard_interactive_dialog.dart';
import '../widgets/profile_manager.dart';
import '../widgets/key_manager.dart';
import '../widgets/keyboard_shortcut_bar.dart';
Expand Down Expand Up @@ -104,6 +105,12 @@ class _HomeScreenState extends State<HomeScreen> {
presentedFingerprint: presentedFingerprint,
);
};
ssh.onUserInfoRequest = (request) {
if (!mounted) {
return Future<List<String>?>.value(null);
}
return showKeyboardInteractiveDialog(context, request);
};
});
}

Expand Down
Loading
Loading