From 83663817f5404073f8de11ace4d75ef1b0bb2029 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 9 Dec 2020 15:07:01 +0100 Subject: ConfigurationAccessService: disable multithreaded FTP and set explicit data port range The Apache Mina FtpServer library seems to have some issues when handling parallel transfers. This simply disables multithreading in the library to avoid those. The changeset also explicitly define a port range to be used for passive FTP data connections, solving the warnings about unregistered ports. GitHub: see https://github.com/pacien/tincapp/issues/103#issuecomment-741025439 --- .../tincapp/service/ConfigurationAccessService.kt | 25 ++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt b/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt index b083a83..32f74fc 100644 --- a/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/ConfigurationAccessService.kt @@ -25,6 +25,8 @@ import android.os.IBinder import androidx.databinding.ObservableBoolean import ch.qos.logback.classic.Level import ch.qos.logback.classic.Logger +import org.apache.ftpserver.ConnectionConfigFactory +import org.apache.ftpserver.DataConnectionConfigurationFactory import org.apache.ftpserver.FtpServer import org.apache.ftpserver.FtpServerFactory import org.apache.ftpserver.ftplet.* @@ -53,6 +55,7 @@ class ConfigurationAccessService : Service() { private val MINA_FTP_LOGGER_OVERRIDER = MinaLoggerOverrider(Level.WARN) const val FTP_PORT = 65521 // tinc port `concat` FTP port + val FTP_DATA_PORT_RANGE = FTP_PORT + 1..FTP_PORT + 11 const val FTP_USERNAME = "tincapp" val FTP_HOME_DIR = App.getContext().applicationInfo.dataDir!! val FTP_PASSWORD = generateRandomString(8) @@ -115,12 +118,26 @@ class ConfigurationAccessService : Service() { ) } - private fun setupSingleUserServer(ftpUser: User): FtpServer { - return FtpServerFactory() - .apply { addListener("default", ListenerFactory().apply { port = FTP_PORT }.createListener()) } + private fun setupSingleUserServer(ftpUser: User): FtpServer = + FtpServerFactory() + .apply { + addListener("default", ListenerFactory() + .apply { + connectionConfig = ConnectionConfigFactory() + .apply { maxThreads = 1 } // library has issues with multiple threads + .createConnectionConfig() + } + .apply { port = FTP_PORT } + .apply { + dataConnectionConfiguration = DataConnectionConfigurationFactory() + .apply { passivePorts = "${FTP_DATA_PORT_RANGE.first}-${FTP_DATA_PORT_RANGE.last}" } + .createDataConnectionConfiguration() + } + .createListener() + ) + } .apply { userManager = StaticFtpUserManager(listOf(ftpUser)) } .createServer() - } private class StaticFtpUserManager(users: List) : UserManager { private val userMap: Map = users.map { it.name to it }.toMap() -- cgit v1.2.3