@@ -40,6 +40,7 @@ import android.os.PowerManager
40
40
import android.os.SystemClock
41
41
import android.provider.DocumentsContract
42
42
import androidx.core.app.ServiceCompat
43
+ import androidx.core.content.edit
43
44
import androidx.preference.PreferenceManager
44
45
import com.amaze.filemanager.BuildConfig
45
46
import com.amaze.filemanager.R
@@ -52,6 +53,10 @@ import com.amaze.filemanager.ui.notifications.FtpNotification
52
53
import com.amaze.filemanager.ui.notifications.NotificationConstants
53
54
import com.amaze.filemanager.utils.ObtainableServiceBinder
54
55
import com.amaze.filemanager.utils.PasswordUtil
56
+ import kotlinx.coroutines.CoroutineScope
57
+ import kotlinx.coroutines.Dispatchers
58
+ import kotlinx.coroutines.SupervisorJob
59
+ import kotlinx.coroutines.launch
55
60
import org.apache.ftpserver.ConnectionConfigFactory
56
61
import org.apache.ftpserver.FtpServer
57
62
import org.apache.ftpserver.FtpServerFactory
@@ -61,13 +66,13 @@ import org.apache.ftpserver.ssl.ClientAuth
61
66
import org.apache.ftpserver.ssl.impl.DefaultSslConfiguration
62
67
import org.apache.ftpserver.usermanager.impl.BaseUser
63
68
import org.apache.ftpserver.usermanager.impl.WritePermission
64
- import org.greenrobot.eventbus.EventBus
65
69
import org.slf4j.Logger
66
70
import org.slf4j.LoggerFactory
67
71
import java.io.IOException
68
72
import java.security.GeneralSecurityException
69
73
import java.security.KeyStore
70
74
import java.util.LinkedList
75
+ import java.util.concurrent.TimeUnit
71
76
import javax.net.ssl.KeyManagerFactory
72
77
import javax.net.ssl.TrustManagerFactory
73
78
import kotlin.concurrent.thread
@@ -79,6 +84,7 @@ import kotlin.concurrent.thread
79
84
* Edited by zent-co on 30-07-2019 Edited by bowiechen on 2019-10-19.
80
85
*/
81
86
class FtpService : Service (), Runnable {
87
+ private val serviceScope = CoroutineScope (Dispatchers .Default + SupervisorJob ())
82
88
private val binder: IBinder = ObtainableServiceBinder (this )
83
89
84
90
// Service will broadcast via event bus when server start/stop
@@ -95,6 +101,12 @@ class FtpService : Service(), Runnable {
95
101
private var isStartedByTile = false
96
102
private lateinit var wakeLock: PowerManager .WakeLock
97
103
104
+ private fun publishEvent (event : FtpReceiverActions ) {
105
+ serviceScope.launch {
106
+ FtpEventBus .emit(event)
107
+ }
108
+ }
109
+
98
110
override fun onStartCommand (
99
111
intent : Intent ? ,
100
112
flags : Int ,
@@ -126,7 +138,7 @@ class FtpService : Service(), Runnable {
126
138
} else {
127
139
startForeground(NotificationConstants .FTP_ID , notification)
128
140
}
129
- return START_NOT_STICKY
141
+ return START_STICKY
130
142
}
131
143
132
144
override fun onCreate () {
@@ -143,6 +155,8 @@ class FtpService : Service(), Runnable {
143
155
144
156
@Suppress(" LongMethod" )
145
157
override fun run () {
158
+ // Acquire the WakeLock for 1 hour, per recommended.
159
+ wakeLock.acquire(TimeUnit .HOURS .toMillis(1L ))
146
160
val preferences = PreferenceManager .getDefaultSharedPreferences(this )
147
161
FtpServerFactory ().run {
148
162
val connectionConfigFactory = ConnectionConfigFactory ()
@@ -175,7 +189,7 @@ class FtpService : Service(), Runnable {
175
189
}.onFailure {
176
190
log.warn(" failed to decrypt password in ftp service" , it)
177
191
AppConfig .toast(applicationContext, R .string.error)
178
- preferences.edit(). putString(KEY_PREFERENCE_PASSWORD , " " ). apply ()
192
+ preferences.edit { putString(KEY_PREFERENCE_PASSWORD , " " ) }
179
193
isPasswordProtected = false
180
194
}
181
195
}
@@ -224,9 +238,9 @@ class FtpService : Service(), Runnable {
224
238
)
225
239
fac.isImplicitSsl = true
226
240
} catch (e: GeneralSecurityException ) {
227
- preferences.edit(). putBoolean(KEY_PREFERENCE_SECURE , false ). apply ()
241
+ preferences.edit { putBoolean(KEY_PREFERENCE_SECURE , false ) }
228
242
} catch (e: IOException ) {
229
- preferences.edit(). putBoolean(KEY_PREFERENCE_SECURE , false ). apply ()
243
+ preferences.edit { putBoolean(KEY_PREFERENCE_SECURE , false ) }
230
244
}
231
245
}
232
246
fac.port = getPort(preferences)
@@ -237,23 +251,22 @@ class FtpService : Service(), Runnable {
237
251
server =
238
252
createServer().apply {
239
253
start()
240
- EventBus .getDefault()
241
- .post(
242
- if (isStartedByTile) {
243
- FtpReceiverActions .STARTED_FROM_TILE
244
- } else {
245
- FtpReceiverActions .STARTED
246
- },
247
- )
254
+ publishEvent(
255
+ if (isStartedByTile) {
256
+ FtpReceiverActions .STARTED_FROM_TILE
257
+ } else {
258
+ FtpReceiverActions .STARTED
259
+ },
260
+ )
248
261
}
249
262
}.onFailure {
250
- EventBus .getDefault().post(FtpReceiverActions .FAILED_TO_START )
263
+ wakeLock.release()
264
+ publishEvent(FtpReceiverActions .FAILED_TO_START )
251
265
}
252
266
}
253
267
}
254
268
255
269
override fun onDestroy () {
256
- wakeLock.release()
257
270
serverThread?.let { serverThread ->
258
271
serverThread.interrupt()
259
272
// wait 10 sec for server thread to finish
@@ -263,9 +276,13 @@ class FtpService : Service(), Runnable {
263
276
Companion .serverThread = null
264
277
}
265
278
server?.stop().also {
266
- EventBus .getDefault().post (FtpReceiverActions .STOPPED )
279
+ publishEvent (FtpReceiverActions .STOPPED )
267
280
}
268
281
}
282
+
283
+ if (wakeLock.isHeld) {
284
+ wakeLock.release()
285
+ }
269
286
}
270
287
271
288
// Restart the service if the app is closed from the recent list
@@ -312,39 +329,6 @@ class FtpService : Service(), Runnable {
312
329
const val TAG_STARTED_BY_TILE = " started_by_tile"
313
330
// attribute of action_started, used by notification
314
331
315
- private lateinit var _enabledCipherSuites : Array <String >
316
-
317
- init {
318
- _enabledCipherSuites =
319
- LinkedList <String >().apply {
320
- if (SDK_INT >= Q ) {
321
- add(" TLS_AES_128_GCM_SHA256" )
322
- add(" TLS_AES_256_GCM_SHA384" )
323
- add(" TLS_CHACHA20_POLY1305_SHA256" )
324
- }
325
- if (SDK_INT >= N ) {
326
- add(" TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" )
327
- add(" TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" )
328
- }
329
- if (SDK_INT >= LOLLIPOP ) {
330
- add(" TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" )
331
- add(" TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" )
332
- add(" TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" )
333
- add(" TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" )
334
- add(" TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" )
335
- add(" TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" )
336
- add(" TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" )
337
- add(" TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" )
338
- add(" TLS_RSA_WITH_AES_128_GCM_SHA256" )
339
- add(" TLS_RSA_WITH_AES_256_GCM_SHA384" )
340
- }
341
- if (SDK_INT < LOLLIPOP ) {
342
- add(" TLS_RSA_WITH_AES_128_CBC_SHA" )
343
- add(" TLS_RSA_WITH_AES_256_CBC_SHA" )
344
- }
345
- }.toTypedArray()
346
- }
347
-
348
332
/* *
349
333
* Return a list of available ciphers for ftpserver.
350
334
*
@@ -355,7 +339,34 @@ class FtpService : Service(), Runnable {
355
339
* @see [javax.net.ssl.SSLEngine]
356
340
*/
357
341
@JvmStatic
358
- val enabledCipherSuites = _enabledCipherSuites
342
+ val enabledCipherSuites: Array <String > =
343
+ LinkedList <String >().apply {
344
+ if (SDK_INT >= Q ) {
345
+ add(" TLS_AES_128_GCM_SHA256" )
346
+ add(" TLS_AES_256_GCM_SHA384" )
347
+ add(" TLS_CHACHA20_POLY1305_SHA256" )
348
+ }
349
+ if (SDK_INT >= N ) {
350
+ add(" TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" )
351
+ add(" TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" )
352
+ }
353
+ if (SDK_INT >= LOLLIPOP ) {
354
+ add(" TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" )
355
+ add(" TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" )
356
+ add(" TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" )
357
+ add(" TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" )
358
+ add(" TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" )
359
+ add(" TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" )
360
+ add(" TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" )
361
+ add(" TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" )
362
+ add(" TLS_RSA_WITH_AES_128_GCM_SHA256" )
363
+ add(" TLS_RSA_WITH_AES_256_GCM_SHA384" )
364
+ }
365
+ if (SDK_INT < LOLLIPOP ) {
366
+ add(" TLS_RSA_WITH_AES_128_CBC_SHA" )
367
+ add(" TLS_RSA_WITH_AES_256_CBC_SHA" )
368
+ }
369
+ }.toTypedArray()
359
370
360
371
private var serverThread: Thread ? = null
361
372
private var server: FtpServer ? = null
0 commit comments