mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-04-16 04:25:46 +00:00
fix: Fix getting the xray inbound api port
This commit is contained in:
parent
8b6ff269b6
commit
aed05fa7f0
1 changed files with 45 additions and 12 deletions
|
|
@ -3,6 +3,7 @@ package job
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -32,6 +33,8 @@ type CheckClientIpJob struct {
|
||||||
|
|
||||||
var job *CheckClientIpJob
|
var job *CheckClientIpJob
|
||||||
|
|
||||||
|
const defaultXrayAPIPort = 62789
|
||||||
|
|
||||||
// NewCheckClientIpJob creates a new client IP monitoring job instance.
|
// NewCheckClientIpJob creates a new client IP monitoring job instance.
|
||||||
func NewCheckClientIpJob() *CheckClientIpJob {
|
func NewCheckClientIpJob() *CheckClientIpJob {
|
||||||
job = new(CheckClientIpJob)
|
job = new(CheckClientIpJob)
|
||||||
|
|
@ -387,18 +390,7 @@ func (j *CheckClientIpJob) updateInboundClientIps(inboundClientIps *model.Inboun
|
||||||
// disconnectClientTemporarily removes and re-adds a client to force disconnect banned connections
|
// disconnectClientTemporarily removes and re-adds a client to force disconnect banned connections
|
||||||
func (j *CheckClientIpJob) disconnectClientTemporarily(inbound *model.Inbound, clientEmail string, clients []model.Client) {
|
func (j *CheckClientIpJob) disconnectClientTemporarily(inbound *model.Inbound, clientEmail string, clients []model.Client) {
|
||||||
var xrayAPI xray.XrayAPI
|
var xrayAPI xray.XrayAPI
|
||||||
|
apiPort := j.resolveXrayAPIPort()
|
||||||
// Get panel settings for API port
|
|
||||||
db := database.GetDB()
|
|
||||||
var apiPort int
|
|
||||||
var apiPortSetting model.Setting
|
|
||||||
if err := db.Where("key = ?", "xrayApiPort").First(&apiPortSetting).Error; err == nil {
|
|
||||||
apiPort, _ = strconv.Atoi(apiPortSetting.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
if apiPort == 0 {
|
|
||||||
apiPort = 10085 // Default API port
|
|
||||||
}
|
|
||||||
|
|
||||||
err := xrayAPI.Init(apiPort)
|
err := xrayAPI.Init(apiPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -439,6 +431,47 @@ func (j *CheckClientIpJob) disconnectClientTemporarily(inbound *model.Inbound, c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveXrayAPIPort returns the API inbound port from running config, then template config, then default.
|
||||||
|
func (j *CheckClientIpJob) resolveXrayAPIPort() int {
|
||||||
|
if port, err := getAPIPortFromConfigPath(xray.GetConfigPath()); err == nil {
|
||||||
|
return port
|
||||||
|
}
|
||||||
|
|
||||||
|
db := database.GetDB()
|
||||||
|
var template model.Setting
|
||||||
|
if err := db.Where("key = ?", "xrayTemplateConfig").First(&template).Error; err == nil {
|
||||||
|
if port, parseErr := getAPIPortFromConfigData([]byte(template.Value)); parseErr == nil {
|
||||||
|
return port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultXrayAPIPort
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAPIPortFromConfigPath(configPath string) (int, error) {
|
||||||
|
configData, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return getAPIPortFromConfigData(configData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAPIPortFromConfigData(configData []byte) (int, error) {
|
||||||
|
xrayConfig := &xray.Config{}
|
||||||
|
if err := json.Unmarshal(configData, xrayConfig); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, inboundConfig := range xrayConfig.InboundConfigs {
|
||||||
|
if inboundConfig.Tag == "api" && inboundConfig.Port > 0 {
|
||||||
|
return inboundConfig.Port, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, errors.New("api inbound port not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (j *CheckClientIpJob) getInboundByEmail(clientEmail string) (*model.Inbound, error) {
|
func (j *CheckClientIpJob) getInboundByEmail(clientEmail string) (*model.Inbound, error) {
|
||||||
db := database.GetDB()
|
db := database.GetDB()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue