📄 sshpropertyhandler.java
字号:
File tmpFile; String fileName = null; boolean strict = strictHostKeyCheck(); if(!strict && sshHomeDir == null) { if(interactor.isVerbose()) interactor.report("File operations disabled, server identity can't be verified"); return true; } if (com.mindbright.util.Util.isNetscapeJava()) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess"); } catch (netscape.security.ForbiddenTargetException e) { } } fileName = sshHomeDir + "hostkeys"; tmpFile = new File(fileName); if(!strict && !tmpFile.exists()) { if(interactor.askConfirmation("Known hosts directory: '" + fileName + "' does not exist, create it?", true)) { try { tmpFile.mkdir(); } catch (Throwable t) { interactor.alert("Could not create known hosts directory."); } } } if(!strict && (!tmpFile.exists() || !tmpFile.isDirectory())) { return interactor.askConfirmation("No hostkeys directory, can't verify host, continue anyway?", false); } fileName += File.separator + "key_" + getProperty("port") + "_" + getProperty("server") + ".pub"; tmpFile = new File(fileName); if(!tmpFile.exists()) { if(strict) { strictHostFailed(); return false; } if(!askSaveKeyConfirmation(fileName)) { return true; } } else { SSH2PublicKeyFile pkif = new SSH2PublicKeyFile(); pkif.load(fileName); if(pkif.sameAs(serverHostKey.getPublicKey())) { return true; } if(!askChangeKeyConfirmation()) { return false; } tmpFile.delete(); } String user = getProperty("username"); // !!! OUCH if(user == null) { user = SSH.VER_MINDTERM; } SSH2PublicKeyFile pkif = new SSH2PublicKeyFile(serverHostKey.getPublicKey(), user, "\"host key for " + getProperty("server") + ", accepted by " + user + " " + (new Date()) + "\""); pkif.store(fileName); return true; } boolean strictHostKeyCheck() { return Boolean.valueOf(getProperty("strict-hostid")).booleanValue(); } void strictHostFailed() { interactor.report("Strict host key checking enabled, please add host key."); } boolean hasKeyTimingNoise() { return Boolean.valueOf(getProperty("key-timing-noise")).booleanValue(); } boolean askSaveKeyConfirmation(String fileName) { if(interactor.isVerbose()) interactor.report("Host key not found in '" + fileName + "'"); if(!interactor.askConfirmation("Do you want to add this host to your set of known hosts (check fingerprint)", true)) { interactor.report("Verification of server key disabled in this session."); return false; } return true; } boolean askChangeKeyConfirmation() { interactor.alert("WARNING: HOST IDENTIFICATION HAS CHANGED! " + "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY, " + "ONLY PROCEED IF YOU KNOW WHAT YOU ARE DOING!"); return interactor.askConfirmation("Do you want to replace the identification of this host?", false); } void showFingerprint(byte[] blob, String type) { StringBuffer msg = new StringBuffer(); msg.append("\r\nServer's hostkey (" + type + ") fingerprint:\r\n"); msg.append("openssh md5: "); msg.append(SSH2KeyFingerprint.md5Hex(blob)); msg.append("\r\nbubblebabble: "); msg.append(SSH2KeyFingerprint.bubbleBabble(blob)); interactor.report(msg.toString()); } // // ProxyAuthenticator interface // public String getProxyUsername(String type, String challenge) throws IOException { String username = getProperty("proxy-user"); if(!interactor.quietPrompts() || (username == null || username.equals(""))) { String chStr = (challenge != null ? (" '" + challenge + "'") : ""); username = interactor.promptLine(type + chStr + " username: ", username); setProperty("proxy-user", username); } return username; } public String getProxyPassword(String type, String challenge) throws IOException { String prxPassword = getProperty("proxy-password"); if(prxPassword == null) { String chStr = (challenge != null ? (" '" + challenge + "'") : ""); prxPassword = interactor.promptPassword(type + chStr + " password: "); setProperty("proxy-password", prxPassword); } return prxPassword; } /** * Extracts the host spec from a string. The string can be in a * number of different formats. * host_name * host_name:port * 1.2.3.4 * 1.2.3.4:port * ::1 * 3ffe:2a00:100:7031::1 * [3ffe:2a00:100:7031::1]:port * * @return host name */ private String extractHost(String host) { int i; if (host.charAt(0) == '[' && -1 != (i = host.indexOf("]", 1))) { return host.substring(1, i); } if (-1 != (i = host.indexOf(":")) && -1 == host.indexOf(":", i+1)) { return host.substring(0, i); } else { return host; } } /** * Extracts the port number from a string. The string can be in a * number of different formats, see extractHost for examples. * * @return the port or zero if no port was specified */ private int extractPort(String host) { int i, j; if (host.charAt(0) == '[' && -1 != (i = host.indexOf("]", 1)) && -1 != (j = host.indexOf(":", i))) { return Integer.parseInt(host.substring(j+1)); } if (-1 != (i = host.indexOf(":")) && -1 == host.indexOf(":", i+1)) { return Integer.parseInt(host.substring(i+1)); } else { return 0; } } // // SSHClientUser interface // boolean kludgeSrvPrompt; public String getSrvHost() throws IOException { String host = getProperty("server"); String alias; kludgeSrvPrompt = false; if(!interactor.quietPrompts() || (host == null || host.equals(""))) { if(currentAlias != null) host = currentAlias; String input = interactor.promptLine("\r\33[2KSSH Server/Alias: ", host); input = input.trim(); if("".equals(input)) { throw new SSHStdIO.SSHExternalMessage(""); } client.hideLogo(); host = extractHost(input); int port = extractPort(input); if (port != 0) { setProperty("port", String.valueOf(port)); alias = host + "_" + port; } else { alias = host; } if(autoLoadProps) { if(isAlias(alias)) { loadAliasFile(alias, true); } else if(isAbsolutFile(alias)) { loadAbsoluteFile(alias, true); } else if(sshHomeDir != null) { String pwdChk = ""; try { do { alias = interactor.promptLine("No settings file for " + host + " found.\n\r" + "(^C = cancel, ^D or empty = don't save)\n\r" + "Save as alias : ", alias); alias = alias.trim(); if(alias.length() > 0 && savePasswords) { pwdChk = interactor.promptPassword(alias + " file password: "); if(pwdChk.length() > 0) propertyPassword = interactor.promptPassword(alias + " password again: "); } } while ((!pwdChk.equals("") && !pwdChk.equals(propertyPassword))); } catch (SSHStdIO.SSHExternalMessage e) { if(e.ctrlC) { throw e; } alias = ""; } alias = alias.trim(); setProperty("server", host); if(alias.length() == 0) { interactor.report("\r\33[2KNo alias set, disabled automatic saving (use 'Save Settings As...' to save)"); } else { setAlias(alias); } // Might be same host/user/pwd but we don't know, it's a // different alias so we better clear stuff here so the user // can change "identity" in another alias (otherwise if // quietPrompts are used the user might not get a chance to // do this). Also, tunnels are no longer "auto-transfered" // between aliases. // clearPasswords(); clearAllForwards(); propsChanged = true; } host = getProperty("server"); } else { setProperty("server", host); } kludgeSrvPrompt = true; } else { interactor.report(""); client.hideLogo(); } activateProperties(); if(currentPropsFile != null) { interactor.report("Current settings file: '" + currentPropsFile + "'"); } return host; } public int getSrvPort() { return Integer.valueOf(getProperty("port")).intValue(); } public Socket getProxyConnection() throws IOException { String proxyType = getProperty("proxy-type"); int proxyTypeId = SSH.PROXY_NONE; try { proxyTypeId = SSH.getProxyType(proxyType); } catch (IllegalArgumentException e) { throw new IOException(e.getMessage()); } if(proxyTypeId == SSH.PROXY_NONE) { return null; } String prxHost = getProperty("proxy-host"); int prxPort = -1; try { prxPort = Integer.valueOf(getProperty("proxy-port")).intValue(); } catch (Exception e) { prxPort = -1; } if(prxHost == null || prxPort == -1) { throw new IOException("When 'proxytype' is set, 'proxyhost' and 'proxyport' must also be set"); } String sshHost = getProperty("server"); int sshPort = getSrvPort(); String prxProt = getProperty("proxyproto"); Socket proxySocket = null; switch(proxyTypeId) { case SSH.PROXY_HTTP: proxySocket = WebProxyTunnelSocket.getProxy(sshHost, sshPort, prxHost, prxPort, prxProt, this, "MindTerm/" + SSH.CVS_NAME); break; case SSH.PROXY_SOCKS4: proxySocket = SocksProxySocket.getSocks4Proxy(sshHost, sshPort, prxHost, prxPort, getProxyUsername("SOCKS4", null)); break; case SSH.PROXY_SOCKS5_DNS: proxySocket = SocksProxySocket.getSocks5Proxy(sshHost, sshPort, prxHost, prxPort, false, this); break; case SSH.PROXY_SOCKS5_IP: proxySocket = SocksProxySocket.getSocks5Proxy(sshHost, sshPort, prxHost, prxPort, true, this); break; } return proxySocket; } public ByteArrayOutputStream readResource(String name) { InputStream in = getClass().getResourceAsStream(name); ByteArrayOutputStream baos = null; if(in != null) { baos = new ByteArrayOutputStream(50000); try { int c; while((c = in.read()) >= 0) baos.write(c); } catch(IOException e) { // !!! System.err.println("ERROR reading resource " + name + " : " + e); } } return baos; } public String getDisplay() { return getProperty("display"); } public int getMaxPacketSz() { return Integer.valueOf(getProperty("mtu")).intValue(); } public int getAliveInterval() { return Integer.valueOf(getProperty("alive")).intValue(); } public int getCompressionLevel() { return Integer.valueOf(getProperty("compression")).intValue(); } public boolean wantX11Forward() { return Boolean.valueOf(getProperty("x11-forward")).booleanValue(); } public boolean wantPTY() { return Boolean.valueOf(getProperty("force-pty")).booleanValue(); } public SSHInteractor getInteractor() { return interactor; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -