Android Question Issue with using FTPS on FTP library

Callan79

New Member
Hi,

I've managed to get my app to connect to me NAS via Non-Secure FTP with no probs and upload / list files folder in the folder, however when I change it to FTPS it seems to connect ok (NAS Log show is connects successfully) however the List_Completed function always returns Success=False and the UploadFile Function uploads the file but returns Success=False.

When I Log the last exceptions it returns:
- Folder is not writable or an error occurred: java.lang.RuntimeException: Error uploading file.
- 200 PORT command successful

Any help would be much appreciated.

Sub TestFTP(url As String, username As String, pwd As String, LocalPath As String)

FTP.Initialize("FTP", url, 21, username, pwd)
FTP.UseSSL = True
FTP.UseSSLExplicit = True
FTP.PassiveMode = False

Dim CustomSSL As CustomTrustManager
CustomSSL.InitializeAcceptAll

FTP.SetCustomSSLTrustManager(CustomSSL)

FTP.List(LocalPath)

End Sub

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Directories() As FTPEntry, Files() As FTPEntry)

Dim msg As String

If Success Then
If Directories.Length > 0 Or Files.Length > 0 Then
Log("Folder is available at " & ServerPath)
Else
Log("Folder is empty.")
End If

Dim testContent As String
testContent = "This is a test file."
File.WriteString(File.DirInternal, "/test.txt", testContent)

' Upload file

FTP.TimeoutMs = 120000
Dim sf As Object = FTP.UploadFile(File.DirInternal, "/test.txt",False, txtLocalPath.text & "test.txt")
Wait For(sf) FTP_UploadCompleted(ServerPath As String, Success As Boolean)
If Success Then
' Delete the test file
FTP.DeleteFile(txtLocalPath.text & "test.txt")
msg = "The Upload path specified is Available and ready for Upload!"
MsgboxAsync(msg,"Local Path Settings Valid")
Log("Folder is writable.")
Else
msg = "Folder is not writable or an error occurred!"
MsgboxAsync(msg,"Local Path Settings Invalid")

Log("Folder is not writable or an error occurred: " & LastException.Message )
End If
Else
msg = "Could not confirm if the folder is Available. Check upload location secutity settings and Try Again."
If ServerPath = txtLocalIP.Text Then
MsgboxAsync(msg,"Local Path NOT Available")
Else
MsgboxAsync(msg,"Remote Path NOT Available")
End If

Log("Error checking folder availability: ")
End If

' Disconnect from the FTP server
If FTP.IsInitialized Then
FTP.Close
End If

End Sub
 

amykonio

Active Member
Licensed User
Longtime User
Hi.
I would first try to set passivemode to true.
Also, why are you using UseSSLExplicit = true? You should only use one of UseSSL = true and UseSSLExplicit = true. The first is more secure.
Take a look at the following link: What is FTP?

Andreas.
 
Upvote 0

Callan79

New Member
Hi.
I would first try to set passivemode to true.
Also, why are you using UseSSLExplicit = true? You should only use one of UseSSL = true and UseSSLExplicit = true. The first is more secure.
Take a look at the following link: What is FTP?

Andreas.
Thanks Andreas,

When I set Passive to true it wont connect at all, My Nas doesnt even see the connection attempt.

Regarding UseSSL & Use SSLExplicit, Ive tried every combination but the best I can get is a Success=True on the List command but it still doesnt return any file/folders eventhough there are.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

From your description, it appears that the issues you're experiencing with your FTPS connection in your B4A (Basic4android) app are related to file permissions and possibly FTP command handling under FTPS mode. Here are a few suggestions and checks you might consider to resolve the issues:

1. Confirm Permissions on the NAS

Ensure that the folder where you're trying to upload files has the correct write permissions for the user account used by the FTP connection. FTPS might be enforcing security measures more strictly compared to non-secure FTP, which might be why the issue appears only under FTPS.

2. Passive Mode vs. Active Mode

You're currently using Active Mode (FTP.PassiveMode = False). FTPS often works better in Passive Mode due to how it handles data connections through firewalls and NATs. Try switching to Passive Mode by setting FTP.PassiveMode = True to see if this resolves the issue.

3. Adjusting the SSL Settings

You have both FTP.UseSSL = True and FTP.UseSSLExplicit = True. Ensure that your NAS is configured to support explicit SSL (FTPS) on the port you are connecting to (commonly port 21 for explicit SSL). Some servers might require implicit SSL, which typically uses port 990.

4. Detailed Error Logging

You're logging errors but consider adding more detailed logging around each step of your connection and upload process to help identify at which point the failure occurs. Also, ensure that LastException is being captured properly in all relevant places to get more detailed error messages.

5. Check Server Response Codes

You mentioned getting "200 PORT command successful". This is generally a positive response in Active Mode indicating that the server successfully opened a port for a data connection. If switching to Passive Mode doesn't help, you may need to verify how your server handles data connections under FTPS.

