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

📄 sshpropertyhandler.java

📁 一个非常好的ssh客户端实现
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/****************************************************************************** * * 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.ssh;import java.io.File;import java.io.InputStream;import java.io.FileOutputStream;import java.io.FileInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.net.Socket;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Hashtable;import java.util.Properties;import java.util.NoSuchElementException;import java.util.Enumeration;import java.util.Date;import com.mindbright.jca.security.interfaces.RSAPublicKey;import com.mindbright.net.*;import com.mindbright.terminal.*;import com.mindbright.util.EncryptedProperties;import com.mindbright.ssh2.*;public final class SSHPropertyHandler implements SSHClientUser, SSHAuthenticator, ProxyAuthenticator {    static public final int PROP_NAME    = 0;    static public final int PROP_VALUE   = 1;    static public final String PROPS_FILE_EXT  = ".mtp";    static public final String DEF_IDFILE      = "identity";    public static String hostKeyAlgs  = "ssh-rsa,ssh-dss";    public static String cipherAlgs   = "aes128-ctr,aes128-cbc,blowfish-ctr,blowfish-cbc,twofish128-cbc,aes192-cbc,aes256-cbc,twofish128-ctr,twofish-cbc,cast128-cbc,3des-ctr,3des-cbc,arcfour";    public static String macAlgs      = "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96,hmac-ripemd160";    public static String ciphAlgsSort = "aes128-ctr,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-ctr,blowfish-cbc,twofish128-ctr,twofish128-cbc,twofish192-cbc,twofish256-cbc,cast128-cbc,3des-ctr,3des-cbc,arcfour";    static public final Properties defaultProperties = new Properties();    static public final Hashtable  defaultPropNames  = new Hashtable();    static public final Hashtable  oldPropNames      = new Hashtable();    static public final String[][] defaultPropDesc = {	{ "protocol",         "auto"                                },	{ "server",           null                                  },	{ "real-server",      null                                  },	{ "local-bind",       "127.0.0.1"                           },	{ "port",             String.valueOf(SSH.DEFAULTPORT)       },	{ "proxy-type",       "none"                                },	{ "proxy-host",       null                                  },	{ "proxy-port",       null                                  },	{ "proxy-user",       null                                  },	{ "username",         null                                  },	{ "password",         null                                  },	{ "tispassword",      null                                  },	{ "passphrase",       null                                  },	{ "proxy-password",   null                                  },	{ "ssh1-cipher",      SSH.getCipherName(SSH.CIPHER_DEFAULT) },	{ "auth-method",      "password"                            },	{ "private-key",      DEF_IDFILE                            },	{ "x11-display",      "127.0.0.1:0"                         },	{ "mtu",              "0"                                   },	{ "alive",            "0"                                   },	{ "compression",      "0"                                   },	{ "x11-forward",      "false"                               },	{ "force-pty",        "true"                                },	{ "remfwd",           "false"                               },	{ "portftp",          "false"                               },	{ "sftpbridge-host",  ""                                    },	{ "sftpbridge-port",  ""                                    },	{ "strict-hostid",    "false"                               },	{ "key-timing-noise", "false"                               },	{ "kex-algorithms",	  "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1" },	{ "server-host-key-algorithms", hostKeyAlgs },	{ "enc-algorithms-cli2srv", cipherAlgs },	{ "enc-algorithms-srv2cli", cipherAlgs },	{ "mac-algorithms-cli2srv", macAlgs },	{ "mac-algorithms-srv2cli", macAlgs },	{ "comp-algorithms-cli2srv", "none" },	{ "comp-algorithms-srv2cli", "none" },	{ "languages-cli2srv", "" },	{ "languages-srv2cli", "" },	{ "package-version", "MindTerm_" + Version.version },	{ "filelist-remote-command", "ls -A -L -F -1\n" },	{ "fingerprint", null },	{ "jar-path", "."},	// !!! REMOVE ???	{ "module0", "com.mindbright.application.ModuleTelnet" },	{ "module1", "com.mindbright.application.ModuleSFTP" },	{ "module2", "com.mindbright.application.ModuleSCP" },	{ "module3", "com.mindbright.application.ModuleFTPOverSFTP" },	{ "module4", "com.mindbright.application.ModuleTelnetProxy" },	{ "module5", "com.mindbright.application.ModuleTerminal" },	{ "module0.label", "Telnet Terminal" },	{ "module1.label", "SFTP File Transfer..." },	{ "module2.label", "SCP File Transfer..." },	{ "module3.label", "FTP To SFTP Bridge..." },	{ "module4.label", "Telnet Proxy..." },	{ "module.telnet.havemenus", "true" },	{ "module.terminal.havemenus", "true" },    };    static {	for(int i = 0; i < defaultPropDesc.length; i++) {	    String name  = defaultPropDesc[i][PROP_NAME];	    String value = defaultPropDesc[i][PROP_VALUE];	    if(value != null)		defaultProperties.put(name, value);	    defaultPropNames.put(name, "");	}	oldPropNames.put("realsrv", "real-server");	oldPropNames.put("localhst", "local-bind");	oldPropNames.put("usrname", "username");	oldPropNames.put("passwd", "password");	oldPropNames.put("rsapassword", "passphrase");	oldPropNames.put("proxytype", "proxy-type");	oldPropNames.put("proxyhost", "proxy-host");	oldPropNames.put("proxyport", "proxy-port");	oldPropNames.put("proxyuser", "proxy-user");	oldPropNames.put("prxpassword", "proxy-password");	oldPropNames.put("cipher", "ssh1-cipher");	oldPropNames.put("authtyp", "auth-method");	oldPropNames.put("idfile", "private-key");	oldPropNames.put("x11fwd", "x11-forward");	oldPropNames.put("forcpty", "force-pty");	oldPropNames.put("stricthostid", "strict-hostid");	oldPropNames.put("display", "x11-display");    }    public static String backwardCompatProp(String key) {	String newName = (String)oldPropNames.get(key);	if(newName != null) {	    key = newName;	}	return key;    }    public static void setAsDefault(Properties props) {	Enumeration enum = props.keys();	while(enum.hasMoreElements()) {	    String name  = (String)enum.nextElement();	    String value = props.getProperty(name);	    name = backwardCompatProp(name);	    defaultProperties.put(name, value);	}    }    String        sshHomeDir;    String        knownHosts;    SSHRSAKeyFile keyFile;    SSHInteractiveClient client;    SSHInteractor        interactor;    boolean              activeProps;    private EncryptedProperties props;    protected String currentPropsFile;    protected String currentAlias;    boolean autoSaveProps;    boolean autoLoadProps;    boolean savePasswords;    boolean readonly;    private String propertyPassword;    public Properties initTermProps;    protected boolean propsChanged;    public SSHPropertyHandler(Properties initProps, boolean setAsDefault) {	this.knownHosts = SSH.KNOWN_HOSTS_FILE;	if(setAsDefault) {	    setAsDefault(initProps);	}	setProperties(initProps);	this.activeProps  = false;	this.propsChanged = false;    }    public SSHPropertyHandler(SSHPropertyHandler clone) {	this(clone.props, false);	this.sshHomeDir       = clone.sshHomeDir;	this.keyFile          = clone.keyFile;	this.initTermProps    = clone.initTermProps;	this.propertyPassword = clone.propertyPassword;	this.readonly         = true;    }    public static SSHPropertyHandler fromFile(String fileName, String password) throws IOException {	SSHPropertyHandler fileProps = new SSHPropertyHandler(new Properties(),							      false);	fileProps.setPropertyPassword(password);	fileProps.loadAbsoluteFile(fileName, false);	setAsDefault(fileProps.props);	return fileProps;    }    public void setInteractor(SSHInteractor interactor) {	this.interactor = interactor;    }    public void setClient(SSHInteractiveClient client) {	this.client = client;    }    public void setAutoLoadProps(boolean value) {	if(sshHomeDir != null)	    autoLoadProps = value;    }    public void setAutoSaveProps(boolean value) {	if(sshHomeDir != null)	    autoSaveProps = value;    }    public void setSavePasswords(boolean value) {	savePasswords = value;    }    public void setReadOnly(boolean value) {	readonly = value;    }    public boolean isReadOnly() {	return readonly;    }    public void setPropertyPassword(String password) {	if(password != null)	    this.propertyPassword = password;    }    public boolean emptyPropertyPassword() {	return propertyPassword == null;    }    public boolean setSSHHomeDir(String sshHomeDir) {	if(sshHomeDir == null || sshHomeDir.trim().length() == 0) {	    return true;	}	if(sshHomeDir != null && !sshHomeDir.endsWith(File.separator))	    sshHomeDir += File.separator;                if (com.mindbright.util.Util.isNetscapeJava()) {            try {                netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");            } catch (netscape.security.ForbiddenTargetException e) {            }        }        	try {	    // sshHomeDir always ends with a trailing File.separator. Strip before we	    // try to create it (some platforms don't like ending 'separator' in name)	    //	    File sshDir = new File(sshHomeDir.substring(0, sshHomeDir.length() - 1));	    if(!sshDir.exists()) {                ByteArrayOutputStream baos =                    readResource("/defaults/license.txt");                if (null == baos || !interactor.licenseDialog(baos.toString())) {                    return false;                }		if(interactor.askConfirmation("MindTerm home directory: '" + sshHomeDir +					      "' does not exist, create it?", true)) {		    try {			sshDir.mkdir();		    } catch (Throwable t) {			interactor.alert("Could not create home directory, file operations disabled.");			sshHomeDir = null;		    }		} else {		    interactor.report("No home directory, file operations disabled.");		    sshHomeDir = null;		}	    }	} catch (Throwable t) {	    if(interactor != null && interactor.isVerbose())		interactor.report("Can't access local file system, file operations disabled.");	    sshHomeDir = null;	}	this.sshHomeDir = sshHomeDir;	if(this.sshHomeDir == null) {	    autoSaveProps = false;	    autoLoadProps = false;	}	if(interactor != null)	    interactor.propsStateChanged(this);	return true;    }    public String getSSHHomeDir() {	return sshHomeDir;    }    public boolean hasHomeDir() {	return sshHomeDir != null;    }    //    // Methods delegated to Properties and other property related methods    //    public void resetToDefaults() {	clearServerSetting();	clearAllForwards();	Enumeration enum = defaultPropNames.keys();	while(enum.hasMoreElements()) {	    String name  = (String)enum.nextElement();	    String value = defaultProperties.getProperty(name);	    if(value != null) {		setProperty(name, value);	    } else {		props.remove(name);	    }	}	Terminal term = getTerminal();	if(term != null) {	    term.resetToDefaults();	}    }    public static boolean isProperty(String key) {	key = backwardCompatProp(key);	Properties ssh2Prefs = SSH2Preferences.getDefaultProperties();	return defaultPropNames.containsKey(key) ||	    (key.indexOf("local") == 0) || (key.indexOf("remote") == 0) ||	    (key.indexOf("module") == 0) || ssh2Prefs.containsKey(key) ||	    key.startsWith("fingerprint") ||	    key.startsWith(SSH2Preferences.SOCK_OPT);    }    public String getProperty(String key) {	key = backwardCompatProp(key);	return props.getProperty(key);    }    public String getDefaultProperty(String key) {	key = backwardCompatProp(key);	return (String)defaultProperties.get(key);    }    public void setDefaultProperty(String key, String value) {	key = backwardCompatProp(key);	defaultProperties.put(key, value);    }    public void resetProperty(String key) {	key = backwardCompatProp(key);	setProperty(key, getDefaultProperty(key));    }    public void setProperty(String key, String value)	throws IllegalArgumentException, NoSuchElementException    {	if(value == null)	    return;	key = backwardCompatProp(key);	boolean equalProp = !(value.equals(getProperty(key)));	validateProperty(key, value);	if(activeProps)	    activateProperty(key, value);	if(equalProp) {	    if(interactor != null)		interactor.propsStateChanged(this);	    propsChanged = equalProp;	}	props.put(key, value);    }    final void validateProperty(String key, String value)	throws IllegalArgumentException, NoSuchElementException {

⌨️ 快捷键说明

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