mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 08:10:55 +03:00
Revert "Added support for (unauthenticated) HTTP proxies (fixes #170)"
This reverts commit fc535a5e76.
This commit is contained in:
@@ -1,92 +0,0 @@
|
|||||||
package com.hierynomus.sshj.socket;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Proxy;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.SocketException;
|
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* https://code.google.com/p/java-socket-over-http-proxy-connect/source/browse/trunk/src/sg/com/en/SocketFactory.java
|
|
||||||
*/
|
|
||||||
public class SocketFactory {
|
|
||||||
|
|
||||||
private static final int DEFAULT_CONNECT_TIMEOUT = 0;
|
|
||||||
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
|
||||||
|
|
||||||
private final javax.net.SocketFactory delegateSocketFactory = javax.net.SocketFactory.getDefault();
|
|
||||||
|
|
||||||
public static SocketFactory getDefault() {
|
|
||||||
return new SocketFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Socket createSocket(String address, int port) throws IOException {
|
|
||||||
return createSocket(new InetSocketAddress(address, port));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Socket createSocket(InetSocketAddress inetSocketAddress) throws IOException {
|
|
||||||
Socket socket = delegateSocketFactory.createSocket();
|
|
||||||
socket.connect(inetSocketAddress, connectTimeout);
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Socket createSocket(InetSocketAddress inetSocketAddress, Proxy proxy) throws IOException {
|
|
||||||
if (proxy.type() == Proxy.Type.HTTP) {
|
|
||||||
return createHttpProxySocket(inetSocketAddress, proxy);
|
|
||||||
}
|
|
||||||
Socket socket = new Socket(proxy);
|
|
||||||
socket.connect(inetSocketAddress, connectTimeout);
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Socket createHttpProxySocket(InetSocketAddress inetSocketAddress, Proxy proxy) throws IOException {
|
|
||||||
Socket socket = delegateSocketFactory.createSocket();
|
|
||||||
socket.connect(proxy.address());
|
|
||||||
|
|
||||||
String connect = format("CONNECT %s:%d\n\n", inetSocketAddress.getHostName(), inetSocketAddress.getPort());
|
|
||||||
socket.getOutputStream().write(connect.getBytes());
|
|
||||||
checkAndFlushProxyResponse(socket);
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkAndFlushProxyResponse(Socket socket)throws IOException {
|
|
||||||
InputStream socketInput = socket.getInputStream();
|
|
||||||
byte[] tmpBuffer = new byte[512];
|
|
||||||
int len = socketInput.read(tmpBuffer, 0, tmpBuffer.length);
|
|
||||||
|
|
||||||
if (len == 0) {
|
|
||||||
throw new SocketException("Empty response from proxy");
|
|
||||||
}
|
|
||||||
|
|
||||||
String proxyResponse = new String(tmpBuffer, 0, len, "UTF-8");
|
|
||||||
|
|
||||||
// Expecting HTTP/1.x 200 OK
|
|
||||||
if (proxyResponse.contains("200")) {
|
|
||||||
// Flush any outstanding message in buffer
|
|
||||||
if (socketInput.available() > 0)
|
|
||||||
socketInput.skip(socketInput.available());
|
|
||||||
// Proxy Connect Successful
|
|
||||||
} else {
|
|
||||||
throw new SocketException("Fail to create Socket\nResponse was:" + proxyResponse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Socket createSocket(InetSocketAddress bindpoint, InetSocketAddress endpoint) throws IOException {
|
|
||||||
Socket socket = delegateSocketFactory.createSocket();
|
|
||||||
socket.bind(bindpoint);
|
|
||||||
socket.connect(endpoint, connectTimeout);
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getConnectTimeout() {
|
|
||||||
return connectTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectTimeout(int connectTimeout) {
|
|
||||||
this.connectTimeout = connectTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -15,8 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj;
|
package net.schmizz.sshj;
|
||||||
|
|
||||||
import com.hierynomus.sshj.socket.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -33,7 +32,10 @@ public abstract class SocketClient {
|
|||||||
private InputStream input;
|
private InputStream input;
|
||||||
private OutputStream output;
|
private OutputStream output;
|
||||||
|
|
||||||
private SocketFactory socketFactory = new SocketFactory();
|
private SocketFactory socketFactory = SocketFactory.getDefault();
|
||||||
|
|
||||||
|
private static final int DEFAULT_CONNECT_TIMEOUT = 0;
|
||||||
|
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
||||||
|
|
||||||
private int timeout = 0;
|
private int timeout = 0;
|
||||||
|
|
||||||
@@ -45,13 +47,15 @@ public abstract class SocketClient {
|
|||||||
|
|
||||||
public void connect(InetAddress host, int port)
|
public void connect(InetAddress host, int port)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
socket = socketFactory.createSocket(new InetSocketAddress(host, port));
|
socket = socketFactory.createSocket();
|
||||||
|
socket.connect(new InetSocketAddress(host, port), connectTimeout);
|
||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(InetAddress host, int port, Proxy proxy)
|
public void connect(InetAddress host, int port, Proxy proxy)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
socket = socketFactory.createSocket(new InetSocketAddress(host, port), proxy);
|
socket = new Socket(proxy);
|
||||||
|
socket.connect(new InetSocketAddress(host, port), connectTimeout);
|
||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,9 +74,9 @@ public abstract class SocketClient {
|
|||||||
public void connect(InetAddress host, int port,
|
public void connect(InetAddress host, int port,
|
||||||
InetAddress localAddr, int localPort)
|
InetAddress localAddr, int localPort)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
InetSocketAddress bindpoint = new InetSocketAddress(localAddr, localPort);
|
socket = socketFactory.createSocket();
|
||||||
InetSocketAddress endpoint = new InetSocketAddress(host, port);
|
socket.bind(new InetSocketAddress(localAddr, localPort));
|
||||||
socket = socketFactory.createSocket(bindpoint, endpoint);
|
socket.connect(new InetSocketAddress(host, port), connectTimeout);
|
||||||
onConnect();
|
onConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,11 +160,11 @@ public abstract class SocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getConnectTimeout() {
|
public int getConnectTimeout() {
|
||||||
return socketFactory.getConnectTimeout();
|
return connectTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConnectTimeout(int connectTimeout) {
|
public void setConnectTimeout(int connectTimeout) {
|
||||||
socketFactory.setConnectTimeout(connectTimeout);
|
this.connectTimeout = connectTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTimeout() {
|
public int getTimeout() {
|
||||||
|
|||||||
Reference in New Issue
Block a user