6. Test with Different FTP Client

Try using a desktop FTP client configured for FTPS with the same server settings and credentials. This can help determine if the issue is with the server configuration or the way your app is handling the FTPS connection.

7. Review NAS FTPS Settings

Check the FTPS settings on your NAS. Make sure it's configured correctly to handle FTPS connections. Look for any specific settings related to data transfer modes or SSL/TLS requirements.

Revised Code Snippet

Here’s a slightly adjusted version of your initial setup for testing with Passive Mode:
B4X:
Sub TestFTP(url As String, username As String, pwd As String, LocalPath As String)
    FTP.Initialize("FTP", url, 21, username, pwd)
    FTP.UseSSL = True
    FTP.UseSSLExplicit = True
    FTP.PassiveMode = True  ' Switch to Passive Mode

    Dim CustomSSL As CustomTrustManager
    CustomSSL.InitializeAcceptAll
    FTP.SetCustomSSLTrustManager(CustomSSL)

    FTP.List(LocalPath)
End Sub
Implement these suggestions one at a time to pinpoint which change effectively resolves the issue. If none of these help, you might need to dig deeper into the FTP server logs on your NAS or even consider contacting the NAS vendor for more detailed support regarding FTPS configurations.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
1.Use ftp tools to check whether your Nas supports ftps first.
2.Please use code tags </> when posting code.
 
Upvote 0

Callan79

New Member
ChatGPT says...

From your description, it appears that the issues you're experiencing with your FTPS connection in your B4A (Basic4android) app are related to file permissions and possibly FTP command handling under FTPS mode. Here are a few suggestions and checks you might consider to resolve the issues:

1. Confirm Permissions on the NAS

Ensure that the folder where you're trying to upload files has the correct write permissions for the user account used by the FTP connection. FTPS might be enforcing security measures more strictly compared to non-secure FTP, which might be why the issue appears only under FTPS.

2. Passive Mode vs. Active Mode

You're currently using Active Mode (FTP.PassiveMode = False). FTPS often works better in Passive Mode due to how it handles data connections through firewalls and NATs. Try switching to Passive Mode by setting FTP.PassiveMode = True to see if this resolves the issue.

3. Adjusting the SSL Settings

You have both FTP.UseSSL = True and FTP.UseSSLExplicit = True. Ensure that your NAS is configured to support explicit SSL (FTPS) on the port you are connecting to (commonly port 21 for explicit SSL). Some servers might require implicit SSL, which typically uses port 990.

4. Detailed Error Logging

You're logging errors but consider adding more detailed logging around each step of your connection and upload process to help identify at which point the failure occurs. Also, ensure that LastException is being captured properly in all relevant places to get more detailed error messages.

5. Check Server Response Codes

You mentioned getting "200 PORT command successful". This is generally a positive response in Active Mode indicating that the server successfully opened a port for a data connection. If switching to Passive Mode doesn't help, you may need to verify how your server handles data connections under FTPS.

6. Test with Different FTP Client

Try using a desktop FTP client configured for FTPS with the same server settings and credentials. This can help determine if the issue is with the server configuration or the way your app is handling the FTPS connection.

7. Review NAS FTPS Settings

Check the FTPS settings on your NAS. Make sure it's configured correctly to handle FTPS connections. Look for any specific settings related to data transfer modes or SSL/TLS requirements.

Revised Code Snippet

Here’s a slightly adjusted version of your initial setup for testing with Passive Mode:
B4X:
Sub TestFTP(url As String, username As String, pwd As String, LocalPath As String)
    FTP.Initialize("FTP", url, 21, username, pwd)
    FTP.UseSSL = True
    FTP.UseSSLExplicit = True
    FTP.PassiveMode = True  ' Switch to Passive Mode

    Dim CustomSSL As CustomTrustManager
    CustomSSL.InitializeAcceptAll
    FTP.SetCustomSSLTrustManager(CustomSSL)

    FTP.List(LocalPath)
End Sub
Implement these suggestions one at a time to pinpoint which change effectively resolves the issue. If none of these help, you might need to dig deeper into the FTP server logs on your NAS or even consider contacting the NAS vendor for more detailed support regarding FTPS configurations.
Thanks John,

I had confirmed all the above and it still had the issue, I had also tested the connection using FileZilla and it worked perfectly.

But after digging further into it I found that my NAS had "Use Masquerade Address" set to true and once I disabled it it is now works perfectly.

Thanks for your Help, I thought I was going mad.
 
Upvote 0

Callan79

New Member
1.Use ftp tools to check whether your Nas supports ftps first.
2.Please use code tags </> when posting code.
Thanks teddybear,

My Nas supports it, and Filezilla Connects ok. Thanks for the tip on posting code too.

Ended up being the Nas was using a Masquerade Address. Disabling it fixed it for the app.
 
Upvote 0
Top