Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.net.ConnectException: Connection timed out, Post deployment of jsch-0.2.20.jar #727

Open
AbhishekSingh9996 opened this issue Dec 16, 2024 · 0 comments

Comments

@AbhishekSingh9996
Copy link

I have been using a generic code to create Connection with different data providers but due to key exchange alogirthms limitations for vendor 1 i had to deploy the jsch-0.2.20 jar within the application. Post deployment the Algorithm Negotation Failed was resolved for that data vendor 1 but one perfectly working connection (data vendor 2) is facing failure now. I was facing Algorithm Negotation Failed for ssh-rsa host key that was resolved appending the same in session.config but now i am facing java.net.ConnectException: Connection timed out.
(Also with backporting to older jsch version the connection starts working fine with data-vendor2 again.)
The code base is as below along with some important info logger statements revealing the issue is occuring just at session.connect().

Code:

public void connect(String hostName, int port, String user, String passWord) throws RBException {
System.out.println("Accessing the connect method");
logger.info("Trying to log in on server " + hostName + " as user " + user);
logger.info("Entered method 1 of connect");

JschSFTP loge = new JschSFTP();
JSch.setLogger(loge);

logger.info("Trying to log in on server " + hostName + " as user " + user);

this.hostName = hostName;
this.user = user;
this.passWord = passWord;
JSch jSch = new JSch();

try {
	session = jSch.getSession(user, hostName, port);
	logger.info("Block 1 user,Hostname and port are : "+user+", "+hostName+", "+port);
	config.put("StrictHostKeyChecking", "no");
	session.setConfig(config);
	session.setTimeout(timeOut * 1000);
	session.setPassword(passWord);

	session.connect();
	
	channel = session.openChannel("sftp");
	sftp = (ChannelSftp) channel;
	sftp.connect();

	loggedIn = true;
	logger.error("User " + user + " successfully logged in on server " + hostName);
	cwd = sftp.pwd();
} catch (JSchException e) {
	try {
		logger.info("Entered catch of method 1");
		logger.info("The error message in catch 1 is : "+e.getMessage());
		if (e.getMessage().contains("java.security.InvalidAlgorithmParameterException")||e.getMessage().contains("Algorithm negotiation fail")) {
			session.disconnect();
			Security.insertProviderAt(new BouncyCastleProvider(), 1);
			if(proxyHost==null || proxyHost.isEmpty()) {
				proxyPort=0;
			}
			session = getSession(jSch, hostName, user, passWord, 22, proxyHost, proxyPort);
			logger.info("Session setup done");
			logger.info("Session config after appending for host key : "+(session.getConfig("server_host_key")));
			logger.info("Session timeout is in : "+session.getTimeout());
			session.connect();
			if(session!=null) {
				logger.info("Session established via catch 1");
			}
			channel = session.openChannel("sftp");
			if(channel!=null) {
				logger.info("Channel established via catch 1");
			}
			sftp = (ChannelSftp) channel;
			sftp.connect();
			if(sftp!=null) {
				logger.info("sftp established via catch 1");
			}
			loggedIn = true;
			logger.error("User " + user + " successfully logged in on server " + hostName);
			cwd = sftp.pwd();
		} else {
			logger.info("Entered else block of catch 1");
			loggedIn = false;
			logger.fatal("User " + user + " failed to log in on server " + hostName);
			throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1003L, e);
		}
	} catch (JSchException ex) {
		logger.info("Entered JSCHException of block 1");
		logger.info("The exception from jsch block is "+ex.getMessage());
		loggedIn = false;
		logger.fatal("User " + user + " failed to log in on server " + hostName);
		throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1003L, ex);
	}

	catch (SftpException ex) {
		logger.info("Entered SFTPException of block 1");
		throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1020L, ex);
	} catch (SecurityException ex) {
		logger.fatal(ex);
		throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1020L, ex);
	}
} catch (SftpException e) {
	throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1020L, e);
} catch (SecurityException e) {
	logger.fatal(e);
	throw new RBException(com.j2fe.ra.fileSystem.ftp.FTP.class, 1020L, e);
}

}

public static Session getSession(JSch jsch, String host, String user, String password, int port, String proxyHost,
int proxyPort) throws JSchException {

Session session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
logger.info("Session config for host key : "+(session.getConfig("server_host_key")));
config.put("server_host_key", "ssh-rsa,"+session.getConfig("server_host_key"));
logger.info("Session config after appending for host key : "+(config.get("server_host_key")));
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(config);
session.setTimeout(1800*1000);
if (proxyHost != null) {
	session.setProxy(new ProxySOCKS5(proxyHost, proxyPort));
}
if (password != null) {
	session.setPassword(password);
}
return session;

}

And the logs are:

Retry # 2 of method connect necessary. Problem: ERROR: Failed to login to (S)FTP Server.
Error occoured during reconnect to FTP server
Trying to log in
Entered method 1 of connect
Trying to log in on server
Block 1 user,Hostname and port are *********************************************
Entered catch of method 1
The error message in catch 1 is : Algorithm negotiation fail: algorithmName="server_host_key" jschProposal="ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256" serverProposal="ssh-rsa,ssh-dss"
Session config for host key : ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
Session config after appending for host key : ssh-rsa,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
Session setup done
Session config after appending for host key : ssh-rsa,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256
Session timeout is in : 1800000
Entered JSCHException of block 1
The exception from jsch block is java.net.ConnectException: Connection timed out

Kindly guide why after deploying this version i am facing this error but still after backporting the jsch jar to older version it again starts working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant