xray_traffic_job.go 835 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package job
  2. import (
  3. "x-ui/logger"
  4. "x-ui/web/service"
  5. )
  6. type XrayTrafficJob struct {
  7. xrayService service.XrayService
  8. inboundService service.InboundService
  9. outboundService service.OutboundService
  10. }
  11. func NewXrayTrafficJob() *XrayTrafficJob {
  12. return new(XrayTrafficJob)
  13. }
  14. func (j *XrayTrafficJob) Run() {
  15. if !j.xrayService.IsXrayRunning() {
  16. return
  17. }
  18. traffics, clientTraffics, err := j.xrayService.GetXrayTraffic()
  19. if err != nil {
  20. return
  21. }
  22. err, needRestart0 := j.inboundService.AddTraffic(traffics, clientTraffics)
  23. if err != nil {
  24. logger.Warning("add inbound traffic failed:", err)
  25. }
  26. err, needRestart1 := j.outboundService.AddTraffic(traffics, clientTraffics)
  27. if err != nil {
  28. logger.Warning("add outbound traffic failed:", err)
  29. }
  30. if needRestart0 || needRestart1 {
  31. j.xrayService.SetToNeedRestart()
  32. }
  33. }