mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-02 21:50:43 +08:00
SSH support
This commit is contained in:
@ -14,6 +14,7 @@ import ai.chat2db.spi.Plugin;
|
||||
import ai.chat2db.spi.config.DBConfig;
|
||||
import ai.chat2db.spi.config.DriverConfig;
|
||||
import ai.chat2db.spi.model.SSHInfo;
|
||||
import ai.chat2db.spi.ssh.SSHManager;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -0,0 +1,41 @@
|
||||
package ai.chat2db.spi.ssh;
|
||||
|
||||
import com.jcraft.jsch.UserInfo;
|
||||
|
||||
public class MyUserInfo implements UserInfo {
|
||||
|
||||
private String passphrase;
|
||||
|
||||
public MyUserInfo(String passphrase) {
|
||||
this.passphrase = passphrase;
|
||||
}
|
||||
@Override
|
||||
public String getPassphrase() {
|
||||
return passphrase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean promptPassword(String s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean promptPassphrase(String s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean promptYesNo(String s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMessage(String s) {
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
|
||||
package ai.chat2db.spi.sql;
|
||||
package ai.chat2db.spi.ssh;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import ai.chat2db.spi.model.SSHInfo;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -35,8 +36,14 @@ public class SSHManager {
|
||||
}
|
||||
try {
|
||||
JSch jSch = new JSch();
|
||||
if (StringUtils.isNotBlank(ssh.getKeyFile()) && StringUtils.isNotBlank(ssh.getPassphrase())) {
|
||||
jSch.addIdentity(ssh.getKeyFile(), ssh.getPassphrase());
|
||||
}
|
||||
session = jSch.getSession(ssh.getUserName(), ssh.getHostName(), Integer.parseInt(ssh.getPort()));
|
||||
session.setPassword(ssh.getPassword());
|
||||
if (StringUtils.isBlank(ssh.getKeyFile()) || StringUtils.isBlank(ssh.getPassphrase())) {
|
||||
session.setPassword(ssh.getPassword());
|
||||
}
|
||||
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
session.connect();
|
||||
SSH_SESSION_MAP.put(ssh, session);
|
||||
@ -44,10 +51,11 @@ public class SSHManager {
|
||||
throw new RuntimeException("create ssh session error", e);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(ssh.getLocalPort()) && StringUtils.isNotBlank(ssh.getRHost())
|
||||
&& StringUtils.isNotBlank(ssh.getRPort())) {
|
||||
if (StringUtils.isNotBlank(ssh.getRHost()) && StringUtils.isNotBlank(ssh.getRPort())) {
|
||||
try {
|
||||
int port1 = session.setPortForwardingL(Integer.parseInt(ssh.getLocalPort()), ssh.getRHost(),
|
||||
int localPort = !StringUtils.isBlank(ssh.getLocalPort()) ? Integer.parseInt(ssh.getLocalPort())
|
||||
: NetUtil.getUsableLocalPort();
|
||||
session.setPortForwardingL(localPort, ssh.getRHost(),
|
||||
Integer.parseInt(ssh.getRPort()));
|
||||
} catch (Exception e) {
|
||||
if (session != null && session.isConnected()) {
|
@ -20,7 +20,7 @@ import ai.chat2db.spi.enums.DataTypeEnum;
|
||||
import ai.chat2db.spi.model.DataSourceConnect;
|
||||
import ai.chat2db.spi.model.SSHInfo;
|
||||
import ai.chat2db.spi.sql.IDriverManager;
|
||||
import ai.chat2db.spi.sql.SSHManager;
|
||||
import ai.chat2db.spi.ssh.SSHManager;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
Reference in New Issue
Block a user