inbound.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "x-ui/database"
  9. "x-ui/database/model"
  10. "x-ui/logger"
  11. "x-ui/util/common"
  12. "x-ui/xray"
  13. "gorm.io/gorm"
  14. )
  15. type InboundService struct {
  16. xrayApi xray.XrayAPI
  17. }
  18. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  19. db := database.GetDB()
  20. var inbounds []*model.Inbound
  21. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Find(&inbounds).Error
  22. if err != nil && err != gorm.ErrRecordNotFound {
  23. return nil, err
  24. }
  25. return inbounds, nil
  26. }
  27. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  28. db := database.GetDB()
  29. var inbounds []*model.Inbound
  30. err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
  31. if err != nil && err != gorm.ErrRecordNotFound {
  32. return nil, err
  33. }
  34. return inbounds, nil
  35. }
  36. func (s *InboundService) checkPortExist(listen string, port int, ignoreId int) (bool, error) {
  37. db := database.GetDB()
  38. if listen == "" || listen == "0.0.0.0" || listen == "::" || listen == "::0" {
  39. db = db.Model(model.Inbound{}).Where("port = ?", port)
  40. } else {
  41. db = db.Model(model.Inbound{}).
  42. Where("port = ?", port).
  43. Where(
  44. db.Model(model.Inbound{}).Where(
  45. "listen = ?", listen,
  46. ).Or(
  47. "listen = \"\"",
  48. ).Or(
  49. "listen = \"0.0.0.0\"",
  50. ).Or(
  51. "listen = \"::\"",
  52. ).Or(
  53. "listen = \"::0\""))
  54. }
  55. if ignoreId > 0 {
  56. db = db.Where("id != ?", ignoreId)
  57. }
  58. var count int64
  59. err := db.Count(&count).Error
  60. if err != nil {
  61. return false, err
  62. }
  63. return count > 0, nil
  64. }
  65. func (s *InboundService) GetClients(inbound *model.Inbound) ([]model.Client, error) {
  66. settings := map[string][]model.Client{}
  67. json.Unmarshal([]byte(inbound.Settings), &settings)
  68. if settings == nil {
  69. return nil, fmt.Errorf("setting is null")
  70. }
  71. clients := settings["clients"]
  72. if clients == nil {
  73. return nil, nil
  74. }
  75. return clients, nil
  76. }
  77. func (s *InboundService) getAllEmails() ([]string, error) {
  78. db := database.GetDB()
  79. var emails []string
  80. err := db.Raw(`
  81. SELECT JSON_EXTRACT(client.value, '$.email')
  82. FROM inbounds,
  83. JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
  84. `).Scan(&emails).Error
  85. if err != nil {
  86. return nil, err
  87. }
  88. return emails, nil
  89. }
  90. func (s *InboundService) contains(slice []string, str string) bool {
  91. for _, s := range slice {
  92. if s == str {
  93. return true
  94. }
  95. }
  96. return false
  97. }
  98. func (s *InboundService) checkEmailsExistForClients(clients []model.Client) (string, error) {
  99. allEmails, err := s.getAllEmails()
  100. if err != nil {
  101. return "", err
  102. }
  103. var emails []string
  104. for _, client := range clients {
  105. if client.Email != "" {
  106. if s.contains(emails, client.Email) {
  107. return client.Email, nil
  108. }
  109. if s.contains(allEmails, client.Email) {
  110. return client.Email, nil
  111. }
  112. emails = append(emails, client.Email)
  113. }
  114. }
  115. return "", nil
  116. }
  117. func (s *InboundService) checkEmailExistForInbound(inbound *model.Inbound) (string, error) {
  118. clients, err := s.GetClients(inbound)
  119. if err != nil {
  120. return "", err
  121. }
  122. allEmails, err := s.getAllEmails()
  123. if err != nil {
  124. return "", err
  125. }
  126. var emails []string
  127. for _, client := range clients {
  128. if client.Email != "" {
  129. if s.contains(emails, client.Email) {
  130. return client.Email, nil
  131. }
  132. if s.contains(allEmails, client.Email) {
  133. return client.Email, nil
  134. }
  135. emails = append(emails, client.Email)
  136. }
  137. }
  138. return "", nil
  139. }
  140. func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  141. exist, err := s.checkPortExist(inbound.Listen, inbound.Port, 0)
  142. if err != nil {
  143. return inbound, false, err
  144. }
  145. if exist {
  146. return inbound, false, common.NewError("Port already exists:", inbound.Port)
  147. }
  148. existEmail, err := s.checkEmailExistForInbound(inbound)
  149. if err != nil {
  150. return inbound, false, err
  151. }
  152. if existEmail != "" {
  153. return inbound, false, common.NewError("Duplicate email:", existEmail)
  154. }
  155. clients, err := s.GetClients(inbound)
  156. if err != nil {
  157. return inbound, false, err
  158. }
  159. // Secure client ID
  160. for _, client := range clients {
  161. if inbound.Protocol == "trojan" {
  162. if client.Password == "" {
  163. return inbound, false, common.NewError("empty client ID")
  164. }
  165. } else if inbound.Protocol == "shadowsocks" {
  166. if client.Email == "" {
  167. return inbound, false, common.NewError("empty client ID")
  168. }
  169. } else {
  170. if client.ID == "" {
  171. return inbound, false, common.NewError("empty client ID")
  172. }
  173. }
  174. }
  175. db := database.GetDB()
  176. tx := db.Begin()
  177. defer func() {
  178. if err == nil {
  179. tx.Commit()
  180. } else {
  181. tx.Rollback()
  182. }
  183. }()
  184. err = tx.Save(inbound).Error
  185. if err == nil {
  186. if len(inbound.ClientStats) == 0 {
  187. for _, client := range clients {
  188. s.AddClientStat(tx, inbound.Id, &client)
  189. }
  190. }
  191. } else {
  192. return inbound, false, err
  193. }
  194. needRestart := false
  195. if inbound.Enable {
  196. s.xrayApi.Init(p.GetAPIPort())
  197. inboundJson, err1 := json.MarshalIndent(inbound.GenXrayInboundConfig(), "", " ")
  198. if err1 != nil {
  199. logger.Debug("Unable to marshal inbound config:", err1)
  200. }
  201. err1 = s.xrayApi.AddInbound(inboundJson)
  202. if err1 == nil {
  203. logger.Debug("New inbound added by api:", inbound.Tag)
  204. } else {
  205. logger.Debug("Unable to add inbound by api:", err1)
  206. needRestart = true
  207. }
  208. s.xrayApi.Close()
  209. }
  210. return inbound, needRestart, err
  211. }
  212. func (s *InboundService) DelInbound(id int) (bool, error) {
  213. db := database.GetDB()
  214. var tag string
  215. needRestart := false
  216. result := db.Model(model.Inbound{}).Select("tag").Where("id = ? and enable = ?", id, true).First(&tag)
  217. if result.Error == nil {
  218. s.xrayApi.Init(p.GetAPIPort())
  219. err1 := s.xrayApi.DelInbound(tag)
  220. if err1 == nil {
  221. logger.Debug("Inbound deleted by api:", tag)
  222. } else {
  223. logger.Debug("Unable to delete inbound by api:", err1)
  224. needRestart = true
  225. }
  226. s.xrayApi.Close()
  227. } else {
  228. logger.Debug("No enabled inbound founded to removing by api", tag)
  229. }
  230. // Delete client traffics of inbounds
  231. err := db.Where("inbound_id = ?", id).Delete(xray.ClientTraffic{}).Error
  232. if err != nil {
  233. return false, err
  234. }
  235. inbound, err := s.GetInbound(id)
  236. if err != nil {
  237. return false, err
  238. }
  239. clients, err := s.GetClients(inbound)
  240. if err != nil {
  241. return false, err
  242. }
  243. for _, client := range clients {
  244. err := s.DelClientIPs(db, client.Email)
  245. if err != nil {
  246. return false, err
  247. }
  248. }
  249. return needRestart, db.Delete(model.Inbound{}, id).Error
  250. }
  251. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  252. db := database.GetDB()
  253. inbound := &model.Inbound{}
  254. err := db.Model(model.Inbound{}).First(inbound, id).Error
  255. if err != nil {
  256. return nil, err
  257. }
  258. return inbound, nil
  259. }
  260. func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  261. exist, err := s.checkPortExist(inbound.Listen, inbound.Port, inbound.Id)
  262. if err != nil {
  263. return inbound, false, err
  264. }
  265. if exist {
  266. return inbound, false, common.NewError("Port already exists:", inbound.Port)
  267. }
  268. oldInbound, err := s.GetInbound(inbound.Id)
  269. if err != nil {
  270. return inbound, false, err
  271. }
  272. tag := oldInbound.Tag
  273. db := database.GetDB()
  274. tx := db.Begin()
  275. defer func() {
  276. if err != nil {
  277. tx.Rollback()
  278. } else {
  279. tx.Commit()
  280. }
  281. }()
  282. err = s.updateClientTraffics(tx, oldInbound, inbound)
  283. if err != nil {
  284. return inbound, false, err
  285. }
  286. oldInbound.Up = inbound.Up
  287. oldInbound.Down = inbound.Down
  288. oldInbound.Total = inbound.Total
  289. oldInbound.Remark = inbound.Remark
  290. oldInbound.Enable = inbound.Enable
  291. oldInbound.ExpiryTime = inbound.ExpiryTime
  292. oldInbound.Listen = inbound.Listen
  293. oldInbound.Port = inbound.Port
  294. oldInbound.Protocol = inbound.Protocol
  295. oldInbound.Settings = inbound.Settings
  296. oldInbound.StreamSettings = inbound.StreamSettings
  297. oldInbound.Sniffing = inbound.Sniffing
  298. if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
  299. oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
  300. } else {
  301. oldInbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
  302. }
  303. needRestart := false
  304. s.xrayApi.Init(p.GetAPIPort())
  305. if s.xrayApi.DelInbound(tag) == nil {
  306. logger.Debug("Old inbound deleted by api:", tag)
  307. }
  308. if inbound.Enable {
  309. inboundJson, err2 := json.MarshalIndent(oldInbound.GenXrayInboundConfig(), "", " ")
  310. if err2 != nil {
  311. logger.Debug("Unable to marshal updated inbound config:", err2)
  312. needRestart = true
  313. } else {
  314. err2 = s.xrayApi.AddInbound(inboundJson)
  315. if err2 == nil {
  316. logger.Debug("Updated inbound added by api:", oldInbound.Tag)
  317. } else {
  318. logger.Debug("Unable to update inbound by api:", err2)
  319. needRestart = true
  320. }
  321. }
  322. }
  323. s.xrayApi.Close()
  324. return inbound, needRestart, tx.Save(oldInbound).Error
  325. }
  326. func (s *InboundService) updateClientTraffics(tx *gorm.DB, oldInbound *model.Inbound, newInbound *model.Inbound) error {
  327. oldClients, err := s.GetClients(oldInbound)
  328. if err != nil {
  329. return err
  330. }
  331. newClients, err := s.GetClients(newInbound)
  332. if err != nil {
  333. return err
  334. }
  335. var emailExists bool
  336. for _, oldClient := range oldClients {
  337. emailExists = false
  338. for _, newClient := range newClients {
  339. if oldClient.Email == newClient.Email {
  340. emailExists = true
  341. break
  342. }
  343. }
  344. if !emailExists {
  345. err = s.DelClientStat(tx, oldClient.Email)
  346. if err != nil {
  347. return err
  348. }
  349. }
  350. }
  351. for _, newClient := range newClients {
  352. emailExists = false
  353. for _, oldClient := range oldClients {
  354. if newClient.Email == oldClient.Email {
  355. emailExists = true
  356. break
  357. }
  358. }
  359. if !emailExists {
  360. err = s.AddClientStat(tx, oldInbound.Id, &newClient)
  361. if err != nil {
  362. return err
  363. }
  364. }
  365. }
  366. return nil
  367. }
  368. func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) {
  369. clients, err := s.GetClients(data)
  370. if err != nil {
  371. return false, err
  372. }
  373. var settings map[string]interface{}
  374. err = json.Unmarshal([]byte(data.Settings), &settings)
  375. if err != nil {
  376. return false, err
  377. }
  378. interfaceClients := settings["clients"].([]interface{})
  379. existEmail, err := s.checkEmailsExistForClients(clients)
  380. if err != nil {
  381. return false, err
  382. }
  383. if existEmail != "" {
  384. return false, common.NewError("Duplicate email:", existEmail)
  385. }
  386. oldInbound, err := s.GetInbound(data.Id)
  387. if err != nil {
  388. return false, err
  389. }
  390. // Secure client ID
  391. for _, client := range clients {
  392. if oldInbound.Protocol == "trojan" {
  393. if client.Password == "" {
  394. return false, common.NewError("empty client ID")
  395. }
  396. } else if oldInbound.Protocol == "shadowsocks" {
  397. if client.Email == "" {
  398. return false, common.NewError("empty client ID")
  399. }
  400. } else {
  401. if client.ID == "" {
  402. return false, common.NewError("empty client ID")
  403. }
  404. }
  405. }
  406. var oldSettings map[string]interface{}
  407. err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  408. if err != nil {
  409. return false, err
  410. }
  411. oldClients := oldSettings["clients"].([]interface{})
  412. oldClients = append(oldClients, interfaceClients...)
  413. oldSettings["clients"] = oldClients
  414. newSettings, err := json.MarshalIndent(oldSettings, "", " ")
  415. if err != nil {
  416. return false, err
  417. }
  418. oldInbound.Settings = string(newSettings)
  419. db := database.GetDB()
  420. tx := db.Begin()
  421. defer func() {
  422. if err != nil {
  423. tx.Rollback()
  424. } else {
  425. tx.Commit()
  426. }
  427. }()
  428. needRestart := false
  429. s.xrayApi.Init(p.GetAPIPort())
  430. for _, client := range clients {
  431. if len(client.Email) > 0 {
  432. s.AddClientStat(tx, data.Id, &client)
  433. if client.Enable {
  434. cipher := ""
  435. if oldInbound.Protocol == "shadowsocks" {
  436. cipher = oldSettings["method"].(string)
  437. }
  438. err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
  439. "email": client.Email,
  440. "id": client.ID,
  441. "security": client.Security,
  442. "flow": client.Flow,
  443. "password": client.Password,
  444. "cipher": cipher,
  445. })
  446. if err1 == nil {
  447. logger.Debug("Client added by api:", client.Email)
  448. } else {
  449. logger.Debug("Error in adding client by api:", err1)
  450. needRestart = true
  451. }
  452. }
  453. } else {
  454. needRestart = true
  455. }
  456. }
  457. s.xrayApi.Close()
  458. return needRestart, tx.Save(oldInbound).Error
  459. }
  460. func (s *InboundService) DelInboundClient(inboundId int, clientId string) (bool, error) {
  461. oldInbound, err := s.GetInbound(inboundId)
  462. if err != nil {
  463. logger.Error("Load Old Data Error")
  464. return false, err
  465. }
  466. var settings map[string]interface{}
  467. err = json.Unmarshal([]byte(oldInbound.Settings), &settings)
  468. if err != nil {
  469. return false, err
  470. }
  471. email := ""
  472. client_key := "id"
  473. if oldInbound.Protocol == "trojan" {
  474. client_key = "password"
  475. }
  476. if oldInbound.Protocol == "shadowsocks" {
  477. client_key = "email"
  478. }
  479. interfaceClients := settings["clients"].([]interface{})
  480. var newClients []interface{}
  481. needApiDel := false
  482. for _, client := range interfaceClients {
  483. c := client.(map[string]interface{})
  484. c_id := c[client_key].(string)
  485. if c_id == clientId {
  486. email, _ = c["email"].(string)
  487. needApiDel, _ = c["enable"].(bool)
  488. } else {
  489. newClients = append(newClients, client)
  490. }
  491. }
  492. if len(newClients) == 0 {
  493. return false, common.NewError("no client remained in Inbound")
  494. }
  495. settings["clients"] = newClients
  496. newSettings, err := json.MarshalIndent(settings, "", " ")
  497. if err != nil {
  498. return false, err
  499. }
  500. oldInbound.Settings = string(newSettings)
  501. db := database.GetDB()
  502. err = s.DelClientIPs(db, email)
  503. if err != nil {
  504. logger.Error("Error in delete client IPs")
  505. return false, err
  506. }
  507. needRestart := false
  508. if len(email) > 0 {
  509. notDepleted := true
  510. err = db.Model(xray.ClientTraffic{}).Select("enable").Where("email = ?", email).First(&notDepleted).Error
  511. if err != nil {
  512. logger.Error("Get stats error")
  513. return false, err
  514. }
  515. err = s.DelClientStat(db, email)
  516. if err != nil {
  517. logger.Error("Delete stats Data Error")
  518. return false, err
  519. }
  520. if needApiDel && notDepleted {
  521. s.xrayApi.Init(p.GetAPIPort())
  522. err1 := s.xrayApi.RemoveUser(oldInbound.Tag, email)
  523. if err1 == nil {
  524. logger.Debug("Client deleted by api:", email)
  525. needRestart = false
  526. } else {
  527. logger.Debug("Unable to del client by api:", err1)
  528. needRestart = true
  529. }
  530. s.xrayApi.Close()
  531. }
  532. }
  533. return needRestart, db.Save(oldInbound).Error
  534. }
  535. func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) (bool, error) {
  536. clients, err := s.GetClients(data)
  537. if err != nil {
  538. return false, err
  539. }
  540. var settings map[string]interface{}
  541. err = json.Unmarshal([]byte(data.Settings), &settings)
  542. if err != nil {
  543. return false, err
  544. }
  545. interfaceClients := settings["clients"].([]interface{})
  546. oldInbound, err := s.GetInbound(data.Id)
  547. if err != nil {
  548. return false, err
  549. }
  550. oldClients, err := s.GetClients(oldInbound)
  551. if err != nil {
  552. return false, err
  553. }
  554. oldEmail := ""
  555. newClientId := ""
  556. clientIndex := -1
  557. for index, oldClient := range oldClients {
  558. oldClientId := ""
  559. if oldInbound.Protocol == "trojan" {
  560. oldClientId = oldClient.Password
  561. newClientId = clients[0].Password
  562. } else if oldInbound.Protocol == "shadowsocks" {
  563. oldClientId = oldClient.Email
  564. newClientId = clients[0].Email
  565. } else {
  566. oldClientId = oldClient.ID
  567. newClientId = clients[0].ID
  568. }
  569. if clientId == oldClientId {
  570. oldEmail = oldClient.Email
  571. clientIndex = index
  572. break
  573. }
  574. }
  575. // Validate new client ID
  576. if newClientId == "" || clientIndex == -1 {
  577. return false, common.NewError("empty client ID")
  578. }
  579. if len(clients[0].Email) > 0 && clients[0].Email != oldEmail {
  580. existEmail, err := s.checkEmailsExistForClients(clients)
  581. if err != nil {
  582. return false, err
  583. }
  584. if existEmail != "" {
  585. return false, common.NewError("Duplicate email:", existEmail)
  586. }
  587. }
  588. var oldSettings map[string]interface{}
  589. err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  590. if err != nil {
  591. return false, err
  592. }
  593. settingsClients := oldSettings["clients"].([]interface{})
  594. settingsClients[clientIndex] = interfaceClients[0]
  595. oldSettings["clients"] = settingsClients
  596. newSettings, err := json.MarshalIndent(oldSettings, "", " ")
  597. if err != nil {
  598. return false, err
  599. }
  600. oldInbound.Settings = string(newSettings)
  601. db := database.GetDB()
  602. tx := db.Begin()
  603. defer func() {
  604. if err != nil {
  605. tx.Rollback()
  606. } else {
  607. tx.Commit()
  608. }
  609. }()
  610. if len(clients[0].Email) > 0 {
  611. if len(oldEmail) > 0 {
  612. err = s.UpdateClientStat(tx, oldEmail, &clients[0])
  613. if err != nil {
  614. return false, err
  615. }
  616. err = s.UpdateClientIPs(tx, oldEmail, clients[0].Email)
  617. if err != nil {
  618. return false, err
  619. }
  620. } else {
  621. s.AddClientStat(tx, data.Id, &clients[0])
  622. }
  623. } else {
  624. err = s.DelClientStat(tx, oldEmail)
  625. if err != nil {
  626. return false, err
  627. }
  628. err = s.DelClientIPs(tx, oldEmail)
  629. if err != nil {
  630. return false, err
  631. }
  632. }
  633. needRestart := false
  634. if len(oldEmail) > 0 {
  635. s.xrayApi.Init(p.GetAPIPort())
  636. if oldClients[clientIndex].Enable {
  637. err1 := s.xrayApi.RemoveUser(oldInbound.Tag, oldEmail)
  638. if err1 == nil {
  639. logger.Debug("Old client deleted by api:", clients[0].Email)
  640. } else {
  641. logger.Debug("Error in deleting client by api:", err1)
  642. needRestart = true
  643. }
  644. }
  645. if clients[0].Enable {
  646. cipher := ""
  647. if oldInbound.Protocol == "shadowsocks" {
  648. cipher = oldSettings["method"].(string)
  649. }
  650. err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
  651. "email": clients[0].Email,
  652. "id": clients[0].ID,
  653. "security": clients[0].Security,
  654. "flow": clients[0].Flow,
  655. "password": clients[0].Password,
  656. "cipher": cipher,
  657. })
  658. if err1 == nil {
  659. logger.Debug("Client edited by api:", clients[0].Email)
  660. } else {
  661. logger.Debug("Error in adding client by api:", err1)
  662. needRestart = true
  663. }
  664. }
  665. s.xrayApi.Close()
  666. } else {
  667. logger.Debug("Client old email not found")
  668. needRestart = true
  669. }
  670. return needRestart, tx.Save(oldInbound).Error
  671. }
  672. func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) {
  673. var err error
  674. db := database.GetDB()
  675. tx := db.Begin()
  676. defer func() {
  677. if err != nil {
  678. tx.Rollback()
  679. } else {
  680. tx.Commit()
  681. }
  682. }()
  683. err = s.addInboundTraffic(tx, inboundTraffics)
  684. if err != nil {
  685. return err, false
  686. }
  687. err = s.addClientTraffic(tx, clientTraffics)
  688. if err != nil {
  689. return err, false
  690. }
  691. needRestart0, count, err := s.autoRenewClients(tx)
  692. if err != nil {
  693. logger.Warning("Error in renew clients:", err)
  694. } else if count > 0 {
  695. logger.Debugf("%v clients renewed", count)
  696. }
  697. needRestart1, count, err := s.disableInvalidClients(tx)
  698. if err != nil {
  699. logger.Warning("Error in disabling invalid clients:", err)
  700. } else if count > 0 {
  701. logger.Debugf("%v clients disabled", count)
  702. }
  703. needRestart2, count, err := s.disableInvalidInbounds(tx)
  704. if err != nil {
  705. logger.Warning("Error in disabling invalid inbounds:", err)
  706. } else if count > 0 {
  707. logger.Debugf("%v inbounds disabled", count)
  708. }
  709. return nil, (needRestart0 || needRestart1 || needRestart2)
  710. }
  711. func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  712. if len(traffics) == 0 {
  713. return nil
  714. }
  715. var err error
  716. for _, traffic := range traffics {
  717. if traffic.IsInbound {
  718. err = tx.Model(&model.Inbound{}).Where("tag = ?", traffic.Tag).
  719. Updates(map[string]interface{}{
  720. "up": gorm.Expr("up + ?", traffic.Up),
  721. "down": gorm.Expr("down + ?", traffic.Down),
  722. }).Error
  723. if err != nil {
  724. return err
  725. }
  726. }
  727. }
  728. return nil
  729. }
  730. func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
  731. if len(traffics) == 0 {
  732. // Empty onlineUsers
  733. if p != nil {
  734. p.SetOnlineClients(nil)
  735. }
  736. return nil
  737. }
  738. var onlineClients []string
  739. emails := make([]string, 0, len(traffics))
  740. for _, traffic := range traffics {
  741. emails = append(emails, traffic.Email)
  742. }
  743. dbClientTraffics := make([]*xray.ClientTraffic, 0, len(traffics))
  744. err = tx.Model(xray.ClientTraffic{}).Where("email IN (?)", emails).Find(&dbClientTraffics).Error
  745. if err != nil {
  746. return err
  747. }
  748. // Avoid empty slice error
  749. if len(dbClientTraffics) == 0 {
  750. return nil
  751. }
  752. dbClientTraffics, err = s.adjustTraffics(tx, dbClientTraffics)
  753. if err != nil {
  754. return err
  755. }
  756. for dbTraffic_index := range dbClientTraffics {
  757. for traffic_index := range traffics {
  758. if dbClientTraffics[dbTraffic_index].Email == traffics[traffic_index].Email {
  759. dbClientTraffics[dbTraffic_index].Up += traffics[traffic_index].Up
  760. dbClientTraffics[dbTraffic_index].Down += traffics[traffic_index].Down
  761. // Add user in onlineUsers array on traffic
  762. if traffics[traffic_index].Up+traffics[traffic_index].Down > 0 {
  763. onlineClients = append(onlineClients, traffics[traffic_index].Email)
  764. }
  765. break
  766. }
  767. }
  768. }
  769. // Set onlineUsers
  770. p.SetOnlineClients(onlineClients)
  771. err = tx.Save(dbClientTraffics).Error
  772. if err != nil {
  773. logger.Warning("AddClientTraffic update data ", err)
  774. }
  775. return nil
  776. }
  777. func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
  778. inboundIds := make([]int, 0, len(dbClientTraffics))
  779. for _, dbClientTraffic := range dbClientTraffics {
  780. if dbClientTraffic.ExpiryTime < 0 {
  781. inboundIds = append(inboundIds, dbClientTraffic.InboundId)
  782. }
  783. }
  784. if len(inboundIds) > 0 {
  785. var inbounds []*model.Inbound
  786. err := tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
  787. if err != nil {
  788. return nil, err
  789. }
  790. for inbound_index := range inbounds {
  791. settings := map[string]interface{}{}
  792. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  793. clients, ok := settings["clients"].([]interface{})
  794. if ok {
  795. var newClients []interface{}
  796. for client_index := range clients {
  797. c := clients[client_index].(map[string]interface{})
  798. for traffic_index := range dbClientTraffics {
  799. if dbClientTraffics[traffic_index].ExpiryTime < 0 && c["email"] == dbClientTraffics[traffic_index].Email {
  800. oldExpiryTime := c["expiryTime"].(float64)
  801. newExpiryTime := (time.Now().Unix() * 1000) - int64(oldExpiryTime)
  802. c["expiryTime"] = newExpiryTime
  803. dbClientTraffics[traffic_index].ExpiryTime = newExpiryTime
  804. break
  805. }
  806. }
  807. newClients = append(newClients, interface{}(c))
  808. }
  809. settings["clients"] = newClients
  810. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  811. if err != nil {
  812. return nil, err
  813. }
  814. inbounds[inbound_index].Settings = string(modifiedSettings)
  815. }
  816. }
  817. err = tx.Save(inbounds).Error
  818. if err != nil {
  819. logger.Warning("AddClientTraffic update inbounds ", err)
  820. logger.Error(inbounds)
  821. }
  822. }
  823. return dbClientTraffics, nil
  824. }
  825. func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
  826. // check for time expired
  827. var traffics []*xray.ClientTraffic
  828. now := time.Now().Unix() * 1000
  829. var err, err1 error
  830. err = tx.Model(xray.ClientTraffic{}).Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).Find(&traffics).Error
  831. if err != nil {
  832. return false, 0, err
  833. }
  834. // return if there is no client to renew
  835. if len(traffics) == 0 {
  836. return false, 0, nil
  837. }
  838. var inbound_ids []int
  839. var inbounds []*model.Inbound
  840. needRestart := false
  841. var clientsToAdd []struct {
  842. protocol string
  843. tag string
  844. client map[string]interface{}
  845. }
  846. for _, traffic := range traffics {
  847. inbound_ids = append(inbound_ids, traffic.InboundId)
  848. }
  849. err = tx.Model(model.Inbound{}).Where("id IN ?", inbound_ids).Find(&inbounds).Error
  850. if err != nil {
  851. return false, 0, err
  852. }
  853. for inbound_index := range inbounds {
  854. settings := map[string]interface{}{}
  855. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  856. clients := settings["clients"].([]interface{})
  857. for client_index := range clients {
  858. c := clients[client_index].(map[string]interface{})
  859. for traffic_index, traffic := range traffics {
  860. if traffic.Email == c["email"].(string) {
  861. newExpiryTime := traffic.ExpiryTime
  862. for newExpiryTime < now {
  863. newExpiryTime += (int64(traffic.Reset) * 86400000)
  864. }
  865. c["expiryTime"] = newExpiryTime
  866. traffics[traffic_index].ExpiryTime = newExpiryTime
  867. traffics[traffic_index].Down = 0
  868. traffics[traffic_index].Up = 0
  869. if !traffic.Enable {
  870. traffics[traffic_index].Enable = true
  871. clientsToAdd = append(clientsToAdd,
  872. struct {
  873. protocol string
  874. tag string
  875. client map[string]interface{}
  876. }{
  877. protocol: string(inbounds[inbound_index].Protocol),
  878. tag: inbounds[inbound_index].Tag,
  879. client: c,
  880. })
  881. }
  882. clients[client_index] = interface{}(c)
  883. break
  884. }
  885. }
  886. }
  887. settings["clients"] = clients
  888. newSettings, err := json.MarshalIndent(settings, "", " ")
  889. if err != nil {
  890. return false, 0, err
  891. }
  892. inbounds[inbound_index].Settings = string(newSettings)
  893. }
  894. err = tx.Save(inbounds).Error
  895. if err != nil {
  896. return false, 0, err
  897. }
  898. err = tx.Save(traffics).Error
  899. if err != nil {
  900. return false, 0, err
  901. }
  902. if p != nil {
  903. err1 = s.xrayApi.Init(p.GetAPIPort())
  904. if err1 != nil {
  905. return true, int64(len(traffics)), nil
  906. }
  907. for _, clientToAdd := range clientsToAdd {
  908. err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
  909. if err1 != nil {
  910. needRestart = true
  911. }
  912. }
  913. s.xrayApi.Close()
  914. }
  915. return needRestart, int64(len(traffics)), nil
  916. }
  917. func (s *InboundService) disableInvalidInbounds(tx *gorm.DB) (bool, int64, error) {
  918. now := time.Now().Unix() * 1000
  919. needRestart := false
  920. if p != nil {
  921. var tags []string
  922. err := tx.Table("inbounds").
  923. Select("inbounds.tag").
  924. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  925. Scan(&tags).Error
  926. if err != nil {
  927. return false, 0, err
  928. }
  929. s.xrayApi.Init(p.GetAPIPort())
  930. for _, tag := range tags {
  931. err1 := s.xrayApi.DelInbound(tag)
  932. if err1 == nil {
  933. logger.Debug("Inbound disabled by api:", tag)
  934. } else {
  935. logger.Debug("Error in disabling inbound by api:", err1)
  936. needRestart = true
  937. }
  938. }
  939. s.xrayApi.Close()
  940. }
  941. result := tx.Model(model.Inbound{}).
  942. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  943. Update("enable", false)
  944. err := result.Error
  945. count := result.RowsAffected
  946. return needRestart, count, err
  947. }
  948. func (s *InboundService) disableInvalidClients(tx *gorm.DB) (bool, int64, error) {
  949. now := time.Now().Unix() * 1000
  950. needRestart := false
  951. if p != nil {
  952. var results []struct {
  953. Tag string
  954. Email string
  955. }
  956. err := tx.Table("inbounds").
  957. Select("inbounds.tag, client_traffics.email").
  958. Joins("JOIN client_traffics ON inbounds.id = client_traffics.inbound_id").
  959. Where("((client_traffics.total > 0 AND client_traffics.up + client_traffics.down >= client_traffics.total) OR (client_traffics.expiry_time > 0 AND client_traffics.expiry_time <= ?)) AND client_traffics.enable = ?", now, true).
  960. Scan(&results).Error
  961. if err != nil {
  962. return false, 0, err
  963. }
  964. s.xrayApi.Init(p.GetAPIPort())
  965. for _, result := range results {
  966. err1 := s.xrayApi.RemoveUser(result.Tag, result.Email)
  967. if err1 == nil {
  968. logger.Debug("Client disabled by api:", result.Email)
  969. } else {
  970. logger.Debug("Error in disabling client by api:", err1)
  971. needRestart = true
  972. }
  973. }
  974. s.xrayApi.Close()
  975. }
  976. result := tx.Model(xray.ClientTraffic{}).
  977. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  978. Update("enable", false)
  979. err := result.Error
  980. count := result.RowsAffected
  981. return needRestart, count, err
  982. }
  983. func (s *InboundService) GetInboundTags() (string, error) {
  984. db := database.GetDB()
  985. var inboundTags []string
  986. err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
  987. if err != nil && err != gorm.ErrRecordNotFound {
  988. return "", err
  989. }
  990. tags, _ := json.Marshal(inboundTags)
  991. return string(tags), nil
  992. }
  993. func (s *InboundService) MigrationRemoveOrphanedTraffics() {
  994. db := database.GetDB()
  995. db.Exec(`
  996. DELETE FROM client_traffics
  997. WHERE email NOT IN (
  998. SELECT JSON_EXTRACT(client.value, '$.email')
  999. FROM inbounds,
  1000. JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
  1001. )
  1002. `)
  1003. }
  1004. func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
  1005. clientTraffic := xray.ClientTraffic{}
  1006. clientTraffic.InboundId = inboundId
  1007. clientTraffic.Email = client.Email
  1008. clientTraffic.Total = client.TotalGB
  1009. clientTraffic.ExpiryTime = client.ExpiryTime
  1010. clientTraffic.Enable = true
  1011. clientTraffic.Up = 0
  1012. clientTraffic.Down = 0
  1013. clientTraffic.Reset = client.Reset
  1014. result := tx.Create(&clientTraffic)
  1015. err := result.Error
  1016. return err
  1017. }
  1018. func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
  1019. result := tx.Model(xray.ClientTraffic{}).
  1020. Where("email = ?", email).
  1021. Updates(map[string]interface{}{
  1022. "enable": true,
  1023. "email": client.Email,
  1024. "total": client.TotalGB,
  1025. "expiry_time": client.ExpiryTime,
  1026. "reset": client.Reset,
  1027. })
  1028. err := result.Error
  1029. return err
  1030. }
  1031. func (s *InboundService) UpdateClientIPs(tx *gorm.DB, oldEmail string, newEmail string) error {
  1032. return tx.Model(model.InboundClientIps{}).Where("client_email = ?", oldEmail).Update("client_email", newEmail).Error
  1033. }
  1034. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  1035. return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
  1036. }
  1037. func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error {
  1038. return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error
  1039. }
  1040. func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  1041. db := database.GetDB()
  1042. var traffics []*xray.ClientTraffic
  1043. err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
  1044. if err != nil {
  1045. logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
  1046. return nil, nil, err
  1047. }
  1048. if len(traffics) > 0 {
  1049. inbound, err = s.GetInbound(traffics[0].InboundId)
  1050. return traffics[0], inbound, err
  1051. }
  1052. return nil, nil, nil
  1053. }
  1054. func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  1055. db := database.GetDB()
  1056. var traffics []*xray.ClientTraffic
  1057. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  1058. if err != nil {
  1059. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  1060. return nil, nil, err
  1061. }
  1062. if len(traffics) > 0 {
  1063. inbound, err = s.GetInbound(traffics[0].InboundId)
  1064. return traffics[0], inbound, err
  1065. }
  1066. return nil, nil, nil
  1067. }
  1068. func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
  1069. traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1070. if err != nil {
  1071. return nil, nil, err
  1072. }
  1073. if inbound == nil {
  1074. return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
  1075. }
  1076. clients, err := s.GetClients(inbound)
  1077. if err != nil {
  1078. return nil, nil, err
  1079. }
  1080. for _, client := range clients {
  1081. if client.Email == clientEmail {
  1082. return traffic, &client, nil
  1083. }
  1084. }
  1085. return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
  1086. }
  1087. func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId int64) (bool, error) {
  1088. traffic, inbound, err := s.GetClientInboundByTrafficID(trafficId)
  1089. if err != nil {
  1090. return false, err
  1091. }
  1092. if inbound == nil {
  1093. return false, common.NewError("Inbound Not Found For Traffic ID:", trafficId)
  1094. }
  1095. clientEmail := traffic.Email
  1096. oldClients, err := s.GetClients(inbound)
  1097. if err != nil {
  1098. return false, err
  1099. }
  1100. clientId := ""
  1101. for _, oldClient := range oldClients {
  1102. if oldClient.Email == clientEmail {
  1103. if inbound.Protocol == "trojan" {
  1104. clientId = oldClient.Password
  1105. } else if inbound.Protocol == "shadowsocks" {
  1106. clientId = oldClient.Email
  1107. } else {
  1108. clientId = oldClient.ID
  1109. }
  1110. break
  1111. }
  1112. }
  1113. if len(clientId) == 0 {
  1114. return false, common.NewError("Client Not Found For Email:", clientEmail)
  1115. }
  1116. var settings map[string]interface{}
  1117. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  1118. if err != nil {
  1119. return false, err
  1120. }
  1121. clients := settings["clients"].([]interface{})
  1122. var newClients []interface{}
  1123. for client_index := range clients {
  1124. c := clients[client_index].(map[string]interface{})
  1125. if c["email"] == clientEmail {
  1126. c["tgId"] = tgId
  1127. newClients = append(newClients, interface{}(c))
  1128. }
  1129. }
  1130. settings["clients"] = newClients
  1131. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1132. if err != nil {
  1133. return false, err
  1134. }
  1135. inbound.Settings = string(modifiedSettings)
  1136. needRestart, err := s.UpdateInboundClient(inbound, clientId)
  1137. return needRestart, err
  1138. }
  1139. func (s *InboundService) checkIsEnabledByEmail(clientEmail string) (bool, error) {
  1140. _, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1141. if err != nil {
  1142. return false, err
  1143. }
  1144. if inbound == nil {
  1145. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  1146. }
  1147. clients, err := s.GetClients(inbound)
  1148. if err != nil {
  1149. return false, err
  1150. }
  1151. isEnable := false
  1152. for _, client := range clients {
  1153. if client.Email == clientEmail {
  1154. isEnable = client.Enable
  1155. break
  1156. }
  1157. }
  1158. return isEnable, err
  1159. }
  1160. func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, bool, error) {
  1161. _, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1162. if err != nil {
  1163. return false, false, err
  1164. }
  1165. if inbound == nil {
  1166. return false, false, common.NewError("Inbound Not Found For Email:", clientEmail)
  1167. }
  1168. oldClients, err := s.GetClients(inbound)
  1169. if err != nil {
  1170. return false, false, err
  1171. }
  1172. clientId := ""
  1173. clientOldEnabled := false
  1174. for _, oldClient := range oldClients {
  1175. if oldClient.Email == clientEmail {
  1176. if inbound.Protocol == "trojan" {
  1177. clientId = oldClient.Password
  1178. } else if inbound.Protocol == "shadowsocks" {
  1179. clientId = oldClient.Email
  1180. } else {
  1181. clientId = oldClient.ID
  1182. }
  1183. clientOldEnabled = oldClient.Enable
  1184. break
  1185. }
  1186. }
  1187. if len(clientId) == 0 {
  1188. return false, false, common.NewError("Client Not Found For Email:", clientEmail)
  1189. }
  1190. var settings map[string]interface{}
  1191. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  1192. if err != nil {
  1193. return false, false, err
  1194. }
  1195. clients := settings["clients"].([]interface{})
  1196. var newClients []interface{}
  1197. for client_index := range clients {
  1198. c := clients[client_index].(map[string]interface{})
  1199. if c["email"] == clientEmail {
  1200. c["enable"] = !clientOldEnabled
  1201. newClients = append(newClients, interface{}(c))
  1202. }
  1203. }
  1204. settings["clients"] = newClients
  1205. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1206. if err != nil {
  1207. return false, false, err
  1208. }
  1209. inbound.Settings = string(modifiedSettings)
  1210. needRestart, err := s.UpdateInboundClient(inbound, clientId)
  1211. if err != nil {
  1212. return false, needRestart, err
  1213. }
  1214. return !clientOldEnabled, needRestart, nil
  1215. }
  1216. func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) (bool, error) {
  1217. _, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1218. if err != nil {
  1219. return false, err
  1220. }
  1221. if inbound == nil {
  1222. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  1223. }
  1224. oldClients, err := s.GetClients(inbound)
  1225. if err != nil {
  1226. return false, err
  1227. }
  1228. clientId := ""
  1229. for _, oldClient := range oldClients {
  1230. if oldClient.Email == clientEmail {
  1231. if inbound.Protocol == "trojan" {
  1232. clientId = oldClient.Password
  1233. } else if inbound.Protocol == "shadowsocks" {
  1234. clientId = oldClient.Email
  1235. } else {
  1236. clientId = oldClient.ID
  1237. }
  1238. break
  1239. }
  1240. }
  1241. if len(clientId) == 0 {
  1242. return false, common.NewError("Client Not Found For Email:", clientEmail)
  1243. }
  1244. var settings map[string]interface{}
  1245. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  1246. if err != nil {
  1247. return false, err
  1248. }
  1249. clients := settings["clients"].([]interface{})
  1250. var newClients []interface{}
  1251. for client_index := range clients {
  1252. c := clients[client_index].(map[string]interface{})
  1253. if c["email"] == clientEmail {
  1254. c["limitIp"] = count
  1255. newClients = append(newClients, interface{}(c))
  1256. }
  1257. }
  1258. settings["clients"] = newClients
  1259. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1260. if err != nil {
  1261. return false, err
  1262. }
  1263. inbound.Settings = string(modifiedSettings)
  1264. needRestart, err := s.UpdateInboundClient(inbound, clientId)
  1265. return needRestart, err
  1266. }
  1267. func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) (bool, error) {
  1268. _, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1269. if err != nil {
  1270. return false, err
  1271. }
  1272. if inbound == nil {
  1273. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  1274. }
  1275. oldClients, err := s.GetClients(inbound)
  1276. if err != nil {
  1277. return false, err
  1278. }
  1279. clientId := ""
  1280. for _, oldClient := range oldClients {
  1281. if oldClient.Email == clientEmail {
  1282. if inbound.Protocol == "trojan" {
  1283. clientId = oldClient.Password
  1284. } else if inbound.Protocol == "shadowsocks" {
  1285. clientId = oldClient.Email
  1286. } else {
  1287. clientId = oldClient.ID
  1288. }
  1289. break
  1290. }
  1291. }
  1292. if len(clientId) == 0 {
  1293. return false, common.NewError("Client Not Found For Email:", clientEmail)
  1294. }
  1295. var settings map[string]interface{}
  1296. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  1297. if err != nil {
  1298. return false, err
  1299. }
  1300. clients := settings["clients"].([]interface{})
  1301. var newClients []interface{}
  1302. for client_index := range clients {
  1303. c := clients[client_index].(map[string]interface{})
  1304. if c["email"] == clientEmail {
  1305. c["expiryTime"] = expiry_time
  1306. newClients = append(newClients, interface{}(c))
  1307. }
  1308. }
  1309. settings["clients"] = newClients
  1310. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1311. if err != nil {
  1312. return false, err
  1313. }
  1314. inbound.Settings = string(modifiedSettings)
  1315. needRestart, err := s.UpdateInboundClient(inbound, clientId)
  1316. return needRestart, err
  1317. }
  1318. func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, totalGB int) (bool, error) {
  1319. if totalGB < 0 {
  1320. return false, common.NewError("totalGB must be >= 0")
  1321. }
  1322. _, inbound, err := s.GetClientInboundByEmail(clientEmail)
  1323. if err != nil {
  1324. return false, err
  1325. }
  1326. if inbound == nil {
  1327. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  1328. }
  1329. oldClients, err := s.GetClients(inbound)
  1330. if err != nil {
  1331. return false, err
  1332. }
  1333. clientId := ""
  1334. for _, oldClient := range oldClients {
  1335. if oldClient.Email == clientEmail {
  1336. if inbound.Protocol == "trojan" {
  1337. clientId = oldClient.Password
  1338. } else if inbound.Protocol == "shadowsocks" {
  1339. clientId = oldClient.Email
  1340. } else {
  1341. clientId = oldClient.ID
  1342. }
  1343. break
  1344. }
  1345. }
  1346. if len(clientId) == 0 {
  1347. return false, common.NewError("Client Not Found For Email:", clientEmail)
  1348. }
  1349. var settings map[string]interface{}
  1350. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  1351. if err != nil {
  1352. return false, err
  1353. }
  1354. clients := settings["clients"].([]interface{})
  1355. var newClients []interface{}
  1356. for client_index := range clients {
  1357. c := clients[client_index].(map[string]interface{})
  1358. if c["email"] == clientEmail {
  1359. c["totalGB"] = totalGB * 1024 * 1024 * 1024
  1360. newClients = append(newClients, interface{}(c))
  1361. }
  1362. }
  1363. settings["clients"] = newClients
  1364. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1365. if err != nil {
  1366. return false, err
  1367. }
  1368. inbound.Settings = string(modifiedSettings)
  1369. needRestart, err := s.UpdateInboundClient(inbound, clientId)
  1370. return needRestart, err
  1371. }
  1372. func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
  1373. db := database.GetDB()
  1374. result := db.Model(xray.ClientTraffic{}).
  1375. Where("email = ?", clientEmail).
  1376. Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
  1377. err := result.Error
  1378. if err != nil {
  1379. return err
  1380. }
  1381. return nil
  1382. }
  1383. func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (bool, error) {
  1384. needRestart := false
  1385. traffic, err := s.GetClientTrafficByEmail(clientEmail)
  1386. if err != nil {
  1387. return false, err
  1388. }
  1389. if !traffic.Enable {
  1390. inbound, err := s.GetInbound(id)
  1391. if err != nil {
  1392. return false, err
  1393. }
  1394. clients, err := s.GetClients(inbound)
  1395. if err != nil {
  1396. return false, err
  1397. }
  1398. for _, client := range clients {
  1399. if client.Email == clientEmail {
  1400. s.xrayApi.Init(p.GetAPIPort())
  1401. cipher := ""
  1402. if string(inbound.Protocol) == "shadowsocks" {
  1403. var oldSettings map[string]interface{}
  1404. err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
  1405. if err != nil {
  1406. return false, err
  1407. }
  1408. cipher = oldSettings["method"].(string)
  1409. }
  1410. err1 := s.xrayApi.AddUser(string(inbound.Protocol), inbound.Tag, map[string]interface{}{
  1411. "email": client.Email,
  1412. "id": client.ID,
  1413. "security": client.Security,
  1414. "flow": client.Flow,
  1415. "password": client.Password,
  1416. "cipher": cipher,
  1417. })
  1418. if err1 == nil {
  1419. logger.Debug("Client enabled due to reset traffic:", clientEmail)
  1420. } else {
  1421. logger.Debug("Error in enabling client by api:", err1)
  1422. needRestart = true
  1423. }
  1424. s.xrayApi.Close()
  1425. break
  1426. }
  1427. }
  1428. }
  1429. traffic.Up = 0
  1430. traffic.Down = 0
  1431. traffic.Enable = true
  1432. db := database.GetDB()
  1433. err = db.Save(traffic).Error
  1434. if err != nil {
  1435. return false, err
  1436. }
  1437. return needRestart, nil
  1438. }
  1439. func (s *InboundService) ResetAllClientTraffics(id int) error {
  1440. db := database.GetDB()
  1441. whereText := "inbound_id "
  1442. if id == -1 {
  1443. whereText += " > ?"
  1444. } else {
  1445. whereText += " = ?"
  1446. }
  1447. result := db.Model(xray.ClientTraffic{}).
  1448. Where(whereText, id).
  1449. Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
  1450. err := result.Error
  1451. return err
  1452. }
  1453. func (s *InboundService) ResetAllTraffics() error {
  1454. db := database.GetDB()
  1455. result := db.Model(model.Inbound{}).
  1456. Where("user_id > ?", 0).
  1457. Updates(map[string]interface{}{"up": 0, "down": 0})
  1458. err := result.Error
  1459. return err
  1460. }
  1461. func (s *InboundService) DelDepletedClients(id int) (err error) {
  1462. db := database.GetDB()
  1463. tx := db.Begin()
  1464. defer func() {
  1465. if err == nil {
  1466. tx.Commit()
  1467. } else {
  1468. tx.Rollback()
  1469. }
  1470. }()
  1471. whereText := "reset = 0 and inbound_id "
  1472. if id < 0 {
  1473. whereText += "> ?"
  1474. } else {
  1475. whereText += "= ?"
  1476. }
  1477. depletedClients := []xray.ClientTraffic{}
  1478. err = db.Model(xray.ClientTraffic{}).Where(whereText+" and enable = ?", id, false).Select("inbound_id, GROUP_CONCAT(email) as email").Group("inbound_id").Find(&depletedClients).Error
  1479. if err != nil {
  1480. return err
  1481. }
  1482. for _, depletedClient := range depletedClients {
  1483. emails := strings.Split(depletedClient.Email, ",")
  1484. oldInbound, err := s.GetInbound(depletedClient.InboundId)
  1485. if err != nil {
  1486. return err
  1487. }
  1488. var oldSettings map[string]interface{}
  1489. err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  1490. if err != nil {
  1491. return err
  1492. }
  1493. oldClients := oldSettings["clients"].([]interface{})
  1494. var newClients []interface{}
  1495. for _, client := range oldClients {
  1496. deplete := false
  1497. c := client.(map[string]interface{})
  1498. for _, email := range emails {
  1499. if email == c["email"].(string) {
  1500. deplete = true
  1501. break
  1502. }
  1503. }
  1504. if !deplete {
  1505. newClients = append(newClients, client)
  1506. }
  1507. }
  1508. if len(newClients) > 0 {
  1509. oldSettings["clients"] = newClients
  1510. newSettings, err := json.MarshalIndent(oldSettings, "", " ")
  1511. if err != nil {
  1512. return err
  1513. }
  1514. oldInbound.Settings = string(newSettings)
  1515. err = tx.Save(oldInbound).Error
  1516. if err != nil {
  1517. return err
  1518. }
  1519. } else {
  1520. // Delete inbound if no client remains
  1521. s.DelInbound(depletedClient.InboundId)
  1522. }
  1523. }
  1524. err = tx.Where(whereText+" and enable = ?", id, false).Delete(xray.ClientTraffic{}).Error
  1525. if err != nil {
  1526. return err
  1527. }
  1528. return nil
  1529. }
  1530. func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
  1531. db := database.GetDB()
  1532. var inbounds []*model.Inbound
  1533. // Retrieve inbounds where settings contain the given tgId
  1534. err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
  1535. if err != nil && err != gorm.ErrRecordNotFound {
  1536. logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
  1537. return nil, err
  1538. }
  1539. var emails []string
  1540. for _, inbound := range inbounds {
  1541. clients, err := s.GetClients(inbound)
  1542. if err != nil {
  1543. logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
  1544. continue
  1545. }
  1546. for _, client := range clients {
  1547. if client.TgID == tgId {
  1548. emails = append(emails, client.Email)
  1549. }
  1550. }
  1551. }
  1552. var traffics []*xray.ClientTraffic
  1553. err = db.Model(xray.ClientTraffic{}).Where("email IN ?", emails).Find(&traffics).Error
  1554. if err != nil {
  1555. if err == gorm.ErrRecordNotFound {
  1556. logger.Warning("No ClientTraffic records found for emails:", emails)
  1557. return nil, nil
  1558. }
  1559. logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", emails, err)
  1560. return nil, err
  1561. }
  1562. return traffics, nil
  1563. }
  1564. func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
  1565. db := database.GetDB()
  1566. var traffics []*xray.ClientTraffic
  1567. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  1568. if err != nil {
  1569. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  1570. return nil, err
  1571. }
  1572. if len(traffics) > 0 {
  1573. return traffics[0], nil
  1574. }
  1575. return nil, nil
  1576. }
  1577. func (s *InboundService) GetClientTrafficByID(id string) ([]xray.ClientTraffic, error) {
  1578. db := database.GetDB()
  1579. var traffics []xray.ClientTraffic
  1580. err := db.Model(xray.ClientTraffic{}).Where(`email IN(
  1581. SELECT JSON_EXTRACT(client.value, '$.email') as email
  1582. FROM inbounds,
  1583. JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
  1584. WHERE
  1585. JSON_EXTRACT(client.value, '$.id') in (?)
  1586. )`, id).Find(&traffics).Error
  1587. if err != nil {
  1588. logger.Debug(err)
  1589. return nil, err
  1590. }
  1591. return traffics, err
  1592. }
  1593. func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
  1594. db := database.GetDB()
  1595. inbound := &model.Inbound{}
  1596. traffic = &xray.ClientTraffic{}
  1597. // Search for inbound settings that contain the query
  1598. err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
  1599. if err != nil {
  1600. if err == gorm.ErrRecordNotFound {
  1601. logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
  1602. return nil, err
  1603. }
  1604. logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
  1605. return nil, err
  1606. }
  1607. traffic.InboundId = inbound.Id
  1608. // Unmarshal settings to get clients
  1609. settings := map[string][]model.Client{}
  1610. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  1611. logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
  1612. return nil, err
  1613. }
  1614. clients := settings["clients"]
  1615. for _, client := range clients {
  1616. if (client.ID == query || client.Password == query) && client.Email != "" {
  1617. traffic.Email = client.Email
  1618. break
  1619. }
  1620. }
  1621. if traffic.Email == "" {
  1622. logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
  1623. return nil, gorm.ErrRecordNotFound
  1624. }
  1625. // Retrieve ClientTraffic based on the found email
  1626. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  1627. if err != nil {
  1628. if err == gorm.ErrRecordNotFound {
  1629. logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
  1630. return nil, err
  1631. }
  1632. logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
  1633. return nil, err
  1634. }
  1635. return traffic, nil
  1636. }
  1637. func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
  1638. db := database.GetDB()
  1639. InboundClientIps := &model.InboundClientIps{}
  1640. err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
  1641. if err != nil {
  1642. return "", err
  1643. }
  1644. return InboundClientIps.Ips, nil
  1645. }
  1646. func (s *InboundService) ClearClientIps(clientEmail string) error {
  1647. db := database.GetDB()
  1648. result := db.Model(model.InboundClientIps{}).
  1649. Where("client_email = ?", clientEmail).
  1650. Update("ips", "")
  1651. err := result.Error
  1652. if err != nil {
  1653. return err
  1654. }
  1655. return nil
  1656. }
  1657. func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
  1658. db := database.GetDB()
  1659. var inbounds []*model.Inbound
  1660. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
  1661. if err != nil && err != gorm.ErrRecordNotFound {
  1662. return nil, err
  1663. }
  1664. return inbounds, nil
  1665. }
  1666. func (s *InboundService) MigrationRequirements() {
  1667. db := database.GetDB()
  1668. tx := db.Begin()
  1669. var err error
  1670. defer func() {
  1671. if err == nil {
  1672. tx.Commit()
  1673. } else {
  1674. tx.Rollback()
  1675. }
  1676. }()
  1677. // Fix inbounds based problems
  1678. var inbounds []*model.Inbound
  1679. err = tx.Model(model.Inbound{}).Where("protocol IN (?)", []string{"vmess", "vless", "trojan"}).Find(&inbounds).Error
  1680. if err != nil && err != gorm.ErrRecordNotFound {
  1681. return
  1682. }
  1683. for inbound_index := range inbounds {
  1684. settings := map[string]interface{}{}
  1685. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  1686. clients, ok := settings["clients"].([]interface{})
  1687. if ok {
  1688. // Fix Client configuration problems
  1689. var newClients []interface{}
  1690. for client_index := range clients {
  1691. c := clients[client_index].(map[string]interface{})
  1692. // Add email='' if it is not exists
  1693. if _, ok := c["email"]; !ok {
  1694. c["email"] = ""
  1695. }
  1696. // Convert string tgId to int64
  1697. if _, ok := c["tgId"]; ok {
  1698. var tgId interface{} = c["tgId"]
  1699. if tgIdStr, ok2 := tgId.(string); ok2 {
  1700. tgIdInt64, err := strconv.ParseInt(strings.ReplaceAll(tgIdStr, " ", ""), 10, 64)
  1701. if err == nil {
  1702. c["tgId"] = tgIdInt64
  1703. }
  1704. }
  1705. }
  1706. // Remove "flow": "xtls-rprx-direct"
  1707. if _, ok := c["flow"]; ok {
  1708. if c["flow"] == "xtls-rprx-direct" {
  1709. c["flow"] = ""
  1710. }
  1711. }
  1712. newClients = append(newClients, interface{}(c))
  1713. }
  1714. settings["clients"] = newClients
  1715. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1716. if err != nil {
  1717. return
  1718. }
  1719. inbounds[inbound_index].Settings = string(modifiedSettings)
  1720. }
  1721. // Add client traffic row for all clients which has email
  1722. modelClients, err := s.GetClients(inbounds[inbound_index])
  1723. if err != nil {
  1724. return
  1725. }
  1726. for _, modelClient := range modelClients {
  1727. if len(modelClient.Email) > 0 {
  1728. var count int64
  1729. tx.Model(xray.ClientTraffic{}).Where("email = ?", modelClient.Email).Count(&count)
  1730. if count == 0 {
  1731. s.AddClientStat(tx, inbounds[inbound_index].Id, &modelClient)
  1732. }
  1733. }
  1734. }
  1735. }
  1736. tx.Save(inbounds)
  1737. // Remove orphaned traffics
  1738. tx.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
  1739. // Migrate old MultiDomain to External Proxy
  1740. var externalProxy []struct {
  1741. Id int
  1742. Port int
  1743. StreamSettings []byte
  1744. }
  1745. err = tx.Raw(`select id, port, stream_settings
  1746. from inbounds
  1747. WHERE protocol in ('vmess','vless','trojan')
  1748. AND json_extract(stream_settings, '$.security') = 'tls'
  1749. AND json_extract(stream_settings, '$.tlsSettings.settings.domains') IS NOT NULL`).Scan(&externalProxy).Error
  1750. if err != nil || len(externalProxy) == 0 {
  1751. return
  1752. }
  1753. for _, ep := range externalProxy {
  1754. var reverses interface{}
  1755. var stream map[string]interface{}
  1756. json.Unmarshal(ep.StreamSettings, &stream)
  1757. if tlsSettings, ok := stream["tlsSettings"].(map[string]interface{}); ok {
  1758. if settings, ok := tlsSettings["settings"].(map[string]interface{}); ok {
  1759. if domains, ok := settings["domains"].([]interface{}); ok {
  1760. for _, domain := range domains {
  1761. if domainMap, ok := domain.(map[string]interface{}); ok {
  1762. domainMap["forceTls"] = "same"
  1763. domainMap["port"] = ep.Port
  1764. domainMap["dest"] = domainMap["domain"].(string)
  1765. delete(domainMap, "domain")
  1766. }
  1767. }
  1768. }
  1769. reverses = settings["domains"]
  1770. delete(settings, "domains")
  1771. }
  1772. }
  1773. stream["externalProxy"] = reverses
  1774. newStream, _ := json.MarshalIndent(stream, " ", " ")
  1775. tx.Model(model.Inbound{}).Where("id = ?", ep.Id).Update("stream_settings", newStream)
  1776. }
  1777. err = tx.Raw(`UPDATE inbounds
  1778. SET tag = REPLACE(tag, '0.0.0.0:', '')
  1779. WHERE INSTR(tag, '0.0.0.0:') > 0;`).Error
  1780. if err != nil {
  1781. return
  1782. }
  1783. }
  1784. func (s *InboundService) MigrateDB() {
  1785. s.MigrationRequirements()
  1786. s.MigrationRemoveOrphanedTraffics()
  1787. }
  1788. func (s *InboundService) GetOnlineClients() []string {
  1789. return p.GetOnlineClients()
  1790. }