mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-04-19 21:42:24 +00:00
Add ExternalTrafficURL Post request call
This commit is contained in:
parent
f7764b5a8d
commit
6eb436b2d2
3 changed files with 53 additions and 1 deletions
|
@ -31,7 +31,7 @@ class AllSetting {
|
||||||
this.subPath = "/sub/";
|
this.subPath = "/sub/";
|
||||||
this.subJsonPath = "/json/";
|
this.subJsonPath = "/json/";
|
||||||
this.subDomain = "";
|
this.subDomain = "";
|
||||||
this.externalInformEnable = false;
|
this.externalTrafficInformEnable = false;
|
||||||
this.externalTrafficInformURI = "";
|
this.externalTrafficInformURI = "";
|
||||||
this.subCertFile = "";
|
this.subCertFile = "";
|
||||||
this.subKeyFile = "";
|
this.subKeyFile = "";
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
package job
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"x-ui/logger"
|
"x-ui/logger"
|
||||||
"x-ui/web/service"
|
"x-ui/web/service"
|
||||||
|
"x-ui/xray"
|
||||||
|
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type XrayTrafficJob struct {
|
type XrayTrafficJob struct {
|
||||||
|
settingService service.SettingService
|
||||||
xrayService service.XrayService
|
xrayService service.XrayService
|
||||||
inboundService service.InboundService
|
inboundService service.InboundService
|
||||||
outboundService service.OutboundService
|
outboundService service.OutboundService
|
||||||
|
@ -31,7 +37,37 @@ func (j *XrayTrafficJob) Run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warning("add outbound traffic failed:", err)
|
logger.Warning("add outbound traffic failed:", err)
|
||||||
}
|
}
|
||||||
|
if ExternalInformEnable, err := j.settingService.GetExternalTrafficInformEnable(); ExternalInformEnable {
|
||||||
|
j.informTrafficToExternalAPI(traffics, clientTraffics)
|
||||||
|
} else if err != nil {
|
||||||
|
logger.Warning("get ExternalTrafficInformEnable failed:", err)
|
||||||
|
}
|
||||||
if needRestart0 || needRestart1 {
|
if needRestart0 || needRestart1 {
|
||||||
j.xrayService.SetToNeedRestart()
|
j.xrayService.SetToNeedRestart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j *XrayTrafficJob) informTrafficToExternalAPI(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) {
|
||||||
|
informURL, err := j.settingService.GetExternalTrafficInformURI()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warning("get ExternalTrafficInformURI failed:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
requestBody, err := json.Marshal(map[string]interface{}{"clientTraffics": clientTraffics, "inboundTraffics": inboundTraffics})
|
||||||
|
if err != nil {
|
||||||
|
logger.Warning("parse client/inbound traffic failed:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
request := fasthttp.AcquireRequest()
|
||||||
|
defer fasthttp.ReleaseRequest(request)
|
||||||
|
request.Header.SetMethod("POST")
|
||||||
|
request.Header.SetContentType("application/json; charset=UTF-8")
|
||||||
|
request.SetBody([]byte(requestBody))
|
||||||
|
request.SetRequestURI(informURL)
|
||||||
|
response := fasthttp.AcquireResponse()
|
||||||
|
defer fasthttp.ReleaseResponse(response)
|
||||||
|
if err := fasthttp.Do(request, response); err != nil {
|
||||||
|
fmt.Println("err ", err)
|
||||||
|
logger.Warning("POST ExternalTrafficInformURI failed:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -496,6 +496,22 @@ func (s *SettingService) SetWarp(data string) error {
|
||||||
return s.setString("warp", data)
|
return s.setString("warp", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
|
||||||
|
return s.getBool("externalTrafficInformEnable")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
|
||||||
|
return s.setBool("externalTrafficInformEnable", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
|
||||||
|
return s.getString("externalTrafficInformURI")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
|
||||||
|
return s.setString("externalTrafficInformURI", InformURI)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SettingService) GetIpLimitEnable() (bool, error) {
|
func (s *SettingService) GetIpLimitEnable() (bool, error) {
|
||||||
accessLogPath, err := xray.GetAccessLogPath()
|
accessLogPath, err := xray.GetAccessLogPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue