Imports SBSimpleFTPS
Imports SBX509
Imports System.IO
Module ConsoleFTPSClient
Dim WithEvents Client As TElSimpleFTPSClient
' command line parameters
Const OP_LIST As Integer = 1
Const OP_DOWNLOAD As Integer = 2
Const OP_UPLOAD As Integer = 3
Dim parOperation As Integer = 0
Dim parRemoteName As String = "/"
Dim parLocalName As String
Dim parUsername As String
Dim parPassword As String
Dim parAddress As String
Dim parPort As Integer = 21
Function ParseCommandLine() As Boolean
Dim Args As String()
Args = Environment.GetCommandLineArgs
Dim arg As String
Dim i As Integer = 1
If (Args.Length = 1) Then
ParseCommandLine = False
Exit Function
End If
While i < Args.Length
arg = Args(i)
Select Case arg
Case "-down", "-download"
parOperation = OP_DOWNLOAD
Exit Select
Case "-up", "-upload"
parOperation = OP_UPLOAD
Exit Select
Case "-list"
parOperation = OP_LIST
Exit Select
Case "-addr", "-address"
i = i + 1
parAddress = Args(i)
Exit Select
Case "-port"
i = i + 1
Try
parPort = Int32.Parse(Args(i))
Catch
parPort = 21
End Try
Exit Select
Case "-user", "-username"
i = i + 1
parUsername = Args(i)
Exit Select
Case "-pass", "-password"
i = i + 1
parPassword = Args(i)
Exit Select
Case "-local"
i = i + 1
parLocalName = Args(i)
Exit Select
Case "-remote"
i = i + 1
parRemoteName = Args(i)
Exit Select
End Select
i = i + 1
End While
ParseCommandLine = parOperation <> 0
End Function
Sub Download()
Dim stream As FileStream = New FileStream(parLocalName, FileMode.Create)
Try
Client.Receive(parRemoteName, stream)
Catch
stream.Close()
End Try
End Sub
Sub Upload()
Dim stream As FileStream = New FileStream(parLocalName, FileMode.Open)
Try
Client.Send(Stream, parRemoteName)
Catch
stream.Close()
End Try
End Sub
Sub List()
If Client.ExtMLSTSupported Then
Client.MLSD()
Else
Client.GetFileList()
End If
End Sub
Private Sub m_Client_OnCertificateValidate(ByVal Sender As Object, ByVal Certificate As SBX509.TElX509Certificate, ByRef Validate As Boolean) Handles Client.OnCertificateValidate
System.Console.WriteLine("Server certificate received")
Validate = True ' NEVER do this. You MUST check the key validity somehow
End Sub
Private Sub m_Client_OnTextDataLine(ByVal Sender As Object, ByVal TextLine As Byte()) Handles Client.OnTextDataLine
Dim response As String = System.Text.UTF8Encoding.UTF8.GetString(TextLine)
Dim FileInfo As TSBFTPFileInfo = New TSBFTPFileInfo
Try
If (Client.ExtMLSTSupported) Then
TElSimpleFTPSClient.ParseMLSDEntry(response, FileInfo)
Else
TElSimpleFTPSClient.ParseFileListEntry(response, FileInfo)
End If
System.Console.WriteLine(FileInfo.FileName)
Catch
' Couldn't parse the entry, so just write what we've received
System.Console.WriteLine(response)
End Try
End Sub
Sub Main()
SBUtils.Unit.SetLicenseKey("Your license key here")
If Not ParseCommandLine() Then
Return
End If
Client = New TElSimpleFTPSClient
Client.Address = parAddress
Client.Port = parPort
Client.Username = parUsername
Client.Password = parPassword
Try
Try
Client.Open()
Client.Login()
Catch E As Exception
System.Console.WriteLine("Connection failed due to exception: " + E.Message)
System.Console.WriteLine("If you have ensured that all connection parameters are correct and you still can't connect,")
System.Console.WriteLine("please contact EldoS support as described on http://www.eldos.com/sbb/support-tech.php")
System.Console.WriteLine("Remember to provide details about the error that happened.")
Try
Client.Close(True)
Catch
End Try
Return
End Try
Try
Select Case parOperation
Case OP_DOWNLOAD
Download()
Exit Select
Case OP_UPLOAD
Upload()
Exit Select
Case OP_LIST
List()
Exit Select
End Select
Finally
Client.Close(False)
End Try
Catch ex As Exception
System.Console.WriteLine(ex.Message)
End Try
End Sub
End Module