Skip to content

Instantly share code, notes, and snippets.

View s1monw1's full-sized avatar
☁️
Hello, cloud.

Simon Wirtz s1monw1

☁️
Hello, cloud.
View GitHub Profile
public class TLSConfiguration { ... }
public class StoreType { ... }
public void connectSSL(String host, int port,
TLSConfiguration tlsConfiguration) throws IOException {
String tlsVersion = tlsConfiguration.getProtocol();
StoreType keystore = tlsConfiguration.getKeystore();
StoreType trustStore = tlsConfiguration.getTruststore();
try {
fun connectSSL(host: String, port: Int, protocols: List<String>, kmConfig: Store?, tmConfig: Store?){
val context = createSSLContext(protocols, kmConfig, tmConfig)
val sslSocket = context.socketFactory.createSocket(host, port) as SSLSocket
sslSocket.startHandshake()
}
fun createSSLContext(protocols: List<String>, kmConfig: Store?, tmConfig: Store?): SSLContext {
if (protocols.isEmpty()) {
throw IllegalArgumentException("At least one protocol must be provided.")
}
return SSLContext.getInstance(protocols[0]).apply {
val fac = socketFactory {
trustManager {
if (System.currentTimeMillis() % 2 == 0L) {
open("any") withPass "123456" beingA "jks"
} else {
open("other") withPass "123456" beingA "jks"
}
}
}
val fac = socketFactory {
keyManager {
open("certsandstores/clientkeystore") withPass "123456" beingA "jks"
}
trustManager {
open("certsandstores/myTruststore") withPass "123456" beingA "jks"
}
sockets {
cipherSuites =
listOf("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
class TLSSocketFactoryProvider(init: ProviderConfiguration.() -> Unit) {
private val config: ProviderConfiguration = ProviderConfiguration().apply(init)
fun createSocketFactory(protocols: List<String>): SSLSocketFactory = with(createSSLContext(protocols)) {
return ExtendedSSLSocketFactory(
socketFactory, protocols.toTypedArray(),
getOptionalCipherSuites() ?: socketFactory.defaultCipherSuites)
}
fun createServerSocketFactory(protocols: List<String>): SSLServerSocketFactory = with(createSSLContext(protocols)) {
return ExtendedSSLServerSocketFactory(
serverSocketFactory, protocols.toTypedArray(),
data class SocketConfiguration(
var cipherSuites: List<String>? = null, var timeout: Int? = null,
var clientAuth: Boolean = false)
class Store(val name: String) {
var algorithm: String? = null
var password: CharArray? = null
var fileType: String = "JKS"
infix fun withPass(pass: String) = apply {
password = pass.toCharArray()
}
class ProviderConfiguration {
var kmConfig: Store? = null
var tmConfig: Store? = null
var socketConfig: SocketConfiguration? = null
fun open(name: String) = Store(name)
fun sockets(configInit: SocketConfiguration.() -> Unit) {
this.socketConfig = SocketConfiguration().apply(configInit)
}
fun keyManager(store: () -> Store) {
this.kmConfig = store()
@s1monw1
s1monw1 / build.gradle
Created September 21, 2017 12:49
build.gradle
buildscript {
ext {
kotlinVersion = '1.1.4-3'
springBootVersion = '2.0.0.M4'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
public class MultiReturn {
public static void main(String[] args) {
new MultiReturn().useMulti();
}
public void useMulti() {
Multi multi = helper();
System.out.println("Multi with " + multi.count + " and " + multi.name);
}
private Multi helper() {
return new Multi(2, "test");
fun deployVerticles() {
fun deploy(verticleClassName: String) {
vertx.deployVerticle(verticleClassName, opt, { deploy ->
LOG.info("$verticleClassName has been deployed? ${deploy.succeeded()}")
})
}
deploy("ServiceVerticle")
deploy("WebVerticle")
}