1
0

inbound.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "x-ui/database"
  7. "x-ui/database/model"
  8. "x-ui/logger"
  9. "x-ui/util/common"
  10. "x-ui/xray"
  11. "gorm.io/gorm"
  12. )
  13. type InboundService struct {
  14. }
  15. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  16. db := database.GetDB()
  17. var inbounds []*model.Inbound
  18. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Find(&inbounds).Error
  19. if err != nil && err != gorm.ErrRecordNotFound {
  20. return nil, err
  21. }
  22. return inbounds, nil
  23. }
  24. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  25. db := database.GetDB()
  26. var inbounds []*model.Inbound
  27. err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
  28. if err != nil && err != gorm.ErrRecordNotFound {
  29. return nil, err
  30. }
  31. return inbounds, nil
  32. }
  33. func (s *InboundService) checkPortExist(port int, ignoreId int) (bool, error) {
  34. db := database.GetDB()
  35. db = db.Model(model.Inbound{}).Where("port = ?", port)
  36. if ignoreId > 0 {
  37. db = db.Where("id != ?", ignoreId)
  38. }
  39. var count int64
  40. err := db.Count(&count).Error
  41. if err != nil {
  42. return false, err
  43. }
  44. return count > 0, nil
  45. }
  46. func (s *InboundService) getClients(inbound *model.Inbound) ([]model.Client, error) {
  47. settings := map[string][]model.Client{}
  48. json.Unmarshal([]byte(inbound.Settings), &settings)
  49. if settings == nil {
  50. return nil, fmt.Errorf("Setting is null")
  51. }
  52. clients := settings["clients"]
  53. if clients == nil {
  54. return nil, nil
  55. }
  56. return clients, nil
  57. }
  58. func (s *InboundService) checkEmailsExist(emails map[string]bool, ignoreId int) (string, error) {
  59. db := database.GetDB()
  60. var inbounds []*model.Inbound
  61. db = db.Model(model.Inbound{}).Where("Protocol in ?", []model.Protocol{model.VMess, model.VLESS, model.Trojan})
  62. if ignoreId > 0 {
  63. db = db.Where("id != ?", ignoreId)
  64. }
  65. db = db.Find(&inbounds)
  66. if db.Error != nil {
  67. return "", db.Error
  68. }
  69. for _, inbound := range inbounds {
  70. clients, err := s.getClients(inbound)
  71. if err != nil {
  72. return "", err
  73. }
  74. for _, client := range clients {
  75. if emails[client.Email] {
  76. return client.Email, nil
  77. }
  78. }
  79. }
  80. return "", nil
  81. }
  82. func (s *InboundService) checkEmailExistForInbound(inbound *model.Inbound) (string, error) {
  83. clients, err := s.getClients(inbound)
  84. if err != nil {
  85. return "", err
  86. }
  87. emails := make(map[string]bool)
  88. for _, client := range clients {
  89. if client.Email != "" {
  90. if emails[client.Email] {
  91. return client.Email, nil
  92. }
  93. emails[client.Email] = true
  94. }
  95. }
  96. return s.checkEmailsExist(emails, inbound.Id)
  97. }
  98. func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, error) {
  99. exist, err := s.checkPortExist(inbound.Port, 0)
  100. if err != nil {
  101. return inbound, err
  102. }
  103. if exist {
  104. return inbound, common.NewError("Port already exists:", inbound.Port)
  105. }
  106. existEmail, err := s.checkEmailExistForInbound(inbound)
  107. if err != nil {
  108. return inbound, err
  109. }
  110. if existEmail != "" {
  111. return inbound, common.NewError("Duplicate email:", existEmail)
  112. }
  113. db := database.GetDB()
  114. err = db.Save(inbound).Error
  115. if err == nil {
  116. s.UpdateClientStat(inbound.Id, inbound.Settings)
  117. }
  118. return inbound, err
  119. }
  120. func (s *InboundService) AddInbounds(inbounds []*model.Inbound) error {
  121. for _, inbound := range inbounds {
  122. exist, err := s.checkPortExist(inbound.Port, 0)
  123. if err != nil {
  124. return err
  125. }
  126. if exist {
  127. return common.NewError("Port already exists:", inbound.Port)
  128. }
  129. }
  130. db := database.GetDB()
  131. tx := db.Begin()
  132. var err error
  133. defer func() {
  134. if err == nil {
  135. tx.Commit()
  136. } else {
  137. tx.Rollback()
  138. }
  139. }()
  140. for _, inbound := range inbounds {
  141. err = tx.Save(inbound).Error
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }
  148. func (s *InboundService) DelInbound(id int) error {
  149. db := database.GetDB()
  150. return db.Delete(model.Inbound{}, id).Error
  151. }
  152. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  153. db := database.GetDB()
  154. inbound := &model.Inbound{}
  155. err := db.Model(model.Inbound{}).First(inbound, id).Error
  156. if err != nil {
  157. return nil, err
  158. }
  159. return inbound, nil
  160. }
  161. func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, error) {
  162. exist, err := s.checkPortExist(inbound.Port, inbound.Id)
  163. if err != nil {
  164. return inbound, err
  165. }
  166. if exist {
  167. return inbound, common.NewError("Port already exists:", inbound.Port)
  168. }
  169. existEmail, err := s.checkEmailExistForInbound(inbound)
  170. if err != nil {
  171. return inbound, err
  172. }
  173. if existEmail != "" {
  174. return inbound, common.NewError("Duplicate email:", existEmail)
  175. }
  176. oldInbound, err := s.GetInbound(inbound.Id)
  177. if err != nil {
  178. return inbound, err
  179. }
  180. oldInbound.Up = inbound.Up
  181. oldInbound.Down = inbound.Down
  182. oldInbound.Total = inbound.Total
  183. oldInbound.Remark = inbound.Remark
  184. oldInbound.Enable = inbound.Enable
  185. oldInbound.ExpiryTime = inbound.ExpiryTime
  186. oldInbound.Listen = inbound.Listen
  187. oldInbound.Port = inbound.Port
  188. oldInbound.Protocol = inbound.Protocol
  189. oldInbound.Settings = inbound.Settings
  190. oldInbound.StreamSettings = inbound.StreamSettings
  191. oldInbound.Sniffing = inbound.Sniffing
  192. oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  193. s.UpdateClientStat(inbound.Id, inbound.Settings)
  194. db := database.GetDB()
  195. return inbound, db.Save(oldInbound).Error
  196. }
  197. func (s *InboundService) AddTraffic(traffics []*xray.Traffic) (err error) {
  198. if len(traffics) == 0 {
  199. return nil
  200. }
  201. db := database.GetDB()
  202. db = db.Model(model.Inbound{})
  203. tx := db.Begin()
  204. defer func() {
  205. if err != nil {
  206. tx.Rollback()
  207. } else {
  208. tx.Commit()
  209. }
  210. }()
  211. for _, traffic := range traffics {
  212. if traffic.IsInbound {
  213. err = tx.Where("tag = ?", traffic.Tag).
  214. UpdateColumn("up", gorm.Expr("up + ?", traffic.Up)).
  215. UpdateColumn("down", gorm.Expr("down + ?", traffic.Down)).
  216. Error
  217. if err != nil {
  218. return
  219. }
  220. }
  221. }
  222. return
  223. }
  224. func (s *InboundService) AddClientTraffic(traffics []*xray.ClientTraffic) (err error) {
  225. if len(traffics) == 0 {
  226. return nil
  227. }
  228. db := database.GetDB()
  229. dbInbound := db.Model(model.Inbound{})
  230. db = db.Model(xray.ClientTraffic{})
  231. tx := db.Begin()
  232. defer func() {
  233. if err != nil {
  234. tx.Rollback()
  235. } else {
  236. tx.Commit()
  237. }
  238. }()
  239. txInbound := dbInbound.Begin()
  240. defer func() {
  241. if err != nil {
  242. txInbound.Rollback()
  243. } else {
  244. txInbound.Commit()
  245. }
  246. }()
  247. for _, traffic := range traffics {
  248. inbound := &model.Inbound{}
  249. err := txInbound.Where("settings like ?", "%"+traffic.Email+"%").First(inbound).Error
  250. traffic.InboundId = inbound.Id
  251. if err != nil {
  252. if err == gorm.ErrRecordNotFound {
  253. // delete removed client record
  254. clientErr := s.DelClientStat(tx, traffic.Email)
  255. logger.Warning(err, traffic.Email, clientErr)
  256. }
  257. continue
  258. }
  259. // get settings clients
  260. settings := map[string][]model.Client{}
  261. json.Unmarshal([]byte(inbound.Settings), &settings)
  262. clients := settings["clients"]
  263. for _, client := range clients {
  264. if traffic.Email == client.Email {
  265. traffic.ExpiryTime = client.ExpiryTime
  266. traffic.Total = client.TotalGB
  267. }
  268. }
  269. if tx.Where("inbound_id = ?", inbound.Id).Where("email = ?", traffic.Email).
  270. UpdateColumn("enable", true).
  271. UpdateColumn("expiry_time", traffic.ExpiryTime).
  272. UpdateColumn("total", traffic.Total).
  273. UpdateColumn("up", gorm.Expr("up + ?", traffic.Up)).
  274. UpdateColumn("down", gorm.Expr("down + ?", traffic.Down)).RowsAffected == 0 {
  275. err = tx.Create(traffic).Error
  276. }
  277. if err != nil {
  278. logger.Warning("AddClientTraffic update data ", err)
  279. continue
  280. }
  281. }
  282. return
  283. }
  284. func (s *InboundService) DisableInvalidInbounds() (int64, error) {
  285. db := database.GetDB()
  286. now := time.Now().Unix() * 1000
  287. result := db.Model(model.Inbound{}).
  288. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  289. Update("enable", false)
  290. err := result.Error
  291. count := result.RowsAffected
  292. return count, err
  293. }
  294. func (s *InboundService) DisableInvalidClients() (int64, error) {
  295. db := database.GetDB()
  296. now := time.Now().Unix() * 1000
  297. result := db.Model(xray.ClientTraffic{}).
  298. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  299. Update("enable", false)
  300. err := result.Error
  301. count := result.RowsAffected
  302. return count, err
  303. }
  304. func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string) error {
  305. db := database.GetDB()
  306. // get settings clients
  307. settings := map[string][]model.Client{}
  308. json.Unmarshal([]byte(inboundSettings), &settings)
  309. clients := settings["clients"]
  310. for _, client := range clients {
  311. result := db.Model(xray.ClientTraffic{}).
  312. Where("inbound_id = ? and email = ?", inboundId, client.Email).
  313. Updates(map[string]interface{}{"enable": true, "total": client.TotalGB, "expiry_time": client.ExpiryTime})
  314. if result.RowsAffected == 0 {
  315. clientTraffic := xray.ClientTraffic{}
  316. clientTraffic.InboundId = inboundId
  317. clientTraffic.Email = client.Email
  318. clientTraffic.Total = client.TotalGB
  319. clientTraffic.ExpiryTime = client.ExpiryTime
  320. clientTraffic.Enable = true
  321. clientTraffic.Up = 0
  322. clientTraffic.Down = 0
  323. db.Create(&clientTraffic)
  324. }
  325. err := result.Error
  326. if err != nil {
  327. return err
  328. }
  329. }
  330. return nil
  331. }
  332. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  333. return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
  334. }
  335. func (s *InboundService) ResetClientTraffic(clientEmail string) error {
  336. db := database.GetDB()
  337. result := db.Model(xray.ClientTraffic{}).
  338. Where("email = ?", clientEmail).
  339. Update("up", 0).
  340. Update("down", 0)
  341. err := result.Error
  342. if err != nil {
  343. return err
  344. }
  345. return nil
  346. }
  347. func (s *InboundService) GetClientTrafficById(uuid string) (traffic *xray.ClientTraffic, err error) {
  348. db := database.GetDB()
  349. inbound := &model.Inbound{}
  350. traffic = &xray.ClientTraffic{}
  351. err = db.Model(model.Inbound{}).Where("settings like ?", "%"+uuid+"%").First(inbound).Error
  352. if err != nil {
  353. if err == gorm.ErrRecordNotFound {
  354. logger.Warning(err)
  355. return nil, err
  356. }
  357. }
  358. traffic.InboundId = inbound.Id
  359. // get settings clients
  360. settings := map[string][]model.Client{}
  361. json.Unmarshal([]byte(inbound.Settings), &settings)
  362. clients := settings["clients"]
  363. for _, client := range clients {
  364. if uuid == client.ID {
  365. traffic.Email = client.Email
  366. }
  367. }
  368. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  369. if err != nil {
  370. logger.Warning(err)
  371. return nil, err
  372. }
  373. return traffic, err
  374. }