Guest User

Untitled

a guest
Dec 7th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. // FTPS = SSL encrypted FTP.
  2. // SFTP = FTP over SSH.
  3.  
  4. // Set this as config, as the server may change the certificate.
  5. let ``trusted server certificate hash`` = "603780E732DB12D0F6BA434BA8E04D141904A165"
  6.  
  7. open System
  8. open System.IO
  9. open System.Net
  10. open System.Net.Security
  11.  
  12. type FTPSClient(username:string, password:string) =
  13. inherit WebClient(Credentials =
  14. (NetworkCredential(username.Normalize(), password.Normalize()) :> ICredentials),
  15. UseDefaultCredentials = false)
  16. override __.GetWebRequest(address) =
  17. let request = base.GetWebRequest(address) :?> FtpWebRequest
  18. request.Credentials <- NetworkCredential(username.Normalize(), password.Normalize())
  19. request.EnableSsl <- true
  20. request.UsePassive <- true
  21. //request.UseBinary <- t
  22. ServicePointManager.ServerCertificateValidationCallback <-
  23. RemoteCertificateValidationCallback(
  24. fun obj serverCert chain errors ->
  25. // Validate server security certificate here!
  26. if serverCert.GetCertHashString() = ``trusted server certificate hash`` then
  27. true
  28. else
  29. match errors with
  30. | SslPolicyErrors.None -> true
  31. | x -> Console.WriteLine("Server SSL invalid: " +
  32. Enum.GetName(typeof<SslPolicyErrors>, (box x)))
  33. false)
  34. let cert =
  35. request.ClientCertificates.Add(
  36. new System.Security.Cryptography.X509Certificates.X509Certificate())
  37. request :> WebRequest
  38.  
  39. let downloadFTPS host username (password:string) fileName targetPath =
  40. async {
  41. use client = new FTPSClient(username, password)
  42. let fileToGet = Uri("ftp://" + host + "/" + fileName)
  43. let target = Path.Combine [| targetPath; fileName |]
  44. return! client.DownloadFileTaskAsync(fileToGet, target) |> Async.AwaitTask
  45. }
  46.  
  47. let uploadFTPS host username (password:string) filePath =
  48. async {
  49. let fileName = Path.GetFileName(filePath)
  50. use client = new FTPSClient(username, password)
  51. let fileToSend = Uri("ftp://" + host + "/" + fileName)
  52. return!
  53. client.UploadFileTaskAsync(fileToSend, WebRequestMethods.Ftp.UploadFile, filePath)
  54. |> Async.AwaitTask |> Async.Catch
  55. }
  56.  
  57. //downloadFTPS "test.rebex.net" "demo" "password" "readme.txt" @"c:\temp\"
  58. //|> Async.RunSynchronously
  59.  
  60. //uploadFTPS "test.rebex.net" "demo" "password" @"C:\Temp\test3.txt"
  61. //|> Async.RunSynchronously
Add Comment
Please, Sign In to add comment