waf.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package task
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  9. "github.com/hashicorp/go-multierror"
  10. "go.uber.org/zap"
  11. "sync"
  12. "time"
  13. )
  14. type WafTask interface {
  15. //获取到期时间小于3天的同步时间
  16. SynchronizationTime(ctx context.Context) error
  17. StopPlan(ctx context.Context) error
  18. RecoverStopPlan(ctx context.Context) error
  19. }
  20. func NewWafTask (
  21. webForWardingRep repository.WebForwardingRepository,
  22. tcpforwardingRep repository.TcpforwardingRepository,
  23. udpForWardingRep repository.UdpForWardingRepository,
  24. cdn service.CdnService,
  25. hostRep repository.HostRepository,
  26. globalLimitRep repository.GlobalLimitRepository,
  27. expiredRep repository.ExpiredRepository,
  28. task *Task,
  29. ) WafTask{
  30. return &wafTask{
  31. Task: task,
  32. webForWardingRep: webForWardingRep,
  33. tcpforwardingRep: tcpforwardingRep,
  34. udpForWardingRep: udpForWardingRep,
  35. cdn: cdn,
  36. hostRep: hostRep,
  37. globalLimitRep: globalLimitRep,
  38. expiredRep: expiredRep,
  39. }
  40. }
  41. type wafTask struct {
  42. *Task
  43. webForWardingRep repository.WebForwardingRepository
  44. tcpforwardingRep repository.TcpforwardingRepository
  45. udpForWardingRep repository.UdpForWardingRepository
  46. cdn service.CdnService
  47. hostRep repository.HostRepository
  48. globalLimitRep repository.GlobalLimitRepository
  49. expiredRep repository.ExpiredRepository
  50. }
  51. const (
  52. // 1天后秒数
  53. OneDaysInSeconds = 1 * 24 * 60 * 60
  54. // 7天前秒数
  55. SevenDaysInSeconds = 7 * 24 * 60 * 60 * -1
  56. )
  57. // 获取cdn web id
  58. func (t wafTask) GetCdnWebId(ctx context.Context,hostId []int) ([]int, error) {
  59. tcpIds, err := t.tcpforwardingRep.GetTcpAll(ctx, hostId)
  60. if err != nil {
  61. return nil, err
  62. }
  63. udpIds, err := t.udpForWardingRep.GetUdpAll(ctx, hostId)
  64. if err != nil {
  65. return nil, err
  66. }
  67. webIds, err := t.webForWardingRep.GetWebAll(ctx, hostId)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var ids []int
  72. ids = append(ids, tcpIds...)
  73. ids = append(ids, udpIds...)
  74. ids = append(ids, webIds...)
  75. return ids, nil
  76. }
  77. // 启用/禁用 网站
  78. func (t wafTask) BanServer(ctx context.Context, ids []int, isBan bool) error {
  79. var wg sync.WaitGroup
  80. errChan := make(chan error, len(ids))
  81. // 修正1:为每个 goroutine 增加 WaitGroup 的计数
  82. wg.Add(len(ids))
  83. for _, id := range ids {
  84. go func(id int) {
  85. // 修正2:确保每个 goroutine 在退出时都调用 Done()
  86. defer wg.Done()
  87. err := t.cdn.EditWebIsOn(ctx, int64(id), isBan)
  88. if err != nil {
  89. errChan <- err
  90. // 这里不需要 return,因为 defer wg.Done() 会在函数退出时执行
  91. }
  92. }(id)
  93. }
  94. // 现在 wg.Wait() 会正确地阻塞,直到所有 goroutine 都调用了 Done()
  95. wg.Wait()
  96. // 在所有 goroutine 都结束后,安全地关闭 channel
  97. close(errChan)
  98. var result error
  99. for err := range errChan {
  100. result = multierror.Append(result, err) // 将多个 error 对象合并成一个单一的 error 对象
  101. }
  102. // 修正3:返回收集到的错误,而不是 nil
  103. return result
  104. }
  105. // 获取指定到期时间
  106. func (t wafTask) GetAlmostExpiring(ctx context.Context,hostIds []int,addTime int64) ([]v1.GetAlmostExpireHostResponse,error) {
  107. // 3 天
  108. res, err := t.hostRep.GetAlmostExpired(ctx, hostIds, addTime)
  109. if err != nil {
  110. return nil,err
  111. }
  112. return res, nil
  113. }
  114. // 获取waf全局到期时间
  115. func (t wafTask) GetGlobalAlmostExpiring(ctx context.Context,addTime int64) ([]model.GlobalLimit,error) {
  116. res, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, addTime)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return res, nil
  121. }
  122. // 修改全局续费
  123. func (t wafTask) EditGlobalExpired(ctx context.Context, req []struct{
  124. hostId int
  125. expiredAt int64
  126. }, state bool) error {
  127. var result *multierror.Error // 使用 multierror
  128. for _, v := range req {
  129. err := t.globalLimitRep.UpdateGlobalLimitByHostId(ctx, &model.GlobalLimit{
  130. HostId: v.hostId,
  131. ExpiredAt: v.expiredAt,
  132. State: state,
  133. })
  134. if err != nil {
  135. // 收集错误,而不是直接返回
  136. result = multierror.Append(result, err)
  137. }
  138. }
  139. // 返回所有收集到的错误
  140. return result.ErrorOrNil()
  141. }
  142. // 续费套餐
  143. func (t wafTask) EnablePlan(ctx context.Context, req []struct{
  144. planId int
  145. expiredAt int64
  146. }) error {
  147. var result *multierror.Error
  148. for _, v := range req {
  149. err := t.cdn.RenewPlan(ctx, v1.RenewalPlan{
  150. UserPlanId: int64(v.planId),
  151. IsFree: true,
  152. DayTo: time.Unix(v.expiredAt, 0).Format("2006-01-02"),
  153. Period: "monthly",
  154. CountPeriod: 1,
  155. PeriodDayTo: time.Unix(v.expiredAt, 0).Format("2006-01-02"),
  156. })
  157. if err != nil {
  158. result = multierror.Append(result, err)
  159. }
  160. }
  161. return result.ErrorOrNil()
  162. }
  163. // 续费操作
  164. type RenewalRequest struct {
  165. HostId int
  166. PlanId int
  167. ExpiredAt int64
  168. }
  169. // 续费操作
  170. func (t wafTask) EditExpired(ctx context.Context, reqs []RenewalRequest) error {
  171. // 如果请求为空,直接返回
  172. if len(reqs) == 0 {
  173. return nil
  174. }
  175. // 1. 准备用于更新 GlobalLimit 的数据
  176. var globalLimitUpdates []struct {
  177. hostId int
  178. expiredAt int64
  179. }
  180. for _, req := range reqs {
  181. globalLimitUpdates = append(globalLimitUpdates, struct {
  182. hostId int
  183. expiredAt int64
  184. }{req.HostId, req.ExpiredAt})
  185. }
  186. // 2. 准备用于续费套餐的数据
  187. var planRenewals []struct {
  188. planId int
  189. expiredAt int64
  190. }
  191. for _, req := range reqs {
  192. planRenewals = append(planRenewals, struct {
  193. planId int
  194. expiredAt int64
  195. }{req.PlanId, req.ExpiredAt})
  196. }
  197. var result *multierror.Error
  198. // 3. 执行更新,并收集错误
  199. if err := t.EditGlobalExpired(ctx, globalLimitUpdates, true); err != nil {
  200. result = multierror.Append(result, err)
  201. }
  202. if err := t.EnablePlan(ctx, planRenewals); err != nil {
  203. result = multierror.Append(result, err)
  204. }
  205. return result.ErrorOrNil()
  206. }
  207. // findMismatchedExpirations 检查 WAF 和 Host 的到期时间差异,并返回需要同步的请求。
  208. func (t *wafTask) findMismatchedExpirations(ctx context.Context, wafLimits []model.GlobalLimit) ([]RenewalRequest, error) {
  209. if len(wafLimits) == 0 {
  210. return nil, nil
  211. }
  212. // 2. 将 WAF 数据组织成 Map
  213. wafExpiredMap := make(map[int]int64, len(wafLimits))
  214. wafPlanMap := make(map[int]int, len(wafLimits))
  215. var hostIds []int
  216. for _, limit := range wafLimits {
  217. hostIds = append(hostIds, limit.HostId)
  218. wafExpiredMap[limit.HostId] = limit.ExpiredAt
  219. wafPlanMap[limit.HostId] = limit.RuleId
  220. }
  221. // 3. 获取对应 Host 的到期时间
  222. hostExpirations, err := t.hostRep.GetExpireTimeByHostId(ctx, hostIds)
  223. if err != nil {
  224. return nil, fmt.Errorf("获取主机到期时间失败: %w", err)
  225. }
  226. hostExpiredMap := make(map[int]int64, len(hostExpirations))
  227. for _, h := range hostExpirations {
  228. hostExpiredMap[h.HostId] = h.ExpiredAt
  229. }
  230. // 4. 找出时间不一致的记录
  231. var renewalRequests []RenewalRequest
  232. for hostId, wafExpiredTime := range wafExpiredMap {
  233. hostTime, ok := hostExpiredMap[hostId]
  234. // 如果 Host 时间与 WAF 时间不一致,则需要同步
  235. if !ok || hostTime != wafExpiredTime {
  236. planId, planOk := wafPlanMap[hostId]
  237. if !planOk {
  238. t.logger.Warn("数据不一致:在waf_limits中找不到hostId对应的套餐ID", zap.Int("hostId", hostId))
  239. continue
  240. }
  241. renewalRequests = append(renewalRequests, RenewalRequest{
  242. HostId: hostId,
  243. ExpiredAt: hostTime, // 以 host 表的时间为准
  244. PlanId: planId,
  245. })
  246. }
  247. }
  248. return renewalRequests, nil
  249. }
  250. //获取同步到期时间小于1天的套餐
  251. func (t *wafTask) SynchronizationTime(ctx context.Context) error {
  252. // 1. 获取 WAF 全局配置中即将到期(小于3天)的数据
  253. wafLimits, err := t.GetGlobalAlmostExpiring(ctx, OneDaysInSeconds)
  254. if err != nil {
  255. return fmt.Errorf("获取全局到期配置失败: %w", err)
  256. }
  257. // 2. 找出需要同步的数据
  258. renewalRequests, err := t.findMismatchedExpirations(ctx, wafLimits)
  259. if err != nil {
  260. return err // 错误已在辅助函数中包装
  261. }
  262. // 3. 如果有需要同步的数据,执行续费操作
  263. if len(renewalRequests) > 0 {
  264. t.logger.Info("发现记录需要同步到期时间。", zap.Int("数量", len(renewalRequests)))
  265. return t.EditExpired(ctx, renewalRequests)
  266. }
  267. return nil
  268. }
  269. // 获取到期的进行关闭套餐操作
  270. func (t *wafTask) StopPlan(ctx context.Context) error {
  271. // 1. 获取 WAF 全局配置中已经到期的数据
  272. // 使用 time.Now().Unix() 表示获取所有 expired_at <= 当前时间的记录
  273. wafLimits, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, 0)
  274. if err != nil {
  275. return fmt.Errorf("获取全局到期配置失败: %w", err)
  276. }
  277. if len(wafLimits) == 0 {
  278. return nil // 没有到期的,任务完成
  279. }
  280. // 2. (可选,但推荐)先同步任何时间不一致的数据,确保状态准确
  281. renewalRequests, err := t.findMismatchedExpirations(ctx, wafLimits)
  282. if err != nil {
  283. t.logger.Error("在关闭套餐前,同步时间失败", zap.Error(err))
  284. // 根据业务决定是否要继续,这里我们选择继续,但记录错误
  285. }
  286. if len(renewalRequests) > 0 {
  287. t.logger.Info("关闭套餐前,发现并同步不一致的时间记录", zap.Int("数量", len(renewalRequests)))
  288. if err := t.EditExpired(ctx, renewalRequests); err != nil {
  289. t.logger.Error("同步不一致的时间记录失败", zap.Error(err))
  290. }
  291. }
  292. // 3. 筛选出尚未被关闭的套餐
  293. var plansToClose []model.GlobalLimit
  294. for _, limit := range wafLimits {
  295. isClosed, err := t.expiredRep.IsPlanClosed(ctx, int64(limit.HostId))
  296. if err != nil {
  297. t.logger.Error("检查Redis中套餐关闭状态失败", zap.Int("hostId", limit.HostId), zap.Error(err))
  298. continue // 跳过这个,处理下一个
  299. }
  300. if !isClosed {
  301. plansToClose = append(plansToClose, limit)
  302. }
  303. }
  304. if len(plansToClose) == 0 {
  305. t.logger.Info("没有新的到期套餐需要关闭")
  306. return nil
  307. }
  308. // 4. 对筛选出的套餐执行关闭操作
  309. t.logger.Info("开始关闭新的到期WAF服务", zap.Int("数量", len(plansToClose)))
  310. var allErrors *multierror.Error
  311. var webIds []int
  312. for _, limit := range plansToClose {
  313. webIds = append(webIds, limit.HostId)
  314. }
  315. if err := t.BanServer(ctx, webIds, false); err != nil {
  316. allErrors = multierror.Append(allErrors, fmt.Errorf("关闭hostId %v 的服务失败: %w", webIds, err))
  317. } else {
  318. // 服务关闭成功后,将这些套餐信息添加到 Redis
  319. var expiredInfos []repository.ExpiredInfo
  320. for _, limit := range plansToClose {
  321. expiredInfos = append(expiredInfos, repository.ExpiredInfo{
  322. HostID: int64(limit.HostId),
  323. Expiry: time.Unix(limit.ExpiredAt, 0),
  324. })
  325. }
  326. if len(expiredInfos) > 0 {
  327. if err := t.expiredRep.AddClosePlans(ctx, expiredInfos...); err != nil {
  328. allErrors = multierror.Append(allErrors, fmt.Errorf("添加已关闭套餐信息到Redis失败: %w", err))
  329. }
  330. }
  331. }
  332. return allErrors.ErrorOrNil()
  333. }
  334. //对于到期7天内续费的产品需要进行恢复操作
  335. func (t *wafTask) RecoverStopPlan(ctx context.Context) error {
  336. // 1. 获取所有已过期(expired_at < now)但状态仍为 true 的 WAF 记录
  337. // StopPlan 任务会禁用这些服务,但不会改变它们的 state
  338. wafLimits, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, SevenDaysInSeconds) // addTime=0 表示获取所有当前时间之前到期的
  339. if err != nil {
  340. return fmt.Errorf("获取过期WAF配置失败: %w", err)
  341. }
  342. if len(wafLimits) == 0 {
  343. t.logger.Info("没有已过期且需要检查恢复的服务")
  344. return nil
  345. }
  346. // 2. 检查这些记录对应的 host 是否已续费
  347. // findMismatchedExpirations 会比较 waf.expired_at 和 host.nextduedate
  348. renewalRequests, err := t.findMismatchedExpirations(ctx, wafLimits)
  349. if err != nil {
  350. return fmt.Errorf("检查续费状态失败: %w", err)
  351. }
  352. if len(renewalRequests) == 0 {
  353. t.logger.Info("在已过期的服务中,没有发现已续费且需要恢复的服务")
  354. return nil
  355. }
  356. // 3. 对已续费的服务执行恢复操作
  357. t.logger.Info("发现已续费、需要恢复的WAF服务", zap.Int("数量", len(renewalRequests)))
  358. var allErrors *multierror.Error
  359. var webIds []int
  360. for _, req := range renewalRequests {
  361. webIds = append(webIds, req.HostId)
  362. }
  363. if err := t.BanServer(ctx, webIds, true); err != nil {
  364. allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %v: 启用服务失败: %w", webIds, err))
  365. } else {
  366. // 服务恢复成功后,从 Redis 中移除这些套餐的关闭记录
  367. planIds := make([]int64, len(webIds))
  368. for i, id := range webIds {
  369. planIds[i] = int64(id)
  370. }
  371. if err := t.expiredRep.RemoveClosePlanIds(ctx, planIds...); err != nil {
  372. allErrors = multierror.Append(allErrors, fmt.Errorf("从Redis移除已恢复的套餐失败: %w", err))
  373. }
  374. }
  375. if len(renewalRequests) > 0 {
  376. // 统一执行续费和数据库更新操作
  377. if err := t.EditExpired(ctx, renewalRequests); err != nil {
  378. allErrors = multierror.Append(allErrors, fmt.Errorf("批量更新已恢复服务的数据库状态失败: %w", err))
  379. }
  380. }
  381. return allErrors.ErrorOrNil()
  382. }
  383. //对于大于7天的药进行数据情侣操作