Dart
Dart
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final ssh = CkSsh();
// Connect and authenticate a standalone SSH object.
try {
ssh.connect('ssh.example.com', 22);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// The SSH password should come from a secure source rather than being hard-coded.
final sshPassword = 'mySshPassword';
try {
ssh.authenticatePw('sshUser', sshPassword);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final socket = CkSocket();
// Route subsequent Connect calls through the already connected and authenticated SSH object. The
// destination is reached by SSH port forwarding rather than by a direct TCP connection.
try {
socket.useSsh(ssh);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Connect to the destination through the SSH tunnel.
final bTls = false;
final maxWaitMs = 5000;
try {
socket.connect('db.internal', 5432, bTls, maxWaitMs);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Connected to the destination through the SSH tunnel.');
}