inbound.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. UpdateColumns(map[string]interface{}{
  215. "up": gorm.Expr("up + ?", traffic.Up),
  216. "down": gorm.Expr("down + ?", traffic.Down)}).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. client := &xray.ClientTraffic{}
  250. err := tx.Where("email = ?", traffic.Email).First(client).Error
  251. if err != nil {
  252. if err == gorm.ErrRecordNotFound {
  253. logger.Warning(err, traffic.Email)
  254. }
  255. continue
  256. }
  257. err = txInbound.Where("id=?", client.InboundId).First(inbound).Error
  258. if err != nil {
  259. if err == gorm.ErrRecordNotFound {
  260. logger.Warning(err, traffic.Email)
  261. }
  262. continue
  263. }
  264. // get settings clients
  265. settings := map[string][]model.Client{}
  266. json.Unmarshal([]byte(inbound.Settings), &settings)
  267. clients := settings["clients"]
  268. for _, client := range clients {
  269. if traffic.Email == client.Email {
  270. traffic.ExpiryTime = client.ExpiryTime
  271. traffic.Total = client.TotalGB
  272. }
  273. }
  274. if tx.Where("inbound_id = ?", inbound.Id).Where("email = ?", traffic.Email).
  275. UpdateColumns(map[string]interface{}{
  276. "enable": true,
  277. "expiry_time": traffic.ExpiryTime,
  278. "total": traffic.Total,
  279. "up": gorm.Expr("up + ?", traffic.Up),
  280. "down": gorm.Expr("down + ?", traffic.Down)}).RowsAffected == 0 {
  281. err = tx.Create(traffic).Error
  282. }
  283. if err != nil {
  284. logger.Warning("AddClientTraffic update data ", err)
  285. continue
  286. }
  287. }
  288. return
  289. }
  290. func (s *InboundService) DisableInvalidInbounds() (int64, error) {
  291. db := database.GetDB()
  292. now := time.Now().Unix() * 1000
  293. result := db.Model(model.Inbound{}).
  294. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  295. Update("enable", false)
  296. err := result.Error
  297. count := result.RowsAffected
  298. return count, err
  299. }
  300. func (s *InboundService) DisableInvalidClients() (int64, error) {
  301. db := database.GetDB()
  302. now := time.Now().Unix() * 1000
  303. result := db.Model(xray.ClientTraffic{}).
  304. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  305. Update("enable", false)
  306. err := result.Error
  307. count := result.RowsAffected
  308. return count, err
  309. }
  310. func (s *InboundService) UpdateClientStat(inboundId int, inboundSettings string) error {
  311. db := database.GetDB()
  312. // get settings clients
  313. settings := map[string][]model.Client{}
  314. json.Unmarshal([]byte(inboundSettings), &settings)
  315. clients := settings["clients"]
  316. for _, client := range clients {
  317. result := db.Model(xray.ClientTraffic{}).
  318. Where("inbound_id = ? and email = ?", inboundId, client.Email).
  319. Updates(map[string]interface{}{"enable": true, "total": client.TotalGB, "expiry_time": client.ExpiryTime})
  320. if result.RowsAffected == 0 {
  321. clientTraffic := xray.ClientTraffic{}
  322. clientTraffic.InboundId = inboundId
  323. clientTraffic.Email = client.Email
  324. clientTraffic.Total = client.TotalGB
  325. clientTraffic.ExpiryTime = client.ExpiryTime
  326. clientTraffic.Enable = true
  327. clientTraffic.Up = 0
  328. clientTraffic.Down = 0
  329. db.Create(&clientTraffic)
  330. }
  331. err := result.Error
  332. if err != nil {
  333. return err
  334. }
  335. }
  336. return nil
  337. }
  338. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  339. return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
  340. }
  341. func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
  342. db := database.GetDB()
  343. InboundClientIps := &model.InboundClientIps{}
  344. err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
  345. if err != nil {
  346. return "", err
  347. }
  348. return InboundClientIps.Ips, nil
  349. }
  350. func (s *InboundService) ClearClientIps(clientEmail string) (error) {
  351. db := database.GetDB()
  352. result := db.Model(model.InboundClientIps{}).
  353. Where("client_email = ?", clientEmail).
  354. Update("ips", "")
  355. err := result.Error
  356. if err != nil {
  357. return err
  358. }
  359. return nil
  360. }
  361. func (s *InboundService) ResetClientTraffic(clientEmail string) error {
  362. db := database.GetDB()
  363. result := db.Model(xray.ClientTraffic{}).
  364. Where("email = ?", clientEmail).
  365. Updates(map[string]interface{}{"up": 0, "down": 0})
  366. err := result.Error
  367. if err != nil {
  368. return err
  369. }
  370. return nil
  371. }
  372. func (s *InboundService) GetClientTrafficById(uuid string) (traffic *xray.ClientTraffic, err error) {
  373. db := database.GetDB()
  374. inbound := &model.Inbound{}
  375. traffic = &xray.ClientTraffic{}
  376. err = db.Model(model.Inbound{}).Where("settings like ?", "%"+uuid+"%").First(inbound).Error
  377. if err != nil {
  378. if err == gorm.ErrRecordNotFound {
  379. logger.Warning(err)
  380. return nil, err
  381. }
  382. }
  383. traffic.InboundId = inbound.Id
  384. // get settings clients
  385. settings := map[string][]model.Client{}
  386. json.Unmarshal([]byte(inbound.Settings), &settings)
  387. clients := settings["clients"]
  388. for _, client := range clients {
  389. if uuid == client.ID {
  390. traffic.Email = client.Email
  391. }
  392. }
  393. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  394. if err != nil {
  395. logger.Warning(err)
  396. return nil, err
  397. }
  398. return traffic, err
  399. }