From 9cf56ceee94e5892ed6f2437d012fe745aba4895 Mon Sep 17 00:00:00 2001 From: Michael Scharf Date: Thu, 7 Dec 2006 02:40:58 +0000 Subject: [PATCH] initial version --- .../org.eclipse.tm.terminal.ssh/.classpath | 7 + terminal/org.eclipse.tm.terminal.ssh/.project | 34 +++++ .../META-INF/MANIFEST.MF | 12 ++ .../build.properties | 5 + .../org.eclipse.tm.terminal.ssh/plugin.xml | 9 ++ .../eclipse/tm/terminal/ssh/ISshSettings.java | 25 +++ .../tm/terminal/ssh/SshConnection.java | 144 ++++++++++++++++++ .../eclipse/tm/terminal/ssh/SshConnector.java | 132 ++++++++++++++++ .../eclipse/tm/terminal/ssh/SshMessages.java | 30 ++++ .../tm/terminal/ssh/SshMessages.properties | 19 +++ .../eclipse/tm/terminal/ssh/SshSettings.java | 98 ++++++++++++ .../tm/terminal/ssh/SshSettingsPage.java | 94 ++++++++++++ 12 files changed, 609 insertions(+) create mode 100644 terminal/org.eclipse.tm.terminal.ssh/.classpath create mode 100644 terminal/org.eclipse.tm.terminal.ssh/.project create mode 100644 terminal/org.eclipse.tm.terminal.ssh/META-INF/MANIFEST.MF create mode 100644 terminal/org.eclipse.tm.terminal.ssh/build.properties create mode 100644 terminal/org.eclipse.tm.terminal.ssh/plugin.xml create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/ISshSettings.java create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnection.java create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnector.java create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.java create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.properties create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettings.java create mode 100644 terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettingsPage.java diff --git a/terminal/org.eclipse.tm.terminal.ssh/.classpath b/terminal/org.eclipse.tm.terminal.ssh/.classpath new file mode 100644 index 00000000000..751c8f2e504 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/terminal/org.eclipse.tm.terminal.ssh/.project b/terminal/org.eclipse.tm.terminal.ssh/.project new file mode 100644 index 00000000000..08d04394ca3 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/.project @@ -0,0 +1,34 @@ + + + org.eclipse.tm.terminal.ssh + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + net.sourceforge.metrics.builder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + net.sourceforge.metrics.nature + + diff --git a/terminal/org.eclipse.tm.terminal.ssh/META-INF/MANIFEST.MF b/terminal/org.eclipse.tm.terminal.ssh/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..300c4da5877 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Terminal ssh +Bundle-SymbolicName: org.eclipse.tm.terminal.ssh;singleton:=true +Bundle-Version: 0.0.9 +Bundle-Vendor: Eclipse.org +Bundle-Localization: plugin +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.eclipse.tm.terminal, + com.jcraft.jsch +Eclipse-LazyStart: false diff --git a/terminal/org.eclipse.tm.terminal.ssh/build.properties b/terminal/org.eclipse.tm.terminal.ssh/build.properties new file mode 100644 index 00000000000..e9863e281ea --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.xml diff --git a/terminal/org.eclipse.tm.terminal.ssh/plugin.xml b/terminal/org.eclipse.tm.terminal.ssh/plugin.xml new file mode 100644 index 00000000000..fe2bdfdf100 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/plugin.xml @@ -0,0 +1,9 @@ + + + + + + + + diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/ISshSettings.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/ISshSettings.java new file mode 100644 index 00000000000..d982c4254d9 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/ISshSettings.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ +package org.eclipse.tm.terminal.ssh; + +import org.eclipse.tm.terminal.ISettingsStore; + +public interface ISshSettings { + String getHost(); + String getUser(); + String getPassword(); + int getTimeout(); + int getPort(); + String getStatusString(String strConnected); + void load(ISettingsStore store); + void save(ISettingsStore store); +} diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnection.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnection.java new file mode 100644 index 00000000000..e61f6f771bd --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnection.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ +package org.eclipse.tm.terminal.ssh; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.tm.terminal.ITerminalControl; +import org.eclipse.tm.terminal.Logger; +import org.eclipse.tm.terminal.TerminalState; + +import com.jcraft.jsch.ChannelShell; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; +import com.jcraft.jsch.UserInfo; + +class SshConnection extends Thread { + private final ITerminalControl fControl; + private final SshConnector fConn; + protected SshConnection(SshConnector conn,ITerminalControl control) { + fControl = control; + fConn = conn; + fControl.setState(TerminalState.CONNECTING); + } + public void run() { + try { + int nTimeout = fConn.getTelnetSettings().getTimeout() * 1000; + String host = fConn.getTelnetSettings().getHost(); + String user = fConn.getTelnetSettings().getUser(); + int port=fConn.getTelnetSettings().getPort(); + Session session=fConn.getJsch().getSession(user, host, port); + //session.setPassword("your password"); + + // username and password will be given via UserInfo interface. + UserInfo ui=new MyUserInfo(fConn.getTelnetSettings().getPassword()); + session.setUserInfo(ui); + +// java.util.Hashtable config=new java.util.Hashtable(); +// config.put("StrictHostKeyChecking", "no"); +// session.setConfig(config); + + //session.connect(); + session.connect(nTimeout); // making connection with timeout. + + ChannelShell channel=(ChannelShell) session.openChannel("shell"); + + //hmm, now it gets a bit complicated + // Input and output streams are somehow confusing + PipedInputStream pin = new PipedInputStream(); + PipedOutputStream out = new PipedOutputStream(pin); + + PipedOutputStream pout = new PipedOutputStream(); + PipedInputStream in = new PipedInputStream(pout); + + + channel.setInputStream(pin); + channel.setOutputStream(pout); + channel.connect(); + + fConn.setInputStream(in); + fConn.setOutputStream(out); + fConn.setChannel(channel); + fControl.setState(TerminalState.CONNECTED); + // read data until the connection gets terminated + readDataForever(in); + } catch (JSchException e) { + connectFailed(e.getMessage(),e.getMessage()); + } catch (IOException e) { + connectFailed(e.getMessage(),e.getMessage()); + } finally { + + } + } + /** + * Read the data from the ssh connection and display it in the terminal. + * @param in + * @throws IOException + */ + private void readDataForever(InputStream in) throws IOException { + // read the data + byte bytes[]=new byte[32*1024]; + int n; + while((n=in.read(bytes))!=-1) { + fControl.writeToTerminal(new String(bytes,0,n)); + } + fControl.setState(TerminalState.CLOSED); + } + + private static class MyUserInfo implements UserInfo { + private String fPassword; + + public MyUserInfo(String password) { + fPassword = password; + } + public String getPassword() { + return fPassword; + } + public boolean promptYesNo(final String str) { + //need to switch to UI thread for prompting + final boolean[] retval = new boolean[1]; + Display.getDefault().syncExec(new Runnable() { + public void run() { + retval[0] = MessageDialog.openQuestion(null, SshMessages.WARNING, str); + } + }); + return retval[0]; + } + public String getPassphrase() { + return null; + } + public boolean promptPassphrase(String message) { + return true; + } + public boolean promptPassword(final String message) { + return true; + } + public void showMessage(final String message) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + MessageDialog.openInformation(null, SshMessages.INFO, message); + } + }); + } + } + private void connectFailed(String terminalText, String msg) { + Logger.log(terminalText); + fControl.displayTextInTerminal(terminalText); + fControl.setState(TerminalState.CLOSED); + fControl.setMsg(msg); + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnector.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnector.java new file mode 100644 index 00000000000..a04f088fcce --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshConnector.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ +package org.eclipse.tm.terminal.ssh; + +import java.io.InputStream; +import java.io.OutputStream; + +import org.eclipse.tm.terminal.ISettingsPage; +import org.eclipse.tm.terminal.ISettingsStore; +import org.eclipse.tm.terminal.ITerminalConnector; +import org.eclipse.tm.terminal.ITerminalControl; +import org.eclipse.tm.terminal.Logger; +import org.eclipse.tm.terminal.TerminalState; + +import com.jcraft.jsch.ChannelShell; +import com.jcraft.jsch.JSch; + +public class SshConnector implements ITerminalConnector { + private OutputStream fOutputStream; + private InputStream fInputStream; + private ITerminalControl fControl; + private JSch fJsch; + private ChannelShell fChannel; + + private final SshSettings fSettings; + public SshConnector() { + this(new SshSettings()); + try { + fJsch=new JSch(); + } catch(NoClassDefFoundError e) { + // ignore + e.printStackTrace(); + } + } + public SshConnector(SshSettings settings) { + fSettings=settings; + } + public String getId() { + return getClass().getName(); + } + public boolean isInstalled() { + return fJsch!=null; + } + public void connect(ITerminalControl control) { + Logger.log("entered."); //$NON-NLS-1$ + fControl=control; + SshConnection worker = new SshConnection(this,control); + worker.start(); + } + public void disconnect() { + Logger.log("entered."); //$NON-NLS-1$ + + if (getInputStream() != null) { + try { + getInputStream().close(); + } catch (Exception exception) { + Logger.logException(exception); + } + } + + if (getOutputStream() != null) { + try { + getOutputStream().close(); + } catch (Exception exception) { + Logger.logException(exception); + } + } + setState(TerminalState.CLOSED); + } + public boolean isLocalEcho() { + return false; + } + public void setTerminalSize(int newWidth, int newHeight) { + if(fChannel!=null) + fChannel.setPtySize(newWidth, newHeight, 8, 8); + + } + public InputStream getInputStream() { + return fInputStream; + } + public OutputStream getOutputStream() { + return fOutputStream; + } + void setInputStream(InputStream inputStream) { + fInputStream = inputStream; + } + void setOutputStream(OutputStream outputStream) { + fOutputStream = outputStream; + } + public void writeToTerminal(String txt) { + fControl.writeToTerminal(txt); + + } + public void setState(TerminalState state) { + fControl.setState(state); + + } + public ISshSettings getTelnetSettings() { + return fSettings; + } + public ISettingsPage makeSettingsPage() { + return new SshSettingsPage(fSettings); + } + public String getStatusString(String strConnected) { + return fSettings.getStatusString(strConnected); + } + public void load(ISettingsStore store) { + fSettings.load(store); + + } + public void save(ISettingsStore store) { + fSettings.save(store); + } + protected JSch getJsch() { + return fJsch; + } + ChannelShell getChannel() { + return fChannel; + } + void setChannel(ChannelShell channel) { + fChannel = channel; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.java new file mode 100644 index 00000000000..aa7287632c3 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ + +package org.eclipse.tm.terminal.ssh; + +import org.eclipse.osgi.util.NLS; + +public class SshMessages extends NLS { + static { + NLS.initializeMessages(SshMessages.class.getName(), SshMessages.class); + } + public static String CONNTYPE; + public static String USER; + public static String HOST; + public static String PORT; + public static String PASSWORD; + public static String TIMEOUT; + public static String WARNING; + public static String INFO; + + } diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.properties b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.properties new file mode 100644 index 00000000000..eb4b86a7054 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshMessages.properties @@ -0,0 +1,19 @@ +############################################################################### +# Copyright (c) 2006 Wind River Systems, Inc. and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Wind River Systems, Inc. - initial implementation +# +############################################################################### +CONNTYPE = SSH +HOST = Host +USER = User +PORT = Port +PASSWORD = Password +TIMEOUT = Timeout +WARNING = Warning +INFO = Info diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettings.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettings.java new file mode 100644 index 00000000000..abfe1134366 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettings.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ +package org.eclipse.tm.terminal.ssh; + +import org.eclipse.tm.terminal.ISettingsStore; + +public class SshSettings implements ISshSettings { + protected String fHost; + protected String fUser; + protected String fPassword; + protected String fPort; + protected String fTimeout; + public String getHost() { + return fHost; + } + + public void setHost(String strHost) { + fHost = strHost; + } + + public String getStatusString(String strConnected) { + return " (" + //$NON-NLS-1$ + getHost() + ":" + //$NON-NLS-1$ + getUser() + " - " + //$NON-NLS-1$ + strConnected + ")"; //$NON-NLS-1$ + } + + public void load(ISettingsStore store) { + fHost = store.get("Host");//$NON-NLS-1$ + fUser = store.get("User");//$NON-NLS-1$ + fPort = store.get("Port");//$NON-NLS-1$ + fTimeout = store.get("Timeout");//$NON-NLS-1$ + } + + + public void save(ISettingsStore store) { + store.put("Host", fHost);//$NON-NLS-1$ + store.put("User", fUser);//$NON-NLS-1$ + store.put("Port", fPort);//$NON-NLS-1$ + store.put("Timeout", fTimeout);//$NON-NLS-1$ + } + + + public int getTimeout() { + try { + return Integer.parseInt(fTimeout); + } catch (NumberFormatException numberFormatException) { + return 10; + } + } + public String getTimeoutString() { + return fTimeout; + } + + public void setTimeout(String timeout) { + fTimeout = timeout; + } + + public String getUser() { + return fUser; + } + + public void setUser(String user) { + fUser = user; + } + public int getPort() { + try { + return Integer.parseInt(fPort); + } catch (NumberFormatException numberFormatException) { + return 22; + } + } + + public String getPortString() { + return fPort; + } + + public void setPort(String port) { + fPort = port; + } + + public String getPassword() { + return fPassword; + } + + public void setPassword(String password) { + fPassword = password; + } +} diff --git a/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettingsPage.java b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettingsPage.java new file mode 100644 index 00000000000..6b310d7ef7a --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.ssh/src/org/eclipse/tm/terminal/ssh/SshSettingsPage.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial implementation + * + *******************************************************************************/ + +package org.eclipse.tm.terminal.ssh; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.eclipse.tm.terminal.ISettingsPage; + +public class SshSettingsPage implements ISettingsPage { + private Text fHostText; + private Text fUser; + private Text fTimeout; + private final SshSettings fTerminalSettings; + private Text fPort; + private Text fPassword; + + public SshSettingsPage(SshSettings settings) { + fTerminalSettings=settings; + } + public void saveSettings() { + fTerminalSettings.setHost(fHostText.getText()); + fTerminalSettings.setUser(fUser.getText()); + fTerminalSettings.setPassword(fPassword.getText()); + fTerminalSettings.setPort(fPort.getText()); + fTerminalSettings.setTimeout(fTimeout.getText()); + } + + public void loadSettings() { + if(fTerminalSettings!=null) { + fHostText.setText(get(fTerminalSettings.getHost(),""));//$NON-NLS-1$ + fTimeout.setText(get(fTerminalSettings.getTimeoutString(),"0"));//$NON-NLS-1$ + fUser.setText(get(fTerminalSettings.getUser(),""));//$NON-NLS-1$ + fPort.setText(get(fTerminalSettings.getPortString(),"22"));//$NON-NLS-1$ + fPassword.setText(get(fTerminalSettings.getPassword(),""));//$NON-NLS-1$ + } + } + String get(String value, String def) { + if(value==null || value.length()==0) + return def; + return value; + } + public boolean validateSettings() { + return true; + } + public void createControl(Composite parent) { + Composite composite = new Composite(parent, SWT.NONE); + GridLayout gridLayout = new GridLayout(2, false); + GridData gridData = new GridData(GridData.FILL_HORIZONTAL); + + composite.setLayout(gridLayout); + composite.setLayoutData(gridData); + + fHostText = createTextField(composite, SshMessages.HOST); + fUser = createTextField(composite, SshMessages.USER); + fPassword = createTextField(composite, SshMessages.PASSWORD,SWT.PASSWORD); + fTimeout = createTextField(composite, SshMessages.TIMEOUT); + fPort = createTextField(composite, SshMessages.PORT); + loadSettings(); + } + private Text createTextField(Composite composite, String labelTxt, int textOptions) { + GridData gridData; + // Add label + Label ctlLabel = new Label(composite, SWT.RIGHT); + ctlLabel.setText(labelTxt + ":"); //$NON-NLS-1$ + + // Add control + gridData = new GridData(GridData.FILL_HORIZONTAL); + Text text= new Text(composite, SWT.BORDER | textOptions); + text.setLayoutData(gridData); + return text; + } + private Text createTextField(Composite composite, String labelTxt) { + return createTextField(composite, labelTxt, 0); + } + + public String getName() { + return SshMessages.CONNTYPE; + } + +}