1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 21:35:40 +02:00

[240420][terminal][ssh] Support Disconnect before authenticating

This commit is contained in:
Martin Oberhuber 2008-07-11 15:08:17 +00:00
parent efa0518db4
commit 1511689c15

View file

@ -47,11 +47,6 @@ class SshConnection extends Thread {
private static int fgNo; private static int fgNo;
private final ITerminalControl fControl; private final ITerminalControl fControl;
private final SshConnector fConn; private final SshConnector fConn;
/**
* if true, the terminal has been disconnected. Used to tack
* disconnects that happen while the the terminal is still connecting.
*/
private boolean fDisconnected;
private Session fSession; private Session fSession;
protected SshConnection(SshConnector conn,ITerminalControl control) { protected SshConnection(SshConnector conn,ITerminalControl control) {
super("SshConnection-"+fgNo++); //$NON-NLS-1$ super("SshConnection-"+fgNo++); //$NON-NLS-1$
@ -113,6 +108,9 @@ class SshConnection extends Thread {
Session session = createSession(user, password, host, port, Session session = createSession(user, password, host, port,
ui, new NullProgressMonitor()); ui, new NullProgressMonitor());
synchronized (this) {
fSession = session;
}
//java.util.Hashtable config=new java.util.Hashtable(); //java.util.Hashtable config=new java.util.Hashtable();
//config.put("StrictHostKeyChecking", "no"); //config.put("StrictHostKeyChecking", "no");
@ -122,55 +120,56 @@ class SshConnection extends Thread {
session.setServerAliveInterval(nKeepalive); //default is 5 minutes session.setServerAliveInterval(nKeepalive); //default is 5 minutes
} }
session.connect(nTimeout); // making connection with timeout. session.connect(nTimeout); // making connection with timeout.
setSession(session);
ChannelShell channel=(ChannelShell) session.openChannel("shell"); //$NON-NLS-1$ ChannelShell channel=(ChannelShell) session.openChannel("shell"); //$NON-NLS-1$
channel.setPtyType("ansi"); //$NON-NLS-1$ channel.setPtyType("ansi"); //$NON-NLS-1$
channel.connect(); channel.connect();
// maybe the terminal was disconnected while we were connecting
if (isSessionConnected() && channel.isConnected()) {
fConn.setInputStream(channel.getInputStream()); fConn.setInputStream(channel.getInputStream());
fConn.setOutputStream(channel.getOutputStream()); fConn.setOutputStream(channel.getOutputStream());
fConn.setChannel(channel); fConn.setChannel(channel);
fControl.setState(TerminalState.CONNECTED); fControl.setState(TerminalState.CONNECTED);
// maybe the terminal was disconnected while we were connecting
// if that happened, lets disconnect....
if(isDisconnected()) {
session.disconnect();
return;
}
try { try {
// read data until the connection gets terminated // read data until the connection gets terminated
readDataForever(fConn.getInputStream()); readDataForever(fConn.getInputStream());
} catch (InterruptedIOException e) { } catch (InterruptedIOException e) {
// we got interrupted: we are done... // we got interrupted: we are done...
} }
// when reading is done, we set the state to closed }
fControl.setState(TerminalState.CLOSED);
} catch (Exception e) { } catch (Exception e) {
connectFailed(e.getMessage(),e.getMessage()); connectFailed(e.getMessage(),e.getMessage());
} finally { } finally {
// make sure the terminal is disconnected when the thread ends // make sure the terminal is disconnected when the thread ends
try {
disconnect(); disconnect();
} finally {
// when reading is done, we set the state to closed
fControl.setState(TerminalState.CLOSED);
} }
} }
synchronized void setSession(Session session) {
fSession = session;
} }
synchronized boolean isDisconnected() {
return fDisconnected; private synchronized boolean isSessionConnected() {
return fSession != null && fSession.isConnected();
} }
/** /**
* disconnect the ssh session * disconnect the ssh session
*/ */
void disconnect() { void disconnect() {
interrupt(); interrupt();
synchronized (this) { synchronized (this) {
fDisconnected=true;
if(fSession!=null) { if(fSession!=null) {
try {
fSession.disconnect(); fSession.disconnect();
} catch (Exception e) {
// Ignore NPE due to bug in JSch if disconnecting
// while not yet authenticated
}
fSession=null; fSession=null;
} }
} }
} }
/** /**
@ -218,10 +217,14 @@ class SshConnection extends Thread {
public void run() { public void run() {
// [168197] Replace JFace MessagDialog by SWT MessageBox // [168197] Replace JFace MessagDialog by SWT MessageBox
//retval[0] = MessageDialog.openQuestion(null, SshMessages.WARNING, str); //retval[0] = MessageDialog.openQuestion(null, SshMessages.WARNING, str);
if (isSessionConnected()) {
MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
mb.setText(SshMessages.WARNING); mb.setText(SshMessages.WARNING);
mb.setMessage(str); mb.setMessage(str);
retval[0] = (mb.open() == SWT.YES); retval[0] = (mb.open() == SWT.YES);
} else {
retval[0] = false;
}
} }
}); });
return retval[0]; return retval[0];
@ -230,6 +233,7 @@ class SshConnection extends Thread {
final String[] retval = new String[1]; final String[] retval = new String[1];
getStandardDisplay().syncExec(new Runnable() { getStandardDisplay().syncExec(new Runnable() {
public void run() { public void run() {
if (isSessionConnected()) {
UserValidationDialog uvd = new UserValidationDialog(null, fConnectionId, fUser, message); UserValidationDialog uvd = new UserValidationDialog(null, fConnectionId, fUser, message);
uvd.setUsernameMutable(false); uvd.setUsernameMutable(false);
if (uvd.open() == Window.OK) { if (uvd.open() == Window.OK) {
@ -237,6 +241,9 @@ class SshConnection extends Thread {
} else { } else {
retval[0] = null; retval[0] = null;
} }
} else {
retval[0] = null;
}
} }
}); });
return retval[0]; return retval[0];
@ -261,11 +268,13 @@ class SshConnection extends Thread {
public void run() { public void run() {
// [168197] Replace JFace MessagDialog by SWT MessageBox // [168197] Replace JFace MessagDialog by SWT MessageBox
// MessageDialog.openInformation(null, SshMessages.INFO, message); // MessageDialog.openInformation(null, SshMessages.INFO, message);
if (isSessionConnected()) {
MessageBox mb = new MessageBox(null, SWT.ICON_INFORMATION | SWT.OK); MessageBox mb = new MessageBox(null, SWT.ICON_INFORMATION | SWT.OK);
mb.setText(SshMessages.INFO); mb.setText(SshMessages.INFO);
mb.setMessage(message); mb.setMessage(message);
mb.open(); mb.open();
} }
}
}); });
} }
public String[] promptKeyboardInteractive(final String destination, public String[] promptKeyboardInteractive(final String destination,
@ -285,10 +294,13 @@ class SshConnection extends Thread {
final String[][] finResult = new String[1][]; final String[][] finResult = new String[1][];
getStandardDisplay().syncExec(new Runnable() { getStandardDisplay().syncExec(new Runnable() {
public void run() { public void run() {
KeyboardInteractiveDialog dialog = new KeyboardInteractiveDialog(null, if (isSessionConnected()) {
fConnectionId, destination, name, instruction, prompt, echo); KeyboardInteractiveDialog dialog = new KeyboardInteractiveDialog(null, fConnectionId, destination, name, instruction, prompt, echo);
dialog.open(); dialog.open();
finResult[0]=dialog.getResult(); finResult[0] = dialog.getResult();
} else {
finResult[0] = null; // indicate cancel to JSch
}
} }
}); });
String[] result=finResult[0]; String[] result=finResult[0];
@ -304,24 +316,11 @@ class SshConnection extends Thread {
return null; return null;
} }
} }
/**
* Callback to indicate that a connection is about to be attempted
*/
public void aboutToConnect() {
fAttemptCount = 0;
}
/**
* Callback to indicate that a connection was made
*/
public void connectionMade() {
fAttemptCount = 0;
}
} }
private void connectFailed(String terminalText, String msg) { private void connectFailed(String terminalText, String msg) {
Logger.log(terminalText); Logger.log(terminalText);
fControl.displayTextInTerminal(terminalText); fControl.displayTextInTerminal(terminalText);
fControl.setState(TerminalState.CLOSED);
fControl.setMsg(msg); fControl.setMsg(msg);
} }
} }