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 setProxySOCKS5(String proxy_host, int proxy_port, String proxy_user, String proxy_password){ proxySOCKS5 = new ProxySOCKS5(proxy_host, proxy_port); if (proxy_user!=null) proxySOCKS5.setUserPasswd(proxy_user, proxy_password); proxyType=SOCKS5; if (myLogger!=null) myLogger.log(com.jcraft.jsch.Logger.DEBUG, "PROXY_SOCKS5 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); } }
/** * 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); }