FTPManager.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. using Fort23.Core;
  8. using Fort23.UTool;
  9. using UnityEngine;
  10. using Utility;
  11. public class FTPManager : Singleton<FTPManager>
  12. {
  13. public string ftpServer = "ftp://139.155.99.185:21";
  14. public string ftpUser = "ck";
  15. public string ftpPass = "ck44526996"; // 或用私钥
  16. private string LocalLogPath => Path.Combine(Application.persistentDataPath, "GameLog.txt");
  17. private string LocalDataPath => Path.Combine(Application.persistentDataPath, "playerData.txt");
  18. private string ZipPath => Path.Combine(Application.persistentDataPath, "upload_logs.zip");
  19. [ContextMenu("asdada")]
  20. public async CTask<bool> UploadWithFTP()
  21. {
  22. try
  23. {
  24. CreateZipPackage();
  25. await UploadZipToFTPAsync();
  26. if (File.Exists(ZipPath)) File.Delete(ZipPath);
  27. Debug.Log("FTP 日志上传成功!");
  28. return true;
  29. }
  30. catch (System.Exception ex)
  31. {
  32. LogTool.Error($"上传失败: {ex.Message}\n{ex.StackTrace}");
  33. }
  34. return false;
  35. }
  36. private void CreateZipPackage()
  37. {
  38. if (File.Exists(ZipPath)) File.Delete(ZipPath);
  39. using (var zip = ZipFile.Open(ZipPath, ZipArchiveMode.Create))
  40. {
  41. if (File.Exists(LocalLogPath)) zip.CreateEntryFromFile(LocalLogPath, "GameLog.txt");
  42. if (File.Exists(LocalDataPath)) zip.CreateEntryFromFile(LocalDataPath, "playerData.txt");
  43. }
  44. }
  45. private async Task UploadZipToFTPAsync()
  46. {
  47. string fileName =
  48. $"logs_{DateTime.Now:yyyyMMdd_HHmmss}_{SystemInfo.deviceUniqueIdentifier}_{AccountFileInfo.Instance.playerData.playerId}.zip";
  49. string remoteUrl = new Uri(new Uri(ftpServer), fileName).ToString();
  50. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remoteUrl);
  51. request.Method = WebRequestMethods.Ftp.UploadFile;
  52. request.Credentials = new NetworkCredential(ftpUser, ftpPass);
  53. request.UseBinary = true;
  54. request.UsePassive = true;
  55. request.KeepAlive = false;
  56. request.Timeout = 30000;
  57. byte[] buffer = File.ReadAllBytes(ZipPath);
  58. request.ContentLength = buffer.Length;
  59. using (Stream requestStream = await request.GetRequestStreamAsync())
  60. {
  61. int offset = 0;
  62. int chunkSize = 64 * 1024; // 64KB
  63. while (offset < buffer.Length)
  64. {
  65. int size = Math.Min(chunkSize, buffer.Length - offset);
  66. await requestStream.WriteAsync(buffer, offset, size);
  67. offset += size;
  68. // 进度日志(主线程安全)
  69. float progress = (float)offset / buffer.Length;
  70. LogTool.Log($"[FTP] 上传中: {progress:P1}");
  71. }
  72. }
  73. using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
  74. {
  75. LogTool.Log($"[FTP] 服务器响应: {response.StatusDescription}");
  76. }
  77. }
  78. }