save.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. encryptPasswd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.Password)
  45. if err != nil {
  46. writer.Write([]byte("加密失败"))
  47. return
  48. }
  49. conf.Password = encryptPasswd
  50. }
  51. forms := request.PostForm
  52. for index, projectName := range forms["ProjectName"] {
  53. saveDays, _ := strconv.Atoi(forms["SaveDays"][index])
  54. saveDaysS3, _ := strconv.Atoi(forms["SaveDaysS3"][index])
  55. startTime, _ := strconv.Atoi(forms["StartTime"][index])
  56. period, _ := strconv.Atoi(forms["Period"][index])
  57. backupType, _ := strconv.Atoi(forms["BackupType"][index])
  58. enabled, _ := strconv.Atoi(forms["Enabled"][index])
  59. conf.BackupConfig = append(
  60. conf.BackupConfig,
  61. entity.BackupConfig{
  62. ProjectName: projectName,
  63. Command: forms["Command"][index],
  64. SaveDays: saveDays,
  65. SaveDaysS3: saveDaysS3,
  66. StartTime: startTime,
  67. Period: period,
  68. Pwd: forms["Pwd"][index],
  69. BackupType: backupType,
  70. Enabled: enabled,
  71. },
  72. )
  73. }
  74. for i := 0; i < len(conf.BackupConfig); i++ {
  75. if conf.BackupConfig[i].Pwd != "" &&
  76. (len(oldConf.BackupConfig) == 0 || conf.BackupConfig[i].Pwd != oldConf.BackupConfig[i].Pwd) {
  77. encryptPwd, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.BackupConfig[i].Pwd)
  78. if err != nil {
  79. writer.Write([]byte("加密失败"))
  80. return
  81. }
  82. conf.BackupConfig[i].Pwd = encryptPwd
  83. }
  84. }
  85. // Webhook
  86. conf.WebhookURL = strings.TrimSpace(request.FormValue("WebhookURL"))
  87. conf.WebhookRequestBody = strings.TrimSpace(request.FormValue("WebhookRequestBody"))
  88. // S3
  89. conf.Endpoint = strings.TrimSpace(request.FormValue("Endpoint"))
  90. conf.AccessKey = strings.TrimSpace(request.FormValue("AccessKey"))
  91. conf.SecretKey = strings.TrimSpace(request.FormValue("SecretKey"))
  92. conf.BucketName = strings.TrimSpace(request.FormValue("BucketName"))
  93. if conf.SecretKey != "" && conf.SecretKey != oldConf.SecretKey {
  94. secretKey, err := util.EncryptByEncryptKey(conf.EncryptKey, conf.SecretKey)
  95. if err != nil {
  96. writer.Write([]byte("加密失败"))
  97. return
  98. }
  99. conf.SecretKey = secretKey
  100. }
  101. // 保存到用户目录
  102. err := conf.SaveConfig()
  103. // 没有错误
  104. if err == nil {
  105. conf.CreateBucketIfNotExist()
  106. if request.URL.Query().Get("backupAll") == "true" {
  107. go client.RunOnce()
  108. }
  109. if request.URL.Query().Get("backupIdx") != "" {
  110. idx, err := strconv.Atoi(request.URL.Query().Get("backupIdx"))
  111. if err == nil {
  112. go client.RunByIdx(idx)
  113. } else {
  114. log.Println("索引号不正确" + request.URL.Query().Get("backupIdx"))
  115. }
  116. }
  117. // 重新进行循环
  118. client.StopRunLoop()
  119. go client.RunLoop(100 * time.Millisecond)
  120. }
  121. // 回写错误信息
  122. if err == nil {
  123. writer.Write([]byte("ok"))
  124. } else {
  125. writer.Write([]byte(err.Error()))
  126. }
  127. }