Skip to content

Commit c6a4165

Browse files
committed
feat: use the same host for both HTTPS and HTTP connections
1 parent 2b23878 commit c6a4165

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/main/kotlin/com/sakethh/linkora/Application.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fun main() {
4949
factory = Netty, configure = {
5050
sslConnector(builder = {
5151
this.port = serverConfig.httpsPort
52-
this.host = hostIp?.hostAddress ?: localhost.hostAddress
52+
this.host = serverConfig.serverHost
5353
enabledProtocols = listOf("TLSv1.3", "TLSv1.2")
5454
}, keyStore = serverKeyStore, keyAlias = Constants.KEY_STORE_ALIAS, keyStorePassword = {
5555
serverConfig.keyStorePassword.toCharArray()
@@ -60,12 +60,12 @@ fun main() {
6060
// for http connections
6161
connector {
6262
this.port = serverConfig.httpPort
63-
this.host = serverConfig.hostAddress
63+
this.host = serverConfig.serverHost
6464
}
6565
}, module = Application::module).start(wait = true)
6666
}
6767

68-
private val hostIp = try {
68+
val hostIp = try {
6969
NetworkInterface.getNetworkInterfaces().asSequence().flatMap { it.inetAddresses.asSequence() }.firstOrNull {
7070
!it.isLoopbackAddress && it is Inet4Address && it.isSiteLocalAddress
7171
}
@@ -74,15 +74,15 @@ private val hostIp = try {
7474
null
7575
}
7676

77-
private val localhost = Inet4Address.getLocalHost()
77+
val localhost: InetAddress = Inet4Address.getLocalHost()
7878

7979
object ServerConfiguration {
8080
private val json = Json {
8181
prettyPrint = true
8282
encodeDefaults = true
8383
}
84-
val jarDir = Paths.get(this::class.java.protectionDomain.codeSource.location.toURI()).parent
85-
private val configFilePath = jarDir.resolve("linkoraConfig.json")
84+
val serverJarDir = Paths.get(this::class.java.protectionDomain.codeSource.location.toURI()).parent
85+
private val configFilePath = serverJarDir.resolve("linkoraConfig.json")
8686

8787
private fun doesConfigFileExists(): Boolean {
8888
return Files.exists(configFilePath)
@@ -143,7 +143,7 @@ object ServerConfiguration {
143143
databaseUrl = "jdbc:" + System.getenv(SysEnvKey.LINKORA_DATABASE_URL.name),
144144
databaseUser = System.getenv(SysEnvKey.LINKORA_DATABASE_USER.name),
145145
databasePassword = System.getenv(SysEnvKey.LINKORA_DATABASE_PASSWORD.name),
146-
hostAddress = try {
146+
serverHost = try {
147147
// manually throw the exception as `getenv` may return null, and no conversion is happening here to auto-throw
148148
System.getenv(SysEnvKey.LINKORA_HOST_ADDRESS.name) ?: throw NullPointerException()
149149
} catch (_: Exception) {
@@ -188,8 +188,8 @@ object ServerConfiguration {
188188
}
189189

190190
fun exportSignedCertificates(keyStore: KeyStore) {
191-
val destinationPathForCer = jarDir.resolve("linkoraServerCert.cer")
192-
val destinationPathForPem = jarDir.resolve("linkoraServerCert.pem")
191+
val destinationPathForCer = serverJarDir.resolve("linkoraServerCert.cer")
192+
val destinationPathForPem = serverJarDir.resolve("linkoraServerCert.pem")
193193
val signedCertificate = keyStore.getCertificate(Constants.KEY_STORE_ALIAS)
194194

195195
if (!destinationPathForCer.exists()) {
@@ -215,7 +215,7 @@ object ServerConfiguration {
215215
this.certificate(
216216
alias = Constants.KEY_STORE_ALIAS, block = {
217217
this.password = serverConfig.keyStorePassword
218-
this.domains = listOf(hostIp?.hostAddress ?: localhost.hostAddress)
218+
this.domains = listOf(serverConfig.serverHost)
219219
this.ipAddresses = listOf(hostIp ?: localhost)
220220
this.daysValid = 365
221221
this.subject = X500Principal("CN=linkora-sync, OU=Linkora, O=Linkora, C=West Elizabeth")
@@ -224,7 +224,7 @@ object ServerConfiguration {
224224
this.sign = SignatureAlgorithm.RSA
225225
})
226226
}
227-
return jarDir.resolve("linkoraServerCert.jks").run {
227+
return serverJarDir.resolve("linkoraServerCert.jks").run {
228228
if (forceCreate.not() && exists()) {
229229
FileInputStream(toFile()).use { keyStoreFile ->
230230
KeyStore.getInstance(KeyStore.getDefaultType()).also {

src/main/kotlin/com/sakethh/linkora/domain/model/ServerConfig.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.sakethh.linkora.domain.model
22

3+
import com.sakethh.linkora.hostIp
4+
import com.sakethh.linkora.localhost
35
import kotlinx.serialization.SerialName
46
import kotlinx.serialization.Serializable
5-
import java.net.InetAddress
67

78
@Serializable
89
data class ServerConfig(
910
val databaseUrl: String = "database_url",
1011
val databaseUser: String = "database_user",
1112
val databasePassword: String = "database_password",
12-
val hostAddress: String = InetAddress.getLocalHost().hostAddress,
13+
@SerialName("hostAddress") val serverHost: String = try {
14+
require(hostIp != null)
15+
hostIp.hostAddress
16+
} catch (e: Exception) {
17+
e.printStackTrace()
18+
localhost.hostAddress
19+
},
1320
@SerialName("serverPort") val httpPort: Int = 45454,
1421
val httpsPort: Int = 54545,
1522
val serverAuthToken: String = "TOKEN",
@@ -27,4 +34,4 @@ data class ServerConfig(
2734
}
2835
}
2936
}
30-
}
37+
}

src/main/kotlin/com/sakethh/linkora/presentation/routing/Routing.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fun Application.configureRouting(serverConfig: ServerConfig, markdownManagerRepo
4545
call.respond(message = HttpStatusCode.OK, status = HttpStatusCode.OK)
4646
}
4747
post(path = "/generate/certs-and-keystore") {
48-
val zipFile = ServerConfiguration.jarDir.resolve("linkora-certs-and-keystore.zip").run {
48+
val zipFile = ServerConfiguration.serverJarDir.resolve("linkora-certs-and-keystore.zip").run {
4949
if (exists()) {
5050
this.toFile()
5151
} else {
@@ -55,9 +55,9 @@ fun Application.configureRouting(serverConfig: ServerConfig, markdownManagerRepo
5555

5656
ZipOutputStream(zipFile.outputStream()).use { zipOutputStream ->
5757
val files = listOf<File>(
58-
ServerConfiguration.jarDir.resolve("linkoraServerCert.cer").toFile(),
59-
ServerConfiguration.jarDir.resolve("linkoraServerCert.pem").toFile(),
60-
ServerConfiguration.jarDir.resolve("linkoraServerCert.jks").toFile(),
58+
ServerConfiguration.serverJarDir.resolve("linkoraServerCert.cer").toFile(),
59+
ServerConfiguration.serverJarDir.resolve("linkoraServerCert.pem").toFile(),
60+
ServerConfiguration.serverJarDir.resolve("linkoraServerCert.jks").toFile(),
6161
)
6262

6363
files.forEach {
@@ -90,7 +90,7 @@ fun Application.configureRouting(serverConfig: ServerConfig, markdownManagerRepo
9090
if (call.queryParameters["download"] == "true") {
9191
call.respondFile(file = zipFile)
9292
} else {
93-
call.respond("Certificates and keystore have been successfully generated at ${ServerConfiguration.jarDir.pathString}. A ZIP file containing all these files is also saved in the same directory.")
93+
call.respond("Certificates and keystore have been successfully generated at ${ServerConfiguration.serverJarDir.pathString}. A ZIP file containing all these files is also saved in the same directory.")
9494
}
9595
}
9696
}
@@ -247,7 +247,7 @@ fun Application.configureRouting(serverConfig: ServerConfig, markdownManagerRepo
247247

248248
get(Route.Sync.SERVER_IS_CONFIGURED.name) {
249249
val placeHolderValue =
250-
if ((useSysEnvValues().not() && serverConfig.hostAddress != InetAddress.getLocalHost().hostAddress) || (useSysEnvValues() && System.getenv(
250+
if ((useSysEnvValues().not() && serverConfig.serverHost != InetAddress.getLocalHost().hostAddress) || (useSysEnvValues() && System.getenv(
251251
SysEnvKey.LINKORA_HOST_ADDRESS.name
252252
) != InetAddress.getLocalHost().hostAddress)
253253
) {

0 commit comments

Comments
 (0)