inbound.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package service
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "x-ui/database"
  6. "x-ui/database/model"
  7. "x-ui/xray"
  8. )
  9. type InboundService struct {
  10. }
  11. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  12. db := database.GetDB()
  13. var inbounds []*model.Inbound
  14. err := db.Model(model.Inbound{}).Where("user_id = ?", userId).Find(&inbounds).Error
  15. if err != nil && err != gorm.ErrRecordNotFound {
  16. return nil, err
  17. }
  18. return inbounds, nil
  19. }
  20. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  21. db := database.GetDB()
  22. var inbounds []*model.Inbound
  23. err := db.Model(model.Inbound{}).Find(&inbounds).Error
  24. if err != nil && err != gorm.ErrRecordNotFound {
  25. return nil, err
  26. }
  27. return inbounds, nil
  28. }
  29. func (s *InboundService) AddInbound(inbound *model.Inbound) error {
  30. db := database.GetDB()
  31. return db.Save(inbound).Error
  32. }
  33. func (s *InboundService) DelInbound(id int) error {
  34. db := database.GetDB()
  35. return db.Delete(model.Inbound{}, id).Error
  36. }
  37. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  38. db := database.GetDB()
  39. inbound := &model.Inbound{}
  40. err := db.Model(model.Inbound{}).First(inbound, id).Error
  41. if err != nil {
  42. return nil, err
  43. }
  44. return inbound, nil
  45. }
  46. func (s *InboundService) UpdateInbound(inbound *model.Inbound) error {
  47. oldInbound, err := s.GetInbound(inbound.Id)
  48. if err != nil {
  49. return err
  50. }
  51. oldInbound.Up = inbound.Up
  52. oldInbound.Down = inbound.Down
  53. oldInbound.Total = inbound.Total
  54. oldInbound.Remark = inbound.Remark
  55. oldInbound.Enable = inbound.Enable
  56. oldInbound.ExpiryTime = inbound.ExpiryTime
  57. oldInbound.Listen = inbound.Listen
  58. oldInbound.Port = inbound.Port
  59. oldInbound.Protocol = inbound.Protocol
  60. oldInbound.Settings = inbound.Settings
  61. oldInbound.StreamSettings = inbound.StreamSettings
  62. oldInbound.Sniffing = inbound.Sniffing
  63. oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  64. db := database.GetDB()
  65. return db.Save(oldInbound).Error
  66. }
  67. func (s *InboundService) AddTraffic(traffics []*xray.Traffic) (err error) {
  68. if len(traffics) == 0 {
  69. return nil
  70. }
  71. db := database.GetDB()
  72. db = db.Model(model.Inbound{})
  73. tx := db.Begin()
  74. defer func() {
  75. if err != nil {
  76. tx.Rollback()
  77. } else {
  78. tx.Commit()
  79. }
  80. }()
  81. for _, traffic := range traffics {
  82. if traffic.IsInbound {
  83. err = tx.Where("tag = ?", traffic.Tag).
  84. UpdateColumn("up", gorm.Expr("up + ?", traffic.Up)).
  85. UpdateColumn("down", gorm.Expr("down + ?", traffic.Down)).
  86. Error
  87. if err != nil {
  88. return
  89. }
  90. }
  91. }
  92. return
  93. }
  94. func (s *InboundService) DisableInvalidInbounds() (int64, error) {
  95. db := database.GetDB()
  96. result := db.Model(model.Inbound{}).
  97. Where("up + down >= total and total > 0 and enable = ?", true).
  98. Update("enable", false)
  99. err := result.Error
  100. count := result.RowsAffected
  101. return count, err
  102. }