Library for ESP32 Arduino enabling creation of reverse SSH tunnels using libssh2.
Option A: PlatformIO
# Add to your platformio.ini
lib_deps =
https://github.com/playmiel/ESP-Reverse_Tunneling_Libssh2.git
https://github.com/playmiel/libssh2_esp # libssh2 backend for ESP32Option B: Arduino IDE
- Download the project
- Copy files to your libraries folder
#include "ESP-Reverse_Tunneling_Libssh2.h"
void setup() {
Serial.begin(115200);
// WiFi configuration
WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
// SSH tunnel configuration with password
globalSSHConfig.setSSHServer("server.com", 22, "user", "password");
// OR with SSH key from memory (recommended for LittleFS)
globalSSHConfig.setSSHKeyAuth("server.com", 22, "user", "/ssh_key");
// Create and start tunnel
SSHTunnel tunnel;
tunnel.init();
tunnel.connectSSH();
}This library supports three methods for SSH key authentication:
-
Memory-based authentication (recommended for ESP32/LittleFS):
globalSSHConfig.setSSHKeyAuth("server.com", 22, "user", "/ssh_key");
-
Direct memory loading:
globalSSHConfig.setSSHKeyAuthFromMemory("server.com", 22, "user", privateKey, publicKey);
-
Manual key loading:
globalSSHConfig.loadSSHKeysFromLittleFS("/ssh_key");
📖 Detailed guide: SSH Keys with Memory Authentication
For production environments, enable host key verification to prevent Man-in-the-Middle attacks:
// Configure SSH with host key verification
globalSSHConfig.setSSHKeyAuthFromMemory("server.com", 22, "user", privateKey, publicKey);
// Enable host key verification (recommended for production)
globalSSHConfig.setHostKeyVerification(
"SHA256:abcd1234efgh5678ijkl9012mnop3456qrst7890uvwx1234yz56", // Accept OpenSSH format or 64-char hex
"ssh-ed25519",
true
);
// Optional: receive a diagnostic callback if the fingerprint changes
globalSSHConfig.setHostKeyMismatchCallback(
[](const String& expected, const String& actual, const String& keyType, void*) {
LOGF_W("HOSTKEY", "Mismatch for %s (expected %s, got %s)", keyType.c_str(), expected.c_str(), actual.c_str());
}
);📖 Security guide: Host Key Verification Documentation
pio run # Compilation
pio run --target upload # Upload to ESP32This project provides two example formats:
- File:
examples/src/main.cpp - Usage: Compiled when running
pio runin the examples/ directory - Features: Full PlatformIO integration with advanced logging
For more technical details:
examples/- Usage examplesdocs/SSH_KEYS_MEMORY.md- SSH Key authentication guidedocs/HOST_KEY_VERIFICATION.md- Security and host verification
- Platform: ESP32 only
- Framework: Arduino
- Cryptographic Backend: mbedTLS
- Protocol: SSH2 with reverse tunneling
- Memory:
- ~19% RAM (used 46252 bytes from 327680 bytes)
- ~65% Flash (used 897321 bytes from 1310720 bytes)
Contributions are welcome! See documentation guides for more details.
See LICENSE file for details.
// Configure libssh2 keepalives alongside the existing periodic send
globalSSHConfig.setKeepAliveOptions(true, 30); // want-reply=1, 30s
// Adjust logging without toggling the debugEnabled flag
globalSSHConfig.setLogLevel(LOG_INFO);
// Advanced data-path tuning
globalSSHConfig.setBufferConfig(8192, 10, 1800000, 16 * 1024); // adjust ring buffer size
globalSSHConfig.setDataTaskConfig(6144, 1); // grow stack + pin task to core 1Retrieve the effective reverse tunnel port when you bind to remoteBindPort = 0:
SSHTunnel tunnel;
tunnel.init();
if (tunnel.connectSSH()) {
LOGF_I("SSH", "Remote listener bound on %d", tunnel.getBoundPort());
}