2020-06-22 17:02:36 +00:00
|
|
|
/*
|
|
|
|
Mangadex@Home
|
|
|
|
Copyright (c) 2020, MangaDex Network
|
|
|
|
This file is part of MangaDex@Home.
|
|
|
|
|
|
|
|
MangaDex@Home is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
MangaDex@Home is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this MangaDex@Home. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-30 19:06:12 +00:00
|
|
|
/* ktlint-disable no-wildcard-imports */
|
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-30 19:06:12 +00:00
|
|
|
import io.netty.channel.*
|
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-30 19:06:12 +00:00
|
|
|
import io.netty.handler.codec.http.*
|
2020-06-06 22:52:25 +00:00
|
|
|
import io.netty.handler.ssl.SslContextBuilder
|
|
|
|
import io.netty.handler.stream.ChunkedWriteHandler
|
2020-07-04 17:14:51 +00:00
|
|
|
import io.netty.handler.timeout.ReadTimeoutException
|
2020-06-30 19:06:12 +00:00
|
|
|
import io.netty.handler.timeout.ReadTimeoutHandler
|
2020-07-04 17:14:51 +00:00
|
|
|
import io.netty.handler.timeout.WriteTimeoutException
|
2020-06-30 19:06:12 +00:00
|
|
|
import io.netty.handler.timeout.WriteTimeoutHandler
|
2020-06-06 22:52:25 +00:00
|
|
|
import io.netty.handler.traffic.GlobalTrafficShapingHandler
|
|
|
|
import io.netty.handler.traffic.TrafficCounter
|
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-22 17:08:46 +00:00
|
|
|
import java.net.SocketException
|
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.atomic.AtomicReference
|
2020-06-08 22:10:22 +00:00
|
|
|
import javax.net.ssl.SSLException
|
2020-07-02 16:06:32 +00:00
|
|
|
import mdnet.base.Constants
|
|
|
|
import mdnet.base.data.Statistics
|
2020-07-04 19:39:11 +00:00
|
|
|
import mdnet.base.info
|
2020-08-11 16:47:04 +00:00
|
|
|
import mdnet.base.settings.ServerSettings
|
2020-07-02 16:06:32 +00:00
|
|
|
import mdnet.base.settings.TlsCert
|
2020-07-04 19:39:11 +00:00
|
|
|
import mdnet.base.trace
|
2020-07-02 16:06:32 +00:00
|
|
|
import org.http4k.core.HttpHandler
|
|
|
|
import org.http4k.server.Http4kChannelHandler
|
|
|
|
import org.http4k.server.Http4kServer
|
|
|
|
import org.http4k.server.ServerConfig
|
|
|
|
import org.slf4j.LoggerFactory
|
2020-06-08 22:10:22 +00:00
|
|
|
|
2020-08-11 19:12:01 +00:00
|
|
|
private val LOGGER = LoggerFactory.getLogger("AppNetty")
|
2020-06-06 22:52:25 +00:00
|
|
|
|
2020-08-11 19:12:01 +00:00
|
|
|
class Netty(private val tls: TlsCert, private val serverSettings: ServerSettings, private val statistics: AtomicReference<Statistics>) : ServerConfig {
|
2020-06-06 22:52:25 +00:00
|
|
|
override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
|
2020-08-11 16:47:04 +00:00
|
|
|
private val masterGroup = NioEventLoopGroup(serverSettings.threads)
|
|
|
|
private val workerGroup = NioEventLoopGroup(serverSettings.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-08-11 16:47:04 +00:00
|
|
|
workerGroup, serverSettings.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-08-11 16:47:04 +00:00
|
|
|
LOGGER.info { "Starting Netty with ${serverSettings.threads} threads" }
|
2020-06-11 18:47:43 +00:00
|
|
|
|
2020-06-27 18:15:49 +00:00
|
|
|
val certs = getX509Certs(tls.certificate)
|
2020-06-09 19:21:18 +00:00
|
|
|
val sslContext = SslContextBuilder
|
2020-06-27 18:15:49 +00:00
|
|
|
.forServer(getPrivateKey(tls.privateKey), certs)
|
2020-06-12 17:58:10 +00:00
|
|
|
.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)
|
2020-06-30 19:06:12 +00:00
|
|
|
|
|
|
|
ch.pipeline().addLast("readTimeoutHandler", ReadTimeoutHandler(Constants.MAX_READ_TIME_SECONDS))
|
|
|
|
ch.pipeline().addLast("writeTimeoutHandler", WriteTimeoutHandler(Constants.MAX_WRITE_TIME_SECONDS))
|
|
|
|
|
2020-06-06 22:52:25 +00:00
|
|
|
ch.pipeline().addLast("streamer", ChunkedWriteHandler())
|
|
|
|
ch.pipeline().addLast("handler", Http4kChannelHandler(httpHandler))
|
2020-06-08 22:10:22 +00:00
|
|
|
|
2020-08-11 16:47:04 +00:00
|
|
|
ch.pipeline().addLast("exceptions", object : ChannelInboundHandlerAdapter() {
|
2020-06-08 22:10:22 +00:00
|
|
|
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
|
|
|
|
if (cause is SSLException || (cause is DecoderException && cause.cause is SSLException)) {
|
2020-07-04 19:39:11 +00:00
|
|
|
LOGGER.trace { "Ignored invalid SSL connection" }
|
2020-06-22 17:08:46 +00:00
|
|
|
} else if (cause is IOException || cause is SocketException) {
|
2020-07-04 19:39:11 +00:00
|
|
|
LOGGER.info { "User (downloader) abruptly closed the connection" }
|
|
|
|
LOGGER.trace(cause) { "Exception in pipeline" }
|
2020-07-04 17:14:51 +00:00
|
|
|
} else if (cause !is ReadTimeoutException && cause !is WriteTimeoutException) {
|
2020-06-08 22:10:22 +00:00
|
|
|
ctx.fireExceptionCaught(cause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-06-06 22:52:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.option(ChannelOption.SO_BACKLOG, 1000)
|
|
|
|
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
|
|
2020-08-11 16:47:04 +00:00
|
|
|
val channel = bootstrap.bind(InetSocketAddress(serverSettings.clientHostname, serverSettings.clientPort)).sync().channel()
|
2020-06-06 22:52:25 +00:00
|
|
|
address = channel.localAddress() as InetSocketAddress
|
|
|
|
closeFuture = channel.closeFuture()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun stop() = apply {
|
2020-08-21 15:50:02 +00:00
|
|
|
closeFuture.cancel(false)
|
|
|
|
workerGroup.shutdownGracefully()
|
|
|
|
masterGroup.shutdownGracefully()
|
2020-06-06 22:52:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 16:47:04 +00:00
|
|
|
override fun port(): Int = if (serverSettings.clientPort > 0) serverSettings.clientPort else address.port
|
2020-06-06 22:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 19:21:18 +00:00
|
|
|
|
2020-06-27 18:15:49 +00:00
|
|
|
fun getX509Certs(certificates: String): Collection<X509Certificate> {
|
2020-06-09 19:21:18 +00:00
|
|
|
val targetStream: InputStream = ByteArrayInputStream(certificates.toByteArray())
|
2020-06-27 18:15:49 +00:00
|
|
|
@Suppress("unchecked_cast")
|
|
|
|
return CertificateFactory.getInstance("X509").generateCertificates(targetStream) as Collection<X509Certificate>
|
2020-06-09 19:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun getPrivateKey(privateKey: String): PrivateKey {
|
|
|
|
return loadKey(privateKey)!!
|
2020-06-09 19:21:29 +00:00
|
|
|
}
|