2020-06-19 20:16:24 +00:00
|
|
|
package mdnet.base.netty
|
2020-06-06 22:52:25 +00:00
|
|
|
|
|
|
|
import io.netty.bootstrap.ServerBootstrap
|
2020-06-16 03:50:29 +00:00
|
|
|
import io.netty.channel.ChannelFactory
|
|
|
|
import io.netty.channel.ChannelFuture
|
|
|
|
import io.netty.channel.ChannelHandlerContext
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter
|
|
|
|
import io.netty.channel.ChannelInitializer
|
|
|
|
import io.netty.channel.ChannelOption
|
|
|
|
import io.netty.channel.ServerChannel
|
2020-06-06 22:52:25 +00:00
|
|
|
import io.netty.channel.nio.NioEventLoopGroup
|
|
|
|
import io.netty.channel.socket.SocketChannel
|
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel
|
2020-06-08 22:10:22 +00:00
|
|
|
import io.netty.handler.codec.DecoderException
|
2020-06-06 22:52:25 +00:00
|
|
|
import io.netty.handler.codec.http.HttpObjectAggregator
|
|
|
|
import io.netty.handler.codec.http.HttpServerCodec
|
2020-06-16 03:45:59 +00:00
|
|
|
import io.netty.handler.codec.http.HttpServerKeepAliveHandler
|
2020-06-06 22:52:25 +00:00
|
|
|
import io.netty.handler.ssl.SslContextBuilder
|
|
|
|
import io.netty.handler.stream.ChunkedWriteHandler
|
|
|
|
import io.netty.handler.traffic.GlobalTrafficShapingHandler
|
|
|
|
import io.netty.handler.traffic.TrafficCounter
|
2020-06-19 20:16:24 +00:00
|
|
|
import mdnet.base.Statistics
|
2020-06-12 18:52:50 +00:00
|
|
|
import mdnet.base.settings.ClientSettings
|
2020-06-19 20:16:24 +00:00
|
|
|
import mdnet.base.settings.TlsCert
|
2020-06-06 22:52:25 +00:00
|
|
|
import org.http4k.core.HttpHandler
|
|
|
|
import org.http4k.server.Http4kChannelHandler
|
|
|
|
import org.http4k.server.Http4kServer
|
|
|
|
import org.http4k.server.ServerConfig
|
2020-06-08 22:10:22 +00:00
|
|
|
import org.slf4j.LoggerFactory
|
2020-06-09 19:21:18 +00:00
|
|
|
import java.io.ByteArrayInputStream
|
2020-06-09 14:40:36 +00:00
|
|
|
import java.io.IOException
|
2020-06-09 19:21:18 +00:00
|
|
|
import java.io.InputStream
|
2020-06-06 22:52:25 +00:00
|
|
|
import java.net.InetSocketAddress
|
2020-06-09 19:21:18 +00:00
|
|
|
import java.security.PrivateKey
|
|
|
|
import java.security.cert.CertificateFactory
|
|
|
|
import java.security.cert.X509Certificate
|
2020-06-06 22:52:25 +00:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
2020-06-08 22:10:22 +00:00
|
|
|
import javax.net.ssl.SSLException
|
|
|
|
|
|
|
|
private val LOGGER = LoggerFactory.getLogger("Application")
|
2020-06-06 22:52:25 +00:00
|
|
|
|
2020-06-19 20:16:24 +00:00
|
|
|
class Netty(private val tls: TlsCert, private val clientSettings: ClientSettings, private val statistics: AtomicReference<Statistics>) : ServerConfig {
|
2020-06-06 22:52:25 +00:00
|
|
|
override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
|
2020-06-13 03:35:08 +00:00
|
|
|
private val masterGroup = NioEventLoopGroup(clientSettings.threads)
|
|
|
|
private val workerGroup = NioEventLoopGroup(clientSettings.threads)
|
2020-06-06 22:52:25 +00:00
|
|
|
private lateinit var closeFuture: ChannelFuture
|
|
|
|
private lateinit var address: InetSocketAddress
|
|
|
|
|
|
|
|
private val burstLimiter = object : GlobalTrafficShapingHandler(
|
2020-06-17 23:01:51 +00:00
|
|
|
workerGroup, clientSettings.maxKilobitsPerSecond * 1000L / 8L, 0, 50) {
|
2020-06-06 22:52:25 +00:00
|
|
|
override fun doAccounting(counter: TrafficCounter) {
|
2020-06-13 03:35:08 +00:00
|
|
|
statistics.getAndUpdate {
|
|
|
|
it.copy(bytesSent = it.bytesSent + counter.cumulativeWrittenBytes())
|
|
|
|
}
|
2020-06-09 14:40:36 +00:00
|
|
|
counter.resetCumulativeTime()
|
2020-06-06 22:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun start(): Http4kServer = apply {
|
2020-06-11 18:47:43 +00:00
|
|
|
if (LOGGER.isInfoEnabled) {
|
2020-06-13 22:36:26 +00:00
|
|
|
LOGGER.info("Starting Netty with {} threads", clientSettings.threads)
|
2020-06-11 18:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 19:21:29 +00:00
|
|
|
val (mainCert, chainCert) = getX509Certs(tls.certificate)
|
2020-06-09 19:21:18 +00:00
|
|
|
val sslContext = SslContextBuilder
|
2020-06-12 17:58:10 +00:00
|
|
|
.forServer(getPrivateKey(tls.privateKey), mainCert, chainCert)
|
|
|
|
.protocols("TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1")
|
|
|
|
.build()
|
2020-06-06 22:52:25 +00:00
|
|
|
|
|
|
|
val bootstrap = ServerBootstrap()
|
|
|
|
bootstrap.group(masterGroup, workerGroup)
|
|
|
|
.channelFactory(ChannelFactory<ServerChannel> { NioServerSocketChannel() })
|
|
|
|
.childHandler(object : ChannelInitializer<SocketChannel>() {
|
|
|
|
public override fun initChannel(ch: SocketChannel) {
|
2020-06-09 22:37:42 +00:00
|
|
|
ch.pipeline().addLast("ssl", sslContext.newHandler(ch.alloc()))
|
2020-06-06 22:52:25 +00:00
|
|
|
|
|
|
|
ch.pipeline().addLast("codec", HttpServerCodec())
|
2020-06-16 03:45:59 +00:00
|
|
|
ch.pipeline().addLast("keepAlive", HttpServerKeepAliveHandler())
|
2020-06-06 22:52:25 +00:00
|
|
|
ch.pipeline().addLast("aggregator", HttpObjectAggregator(65536))
|
2020-06-09 19:29:33 +00:00
|
|
|
|
2020-06-06 22:52:25 +00:00
|
|
|
ch.pipeline().addLast("burstLimiter", burstLimiter)
|
|
|
|
ch.pipeline().addLast("streamer", ChunkedWriteHandler())
|
|
|
|
ch.pipeline().addLast("handler", Http4kChannelHandler(httpHandler))
|
2020-06-08 22:10:22 +00:00
|
|
|
|
|
|
|
ch.pipeline().addLast("handle_ssl", object : ChannelInboundHandlerAdapter() {
|
|
|
|
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
|
|
|
|
if (cause is SSLException || (cause is DecoderException && cause.cause is SSLException)) {
|
|
|
|
if (LOGGER.isTraceEnabled) {
|
|
|
|
LOGGER.trace("Ignored invalid SSL connection")
|
|
|
|
}
|
2020-06-13 22:46:18 +00:00
|
|
|
} else if (cause is IOException) {
|
|
|
|
if (LOGGER.isInfoEnabled) {
|
|
|
|
LOGGER.info("User (downloader) abruptly closed the connection")
|
|
|
|
}
|
2020-06-09 14:40:36 +00:00
|
|
|
if (LOGGER.isTraceEnabled) {
|
2020-06-13 22:46:18 +00:00
|
|
|
LOGGER.trace("IOException in pipeline", cause)
|
2020-06-09 14:40:36 +00:00
|
|
|
}
|
2020-06-08 22:10:22 +00:00
|
|
|
} else {
|
|
|
|
ctx.fireExceptionCaught(cause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-06-06 22:52:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.option(ChannelOption.SO_BACKLOG, 1000)
|
|
|
|
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
|
|
2020-06-14 23:47:57 +00:00
|
|
|
val channel = bootstrap.bind(InetSocketAddress(clientSettings.clientHostname, clientSettings.clientPort)).sync().channel()
|
2020-06-06 22:52:25 +00:00
|
|
|
address = channel.localAddress() as InetSocketAddress
|
|
|
|
closeFuture = channel.closeFuture()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun stop() = apply {
|
|
|
|
masterGroup.shutdownGracefully(5, 15, TimeUnit.SECONDS).sync()
|
|
|
|
workerGroup.shutdownGracefully(5, 15, TimeUnit.SECONDS).sync()
|
|
|
|
closeFuture.sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun port(): Int = if (clientSettings.clientPort > 0) clientSettings.clientPort else address.port
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 19:21:18 +00:00
|
|
|
|
|
|
|
fun getX509Certs(certificates: String): Pair<X509Certificate, X509Certificate> {
|
|
|
|
val targetStream: InputStream = ByteArrayInputStream(certificates.toByteArray())
|
|
|
|
return (CertificateFactory.getInstance("X509").generateCertificate(targetStream) as X509Certificate) to (CertificateFactory.getInstance("X509").generateCertificate(targetStream) as X509Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getPrivateKey(privateKey: String): PrivateKey {
|
|
|
|
return loadKey(privateKey)!!
|
2020-06-09 19:21:29 +00:00
|
|
|
}
|