private void connect(String ip, int port, String user, String key, String passphrase, Proxy proxy) throws JSchException { jsch = new JSch(); jsch.addIdentity(key); session = jsch.getSession(user, ip, port); if (proxy != null) { if ("HTTP".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxyHTTP(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS5".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS5(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS4".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS4(proxy.getProxyIp(), proxy.getProxyPort())); } } session.setUserInfo(new MyUserInfo(null, passphrase)); Properties conf = new Properties(); conf.put("StrictHostKeyChecking", "no"); session.setConfig(conf); synchronized (JSch.class) { session.connect(timeout); } }
public void setProxyHTTP(String proxy_host, int proxy_port, String proxy_user, String proxy_password){ proxyHTTP = new ProxyHTTP(proxy_host, proxy_port); if (proxy_user!=null) proxyHTTP.setUserPasswd(proxy_user, proxy_password); proxyType=HTTP; if (myLogger!=null) myLogger.log(com.jcraft.jsch.Logger.DEBUG, "PROXY_HTTP host="+proxy_host+"; port="+proxy_port+"; user="+proxy_user); }
public Response<String> connect(String ip,int port,String user,String passwd, Proxy proxy){ Response<String> resp = new Response<String>(); try { jsch = new JSch(); session = jsch.getSession(user, ip, port); if (proxy != null) { if ("HTTP".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxyHTTP(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS5".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS5(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS4".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS4(proxy.getProxyIp(), proxy.getProxyPort())); } } session.setPassword(passwd); Properties conf = new Properties(); conf.put("StrictHostKeyChecking", "no"); session.setConfig(conf); session.connect(); ftps = (ChannelSftp) session.openChannel("sftp"); ftps.connect(); resp.setCode(Response.OK); connected = true; } catch (JSchException e) { resp.setCode(Response.IO_EXCEPTION); resp.setPhrase("SftpException: " + e.getMessage()); } return resp; }
public Response<String> connect(String ip,int port,String user,String key,String passphrase, Proxy proxy) { Response<String> resp = new Response<String>(); try { jsch = new JSch(); jsch.addIdentity(key); session = jsch.getSession(user, ip, port); if (proxy != null) { if ("HTTP".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxyHTTP(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS5".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS5(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS4".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS4(proxy.getProxyIp(), proxy.getProxyPort())); } } session.setUserInfo(new MyUserInfo(null,passphrase)); Properties conf = new Properties(); conf.put("StrictHostKeyChecking", "no"); session.setConfig(conf); session.connect(); ftps = (ChannelSftp) session.openChannel("sftp"); ftps.connect(); connected = true; resp.setCode(Response.OK); } catch (JSchException e) { resp.setCode(Response.IO_EXCEPTION); resp.setPhrase("SftpException: " + e.getMessage()); } return resp; }
private void connect(String ip, int port, String user, String passwd, Proxy proxy) throws JSchException { jsch = new JSch(); session = jsch.getSession(user, ip, port); session.setPassword(passwd); if (proxy != null) { if ("HTTP".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxyHTTP(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS5".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS5(proxy.getProxyIp(), proxy.getProxyPort())); } else if ("SOCKS4".equalsIgnoreCase(proxy.getType())) { session.setProxy(new ProxySOCKS4(proxy.getProxyIp(), proxy.getProxyPort())); } } Properties conf = new Properties(); conf.put("StrictHostKeyChecking", "no"); session.setConfig(conf); synchronized (JSch.class) { session.connect(timeout); } }
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry(); final ProxyHTTP proxyHTTP = new ProxyHTTP("localhost", proxyPort); proxyHTTP.setUserPasswd("user", "password"); jndi.bind("proxy", proxyHTTP); return jndi; }
/** * Opens up a connection to specified host using the username. Connects to the source using a private key without * prompting for a password. This method does not support connecting to a source using a password, only by private * key * @throws gobblin.source.extractor.filebased.FileBasedHelperException */ @Override public void connect() throws FileBasedHelperException { String privateKey = PasswordManager.getInstance(state).readPassword(state.getProp(ConfigurationKeys.SOURCE_CONN_PRIVATE_KEY)); String knownHosts = state.getProp(ConfigurationKeys.SOURCE_CONN_KNOWN_HOSTS); String userName = state.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME); String hostName = state.getProp(ConfigurationKeys.SOURCE_CONN_HOST_NAME); int port = state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_PORT, ConfigurationKeys.SOURCE_CONN_DEFAULT_PORT); String proxyHost = state.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL); int proxyPort = state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_USE_PROXY_PORT, -1); JSch.setLogger(new JSchLogger()); JSch jsch = new JSch(); log.info( "Attempting to connect to source via SFTP with" + " privateKey: " + privateKey + " knownHosts: " + knownHosts + " userName: " + userName + " hostName: " + hostName + " port: " + port + " proxyHost: " + proxyHost + " proxyPort: " + proxyPort); try { jsch.addIdentity(privateKey); jsch.setKnownHosts(knownHosts); session = jsch.getSession(userName, hostName, port); if (proxyHost != null && proxyPort >= 0) { session.setProxy(new ProxyHTTP(proxyHost, proxyPort)); } UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); log.info("Finished connecting to source"); } catch (JSchException e) { if (session != null) { session.disconnect(); } log.error(e.getMessage(), e); throw new FileBasedHelperException("Cannot connect to SFTP source", e); } }
/** * Opens up a connection to specified host using the username. Connects to the source using a private key without * prompting for a password. This method does not support connecting to a source using a password, only by private * key * @throws org.apache.gobblin.source.extractor.filebased.FileBasedHelperException */ @Override public void connect() throws FileBasedHelperException { String privateKey = PasswordManager.getInstance(this.state) .readPassword(this.state.getProp(ConfigurationKeys.SOURCE_CONN_PRIVATE_KEY)); String password = PasswordManager.getInstance(this.state) .readPassword(this.state.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD)); String knownHosts = this.state.getProp(ConfigurationKeys.SOURCE_CONN_KNOWN_HOSTS); String userName = this.state.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME); String hostName = this.state.getProp(ConfigurationKeys.SOURCE_CONN_HOST_NAME); int port = this.state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_PORT, ConfigurationKeys.SOURCE_CONN_DEFAULT_PORT); String proxyHost = this.state.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL); int proxyPort = this.state.getPropAsInt(ConfigurationKeys.SOURCE_CONN_USE_PROXY_PORT, -1); JSch.setLogger(new JSchLogger()); JSch jsch = new JSch(); log.info("Attempting to connect to source via SFTP with" + " privateKey: " + privateKey + " knownHosts: " + knownHosts + " userName: " + userName + " hostName: " + hostName + " port: " + port + " proxyHost: " + proxyHost + " proxyPort: " + proxyPort); try { if (!Strings.isNullOrEmpty(privateKey)) { List<IdentityStrategy> identityStrategies = ImmutableList.of(new LocalFileIdentityStrategy(), new DistributedCacheIdentityStrategy(), new HDFSIdentityStrategy()); for (IdentityStrategy identityStrategy : identityStrategies) { if (identityStrategy.setIdentity(privateKey, jsch)) { break; } } } this.session = jsch.getSession(userName, hostName, port); this.session.setConfig("PreferredAuthentications", "publickey,password"); if (Strings.isNullOrEmpty(knownHosts)) { log.info("Known hosts path is not set, StrictHostKeyChecking will be turned off"); this.session.setConfig("StrictHostKeyChecking", "no"); } else { jsch.setKnownHosts(knownHosts); } if (!Strings.isNullOrEmpty(password)) { this.session.setPassword(password); } if (proxyHost != null && proxyPort >= 0) { this.session.setProxy(new ProxyHTTP(proxyHost, proxyPort)); } UserInfo ui = new MyUserInfo(); this.session.setUserInfo(ui); this.session.setDaemonThread(true); this.session.connect(); log.info("Finished connecting to source"); } catch (JSchException e) { if (this.session != null) { this.session.disconnect(); } log.error(e.getMessage(), e); throw new FileBasedHelperException("Cannot connect to SFTP source", e); } }
public static ProxyHTTP getHTTPProxy(String proxyHost, String proxyPort, String proxyUsername, String proxyPassword) { if (!StringUtilities.isEmpty(proxyHost)) { int portForProxy = getPortValue(proxyPort, Constants.DEFAULT_PROXY_PORT); return createHTTPProxy(proxyHost, portForProxy, proxyUsername, proxyPassword); } else { return null; } }
public static ProxyHTTP createHTTPProxy(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword){ ProxyConnectionDetails proxyConnectionDetails = new ProxyConnectionDetails(proxyHost, proxyPort, proxyUsername, proxyPassword); ProxyHTTP proxyHTTP = new ProxyHTTP(proxyConnectionDetails.getProxyHost(), proxyConnectionDetails.getProxyPort()); String username = (StringUtilities.isEmpty(proxyUsername) ? null : proxyUsername); String password = (StringUtilities.isEmpty(proxyPassword) ? null : proxyPassword); proxyHTTP.setUserPasswd(username, password); return proxyHTTP; }
/** * Connects the connection. * @throws IOException if the unfortunate happens. */ @Override public synchronized void connect() throws IOException { logger.debug("connecting..."); Authentication auth = authentication; if (updater != null) { Authentication updatedAuth = updater.updateAuthentication(authentication); if (updatedAuth != null && auth != updatedAuth) { auth = updatedAuth; } } try { client = new JSch(); if (auth.getPrivateKeyPhrase() == null) { client.addIdentity(auth.getPrivateKeyFile().getAbsolutePath(), auth.getPrivateKeyFilePassword()); } else { client.addIdentity(auth.getUsername(), auth.getPrivateKeyPhrase(), null, auth.getPrivateKeyFilePassword().getBytes("UTF-8")); } client.setHostKeyRepository(new BlindHostKeyRepository()); connectSession = client.getSession(auth.getUsername(), host, port); connectSession.setConfig("PreferredAuthentications", "publickey"); if (proxy != null && !proxy.isEmpty()) { String[] splitted = proxy.split(":"); if (splitted.length > 2 && splitted[1].length() >= PROTO_HOST_DELIM_LENGTH) { String pproto = splitted[0]; String phost = splitted[1].substring(2); int pport = Integer.parseInt(splitted[2]); if (pproto.equals("socks5") || pproto.equals("http")) { if (pproto.equals("socks5")) { connectSession.setProxy(new ProxySOCKS5(phost, pport)); } else { connectSession.setProxy(new ProxyHTTP(phost, pport)); } } else { throw new MalformedURLException("Only http and socks5 protocols are supported"); } } else { throw new MalformedURLException(proxy); } } connectSession.connect(this.connectionTimeout); logger.debug("Connected: {}", connectSession.isConnected()); connectSession.setServerAliveInterval(ALIVE_INTERVAL); } catch (JSchException ex) { throw new SshException(ex); } }
private com.jcraft.jsch.Proxy createProxy (String proxyHost, int proxyPort) { return USE_PROXY_TUNNELING ? new ProxyHTTP(proxyHost, proxyPort) : new ProxySOCKS5(proxyHost, proxyPort); }
public Map<String, String> execute(SSHShellInputs sshShellInputs) { Map<String, String> returnResult = new HashMap<>(); SSHService service = null; boolean providerAdded = addSecurityProvider(); String sessionId = ""; try { if (StringUtilities.isEmpty(sshShellInputs.getCommand())) { throw new RuntimeException(COMMAND_IS_NOT_SPECIFIED_MESSAGE); } if (sshShellInputs.getArguments() != null) { sshShellInputs.setCommand(sshShellInputs.getCommand() + " " + sshShellInputs.getArguments()); } int portNumber = StringUtils.toInt(sshShellInputs.getPort(), Constants.DEFAULT_PORT); String knownHostsPolicy = StringUtils.toNotEmptyString(sshShellInputs.getKnownHostsPolicy(), Constants.DEFAULT_KNOWN_HOSTS_POLICY); Path knownHostsPath = StringUtils.toPath(sshShellInputs.getKnownHostsPath(), Constants.DEFAULT_KNOWN_HOSTS_PATH); sessionId = "sshSession:" + sshShellInputs.getHost() + "-" + portNumber + "-" + sshShellInputs.getUsername(); // configure ssh parameters ConnectionDetails connection = new ConnectionDetails(sshShellInputs.getHost(), portNumber, sshShellInputs.getUsername(), sshShellInputs.getPassword()); IdentityKey identityKey = IdentityKeyUtils.getIdentityKey(sshShellInputs.getPrivateKeyFile(), sshShellInputs.getPrivateKeyData(), sshShellInputs.getPassword()); KnownHostsFile knownHostsFile = new KnownHostsFile(knownHostsPath, knownHostsPolicy); // get the cached SSH session service = getSshServiceFromCache(sshShellInputs, sessionId); boolean saveSSHSession = false; if (service == null || !service.isConnected()) { saveSSHSession = true; ProxyHTTP proxyHTTP = ProxyUtils.getHTTPProxy(sshShellInputs.getProxyHost(), sshShellInputs.getProxyPort(), sshShellInputs.getProxyUsername(), sshShellInputs.getProxyPassword()); service = new SSHServiceImpl(connection, identityKey, knownHostsFile, sshShellInputs.getConnectTimeout(), sshShellInputs.isAllowExpectCommands(), proxyHTTP, sshShellInputs.getAllowedCiphers()); } runSSHCommand(sshShellInputs, returnResult, service, sessionId, saveSSHSession); } catch (Exception e) { if (service != null) { cleanupService(sshShellInputs, service, sessionId); } populateResult(returnResult, e); } finally { if (providerAdded) { removeSecurityProvider(); } } return returnResult; }