Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Try to use fixed port #303

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class AppConfig(
val appInstanceId: String = UUID.randomUUID().toString(),
val language: String = Locale.getDefault().language,
val isFollowSystemTheme: Boolean = true,
val isDarkTheme: Boolean = false
val isDarkTheme: Boolean = false,
val port: Int = 13129
)
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ object Dependencies {
// net component
single<ClipClient> { DesktopClipClient(get<AppInfo>()) }
single<ClientHandlerManager> { DesktopClientHandlerManager(get(), get(), get(), get()) }
single<ClipServer> { DesktopClipServer(get<ClientHandlerManager>()).start() }
single<ClipServer> { DesktopClipServer(get<ConfigManager>(), get<ClientHandlerManager>()).start() }
single<Lazy<ClipServer>> { lazy { get<ClipServer>() } }
single<ClipBonjourService> { DesktopClipBonjourService(get(), get()).registerService() }
single<DeviceRefresher> { DesktopDeviceRefresher(get<ClientHandlerManager>()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.clipevery.net

import com.clipevery.config.ConfigManager
import com.clipevery.exception.StandardErrorCode
import com.clipevery.net.exception.signalExceptionHandler
import com.clipevery.net.plugin.SignalDecryption
Expand Down Expand Up @@ -27,49 +28,62 @@ import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.serializersModuleOf
import org.signal.libsignal.protocol.IdentityKey
import org.signal.libsignal.protocol.state.PreKeyBundle
import java.net.BindException

class DesktopClipServer(private val clientHandlerManager :ClientHandlerManager): ClipServer {
class DesktopClipServer(private val configManager: ConfigManager,
private val clientHandlerManager :ClientHandlerManager): ClipServer {

private val logger = KotlinLogging.logger {}

private var port = 0

private var server: NettyApplicationEngine = embeddedServer(Netty, port = 0) {
install(ContentNegotiation) {
json(Json {
serializersModule = SerializersModule {
serializersModuleOf(PreKeyBundle::class, PreKeyBundleSerializer)
serializersModuleOf(IdentityKey::class, IdentityKeySerializer)
private var server: NettyApplicationEngine = createServer(port = configManager.config.port)

private fun createServer(port: Int): NettyApplicationEngine {
return embeddedServer(Netty, port = port) {
install(ContentNegotiation) {
json(Json {
serializersModule = SerializersModule {
serializersModuleOf(PreKeyBundle::class, PreKeyBundleSerializer)
serializersModuleOf(IdentityKey::class, IdentityKeySerializer)
}
})
}
install(StatusPages) {
exception(Exception::class) { call, cause ->
logger.error(cause) { "Unhandled exception" }
failResponse(call, StandardErrorCode.UNKNOWN_ERROR.toErrorCode())
}
})
}
install(StatusPages) {
exception(Exception::class) { call, cause ->
logger.error(cause) { "Unhandled exception" }
failResponse(call, StandardErrorCode.UNKNOWN_ERROR.toErrorCode())
signalExceptionHandler()
}
signalExceptionHandler()
}
install(SignalDecryption) {
install(SignalDecryption) {

}
intercept(ApplicationCallPipeline.Setup) {
logger.info {"Received request: ${call.request.httpMethod.value} ${call.request.uri} ${call.request.contentType()}" }
}
routing {
syncRouting()
}
intercept(ApplicationCallPipeline.Setup) {
logger.info {"Received request: ${call.request.httpMethod.value} ${call.request.uri} ${call.request.contentType()}" }
}
routing {
syncRouting()
}
}
}

override fun start(): ClipServer {
clientHandlerManager.start()
server.start(wait = false)
try {
server.start(wait = false)
} catch (e: BindException) {
logger.warn { "Port ${configManager.config.port} is already in use" }
server = createServer(port = 0)
server.start(wait = false)
}
port = runBlocking { server.resolvedConnectors().first().port }
if (port == 0) {
logger.error { "Failed to start server" }
} else {
logger.info { "Server started at port $port" }
if (port != configManager.config.port) {
configManager.updateConfig {
it.copy(port = port)
}
}
logger.info { "Server started at port $port" }
return this
}

Expand Down
Loading