!7 lib 1006627 lib 1006628 // jcterm import com.jcraft.jsch.*; import com.jcraft.jsch.Proxy; import com.jcraft.jcterm.*; //import static com.jcraft.jcterm.Frame.*; // modelled after JCTermSwingFrame sclass JCTermModule extends DynModule implements /*Frame,*/ ActionListener, Runnable { static S COPYRIGHT = "JCTerm 0.0.11\nCopyright (C) 2002,2012 ymnk, JCraft,Inc.\n" +"Official Homepage: http://www.jcraft.com/jcterm/\n" +"This software is licensed under GNU LGPL."; final static int SHELL=0, SFTP=1, EXEC=2; private int mode=SHELL; private String xhost="127.0.0.1"; private int xport=0; private boolean xforwarding=false; private String user=""; private String host="127.0.0.1"; private String proxy_http_host=null; private int proxy_http_port=0; private String proxy_socks5_host=null; private int proxy_socks5_port=0; private JSchSession jschsession=null; private Proxy proxy=null; private int compression=0; private Splash splash=null; private JCTermSwing term=null; private Connection connection=null; private Channel channel=null; private boolean close_on_exit = true; private Frame frame = this; private String configName = "default"; public boolean getCloseOnExit(){ return close_on_exit; } public void setCloseOnExit(boolean close_on_exit) { this.close_on_exit = close_on_exit; } JComponent visualize() { ret term = new JCTermSwing; } void enhanceFrame(JInternalFrame f) { JMenuBar mb = getJMenuBar(); f.setJMenuBar(mb); ComponentAdapter l = new ComponentAdapter(){ public void componentResized(ComponentEvent e){ Component c=e.getComponent(); Container cp = ((JFrame)c).getContentPane(); int cw=c.getWidth(); int ch=c.getHeight(); int cwm=c.getWidth()-cp.getWidth(); int chm=c.getHeight()-cp.getHeight(); cw-=cwm; ch-=chm; term.setSize(cw, ch); } }; f.addComponentListener(l); applyConfig(configName); openSession(); } private Thread thread=null; public void kick(){ this.thread=new Thread(this); this.thread.start(); } public void run(){ String destination = null; while(thread!=null){ try{ int port=22; try{ String[] destinations = JCTermSwing.getCR().load(configName).destinations; String _host = promptDestination(term, destinations); destination = _host; if(_host==null){ break; } String _user=_host.substring(0, _host.indexOf('@')); _host=_host.substring(_host.indexOf('@')+1); if(_host==null||_host.length()==0){ continue; } if(_host.indexOf(':')!=-1){ try{ port=Integer.parseInt(_host.substring(_host.indexOf(':')+1)); } catch(Exception eee){ } _host=_host.substring(0, _host.indexOf(':')); } user=_user; host=_host; } catch(Exception ee){ continue; } try{ UserInfo ui=new MyUserInfo(); jschsession=JSchSession.getSession(user, null, host, port, ui, proxy); setCompression(compression); Configuration conf = JCTermSwing.getCR().load(configName); conf.addDestination(destination); JCTermSwing.getCR().save(conf); } catch(Exception e){ //System.out.println(e); break; } Channel channel=null; OutputStream out=null; InputStream in=null; if(mode==SHELL){ channel=jschsession.getSession().openChannel("shell"); if(xforwarding){ jschsession.getSession().setX11Host(xhost); jschsession.getSession().setX11Port(xport+6000); channel.setXForwarding(true); } out=channel.getOutputStream(); in=channel.getInputStream(); channel.connect(); } else if(mode==SFTP){ out=new PipedOutputStream(); in=new PipedInputStream(); channel=jschsession.getSession().openChannel("sftp"); channel.connect(); (new Sftp((ChannelSftp)channel, (InputStream)(new PipedInputStream( (PipedOutputStream)out)), new PipedOutputStream( (PipedInputStream)in))).kick(); } final OutputStream fout=out; final InputStream fin=in; final Channel fchannel=channel; connection=new Connection(){ public InputStream getInputStream(){ return fin; } public OutputStream getOutputStream(){ return fout; } public void requestResize(Term term){ if(fchannel instanceof ChannelShell){ int c=term.getColumnCount(); int r=term.getRowCount(); ((ChannelShell)fchannel).setPtySize(c, r, c*term.getCharWidth(), r*term.getCharHeight()); } } public void close(){ fchannel.disconnect(); } }; frame.setTitle(user+"@"+host+(port!=22 ? (":"+port) : "")); term.requestFocus(); term.start(connection); } catch(Exception e){ _handleException(e); } break; } thread=null; dispose_connection(); } void dispose_connection(){ synchronized(this){ if(channel!=null){ channel.disconnect(); channel=null; } } } public class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public boolean promptYesNo(String str){ Object[] options= {"yes", "no"}; int foo=JOptionPane.showOptionDialog(term, str, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return foo==0; } String passwd=null; String passphrase=null; JTextField pword=new JPasswordField(20); public String getPassword(){ return passwd; } public String getPassphrase(){ return passphrase; } public boolean promptPassword(String message){ Object[] ob= {pword}; JPanel panel=new JPanel(); panel.add(pword); pword.requestFocusInWindow(); JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){ public void selectInitialValue() { } }; JDialog dialog = pane.createDialog(term, message); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); Object o = pane.getValue(); if(o != null && ((Integer)o).intValue()==JOptionPane.OK_OPTION){ passwd=pword.getText(); return true; } else{ return false; } } public boolean promptPassphrase(String message){ return true; } public void showMessage(String message){ JOptionPane.showMessageDialog(null, message); } final GridBagConstraints gbc=new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0); private Container panel; public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo){ panel=new JPanel(); panel.setLayout(new GridBagLayout()); gbc.weightx=1.0; gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.gridx=0; panel.add(new JLabel(instruction), gbc); gbc.gridy++; gbc.gridwidth=GridBagConstraints.RELATIVE; JTextField[] texts=new JTextField[prompt.length]; for(int i=0; i0; i--){ texts[i].requestFocusInWindow(); } JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){ public void selectInitialValue() { } }; JDialog dialog = pane.createDialog(term, destination+": "+name); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); Object o = pane.getValue(); if(o != null && ((Integer)o).intValue()==JOptionPane.OK_OPTION){ String[] response=new String[prompt.length]; for(int i=0; i