save.go 4.0 KB

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