-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for HTTP CONNECT proxies by implementing custom Socket (F…
…ixes #170)
- Loading branch information
1 parent
8398b6e
commit ace09fa
Showing
3 changed files
with
100 additions
and
29 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/hierynomus/sshj/backport/JavaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.hierynomus.sshj.backport; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class JavaVersion { | ||
public static boolean isJava7OrEarlier() { | ||
String property = System.getProperty("java.specification.version"); | ||
float diff = Float.parseFloat(property) - 1.7f; | ||
|
||
return diff < 0.01; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/hierynomus/sshj/backport/Jdk7HttpProxySocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.hierynomus.sshj.backport; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.*; | ||
import java.nio.charset.Charset; | ||
|
||
public class Jdk7HttpProxySocket extends Socket { | ||
|
||
private Proxy httpProxy = null; | ||
|
||
public Jdk7HttpProxySocket(Proxy proxy) { | ||
super(proxy.type() == Proxy.Type.HTTP ? Proxy.NO_PROXY : proxy); | ||
if (proxy.type() == Proxy.Type.HTTP) { | ||
this.httpProxy = proxy; | ||
} | ||
} | ||
|
||
@Override | ||
public void connect(SocketAddress endpoint, int timeout) throws IOException { | ||
if (httpProxy != null) { | ||
connectHttpProxy(endpoint, timeout); | ||
} else { | ||
super.connect(endpoint, timeout); | ||
} | ||
} | ||
|
||
private void connectHttpProxy(SocketAddress endpoint, int timeout) throws IOException { | ||
super.connect(httpProxy.address(), timeout); | ||
|
||
if (!(endpoint instanceof InetSocketAddress)) { | ||
throw new SocketException("Expected an InetSocketAddress to connect to, got: " + endpoint); | ||
} | ||
InetSocketAddress isa = (InetSocketAddress) endpoint; | ||
String httpConnect = "CONNECT " + isa.getHostName() + ":" + isa.getPort() + " HTTP/1.0\n\n"; | ||
getOutputStream().write(httpConnect.getBytes(Charset.forName("UTF-8"))); | ||
checkAndFlushProxyResponse(); | ||
} | ||
|
||
private void checkAndFlushProxyResponse()throws IOException { | ||
InputStream socketInput = 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters