Skip to content

Commit 5aaa1b3

Browse files
author
Andrey
committed
Add support for custom SMB port (default 445)
1 parent f739cfc commit 5aaa1b3

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

app/src/main/java/com/raival/compose/file/explorer/screen/main/MainActivityManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,15 @@ class MainActivityManager {
227227

228228
fun addSmbDrive(
229229
host: String,
230+
port: Int,
230231
username: String,
231232
password: String,
232233
anonymous: Boolean,
233234
domain: String,
234235
context: Context
235236
): Boolean {
236237
return try {
237-
openSMBFile(SMBFileHolder(host, username, password, anonymous, domain, ""), context)
238+
openSMBFile(SMBFileHolder(host, port, username, password, anonymous, domain, ""), context)
238239
} catch (e: Exception) {
239240
false
240241
}

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/holder/SMBFileHolder.kt

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlinx.coroutines.withContext
2424

2525
class SMBFileHolder(
2626
val host: String,
27+
val port: Int = 445,
2728
val username: String?,
2829
val password: String?,
2930
val anonymous: Boolean,
@@ -95,8 +96,15 @@ class SMBFileHolder(
9596
val client = SMBClient()
9697
try {
9798
client.connect(host).use { connection ->
98-
val session = SMBConnectionManager.getSession(host, username, password, domain, anonymous)
99-
99+
val session = SMBConnectionManager.getSession(
100+
host = host,
101+
port = port,
102+
username = username,
103+
password = password,
104+
domain = domain,
105+
anonymous = anonymous
106+
)
107+
100108
if (shareName.isNotBlank()) {
101109
val share = session.connectShare(shareName) as DiskShare
102110
share.list("").isNotEmpty()
@@ -121,7 +129,14 @@ class SMBFileHolder(
121129

122130
try {
123131
client.connect(host).use { connection: Connection ->
124-
val session = SMBConnectionManager.getSession(host, username, password, domain, anonymous)
132+
val session = SMBConnectionManager.getSession(
133+
host = host,
134+
port = port,
135+
username = username,
136+
password = password,
137+
domain = domain,
138+
anonymous = anonymous
139+
)
125140

126141
if (shareName.isNullOrBlank() || shareName == "/") {
127142
val transport = SMBTransportFactories.SRVSVC.getTransport(session)
@@ -184,7 +199,7 @@ class SMBFileHolder(
184199
override suspend fun getParent(): SMBFileHolder? {
185200
if (pathInsideShare.isBlank()) return null //
186201
val parentPath = pathInsideShare.substringBeforeLast("/", "")
187-
return SMBFileHolder(host, username, password, anonymous, domain, shareName, parentPath)
202+
return SMBFileHolder(host, port, username, password, anonymous, domain, shareName, parentPath)
188203
}
189204

190205
override fun open(context: Context, anonymous: Boolean, skipSupportedExtensions: Boolean, customMimeType: String?) {
@@ -197,7 +212,14 @@ class SMBFileHolder(
197212
val client = SMBClient()
198213
try {
199214
client.connect(host).use { connection ->
200-
val session = SMBConnectionManager.getSession(host, username, password, domain, anonymous)
215+
val session = SMBConnectionManager.getSession(
216+
host = host,
217+
port = port,
218+
username = username,
219+
password = password,
220+
domain = domain,
221+
anonymous = anonymous
222+
)
201223

202224
val share = session.connectShare(shareName) as DiskShare
203225
val newFilePath = if (pathInsideShare.isBlank()) name else "$pathInsideShare/$name"
@@ -235,7 +257,14 @@ class SMBFileHolder(
235257
val client = SMBClient()
236258
try {
237259
client.connect(host).use { connection ->
238-
val session = SMBConnectionManager.getSession(host, username, password, domain, anonymous)
260+
val session = SMBConnectionManager.getSession(
261+
host = host,
262+
port = port,
263+
username = username,
264+
password = password,
265+
domain = domain,
266+
anonymous = anonymous
267+
)
239268

240269
val share = session.connectShare(shareName) as DiskShare
241270

@@ -265,7 +294,14 @@ class SMBFileHolder(
265294
val client = SMBClient()
266295
try {
267296
client.connect(host).use { connection ->
268-
val session = SMBConnectionManager.getSession(host, username, password, domain, anonymous)
297+
val session = SMBConnectionManager.getSession(
298+
host = host,
299+
port = port,
300+
username = username,
301+
password = password,
302+
domain = domain,
303+
anonymous = anonymous
304+
)
269305

270306
val share = session.connectShare(shareName) as DiskShare
271307
for (entry in share.list(pathInsideShare)) {

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/smb/SMBConnectionManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ object SMBConnectionManager {
99

1010
fun getSession(
1111
host: String,
12+
port: Int = 445, // default smb port 445
1213
username: String?,
1314
password: String?,
1415
domain: String?,
1516
anonymous: Boolean
1617
): Session {
17-
val key = "$host|${username ?: "anon"}"
18+
val key = "$domain|$host|$port|${username ?: "anon"}"
1819
return clients[key] ?: run {
1920
val client = SMBClient()
20-
val connection = client.connect(host)
21+
val connection = client.connect(host, port)
2122
val session = if (anonymous || username.isNullOrBlank()) {
2223
connection.authenticate(null)
2324
} else {

app/src/main/java/com/raival/compose/file/explorer/screen/main/ui/AddSMBDriveDialog.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.text.font.FontWeight
2727
import androidx.compose.ui.unit.dp
2828
import androidx.compose.ui.window.Dialog
2929
import androidx.compose.foundation.layout.height
30+
import androidx.compose.foundation.text.KeyboardOptions
3031
import androidx.compose.material3.CardDefaults
3132
import androidx.compose.material3.HorizontalDivider
3233
import androidx.compose.material3.OutlinedButton
@@ -54,6 +55,7 @@ fun AddSMBDriveDialog(
5455
val context = LocalContext.current
5556
val mainActivityManager = globalClass.mainActivityManager
5657
var host by remember { mutableStateOf("") }
58+
var portText by remember { mutableStateOf("") }
5759
var username by remember { mutableStateOf("") }
5860
var password by remember { mutableStateOf("") }
5961
var anonymous by remember { mutableStateOf(false) }
@@ -100,6 +102,31 @@ fun AddSMBDriveDialog(
100102
modifier = Modifier.fillMaxWidth()
101103
)
102104

105+
if (showMore) {
106+
OutlinedTextField(
107+
value = portText,
108+
onValueChange = { newValue ->
109+
if (newValue.all { it.isDigit() }) {
110+
portText = newValue
111+
}
112+
},
113+
label = { Text(stringResource(R.string.port)) },
114+
placeholder = { Text("445") },
115+
singleLine = true,
116+
shape = RoundedCornerShape(6.dp),
117+
colors = TextFieldDefaults.colors(
118+
focusedIndicatorColor = Color.Transparent,
119+
unfocusedIndicatorColor = Color.Transparent,
120+
disabledIndicatorColor = Color.Transparent,
121+
errorIndicatorColor = Color.Transparent
122+
),
123+
keyboardOptions = KeyboardOptions.Default.copy(
124+
keyboardType = androidx.compose.ui.text.input.KeyboardType.Number
125+
),
126+
modifier = Modifier.fillMaxWidth()
127+
)
128+
}
129+
103130
OutlinedTextField(
104131
value = username,
105132
onValueChange = { username = it },
@@ -189,8 +216,10 @@ fun AddSMBDriveDialog(
189216
modifier = Modifier.weight(1f),
190217
onClick = {
191218
CoroutineScope(Dispatchers.IO).launch {
219+
val port = portText.toIntOrNull() ?: 445
220+
192221
val success = mainActivityManager.addSmbDrive(
193-
host, username, password, anonymous, domain, context
222+
host, port, username, password, anonymous, domain, context
194223
)
195224

196225
withContext(Dispatchers.Main) {

app/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,5 @@
465465
<string name="optional">Opcional</string>
466466
<string name="cant_connect_smb">No se pudo conectar. Por favor, verifica tus datos.</string>
467467
<string name="connect">Conectar</string>
468+
<string name="port">Puerto</string>
468469
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,5 @@
465465
<string name="optional">Optional</string>
466466
<string name="cant_connect_smb">Could not connect. Please check your details.</string>
467467
<string name="connect">Connect</string>
468+
<string name="port">Port</string>
468469
</resources>

0 commit comments

Comments
 (0)