diff --git a/src/main/java/net/schmizz/sshj/SocketClient.java b/src/main/java/net/schmizz/sshj/SocketClient.java index 7d7dd9d1..7a843380 100644 --- a/src/main/java/net/schmizz/sshj/SocketClient.java +++ b/src/main/java/net/schmizz/sshj/SocketClient.java @@ -48,12 +48,50 @@ public abstract class SocketClient { this.defaultPort = defaultPort; } - public void connect(InetAddress host, int port) throws IOException { - socket = socketFactory.createSocket(); - socket.connect(new InetSocketAddress(host, port), connectTimeout); + /** + * Connect to a host via a proxy. + * @param hostname The host name to connect to. + * @param proxy The proxy to connect via. + * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} + * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. + */ + @Deprecated + public void connect(String hostname, Proxy proxy) throws IOException { + connect(hostname, defaultPort, proxy); + } + + /** + * Connect to a host via a proxy. + * @param hostname The host name to connect to. + * @param port The port to connect to. + * @param proxy The proxy to connect via. + * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} + * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. + */ + @Deprecated + public void connect(String hostname, int port, Proxy proxy) throws IOException { + this.hostname = hostname; + if (JavaVersion.isJava7OrEarlier() && proxy.type() == Proxy.Type.HTTP) { + // Java7 and earlier have no support for HTTP Connect proxies, return our custom socket. + socket = new Jdk7HttpProxySocket(proxy); + } else { + socket = new Socket(proxy); + } + socket.connect(new InetSocketAddress(hostname, port), connectTimeout); onConnect(); } + /** + * Connect to a host via a proxy. + * @param host The host address to connect to. + * @param proxy The proxy to connect via. + * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} + * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. + */ + @Deprecated + public void connect(InetAddress host, Proxy proxy) throws IOException { + connect(host, defaultPort, proxy); + } /** * Connect to a host via a proxy. @@ -75,23 +113,33 @@ public abstract class SocketClient { onConnect(); } - public void connect(String hostname, int port) throws IOException { - this.hostname = hostname; - connect(InetAddress.getByName(hostname), port); + public void connect(String hostname) throws IOException { + connect(hostname, defaultPort); } - /** - * Connect to a host via a proxy. - * @param hostname The host name to connect to. - * @param port The port to connect to. - * @param proxy The proxy to connect via. - * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} - * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. - */ - @Deprecated - public void connect(String hostname, int port, Proxy proxy) throws IOException { + public void connect(String hostname, int port) throws IOException { this.hostname = hostname; - connect(InetAddress.getByName(hostname), port, proxy); + socket = socketFactory.createSocket(); + socket.connect(new InetSocketAddress(hostname, port), connectTimeout); + onConnect(); + } + + public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException { + this.hostname = hostname; + socket = socketFactory.createSocket(); + socket.bind(new InetSocketAddress(localAddr, localPort)); + socket.connect(new InetSocketAddress(hostname, port), connectTimeout); + onConnect(); + } + + public void connect(InetAddress host) throws IOException { + connect(host, defaultPort); + } + + public void connect(InetAddress host, int port) throws IOException { + socket = socketFactory.createSocket(); + socket.connect(new InetSocketAddress(host, port), connectTimeout); + onConnect(); } public void connect(InetAddress host, int port, InetAddress localAddr, int localPort) @@ -102,43 +150,6 @@ public abstract class SocketClient { onConnect(); } - public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException { - this.hostname = hostname; - connect(InetAddress.getByName(hostname), port, localAddr, localPort); - } - - public void connect(InetAddress host) throws IOException { - connect(host, defaultPort); - } - - public void connect(String hostname) throws IOException { - connect(hostname, defaultPort); - } - - /** - * Connect to a host via a proxy. - * @param host The host address to connect to. - * @param proxy The proxy to connect via. - * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} - * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. - */ - @Deprecated - public void connect(InetAddress host, Proxy proxy) throws IOException { - connect(host, defaultPort, proxy); - } - - /** - * Connect to a host via a proxy. - * @param hostname The host name to connect to. - * @param proxy The proxy to connect via. - * @deprecated This method will be removed after v0.12.0. If you want to connect via a proxy, you can do this by injecting a {@link javax.net.SocketFactory} - * into the SocketClient. The SocketFactory should create sockets using the {@link java.net.Socket#Socket(java.net.Proxy)} constructor. - */ - @Deprecated - public void connect(String hostname, Proxy proxy) throws IOException { - connect(hostname, defaultPort, proxy); - } - public void disconnect() throws IOException { if (socket != null) { socket.close();