save.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package web
  2. import (
  3. "backup-x/client"
  4. "backup-x/entity"
  5. "backup-x/util"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // 服务启动时间
  14. var startTime = time.Now()
  15. // 保存限制时间
  16. var saveLimit = time.Duration(30 * time.Minute)
  17. // Save 保存
  18. func Save(writer http.ResponseWriter, request *http.Request) {
  19. oldConf, _ := entity.GetConfigCache()
  20. conf := &entity.Config{}
  21. conf.EncryptKey = oldConf.EncryptKey
  22. if conf.EncryptKey == "" {
  23. encryptKey, err := util.GenerateEncryptKey()
  24. if err != nil {
  25. writer.Write([]byte("生成Key失败"))
  26. return
  27. }
  28. conf.EncryptKey = encryptKey
  29. }
  30. // 覆盖以前的配置
  31. conf.Username = strings.TrimSpace(request.FormValue("Username"))
  32. conf.Password = request.FormValue("Password")
  33. if conf.Username == "" || conf.Password == "" {
  34. writer.Write([]byte("请输入登录用户名/密码"))
  35. return
  36. }
  37. if conf.Password != oldConf.Password {
  38. if time.Since(startTime) > saveLimit {
  39. writer.Write([]byte(fmt.Sprintf("需在 %s 之前完成用户名密码设置,请重启backup-x", startTime.Add(saveLimit).Format("2006-01-02 15:04:05"))))
  40. return
  41. }
  42. encryptPasswd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.Password)
  43. if err != nil {
  44. writer.Write([]byte("加密失败"))
  45. return
  46. }
  47. conf.Password = encryptPasswd
  48. }
  49. forms := request.PostForm
  50. for index, projectName := range forms["ProjectName"] {
  51. saveDays, _ := strconv.Atoi(forms["SaveDays"][index])
  52. saveDaysS3, _ := strconv.Atoi(forms["SaveDaysS3"][index])
  53. startTime, _ := strconv.Atoi(forms["StartTime"][index])
  54. period, _ := strconv.Atoi(forms["Period"][index])
  55. backupType, _ := strconv.Atoi(forms["BackupType"][index])
  56. enabled, _ := strconv.Atoi(forms["Enabled"][index])
  57. conf.BackupConfig = append(
  58. conf.BackupConfig,
  59. entity.BackupConfig{
  60. ProjectName: projectName,
  61. Command: forms["Command"][index],
  62. SaveDays: saveDays,
  63. SaveDaysS3: saveDaysS3,
  64. StartTime: startTime,
  65. Period: period,
  66. Pwd: forms["Pwd"][index],
  67. BackupType: backupType,
  68. Enabled: enabled,
  69. },
  70. )
  71. }
  72. for i := 0; i < len(conf.BackupConfig); i++ {
  73. if conf.BackupConfig[i].Pwd != "" &&
  74. (len(oldConf.BackupConfig) == 0 || conf.BackupConfig[i].Pwd != oldConf.BackupConfig[i].Pwd) {
  75. encryptPwd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.BackupConfig[i].Pwd)
  76. if err != nil {
  77. writer.Write([]byte("加密失败"))
  78. return
  79. }
  80. conf.BackupConfig[i].Pwd = encryptPwd
  81. }
  82. }
  83. // Webhook
  84. conf.WebhookURL = strings.TrimSpace(request.FormValue("WebhookURL"))
  85. conf.WebhookRequestBody = strings.TrimSpace(request.FormValue("WebhookRequestBody"))
  86. // S3
  87. conf.Endpoint = strings.TrimSpace(request.FormValue("Endpoint"))
  88. conf.AccessKey = strings.TrimSpace(request.FormValue("AccessKey"))
  89. conf.SecretKey = strings.TrimSpace(request.FormValue("SecretKey"))
  90. conf.BucketName = strings.TrimSpace(request.FormValue("BucketName"))
  91. if conf.SecretKey != "" && conf.SecretKey != oldConf.SecretKey {
  92. secretKey, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.SecretKey)
  93. if err != nil {
  94. writer.Write([]byte("加密失败"))
  95. return
  96. }
  97. conf.SecretKey = secretKey
  98. }
  99. // 保存到用户目录
  100. err := conf.SaveConfig()
  101. // 没有错误
  102. if err == nil {
  103. conf.CreateBucketIfNotExist()
  104. if request.URL.Query().Get("backupAll") == "true" {
  105. go client.RunOnce()
  106. }
  107. if request.URL.Query().Get("backupIdx") != "" {
  108. idx, err := strconv.Atoi(request.URL.Query().Get("backupIdx"))
  109. if err == nil {
  110. go client.RunByIdx(idx)
  111. } else {
  112. log.Println("索引号不正确" + request.URL.Query().Get("backupIdx"))
  113. }
  114. }
  115. // 重新进行循环
  116. client.StopRunLoop()
  117. go client.RunLoop(100 * time.Millisecond)
  118. }
  119. // 回写错误信息
  120. if err == nil {
  121. writer.Write([]byte("ok"))
  122. } else {
  123. writer.Write([]byte(err.Error()))
  124. }
  125. }