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

📄 sshpropertyhandler.java

📁 一个非常好的ssh客户端实现
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	//	// Some sanity checks...	//	if(key.equals("auth-method")) {	    SSH.getAuthTypes(value);	    //	} else if(key.equals("x11-forward")  ||		  key.equals("force-pty") || key.equals("remfwd")  ||		  key.equals("strict-hostid")  || key.equals("portftp") ||		  key.equals("key-timing-noise")) {	    if(!(value.equals("true") || value.equals("false")))		throw new IllegalArgumentException("Value for " + key + " must be 'true' or 'false'");	    //	} else if(key.equals("port") || key.equals("proxy-port") || key.equals("mtu") ||		  key.equals("alive") || key.equals("compression")) {	    try {		int val = Integer.valueOf(value).intValue();		if((key.equals("port") || key.equals("proxy-port")) && (val > 65535 || val < 0)) {		    throw new IllegalArgumentException("Not a valid port number: " + value);		} else if(key.equals("mtu") && val != 0 && (val > (256*1024) || val < 4096)) {		    throw new IllegalArgumentException("Mtu must be between 4k and 256k");		} else if(key.equals("alive")) {		    if(val < 0 || val > 600)			throw new IllegalArgumentException("Alive interval must be 0-600");		} else if(key.equals("compression")) {		    if(val < 0 || val > 9)			throw new IllegalArgumentException("Compression Level must be 0-9");		}	    } catch (NumberFormatException e) {		throw new IllegalArgumentException("Value for " + key + " must be an integer");	    }	    //	} else if(key.equals("server")) {	    if(client != null && client.isOpened()) {		throw new IllegalArgumentException("Server can only be set while not connected");	    }	} else if(key.equals("real-server") || key.equals("local-bind")) {	    try {		InetAddress.getByName(value);	    } catch (UnknownHostException e) {		throw new IllegalArgumentException(key + " address must be a legal/known host name");	    }	} else if(key.equals("proxy-type")) {	    SSH.getProxyType(value);	} else if(key.startsWith("local") || key.startsWith("remote")) {	    try {		if(value.startsWith("/general/"))		    value = value.substring(9);		if(key.startsWith("local"))		    addLocalPortForward(value, false);		else		    addRemotePortForward(value, false);	    } catch (Exception e) {		throw new IllegalArgumentException("Not a valid port forward: " + key + " : " + value);	    }	} else if(!isProperty(key)) {	    throw new NoSuchElementException("Unknown ssh property '" + key + "'");	}    }    void activateProperty(String key, String value) {	//	// The properties that needs an action to "activated"	//	if(key.equals("remfwd")) {	    try {		SSHListenChannel.setAllowRemoteConnect((new Boolean(value)).booleanValue());	    } catch (Throwable t) {		// Ignore if we don't have the SSHListenChannel class	    }	} else if(key.equals("portftp")) {	    client.havePORTFtp = (new Boolean(value)).booleanValue();	    if(client.havePORTFtp && SSHProtocolPlugin.getPlugin("ftp") != null) {		SSHProtocolPlugin.getPlugin("ftp").initiate(client);	    }	    //	} else if(key.equals("key-timing-noise")) {	    if(client.isSSH2 && client.termAdapter != null) {		if("true".equals(value)) {		    client.termAdapter.startChaff();		} else {		    client.termAdapter.stopChaff();		}	    }	} else if(key.equals("alive")) {	    if(client.isConnected()) {		client.setAliveInterval(Integer.parseInt(value));	    }	} else if(key.equals("real-server")) {	    try {		if(value != null && value.length() > 0)		    client.setServerRealAddr(InetAddress.getByName(value));		else		    client.setServerRealAddr(null);	    } catch (UnknownHostException e) {		// !!!	    }	} else if(key.equals("local-bind")) {	    try {		client.setLocalAddr(value);	    } catch (UnknownHostException e) {		throw new IllegalArgumentException("localhost address must be a legal/known host name");	    }	} else if(key.startsWith("local")) {	    int n = Integer.parseInt(key.substring(5));	    if(n > client.localForwards.size())		throw new IllegalArgumentException("Port forwards must be given in unbroken sequence");	    if(value.startsWith("/general/"))		value = value.substring(9);	    try {		addLocalPortForward(value, true);	    } catch (IOException e) {		if(!interactor.askConfirmation("Error setting up tunnel '" +					       value + "', continue anyway?",					       true)) {		    throw new IllegalArgumentException("Error creating tunnel: " + e.getMessage());		}	    }	} else if(key.startsWith("remote")) {	    try {		int n = Integer.parseInt(key.substring(6));		if(n > client.remoteForwards.size())		    throw new IllegalArgumentException("Port forwards must be given in unbroken sequence");		if(value.startsWith("/general/"))		    value = value.substring(9);		addRemotePortForward(value, true);	    } catch (Exception e) {		throw new IllegalArgumentException("Not a valid port forward: " + key + " : " + value);	    }	}    }    public void setProperties(Properties newProps) throws IllegalArgumentException,    NoSuchElementException    {	props = new EncryptedProperties(defaultProperties);	mergeProperties(newProps);    }    public Properties getProperties() {	return props;    }    public void mergeProperties(Properties newProps)	throws IllegalArgumentException    {	String name, value;	Enumeration enum;	enum = defaultPropNames.keys();	while(enum.hasMoreElements()) {	    name  = (String)enum.nextElement();	    value = newProps.getProperty(name);	    if(value != null) {		name = backwardCompatProp(name);		props.put(name, value);	    }	}	int i = 0;	while((value = newProps.getProperty("local" + i)) != null) {	    props.put("local" + i, value);	    i++;	}	i = 0;	while((value = newProps.getProperty("remote" + i)) != null) {	    props.put("remote" + i, value);	    i++;	}    }    public Properties getInitTerminalProperties() {	return initTermProps;    }    public void activateProperties() {	if(activeProps)	    return;	String name, value;	Enumeration enum = defaultPropNames.keys();	activeProps = true;	while(enum.hasMoreElements()) {	    name  = (String)enum.nextElement();	    value = props.getProperty(name);	    if(value != null)		activateProperty(name, value);	}	int i = 0;	while((value = props.getProperty("local" + i)) != null) {	    activateProperty("local" + i, value);	    i++;	}	i = 0;	while((value = props.getProperty("remote" + i)) != null) {	    activateProperty("remote" + i, value);	    i++;	}    }    public void passivateProperties() {	activeProps = false;    }    private void saveProperties(String fname) throws IOException {	FileOutputStream f;	Terminal         term      = getTerminal();	Properties       termProps = (term != null ? term.getProperties() : null);        if (com.mindbright.util.Util.isNetscapeJava()) {            try {                netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");            } catch (netscape.security.ForbiddenTargetException e) {            }        }        	if(termProps != null) {	    Enumeration e = termProps.keys();	    while(e.hasMoreElements()) {		String key = (String)e.nextElement();		String val = termProps.getProperty(key);		props.put(key, val);	    }	}	f = new FileOutputStream(fname);	if(savePasswords) {	    // !!! REMOVE	    if(propertyPassword == null) {		propertyPassword = "";	    }	    // TODO: should take default cipher from defaultProperties	    props.save(f, "MindTerm ssh settings",		       propertyPassword, SSH.cipherClasses[SSH.CIPHER_DEFAULT][0]);	} else {	    String prxPwd, stdPwd, tisPwd, rsaPwd;	    stdPwd = props.getProperty("password");	    prxPwd = props.getProperty("proxy-password");	    tisPwd = props.getProperty("tispassword");	    rsaPwd = props.getProperty("passphrase");	    clearPasswords();	    props.save(f, "MindTerm ssh settings");	    if(stdPwd != null) props.put("password", stdPwd);	    if(prxPwd != null) props.put("proxy-password", prxPwd);	    if(tisPwd != null) props.put("tispassword", tisPwd);	    if(rsaPwd != null) props.put("passphrase", rsaPwd);	}	f.close();	propsChanged = false;	if(term != null)	    term.setPropsChanged(false);	interactor.propsStateChanged(this);    }    private void loadProperties(String fname, boolean promptPwd) throws IOException {	Terminal term = getTerminal();        if (com.mindbright.util.Util.isNetscapeJava()) {            try {                netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");            } catch (netscape.security.ForbiddenTargetException e) {            }        }        	FileInputStream f     = new FileInputStream(fname);	byte[]          bytes = new byte[f.available()];	f.read(bytes);	ByteArrayInputStream bytein = new ByteArrayInputStream(bytes);	f.close();	EncryptedProperties loadProps = new EncryptedProperties();	try {	    loadProps.load(bytein, "");	} catch (SSHAccessDeniedException e) {	    try {		bytein.reset();		loadProps.load(bytein, propertyPassword);	    } catch (SSHAccessDeniedException ee) {		try {		    if(promptPwd) {			bytein.reset();			propertyPassword = interactor.promptPassword("File " + fname + " password: ");			loadProps.load(bytein, propertyPassword);		    } else {			throw new SSHAccessDeniedException("");		    }		} catch (SSHAccessDeniedException eee) {		    clearServerSetting();		    throw new SSHClient.AuthFailException("Access denied for '" + fname + "'");		}	    }	}	savePasswords = !loadProps.isNormalPropsFile();	Enumeration enum;	String      name;	String      value;	Properties sshProps  = new Properties();	Properties termProps = new Properties();	enum = loadProps.keys();	while(enum.hasMoreElements()) {	    name  = (String)enum.nextElement();	    value = loadProps.getProperty(name);	    if(isProperty(name)) {		name = backwardCompatProp(name);		sshProps.put(name, value);	    } else if(TerminalDefProps.isProperty(name)) {		name = TerminalDefProps.backwardCompatProp(name);		termProps.put(name, value);	    } else {		if(interactor != null)		    interactor.report("Unknown property '" + name + "' found in file: " + fname);		else		    System.out.println("Unknown property '" + name + "' found in file: " + fname);	    }	}	if(client != null)	    client.clearAllForwards();	passivateProperties();	setProperties(sshProps);	initTermProps = termProps;	if(term != null) {	    term.setProperties(initTermProps, false);	    term.setPropsChanged(false);	}	propsChanged = false;	if(interactor != null)	    interactor.propsStateChanged(this);    }    final void clearPasswords() {	props.remove("password");	props.remove("tispassword");	props.remove("passphrase");	props.remove("proxy-password");    }    final void clearServerSetting() {	setProperty("server", "");	currentPropsFile = null;	currentAlias     = null;	if(interactor != null)	    interactor.propsStateChanged(this);    }    final void clearAllForwards() {	int i = 0;	if(client != null)	    client.clearAllForwards();	for(i = 0; i < 1024; i++) {	    String key = "local" + i;	    if(!props.containsKey(key))		break;	    props.remove(key);	}	for(i = 0; i < 1024; i++) {	    String key = "remote" + i;	    if(!props.containsKey(key))		break;	    props.remove(key);	}    }    public boolean wantSave() {	boolean somePropsChanged = (propsChanged ||				    (getTerminal() != null ?				     getTerminal().getPropsChanged() : false));	return (!isReadOnly() && somePropsChanged && sshHomeDir != null);    }    public final void checkSave() throws IOException {	if(autoSaveProps) {	    saveCurrentFile();	}    }    public void saveCurrentFile() throws IOException {	if(currentPropsFile != null && wantSave())	    saveProperties(currentPropsFile);    }    public void saveAsCurrentFile(String fileName) throws IOException {	propsChanged     = true;	currentPropsFile = fileName;	saveCurrentFile();	currentAlias     = null;    }    public void loadAbsoluteFile(String fileName, boolean promptPwd) throws IOException {	currentAlias     = null;	currentPropsFile = fileName;	loadProperties(currentPropsFile, promptPwd);	if(interactor != null)	    interactor.propsStateChanged(this);

⌨️ 快捷键说明

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