⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mindtermlite.java

📁 一个非常好的ssh客户端实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** * * Copyright (c) 1999-2003 AppGate Network Security AB. All Rights Reserved. *  * This file contains Original Code and/or Modifications of Original Code as * defined in and that are subject to the MindTerm Public Source License, * Version 2.0, (the 'License'). You may not use this file except in compliance * with the License. *  * You should have received a copy of the MindTerm Public Source License * along with this software; see the file LICENSE.  If not, write to * AppGate Network Security AB, Otterhallegatan 2, SE-41118 Goteborg, SWEDEN * *****************************************************************************/package com.mindbright.application;import java.io.File;import java.io.InputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.net.Socket;import java.applet.Applet;import java.applet.AppletContext;import java.util.Properties;import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import com.mindbright.jca.security.SecureRandom;import com.mindbright.sshcommon.SSHConsoleRemote;import com.mindbright.ssh2.SSH2;import com.mindbright.ssh2.SSH2Transport;import com.mindbright.ssh2.SSH2TransportEventAdapter;import com.mindbright.ssh2.SSH2Connection;import com.mindbright.ssh2.SSH2ConsoleRemote;import com.mindbright.ssh2.SSH2SimpleClient;import com.mindbright.ssh2.SSH2Interactor;import com.mindbright.ssh2.SSH2Preferences;import com.mindbright.ssh2.SSH2Signature;import com.mindbright.ssh2.SSH2KeyFingerprint;import com.mindbright.ssh2.SSH2HostKeyVerifier;import com.mindbright.ssh2.SSH2Exception;import com.mindbright.ssh2.SSH2UserCancelException;import com.mindbright.ssh2.SSH2SignatureException;import com.mindbright.net.ProxyAuthenticator;import com.mindbright.util.SecureRandomAndPad;import com.mindbright.util.RandomSeed;import com.mindbright.util.JarLoader;import com.mindbright.gui.AWTGridBagContainer;import com.mindbright.gui.AWTConvenience;import com.mindbright.gui.Logo;import com.mindbright.gui.AlertDialog;import com.mindbright.gui.ConfirmDialog;public class MindTermLite extends Applet implements SSH2Interactor, MindTermApp,						    ProxyAuthenticator, Runnable{    private final static String VERSION = "2.5dev";    private class Actions implements ActionListener, ItemListener {	private int       action;	public Actions(int action) {	    this.action = action;	}	public void actionPerformed(ActionEvent e) {	    if(action == -1) {		(new Thread(MindTermLite.this)).start();	    } else if(action == -2) {		try {		    client.getTransport().normalDisconnect("Disconnect by user");		} catch (Exception ee) {		    // !!!		}	    } else if(action == -3) {		if(e.getActionCommand().equals("Cancel")) {		    pressedCancel = true;		} else {		    pressedCancel = false;		}		promptDialog.setVisible(false);	    } else {		modules[action].activate(MindTermLite.this);	    }	}	public void itemStateChanged(ItemEvent e) {	    if("password".equals(choiceAuthTyp.getSelectedItem())) {		textPwd.setEnabled(true);	    } else {		textPwd.setEnabled(false);	    }	    textPwd.setText("");	}    }    Container container;    Frame     frame;    Logo      logo;    Image     discImg;    Image     connImg;    boolean weAreAnApplet;    boolean haveMenus;    boolean autoSave;    String  saveLocation;    Properties settings;    String host;    int    port;    String username;    String password;    SSH2SimpleClient client;    MindTermModule[] modules;    int              modCnt;    Button[]         modButs;    MenuItem[]       modMenuItems;    Choice     choiceAuthTyp;    TextField  textUser, textSrv, textPwd;    Button     connBut, discBut;    public RandomSeed         randomSeed;    public SecureRandomAndPad secureRandom;    public MindTermLite() {	settings = new Properties();	settings.put("package-version", SSH2.getPackageVersion(getAppName(),					       1, 0, "(non-commercial)"));	modButs      = new Button[32];	modMenuItems = new MenuItem[32];	modules      = new MindTermModule[32];	modCnt       = 0;	logo         = null;    }    public void init() {	container = this;	weAreAnApplet = true;	startMeUp();    }    public static void main(String[] argv) {	final MindTermLite mindterm = new MindTermLite();	mindterm.frame = new Frame();	mindterm.frame.addWindowListener(new WindowAdapter() {		public void windowClosing(WindowEvent e)  { mindterm.windowClosing(e); }	    });	mindterm.frame.setTitle(mindterm.getAppName() + " " + VERSION);	mindterm.container = mindterm.frame;	mindterm.startMeUp();    }    public void startMeUp() {	try {	    InputStream in =		getClass().getResourceAsStream("/defaults/settings.txt");	    if(in != null) {		settings.load(in);	    }	    saveLocation = settings.getProperty("savelocation");	    if(saveLocation == null) {		saveLocation = System.getProperty("user.home");		if(saveLocation == null) {		    saveLocation = System.getProperty("user.dir");		}		if(saveLocation == null) {		    saveLocation = System.getProperty("java.home");		}		if(!saveLocation.endsWith(File.separator)) {		    saveLocation = saveLocation + File.separator;		}		saveLocation = saveLocation + "mtlast.txt";	    }	    File f = new File(saveLocation);	    if(f.exists() && f.canRead()) {		settings.load(new FileInputStream(f));	    }	    haveMenus = (Boolean.valueOf(settings.getProperty("havemenus")).			 booleanValue() && (frame != null));	    autoSave = Boolean.valueOf(settings.getProperty("autosave")).		booleanValue();	    discImg = createImage("/defaults/logo_disc.gif");	    connImg = createImage("/defaults/logo_conn.gif");	    if(discImg != null) {		logo = new Logo(discImg);	    }	    initSeedGenerator();	    container.setLayout(new BorderLayout());	    Label lbl;	    Panel panel = new Panel();	    AWTGridBagContainer grid = new AWTGridBagContainer(panel);	    if(logo != null) {		Panel p = new Panel();		p.add(logo);		container.add(p, BorderLayout.NORTH);	    }	    grid.getConstraints().anchor = GridBagConstraints.WEST;	    lbl = new Label("Server[:port] :");	    grid.add(lbl, 1, 2);	    textSrv = new TextField("", 16);	    grid.add(textSrv, 1, 2);	    lbl = new Label("Username :");	    grid.add(lbl, 2, 2);	    textUser = new TextField("", 16);	    grid.add(textUser, 2, 2);	    lbl = new Label("Authentication :");	    grid.add(lbl, 3, 2);	    choiceAuthTyp = AWTConvenience.newChoice(new String[] { "password", "kbd-interactive"});	    choiceAuthTyp.addItemListener(new Actions(0));	    grid.add(choiceAuthTyp, 3, 2);	    lbl = new Label("Password :");	    grid.add(lbl, 4, 2);	    textPwd = new TextField("", 16);	    textPwd.setEchoChar('*');	    grid.add(textPwd, 4, 2);	    grid.getConstraints().anchor = GridBagConstraints.CENTER;	    grid.getConstraints().fill   = GridBagConstraints.HORIZONTAL;	    Panel bp = new Panel(new FlowLayout());	    bp.add(connBut = new Button("Connect"));	    connBut.addActionListener(new Actions(-1));	    bp.add(discBut = new Button("Disconnect"));	    discBut.addActionListener(new Actions(-2));	    grid.getConstraints().insets = new Insets(4, 24, 12, 24);	    Panel modPanel = getModulesPanel();	    if(modPanel != null) {		grid.add(modPanel, 5, 4);	    }	    grid.getConstraints().insets = new Insets(4, 4, 0, 4);	    grid.add(bp, 6, 4);	    container.add(panel, BorderLayout.CENTER);	    enableButtons();	    String server = settings.getProperty("server");	    String port   = settings.getProperty("port");	    String user   = settings.getProperty("username");	    String auth   = settings.getProperty("auth-method");	    if(server != null) {		if(port != null && server.indexOf(':') == -1) {		    server += ":" + port;		}		textSrv.setText(server);	    }	    if(user != null) {		textUser.setText(user);	    }	    if(frame != null) {		frame.pack();		frame.show();	    }	    if(server == null) {		textSrv.requestFocus();	    } else if(user == null) {		textUser.requestFocus();	    } else if(auth == null || auth.equals("password")) {		textPwd.requestFocus();	    } else {		choiceAuthTyp.select(auth);		connBut.requestFocus();		(new Actions(0)).itemStateChanged(null);	    }	} catch (Throwable t) {	    t.printStackTrace();	}    }    public void run() {	try {	    String srvStr = textSrv.getText();	    int i;	    if((i = srvStr.indexOf(':')) > 0) {		host = srvStr.substring(0, i);		port = Integer.parseInt(srvStr.substring(i + 1));	    } else {		host = srvStr;		port = 22;	    }	    username = textUser.getText();	    password = textPwd.getText();	    connect();	    setProperty("server", host);	    setProperty("port", String.valueOf(port));	    setProperty("username", username);	    setProperty("auth-method", choiceAuthTyp.getSelectedItem());	    enableButtons();	    textPwd.setText("");	} catch (Exception ee) {	    String msg = ee.getMessage();	    if (null == msg) {		msg = ee.toString();	    }	    alert("Error connecting: " + msg);	    disconnect();	}    }    private String getLabel(int module) {	return getProperty("module" + module + ".label");    }    private void enableButtons() {	boolean isConnected = isConnected();	connBut.setEnabled(!isConnected);	discBut.setEnabled(isConnected);	textSrv.setEnabled(!isConnected);	textUser.setEnabled(!isConnected);	textPwd.setEnabled(!isConnected);	choiceAuthTyp.setEnabled(!isConnected);	for(int i = 0; i < modCnt; i++) {	    if(modButs[i] != null) {		modButs[i].setEnabled(modules[i].isAvailable(this));	    }	    if(modMenuItems[i] != null) {		modMenuItems[i].setEnabled(modules[i].isAvailable(this));	    }	}    }    private Panel getModulesPanel() {	int i;	boolean haveVisible = false;	for(i = 0; i < modules.length; i++) {	    if(getLabel(i) != null) {		haveVisible = true;	    }	    String className = settings.getProperty("module" + i);	    if(className == null) {		break;	    }	    try {		modules[i] = (MindTermModule)Class.forName(className).newInstance();		modules[i].init(this);		modCnt++;	    } catch (Exception e) {		// !!! TODO		throw new Error("Module class '" + className + "' not found");	    }	}	Panel p = null;	if(haveVisible) {	    Menu menu = null;	    if(haveMenus) {		frame.setMenuBar(new MenuBar());		menu = new Menu("Plugins");		frame.getMenuBar().add(menu);		frame.addNotify();		frame.validate();	    } else {		p = new Panel(new GridLayout(modCnt, 1));	    }	    for(i = 0; i < modCnt; i++) {		String label = getLabel(i);		if(label != null) {		    if(haveMenus) {			modMenuItems[i] = new MenuItem(label);			modMenuItems[i].addActionListener(new Actions(i));			menu.add(modMenuItems[i]);		    } else {			modButs[i] = new Button(label);			modButs[i].addActionListener(new Actions(i));			p.add(modButs[i]);		    }		}	    }	}	return p;    }    public void connect() throws SSH2Exception, IOException {	Socket socket  = null;	String prxType = getProperty("proxy-type");	if(prxType != null && !prxType.equals("none")) {	    JarLoader cl = new JarLoader(getProperty("jar-path"),					 "lite_proxy.jar"); 	    try {		String proxyClass;		String method; 		if("http".equals(prxType)) {		    proxyClass = "com.mindbright.net.WebProxyTunnelSocket";		    method = "getProxy"; 		} else if("socks4".equals(prxType)) {		    proxyClass = "com.mindbright.net.SocksProxySocket";		    method = "getSocks4Proxy"; 		} else if("socks5".equals(prxType)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -