1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +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();
fConn.setInputStream(channel.getInputStream());
fConn.setOutputStream(channel.getOutputStream());
fConn.setChannel(channel);
fControl.setState(TerminalState.CONNECTED);
// maybe the terminal was disconnected while we were connecting // maybe the terminal was disconnected while we were connecting
// if that happened, lets disconnect.... if (isSessionConnected() && channel.isConnected()) {
if(isDisconnected()) { fConn.setInputStream(channel.getInputStream());
session.disconnect(); fConn.setOutputStream(channel.getOutputStream());
return; fConn.setChannel(channel);
fControl.setState(TerminalState.CONNECTED);
try {
// read data until the connection gets terminated
readDataForever(fConn.getInputStream());
} catch (InterruptedIOException e) {
// we got interrupted: we are done...
}
} }
try {
// read data until the connection gets terminated
readDataForever(fConn.getInputStream());
} catch (InterruptedIOException e) {
// 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
disconnect(); try {
disconnect();
} finally {
// when reading is done, we set the state to closed
fControl.setState(TerminalState.CLOSED);
}
} }
} }
synchronized void setSession(Session session) {
fSession = session; private synchronized boolean isSessionConnected() {
} return fSession != null && fSession.isConnected();
synchronized boolean isDisconnected() {
return fDisconnected;
} }
/** /**
* 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) {
fSession.disconnect(); try {
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);
MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); if (isSessionConnected()) {
mb.setText(SshMessages.WARNING); MessageBox mb = new MessageBox(fControl.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
mb.setMessage(str); mb.setText(SshMessages.WARNING);
retval[0] = (mb.open() == SWT.YES); mb.setMessage(str);
retval[0] = (mb.open() == SWT.YES);
} else {
retval[0] = false;
}
} }
}); });
return retval[0]; return retval[0];
@ -230,10 +233,14 @@ 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() {
UserValidationDialog uvd = new UserValidationDialog(null, fConnectionId, fUser, message); if (isSessionConnected()) {
uvd.setUsernameMutable(false); UserValidationDialog uvd = new UserValidationDialog(null, fConnectionId, fUser, message);
if (uvd.open() == Window.OK) { uvd.setUsernameMutable(false);
retval[0] = uvd.getPassword(); if (uvd.open() == Window.OK) {
retval[0] = uvd.getPassword();
} else {
retval[0] = null;
}
} else { } else {
retval[0] = null; retval[0] = null;
} }
@ -261,10 +268,12 @@ 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);
MessageBox mb = new MessageBox(null, SWT.ICON_INFORMATION | SWT.OK); if (isSessionConnected()) {
mb.setText(SshMessages.INFO); MessageBox mb = new MessageBox(null, SWT.ICON_INFORMATION | SWT.OK);
mb.setMessage(message); mb.setText(SshMessages.INFO);
mb.open(); mb.setMessage(message);
mb.open();
}
} }
}); });
} }
@ -275,7 +284,7 @@ class SshConnection extends Thread {
if (prompt.length == 0) { if (prompt.length == 0) {
// No need to prompt, just return an empty String array // No need to prompt, just return an empty String array
return new String[0]; return new String[0];
} }
try{ try{
if (fAttemptCount == 0 && fPassword != null && prompt.length == 1 && prompt[0].trim().equalsIgnoreCase("password:")) { //$NON-NLS-1$ if (fAttemptCount == 0 && fPassword != null && prompt.length == 1 && prompt[0].trim().equalsIgnoreCase("password:")) { //$NON-NLS-1$
// Return the provided password the first time but always prompt on subsequent tries // Return the provided password the first time but always prompt on subsequent tries
@ -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);
} }
} }