Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

803
LINES

< > BotCompany Repo | #1016175 // JCTerm (SSH Client as Dyn Module, dev.)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 301K of libraries. Click here for Pure Java version (15295L/106K).

!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<ymnk@jcraft.com>, 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; i<prompt.length; i++){
        gbc.fill=GridBagConstraints.NONE;
        gbc.gridx=0;
        gbc.weightx=1;
        panel.add(new JLabel(prompt[i]), gbc);

        gbc.gridx=1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        gbc.weighty=1;
        if(echo[i]){
          texts[i]=new JTextField(20);
        }
        else{
          texts[i]=new JPasswordField(20);
          texts[i].requestFocusInWindow();
        }
        panel.add(texts[i], gbc);
        gbc.gridy++;
      }
      for(int i=prompt.length-1; i>0; 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<prompt.length; i++){
          response[i]=texts[i].getText();
        }
        return response;
      }
      else{
        return null; // cancel
      }
    }
  }

  public void setProxyHttp(String host, int port){
    proxy_http_host=host;
    proxy_http_port=port;
    if(proxy_http_host!=null&&proxy_http_port!=0){
      proxy=new ProxyHTTP(proxy_http_host, proxy_http_port);
    }
    else{
      proxy=null;
    }
  }

  public String getProxyHttpHost(){
    return proxy_http_host;
  }

  public int getProxyHttpPort(){
    return proxy_http_port;
  }

  public void setProxySOCKS5(String host, int port){
    proxy_socks5_host=host;
    proxy_socks5_port=port;
    if(proxy_socks5_host!=null&&proxy_socks5_port!=0){
      proxy=new ProxySOCKS5(proxy_socks5_host, proxy_socks5_port);
    }
    else{
      proxy=null;
    }
  }

  public String getProxySOCKS5Host(){
    return proxy_socks5_host;
  }

  public int getProxySOCKS5Port(){
    return proxy_socks5_port;
  }

  public void setXHost(String xhost){
    this.xhost=xhost;
  }

  public void setXPort(int xport){
    this.xport=xport;
  }

  public void setXForwarding(boolean foo){
    this.xforwarding=foo;
  }

  public void setCompression(int compression){
    if(compression<0||9<compression)
      return;
    this.compression=compression;
    if(jschsession!=null){
      if(compression==0){
        jschsession.getSession().setConfig("compression.s2c", "none");
        jschsession.getSession().setConfig("compression.c2s", "none");
	jschsession.getSession().setConfig("compression_level", "0");
      }
     else{
       jschsession.getSession().setConfig("compression.s2c", "zlib@openssh.com,zlib,none");
       jschsession.getSession().setConfig("compression.c2s", "zlib@openssh.com,zlib,none");
       jschsession.getSession().setConfig("compression_level", 
                                          new Integer(compression).toString());
     }
     try{
       jschsession.getSession().rekey();
     }
     catch(Exception e){
       System.out.println(e);
     }
    }
  }

  public void setFontSize(int size){
    Configuration conf = JCTermSwing.getCR().load(configName);
    conf.font_size = size;
    JCTermSwing.getCR().save(conf);
    _setFontSize(size);
  }
  
  JInternalFrame frame() { ret getInternalFrame(term); }

  void _setFontSize(int size){
    int mwidth = frame().getWidth()-term.getTermWidth();
    int mheight = frame().getHeight()-term.getTermHeight();
    call(term, 'setFont, "Monospaced-"+size);
    frame().setSize(mwidth+term.getTermWidth(), mheight+term.getTermHeight());
    term.clear();
    term.redraw(0, 0, term.getWidth(), term.getHeight());
  }

  public int getCompression(){
    return this.compression;
  }

  public void setLineSpace(int foo){
    term.setLineSpace(foo);
  }

  public void setSplash(Splash foo){
    this.splash=foo;
  }

  public boolean getAntiAliasing(){
    return term.getAntiAliasing();
  }

  public void setAntiAliasing(boolean foo){
    term.setAntiAliasing(foo);
  }

  public void setUserHost(String userhost){
    try{
      String _user=userhost.substring(0, userhost.indexOf('@'));
      String _host=userhost.substring(userhost.indexOf('@')+1);
      this.user=_user;
      this.host=_host;
    }
    catch(Exception e){
    }
  }

  public void openSession(){
    kick();
  }

  public void setPortForwardingL(int port1, String host, int port2){
    if(jschsession==null)
      return;
    try{
      jschsession.getSession().setPortForwardingL(port1, host, port2);
    }
    catch(JSchException e){
    }
  }

  public void setPortForwardingR(int port1, String host, int port2){
    if(jschsession==null)
      return;
    try{
      jschsession.getSession().setPortForwardingR(port1, host, port2);
    }
    catch(JSchException e){
    }
  }

  public void actionPerformed(ActionEvent e){
    String action=e.getActionCommand();

    int _mode = SHELL;
    if(action.equals("Open SHELL Session...")){
      _mode=SHELL;
    }
    else if(action.equals("Open SFTP Session...")){
      _mode=SFTP;
    }

    if(action.equals("Open SHELL Session...")
        ||action.equals("Open SFTP Session...")){
      if(thread==null){
        mode=_mode;
        openSession();
      }
      else {
        infoBox("TODO");
        //frame.openFrame(_mode, configName);
      }
    }
    else if(action.equals("HTTP...")){
      String foo=getProxyHttpHost();
      int bar=getProxyHttpPort();
      String proxy=JOptionPane.showInputDialog(null,
          "HTTP proxy server (hostname:port)", ((foo!=null&&bar!=0) ? foo+":"
              +bar : ""));
      if(proxy==null)
        return;
      if(proxy.length()==0){
        setProxyHttp(null, 0);
        return;
      }

      try{
        foo=proxy.substring(0, proxy.indexOf(':'));
        bar=Integer.parseInt(proxy.substring(proxy.indexOf(':')+1));
        if(foo!=null){
          setProxyHttp(foo, bar);
        }
      }
      catch(Exception ee){
      }
    }
    else if(action.equals("SOCKS5...")){
      String foo=getProxySOCKS5Host();
      int bar=getProxySOCKS5Port();
      String proxy=JOptionPane.showInputDialog(null,
          "SOCKS5 server (hostname:1080)", ((foo!=null&&bar!=0) ? foo+":"+bar
              : ""));
      if(proxy==null)
        return;
      if(proxy.length()==0){
        setProxySOCKS5(null, 0);
        return;
      }

      try{
        foo=proxy.substring(0, proxy.indexOf(':'));
        bar=Integer.parseInt(proxy.substring(proxy.indexOf(':')+1));
        if(foo!=null){
          setProxySOCKS5(foo, bar);
        }
      }
      catch(Exception ee){
      }
    }
    else if(action.equals("X11 Forwarding...")){
      String display=JOptionPane.showInputDialog(null,
          "XDisplay name (hostname:0)", (xhost==null) ? "" : (xhost+":"+xport));
      try{
        if(display!=null){
          xhost=display.substring(0, display.indexOf(':'));
          xport=Integer.parseInt(display.substring(display.indexOf(':')+1));
          xforwarding=true;
        }
      }
      catch(Exception ee){
        xforwarding=false;
        xhost=null;
      }
    }
    else if((action.equals("AntiAliasing"))){
      setAntiAliasing(!getAntiAliasing());
    }
    else if(action.equals("Compression...")){
      String foo=JOptionPane
          .showInputDialog(
              null,
              "Compression level(0-9)\n0 means no compression.\n1 means fast.\n9 means slow, but best.",
              new Integer(compression).toString());
      try{
        if(foo!=null){
          compression=Integer.parseInt(foo);
          setCompression(compression);
        }
      }
      catch(Exception ee){
      }
    }
    else if(action.equals("About..."))
      ret with JOptionPane.showMessageDialog(null, COPYRIGHT);
    else if((action.equals("Local Port..."))||(action.equals("Remote Port..."))){
      if(jschsession==null){
        JOptionPane.showMessageDialog(null,
            "Establish the connection before this setting.");
        return;
      }

      try{
        String title="";
        if(action.equals("Local Port...")){
          title+="Local port forwarding";
        }
        else{
          title+="remote port forwarding";
        }
        title+="(port:host:hostport)";

        String foo=JOptionPane.showInputDialog(null, title, "");
        if(foo==null)
          return;
        int port1=Integer.parseInt(foo.substring(0, foo.indexOf(':')));
        foo=foo.substring(foo.indexOf(':')+1);
        String host=foo.substring(0, foo.indexOf(':'));
        int port2=Integer.parseInt(foo.substring(foo.indexOf(':')+1));

        if(action.equals("Local Port...")){
          setPortForwardingL(port1, host, port2);
        }
        else{
          setPortForwardingR(port1, host, port2);
        }
      }
      catch(Exception ee){
      }
    }
    else if(action.equals("Quit")){
      quit();
    }
  }

  public JMenuBar getJMenuBar(){
    JMenuBar mb=new JMenuBar();
    JMenu m;
    JMenuItem mi;

    m=new JMenu("File");
    mi=new JMenuItem("Open SHELL Session...");
    mi.addActionListener(this);
    mi.setActionCommand("Open SHELL Session...");
    m.add(mi);
    mi=new JMenuItem("Open SFTP Session...");
    mi.addActionListener(this);
    mi.setActionCommand("Open SFTP Session...");
    m.add(mi);
    mi=new JMenuItem("Quit");
    mi.addActionListener(this);
    mi.setActionCommand("Quit");
    m.add(mi);
    mb.add(m);

    m=new JMenu("Proxy");
    mi=new JMenuItem("HTTP...");
    mi.addActionListener(this);
    mi.setActionCommand("HTTP...");
    m.add(mi);
    mi=new JMenuItem("SOCKS5...");
    mi.addActionListener(this);
    mi.setActionCommand("SOCKS5...");
    m.add(mi);
    mb.add(m);

    m=new JMenu("PortForwarding");
    mi=new JMenuItem("Local Port...");
    mi.addActionListener(this);
    mi.setActionCommand("Local Port...");
    m.add(mi);
    mi=new JMenuItem("Remote Port...");
    mi.addActionListener(this);
    mi.setActionCommand("Remote Port...");
    m.add(mi);
    mi=new JMenuItem("X11 Forwarding...");
    mi.addActionListener(this);
    mi.setActionCommand("X11 Forwarding...");
    m.add(mi);
    mb.add(m);

    m=new JMenu("Etc");

    mi=new JMenuItem("AntiAliasing");
    mi.addActionListener(this);
    mi.setActionCommand("AntiAliasing");
    m.add(mi);

    mi=new JMenuItem("Compression...");
    mi.addActionListener(this);
    mi.setActionCommand("Compression...");
    m.add(mi);

    JMenu mcolor=new JMenu("Color");
    final ActionListener mcolor_action = new ActionListener(){
      public void actionPerformed(ActionEvent e){
        setFgBg(e.getActionCommand());
      }
    };
    mcolor.addMenuListener(new MenuListener(){
      public void menuSelected(MenuEvent me){
        JMenu jm = (JMenu)me.getSource();
        String[] fg_bg = JCTermSwing.getCR().load(configName).fg_bg;
        for(int i=0; i < fg_bg.length; i++){
          String[] tmp = fg_bg[i].split(":");
          JMenuItem mi = new JMenuItem("ABC");
          mi.setForeground(toColor(tmp[0]));
          mi.setBackground(toColor(tmp[1]));
          mi.setActionCommand(fg_bg[i]);
          mi.addActionListener(mcolor_action);
          jm.add(mi);
        }
      }
      public void menuDeselected(MenuEvent me){
        JMenu jm = (JMenu)me.getSource();
        jm.removeAll();
      }
      public void menuCanceled(MenuEvent arg){ }
    });
    m.add(mcolor);

    JMenu mfsize=new JMenu("Font size");
    final ActionListener mfsize_action = new ActionListener(){
      public void actionPerformed(ActionEvent e){
        String _font_size=e.getActionCommand();
        try {
          setFontSize(Integer.parseInt(_font_size));
        }
        catch(NumberFormatException nfe){
        }
      }
    };
    mfsize.addMenuListener(new MenuListener(){
      public void menuSelected(MenuEvent me){
        JMenuItem mi;
        JMenu jm = (JMenu)me.getSource();
        int font_size = JCTermSwing.getCR().load(configName).font_size;
        mi = new JMenuItem("Smaller ("+(font_size-1)+")");;
        mi.setActionCommand(""+(font_size-1));
        mi.addActionListener(mfsize_action);
        jm.add(mi);
        mi = new JMenuItem("Larger ("+(font_size+1)+")");
        mi.setActionCommand(""+(font_size+1));
        mi.addActionListener(mfsize_action);
        jm.add(mi);
      }
      public void menuDeselected(MenuEvent me){
        JMenu jm = (JMenu)me.getSource();
        jm.removeAll();
      }
      public void menuCanceled(MenuEvent arg){ }
    });
    m.add(mfsize);

    mb.add(m);

    m=new JMenu("Help");
    mi=new JMenuItem("About...");
    mi.addActionListener(this);
    mi.setActionCommand("About...");
    m.add(mi);
    mb.add(m);

    return mb;
  }

  public void quit(){
    thread=null;
    if(connection!=null){
      connection.close();
      connection=null;
    }
    /*
    if(jschsession!=null){
      jschsession.dispose();
      jschsession=null;
    }
    */
  }

  public void setTerm(JCTermSwing term){
    this.term=term;
  }

  public Term getTerm(){
    return term;
  }

  void setFgBg(String fg_bg){
    Configuration conf = JCTermSwing.getCR().load(configName);
    conf.addFgBg(fg_bg);
    JCTermSwing.getCR().save(conf);
    _setFgBg(fg_bg);
  }

  void _setFgBg(String fg_bg){
    String[] tmp = fg_bg.split(":");
    Color fg = toColor(tmp[0]);
    Color bg = toColor(tmp[1]);
    term.setForeGround(fg);
    term.setDefaultForeGround(fg);
    term.setBackGround(bg);
    term.setDefaultBackGround(bg);
    call(term, 'resetCursorGraphics);
    term.clear();
    term.redraw(0, 0, term.getWidth(), term.getHeight());
  }

  String promptDestination(JComponent term, String[] destinations){
    JTextField tf = jTextField("root@tinybrain.de");
    ret showTitledForm_blocking("Enter destination",
      "Username@Hostname", tf) ? getTextTrim(tf) : null;
  }

  void applyConfig(String configName){
    this.configName = configName;
    Configuration conf = JCTermSwing.getCR().load(configName);
    _setFontSize(conf.font_size);
    _setFgBg(conf.fg_bg[0]);
  }

  /*public static void main(String[] arg){
    JCTermSwing.setCR(new ConfigurationRepositoryFS());

    String s = System.getProperty("jcterm.config.use_ssh_agent");
    if(s != null && s.equals("true"))
      JSchSession.useSSHAgent(true);
  }*/
  
  static java.awt.Color toColor(Object o){
    if(o instanceof String){
      try{
        return java.awt.Color.decode(((String)o).trim());
      }
      catch(java.lang.NumberFormatException e){ }
      return java.awt.Color.getColor(((String)o).trim());
    }
    if(o instanceof java.awt.Color){
      return (java.awt.Color)o;
    }
    return Color.white;
  }
}

Author comment

Began life as a copy of #1006629

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, iveijnkanddl, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1016175
Snippet name: JCTerm (SSH Client as Dyn Module, dev.)
Eternal ID of this version: #1016175/30
Text MD5: 388a8c16b1c7f3f337d17ed07e42a34d
Transpilation MD5: 3c9dee671c38f9bad2752cb34d121125
Author: stefan
Category: javax / gui
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-08 18:33:35
Source code size: 22408 bytes / 803 lines
Pitched / IR pitched: No / No
Views / Downloads: 400 / 1229
Version history: 29 change(s)
Referenced in: