Created
October 7, 2016 16:33
-
-
Save danthegoodman/2b4560e607e760d9895b2c3de10ada01 to your computer and use it in GitHub Desktop.
Ratpack SNI Workaround
This file contains hidden or 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
import ratpack.http.client.HttpClient; | |
import ratpack.test.embed.EmbeddedApp; | |
import javax.net.ssl.SniSslContext; | |
import java.net.URI; | |
class AppTest { | |
static void main(String[] args) throws Exception { | |
EmbeddedApp.fromHandler(ctx -> { | |
URI uri = URI.create("#### A URL POINTING TO A SERVER USING SNI SSL ####"); | |
ctx.get(HttpClient.class).get(uri, req -> { | |
req.sslContext(SniSslContext.forHostAndPort("Default", uri.getHost(), uri.getPort())); | |
}).then(resp -> { | |
ctx.render(resp.getBody().getText()); | |
}); | |
}).test(testHttp -> { | |
System.out.println(testHttp.getText("/")); | |
}); | |
} | |
} |
This file contains hidden or 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
package javax.net.ssl; | |
import sun.security.jca.GetInstance; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.Provider; | |
public class SniSslContext extends SSLContext { | |
static public SSLContext forHostAndPort(String type, String host, int port) throws NoSuchAlgorithmException { | |
GetInstance.Instance var1 = GetInstance.getInstance("SSLContext", SSLContextSpi.class, type); | |
SniSslContextSpi mySpi = new SniSslContextSpi(host, port, (SSLContextSpi) var1.impl); | |
return new SniSslContext(mySpi, var1.provider, type); | |
} | |
private SniSslContext(SSLContextSpi sslContextSpi, Provider provider, String s) { | |
super(sslContextSpi, provider, s); | |
} | |
} |
This file contains hidden or 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
package javax.net.ssl; | |
import java.security.KeyManagementException; | |
import java.security.SecureRandom; | |
public class SniSslContextSpi extends SSLContextSpi { | |
private final String host; | |
private final int port; | |
private final SSLContextSpi delegate; | |
public SniSslContextSpi(String host, int port, SSLContextSpi delegate) { | |
this.host = host; | |
this.port = port; | |
this.delegate = delegate; | |
} | |
@Override | |
protected void engineInit(KeyManager[] keyManagers, TrustManager[] trustManagers, SecureRandom secureRandom) throws KeyManagementException { | |
delegate.engineInit(keyManagers, trustManagers, secureRandom); | |
} | |
@Override | |
protected SSLSocketFactory engineGetSocketFactory() { | |
return delegate.engineGetSocketFactory(); | |
} | |
@Override | |
protected SSLServerSocketFactory engineGetServerSocketFactory() { | |
return delegate.engineGetServerSocketFactory(); | |
} | |
protected SSLEngine engineCreateSSLEngine() { | |
//------------------------------------------ | |
// This is the what makes it work. Ratpack calls this method from RequestActionSupport | |
// when setting up an SSL handler. We switch it out for the version with the host and | |
// port specified, which causes the correct info to be included in the SSL handshake. | |
//------------------------------------------ | |
return delegate.engineCreateSSLEngine(host, port); | |
} | |
@Override | |
protected SSLEngine engineCreateSSLEngine(String host, int port) { | |
return delegate.engineCreateSSLEngine(host, port); | |
} | |
@Override | |
protected SSLSessionContext engineGetServerSessionContext() { | |
return delegate.engineGetServerSessionContext(); | |
} | |
@Override | |
protected SSLSessionContext engineGetClientSessionContext() { | |
return delegate.engineGetClientSessionContext(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment