!7 lib 1006627 lib 1006628 // jcterm import com.jcraft.jsch.*; import com.jcraft.jsch.Proxy; import com.jcraft.jcterm.*; import java.util.Random; //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; transient Thread thread=null; int mode=SHELL; String xhost="127.0.0.1"; int xport=0; boolean xforwarding=false; String user=""; String host="127.0.0.1"; String proxy_http_host=null; int proxy_http_port=0; String proxy_socks5_host=null; int proxy_socks5_port=0; transient JSchSession jschsession=null; transient Proxy proxy=null; int compression=0; transient Splash splash=null; transient JCTermSwing term=null; transient Connection connection=null; transient Channel channel=null; String configName = "default"; JComponent visualize() { ret term = swingNu(JCTermSwing.class); } void enhanceFrame(final JInternalFrame f) { JMenuBar mb = getJMenuBar(); f.setJMenuBar(mb); ComponentAdapter l = new ComponentAdapter(){ public void componentResized(ComponentEvent e){ Container cp = f.getContentPane(); int cw=f.getWidth(); int ch=f.getHeight(); int cwm=f.getWidth()-cp.getWidth(); int chm=f.getHeight()-cp.getHeight(); cw-=cwm; ch-=chm; term.setSize(cw, ch); } }; f.addComponentListener(l); applyConfig(configName); openSession(); } 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(); } }; setModuleName(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 void showMessage(S s) { infoBox(s); } public boolean promptYesNo(S str){ ret confirmYesNo(str); } String passwd=null; String passphrase=null; JTextField pword=swingNu(JPasswordField.class, 20); public String getPassword(){ return passwd; } public String getPassphrase(){ return passphrase; } public boolean promptPassword(String message){ if (!showTitledForm_blocking("Give Password", "Password", pword)) false; ret true with passwd=pword.getText(); } public boolean promptPassphrase(String message){ return true; } final GridBagConstraints gbc=new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0); 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