diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-08-08 12:05:43 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-08-08 12:05:43 +0100 |
commit | 4a11cb6875e196297d44553c3f640d7bbb25256c (patch) | |
tree | 2fbb34d94f9c8e861f006a4cb9a7aa067ec41bc7 /server/src/net/usebox/gemini | |
parent | a42d3883f110831440f2d85977e9251c4a22541a (diff) | |
download | spacebeans-4a11cb6875e196297d44553c3f640d7bbb25256c.tar.gz spacebeans-4a11cb6875e196297d44553c3f640d7bbb25256c.zip |
Formattting
Diffstat (limited to 'server/src/net/usebox/gemini')
7 files changed, 71 insertions, 78 deletions
diff --git a/server/src/net/usebox/gemini/server/Response.scala b/server/src/net/usebox/gemini/server/Response.scala index 82fa95d..9675262 100644 --- a/server/src/net/usebox/gemini/server/Response.scala +++ b/server/src/net/usebox/gemini/server/Response.scala @@ -5,13 +5,12 @@ import java.nio.file.Path import scala.sys.process._ import scala.util.Try -import org.log4s._ +import net.usebox.gemini.server.URIUtils._ +import akka.stream.scaladsl.{FileIO, Source} import akka.stream.ActorAttributes -import akka.stream.scaladsl.{Source, FileIO} import akka.util.ByteString - -import URIUtils._ +import org.log4s._ sealed trait Response { def req: String @@ -58,21 +57,21 @@ case class DirListing( (List(s"# Index of ${uriPath}\n") ++ (if (uriPath != "/") List(s"=> ../ ..") else Nil) ++ - path - .toFile() - .listFiles() - .toList - .sortBy { - case f if f.isDirectory() => 0 - case f if f.isFile() => 1 - case _ => 2 - } - .flatMap { - case f if !f.canRead() || f.getName().startsWith(".") => None - case f if f.isDirectory() => - Some(s"=> ${f.getName().encode()}/ ${f.getName()}/") - case f => Some(s"=> ${f.getName().encode()} ${f.getName()}") - }).mkString("\n") + "\n" + path + .toFile() + .listFiles() + .toList + .sortBy { + case f if f.isDirectory() => 0 + case f if f.isFile() => 1 + case _ => 2 + } + .flatMap { + case f if !f.canRead() || f.getName().startsWith(".") => None + case f if f.isDirectory() => + Some(s"=> ${f.getName().encode()}/ ${f.getName()}/") + case f => Some(s"=> ${f.getName().encode()} ${f.getName()}") + }).mkString("\n") + "\n" } def bodySize: Long = body.size @@ -122,7 +121,9 @@ case class Cgi( Try { val jpb = new java.lang.ProcessBuilder(filename) jpb.environment.clear() - env.foreach { case (k, v) => jpb.environment.put(k, v) } + env.foreach { + case (k, v) => jpb.environment.put(k, v) + } val exit = (Process(jpb) #> output).! output.close() @@ -132,8 +133,7 @@ case class Cgi( case Right(0) => val body = output.toString("UTF-8") body.split("\r\n").headOption match { - case Some(req @ responseRe(status, meta)) - if req.length <= Server.maxReqLen => + case Some(req @ responseRe(status, meta)) if req.length <= Server.maxReqLen => (status.toInt, meta, body) case _ => logger.warn(s"$scriptName: invalid CGI response") diff --git a/server/src/net/usebox/gemini/server/Server.scala b/server/src/net/usebox/gemini/server/Server.scala index 96cf5ef..3925f12 100644 --- a/server/src/net/usebox/gemini/server/Server.scala +++ b/server/src/net/usebox/gemini/server/Server.scala @@ -1,24 +1,24 @@ package net.usebox.gemini.server +import java.net.URI import java.nio.charset.Charset import javax.net.ssl.SSLEngine -import java.net.URI -import scala.util.{Try, Success => TrySuccess} +import scala.concurrent.ExecutionContextExecutor +import scala.util.{Success => TrySuccess, Try} -import org.log4s._ +import net.usebox.gemini.server.handlers.GeminiHandler +import akka.actor.ActorSystem import akka.stream._ import akka.stream.scaladsl._ -import akka.actor.ActorSystem import akka.util.ByteString - -import net.usebox.gemini.server.handlers.GeminiHandler +import org.log4s._ case class Server(conf: ServiceConf) { - implicit val system = ActorSystem("space-beans") - implicit val ec = system.dispatcher + implicit val system: ActorSystem = ActorSystem("space-beans") + implicit val ec: ExecutionContextExecutor = system.dispatcher private[this] val logger = getLogger @@ -89,7 +89,7 @@ case class Server(conf: ServiceConf) { certs.foreach { case (host, (cert, _)) => logger.info(s"Certificate for ${host} - serial-no: ${cert - .getSerialNumber()}, final-date: ${cert.getNotAfter()}") + .getSerialNumber()}, final-date: ${cert.getNotAfter()}") } def createSSLEngine: SSLEngine = { diff --git a/server/src/net/usebox/gemini/server/ServiceConf.scala b/server/src/net/usebox/gemini/server/ServiceConf.scala index ec082cf..c6b5d9a 100644 --- a/server/src/net/usebox/gemini/server/ServiceConf.scala +++ b/server/src/net/usebox/gemini/server/ServiceConf.scala @@ -1,14 +1,13 @@ package net.usebox.gemini.server -import java.nio.file.{Path, FileSystems} +import java.nio.file.{FileSystems, Path} import scala.concurrent.duration.FiniteDuration +import org.log4s._ import pureconfig._ import pureconfig.generic.semiauto._ -import org.log4s._ - case class KeyStore(path: String, alias: String, password: String) case class Directory( @@ -46,7 +45,8 @@ object VirtualHost { vhost.directories .filter(_.allowCgi == Some(true)) .sortWith { - case (d1, d2) => d1.path.length() > d2.path.length() + case (d1, d2) => + d1.path.length() > d2.path.length() } .find(d => path.startsWith( @@ -64,12 +64,10 @@ object VirtualHost { def getRoot(path: String): (String, String) = path match { - case userRe(user, null) - if vhost.userDirectories && vhost.userDirectoryPath.nonEmpty => + case userRe(user, null) if vhost.userDirectories && vhost.userDirectoryPath.nonEmpty => // username with no end slash, force redirect (vhost.userDirectoryPath.get.replace(userTag, user), ".") - case userRe(user, userPath) - if vhost.userDirectories && vhost.userDirectoryPath.nonEmpty => + case userRe(user, userPath) if vhost.userDirectories && vhost.userDirectoryPath.nonEmpty => (vhost.userDirectoryPath.get.replace(userTag, user), userPath) case _ => (vhost.root, path) } @@ -92,10 +90,13 @@ object ServiceConf { private[this] val logger = getLogger - implicit val keyStoreReader = deriveReader[KeyStore] - implicit val directoryHostReader = deriveReader[Directory] - implicit val virtualHostReader = deriveReader[VirtualHost] - implicit val serviceConfReader = deriveReader[ServiceConf] + implicit val keyStoreReader: ConfigReader[KeyStore] = deriveReader[KeyStore] + implicit val directoryHostReader: ConfigReader[Directory] = + deriveReader[Directory] + implicit val virtualHostReader: ConfigReader[VirtualHost] = + deriveReader[VirtualHost] + implicit val serviceConfReader: ConfigReader[ServiceConf] = + deriveReader[ServiceConf] import VirtualHost.userTag diff --git a/server/src/net/usebox/gemini/server/TLSUtils.scala b/server/src/net/usebox/gemini/server/TLSUtils.scala index 03e2216..012da2f 100644 --- a/server/src/net/usebox/gemini/server/TLSUtils.scala +++ b/server/src/net/usebox/gemini/server/TLSUtils.scala @@ -1,29 +1,22 @@ package net.usebox.gemini.server import java.io.FileInputStream -import java.time.Instant import java.math.BigInteger -import java.util.Date +import java.net.Socket +import java.security.{KeyPairGenerator, KeyStore, Principal, PrivateKey, SecureRandom, Security} import java.security.cert.X509Certificate -import java.security.{ - Security, - KeyStore, - SecureRandom, - PrivateKey, - KeyPairGenerator, - Principal -} import java.security.KeyStore.PrivateKeyEntry -import java.net.Socket +import java.time.Instant +import java.util.Date import javax.net.ssl.{ - SSLContext, - TrustManagerFactory, + ExtendedSSLSession, KeyManagerFactory, - X509ExtendedKeyManager, + SNIHostName, + SSLContext, SSLEngine, - ExtendedSSLSession, StandardConstants, - SNIHostName + TrustManagerFactory, + X509ExtendedKeyManager } import scala.concurrent.duration.FiniteDuration @@ -31,9 +24,8 @@ import scala.jdk.CollectionConverters._ import scala.util.Try import org.bouncycastle.jce.provider.BouncyCastleProvider -import org.bouncycastle.x509.X509V3CertificateGenerator import org.bouncycastle.jce.X509Principal - +import org.bouncycastle.x509.X509V3CertificateGenerator import org.log4s._ object TLSUtils { @@ -43,8 +35,7 @@ object TLSUtils { Security.addProvider(new BouncyCastleProvider()) // https://github.com/grahamedgecombe/netty-sni-example/ - class SniKeyManager(keyManager: X509ExtendedKeyManager, defaultAlias: String) - extends X509ExtendedKeyManager { + class SniKeyManager(keyManager: X509ExtendedKeyManager, defaultAlias: String) extends X509ExtendedKeyManager { override def getClientAliases( keyType: String, issuers: Array[Principal] @@ -84,8 +75,7 @@ object TLSUtils { .getRequestedServerNames() .asScala .collectFirst { - case n: SNIHostName - if n.getType() == StandardConstants.SNI_HOST_NAME => + case n: SNIHostName if n.getType() == StandardConstants.SNI_HOST_NAME => n.getAsciiName() } match { case Some(hostname) @@ -136,10 +126,9 @@ object TLSUtils { ( ks.getCertificate(alias).asInstanceOf[X509Certificate], ks.getEntry( - alias, - new KeyStore.PasswordProtection(password.toCharArray()) - ) - .asInstanceOf[PrivateKeyEntry] + alias, + new KeyStore.PasswordProtection(password.toCharArray()) + ).asInstanceOf[PrivateKeyEntry] .getPrivateKey() ) }.toEither diff --git a/server/src/net/usebox/gemini/server/URIUtils.scala b/server/src/net/usebox/gemini/server/URIUtils.scala index 76e2adf..5100a65 100644 --- a/server/src/net/usebox/gemini/server/URIUtils.scala +++ b/server/src/net/usebox/gemini/server/URIUtils.scala @@ -1,8 +1,8 @@ package net.usebox.gemini.server -import java.nio.charset.StandardCharsets -import java.net.{URLEncoder, URLDecoder} +import java.net.{URLDecoder, URLEncoder} import java.net.URI +import java.nio.charset.StandardCharsets import scala.util.Try diff --git a/server/src/net/usebox/gemini/server/handlers/GeminiHandler.scala b/server/src/net/usebox/gemini/server/handlers/GeminiHandler.scala index ad5272c..377d213 100644 --- a/server/src/net/usebox/gemini/server/handlers/GeminiHandler.scala +++ b/server/src/net/usebox/gemini/server/handlers/GeminiHandler.scala @@ -3,10 +3,10 @@ package net.usebox.gemini.server.handlers import java.net.URI import java.nio.file.FileSystems -import org.log4s._ - import net.usebox.gemini.server._ -import URIUtils._ +import net.usebox.gemini.server.URIUtils._ + +import org.log4s._ class GeminiHandler(conf: ServiceConf) extends ProtocolHandler(conf) { diff --git a/server/src/net/usebox/gemini/server/handlers/ProtocolHandler.scala b/server/src/net/usebox/gemini/server/handlers/ProtocolHandler.scala index 7c85850..b89d040 100644 --- a/server/src/net/usebox/gemini/server/handlers/ProtocolHandler.scala +++ b/server/src/net/usebox/gemini/server/handlers/ProtocolHandler.scala @@ -1,11 +1,11 @@ package net.usebox.gemini.server.handlers import java.net.URI -import java.nio.file.{Path, Files} +import java.nio.file.{Files, Path} import scala.util.Try -import net.usebox.gemini.server.{ServiceConf, Response} +import net.usebox.gemini.server.{Response, ServiceConf} abstract class ProtocolHandler(conf: ServiceConf) { @@ -25,9 +25,12 @@ abstract class ProtocolHandler(conf: ServiceConf) { } { types => types .find { - case (t, exts) => exts.exists(path.toString().endsWith(_)) + case (t, exts) => + exts.exists(path.toString().endsWith(_)) + } + .fold(defaultMimeType) { + case (t, _) => t } - .fold(defaultMimeType) { case (t, _) => t } } match { case mime @ "text/gemini" => params.fold(mime)(p => s"$mime; ${p.stripMargin(';').trim()}") |