From cae721822dc17d9e62a412c446ce91da38edbdf9 Mon Sep 17 00:00:00 2001 From: "Mr. Lin" Date: Wed, 14 Feb 2024 01:31:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ftp=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A2=AB=E5=8A=A8=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8B=E7=9A=84=E6=95=B0=E6=8D=AE=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/files/ftpserver/FtpServer.kt | 13 +++++++++++-- .../android/files/ftpserver/FtpServerService.kt | 10 +++++----- .../zhanghai/android/files/settings/Settings.kt | 11 +++++++++++ app/src/main/res/values-zh-rCN/strings.xml | 2 ++ app/src/main/res/values/donottranslate_prefs.xml | 4 ++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/ftp_server.xml | 16 ++++++++++++++-- 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServer.kt b/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServer.kt index e148c9afb..d6e66a8d3 100644 --- a/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServer.kt +++ b/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServer.kt @@ -7,6 +7,7 @@ package me.zhanghai.android.files.ftpserver import java8.nio.file.Path 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.FtpException @@ -18,6 +19,7 @@ class FtpServer( private val username: String, private val password: String?, private val port: Int, + private val passiveDataPorts: String?, private val homeDirectory: Path, private val writable: Boolean ) { @@ -28,8 +30,15 @@ class FtpServer( server = FtpServerFactory() .apply { val listener = ListenerFactory() - .apply { port = this@FtpServer.port } - .createListener() + .apply { + port = this@FtpServer.port + passiveDataPorts?.let { + dataConnectionConfiguration = DataConnectionConfigurationFactory() + .apply { + passivePorts = it + }.createDataConnectionConfiguration() + } + }.createListener() addListener("default", listener) val user = BaseUser().apply { name = username diff --git a/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServerService.kt b/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServerService.kt index 1e0c9aedd..57463560f 100644 --- a/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServerService.kt +++ b/app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServerService.kt @@ -102,9 +102,12 @@ class FtpServerService : Service() { password = Settings.FTP_SERVER_PASSWORD.valueCompat } val port = Settings.FTP_SERVER_PORT.valueCompat + val passiveDataPort: String? = if (Settings.FTP_SERVER_CUSTOM_DATA_PORT.valueCompat) { + Settings.FTP_SERVER_DATA_PORT.valueCompat + } else null val homeDirectory = Settings.FTP_SERVER_HOME_DIRECTORY.valueCompat val writable = Settings.FTP_SERVER_WRITABLE.valueCompat - val server = FtpServer(username, password, port, homeDirectory, writable) + val server = FtpServer(username, password, port, passiveDataPort, homeDirectory, writable) this.server = server try { server.start() @@ -153,9 +156,6 @@ class FtpServerService : Service() { } enum class State { - STARTING, - RUNNING, - STOPPING, - STOPPED + STARTING, RUNNING, STOPPING, STOPPED } } diff --git a/app/src/main/java/me/zhanghai/android/files/settings/Settings.kt b/app/src/main/java/me/zhanghai/android/files/settings/Settings.kt index d2a8e2035..4b40f694a 100644 --- a/app/src/main/java/me/zhanghai/android/files/settings/Settings.kt +++ b/app/src/main/java/me/zhanghai/android/files/settings/Settings.kt @@ -87,6 +87,17 @@ object Settings { R.string.pref_key_ftp_server_port, R.integer.pref_default_value_ftp_server_port ) + val FTP_SERVER_CUSTOM_DATA_PORT: SettingLiveData = + BooleanSettingLiveData( + R.string.pref_key_ftp_server_custom_data_port, + R.bool.pref_default_value_ftp_server_custom_data_port + ) + + val FTP_SERVER_DATA_PORT: SettingLiveData = + StringSettingLiveData( + R.string.pref_key_ftp_server_data_port, R.string.pref_default_value_ftp_server_data_port + ) + val FTP_SERVER_HOME_DIRECTORY: SettingLiveData = ParcelValueSettingLiveData( R.string.pref_key_ftp_server_home_directory, diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 44d8225cf..67c44b530 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -542,6 +542,8 @@ 用户名 密码 端口 + 自定义被动模式数据端口范围 + 数据端口范围 根文件夹 允许写入 diff --git a/app/src/main/res/values/donottranslate_prefs.xml b/app/src/main/res/values/donottranslate_prefs.xml index 5edcd3425..57c6b6d28 100644 --- a/app/src/main/res/values/donottranslate_prefs.xml +++ b/app/src/main/res/values/donottranslate_prefs.xml @@ -29,6 +29,10 @@ key_ftp_server_password key_ftp_server_port 2121 + key_ftp_server_custom_data_port + false + key_ftp_server_data_port + 1024-65535 key_ftp_server_home_directory key_ftp_server_writable true diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 713fb8d9b..f8f06850b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -677,6 +677,8 @@ Username Password Port + Using custom passive data ports + Data Port Range Root folder Allow writing @string/ftp_server_title diff --git a/app/src/main/res/xml/ftp_server.xml b/app/src/main/res/xml/ftp_server.xml index 5d6ee2d89..94c85037d 100644 --- a/app/src/main/res/xml/ftp_server.xml +++ b/app/src/main/res/xml/ftp_server.xml @@ -5,7 +5,7 @@ ~ All Rights Reserved. --> - @@ -51,6 +51,18 @@ android:defaultValue="@integer/pref_default_value_ftp_server_port" app:useSimpleSummaryProvider="true" /> + + + + - +