Skip to content

Commit 99cba05

Browse files
committed
Merge branch 'dev'
2 parents 12bbdd0 + 822ed1a commit 99cba05

21 files changed

Lines changed: 506 additions & 252 deletions

‎.github/workflows/docker-build-dev.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v4
2222

2323
- name: Set up Go
24-
uses: actions/setup-go@v5
24+
uses: actions/setup-go@v6
2525
with:
2626
go-version: '1.25'
2727
check-latest: true

‎.github/workflows/docker-build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
uses: actions/checkout@v4
2424

2525
- name: Set up Go
26-
uses: actions/setup-go@v5
26+
uses: actions/setup-go@v6
2727
with:
2828
go-version: '1.25'
2929
check-latest: true

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
echo "BINARY_NAME=pasarguard-node-${_NAME}" >> $GITHUB_ENV
117117
118118
- name: Set up Go
119-
uses: actions/setup-go@v5
119+
uses: actions/setup-go@v6
120120
with:
121121
go-version: ${{ matrix.gotoolchain || '1.25' }}
122122
check-latest: true

‎.github/workflows/run-tests.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
uses: actions/checkout@v4
1818

1919
- name: Set up Go
20-
uses: actions/setup-go@v5
20+
uses: actions/setup-go@v6
2121
with:
22-
go-version: ${{ matrix.gotoolchain || '1.25' }}
22+
go-version: '1.25'
2323
check-latest: true
2424

2525
- name: Get project dependencies

‎Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ ifeq ($(UNAME_S),Linux)
9595
if [ "$(DISTRO)" = "debian" ] || [ "$(DISTRO)" = "ubuntu" ] || \
9696
[ "$(DISTRO)" = "centos" ] || [ "$(DISTRO)" = "rhel" ] || [ "$(DISTRO)" = "fedora" ] || \
9797
[ "$(DISTRO)" = "arch" ]; then \
98-
sudo bash -c "$$(curl -L https://github.com/Gozargah/Marzban-scripts/raw/master/install_latest_xray.sh)"; \
98+
sudo bash -c "$$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install; \
9999
else \
100-
bash -c "$$(curl -L https://github.com/Gozargah/Marzban-scripts/raw/master/install_latest_xray.sh)"; \
100+
bash -c "$$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install; \
101101
fi
102102

103103
else

‎backend/xray/api/stats.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func (x *XrayHandler) GetOutboundsStats(ctx context.Context, reset bool) (*commo
139139

140140
parts := strings.Split(data, ">>>")
141141
name := parts[1]
142-
statType := parts[2]
143-
link := parts[3]
142+
link := parts[2]
143+
statType := parts[3]
144144

145145
stats.Stats = append(stats.Stats, &common.Stat{
146146
Name: name,

‎backend/xray/core.go‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"regexp"
1414
"sync"
1515

16-
"github.com/pasarguard/node/config"
1716
nodeLogger "github.com/pasarguard/node/logger"
1817
)
1918

@@ -25,16 +24,17 @@ type Core struct {
2524
process *exec.Cmd
2625
restarting bool
2726
logsChan chan string
27+
logger *nodeLogger.Logger
2828
cancelFunc context.CancelFunc
2929
mu sync.Mutex
3030
}
3131

32-
func NewXRayCore(executablePath, assetsPath, configPath string) (*Core, error) {
32+
func NewXRayCore(executablePath, assetsPath, configPath string, logBufferSize int) (*Core, error) {
3333
core := &Core{
3434
executablePath: executablePath,
3535
assetsPath: assetsPath,
3636
configPath: configPath,
37-
logsChan: make(chan string, config.LogBufferSize),
37+
logsChan: make(chan string, logBufferSize),
3838
}
3939

4040
version, err := core.refreshVersion()
@@ -104,7 +104,7 @@ func (c *Core) Started() bool {
104104
return false
105105
}
106106

107-
func (c *Core) Start(xConfig *Config) error {
107+
func (c *Core) Start(xConfig *Config, debugMode bool) error {
108108
logConfig := xConfig.LogConfig
109109
if logConfig == nil {
110110
return errors.New("log config is empty")
@@ -118,7 +118,7 @@ func (c *Core) Start(xConfig *Config) error {
118118
accessFile, errorFile := xConfig.RemoveLogFiles()
119119

120120
bytesConfig, err := xConfig.ToBytes()
121-
if config.Debug {
121+
if debugMode {
122122
if err = c.GenerateConfigFile(bytesConfig); err != nil {
123123
return err
124124
}
@@ -144,7 +144,9 @@ func (c *Core) Start(xConfig *Config) error {
144144
return err
145145
}
146146

147-
if err = nodeLogger.SetLogFile(accessFile, errorFile); err != nil {
147+
// Create a new logger for this core instance
148+
c.logger = nodeLogger.New(debugMode)
149+
if err = c.logger.SetLogFile(accessFile, errorFile); err != nil {
148150
return err
149151
}
150152

@@ -186,10 +188,15 @@ func (c *Core) Stop() {
186188
c.cancelFunc()
187189
}
188190

191+
if c.logger != nil {
192+
c.logger.Close()
193+
c.logger = nil
194+
}
195+
189196
log.Println("xray core stopped")
190197
}
191198

192-
func (c *Core) Restart(config *Config) error {
199+
func (c *Core) Restart(config *Config, debugMode bool) error {
193200
if c.restarting {
194201
return errors.New("xray is already restarted")
195202
}
@@ -206,7 +213,7 @@ func (c *Core) Restart(config *Config) error {
206213

207214
log.Println("restarting Xray core...")
208215
c.Stop()
209-
if err := c.Start(config); err != nil {
216+
if err := c.Start(config, debugMode); err != nil {
210217
return err
211218
}
212219
return nil

‎backend/xray/jobs.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
78
"regexp"
89
"strings"
910
"time"
10-
11-
nodeLogger "github.com/pasarguard/node/logger"
1211
)
1312

1413
func (x *Xray) checkXrayStatus() error {
@@ -70,9 +69,9 @@ func (x *Xray) checkXrayHealth(baseCtx context.Context) {
7069

7170
// Handle other errors by attempting restart
7271
if err = x.Restart(); err != nil {
73-
nodeLogger.Log(nodeLogger.LogError, err.Error())
72+
log.Println(err.Error())
7473
} else {
75-
nodeLogger.Log(nodeLogger.LogInfo, "xray restarted")
74+
log.Println("xray restarted")
7675
}
7776
}
7877
}

‎backend/xray/log.go‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,33 @@ func init() {
2222
}
2323

2424
func (c *Core) detectLogType(newLog string) {
25-
message := ""
26-
level := ""
25+
if c.logger == nil {
26+
return
27+
}
28+
29+
level := nodeLogger.LogDebug
2730

28-
// Find the matches
31+
// Find the matches to detect log level
2932
matches := re.FindStringSubmatch(newLog)
3033
if len(matches) > 3 {
31-
level = strings.Trim(matches[3], "[]")
32-
message = matches[4]
33-
} else {
34-
message = newLog
35-
}
36-
37-
if level == "" {
38-
level = "Debug"
34+
detectedLevel := strings.Trim(matches[3], "[]")
35+
// Map xray log levels to our logger levels
36+
switch strings.ToLower(detectedLevel) {
37+
case "error":
38+
level = nodeLogger.LogError
39+
case "warning":
40+
level = nodeLogger.LogWarning
41+
case "info":
42+
level = nodeLogger.LogInfo
43+
case "debug":
44+
level = nodeLogger.LogDebug
45+
default:
46+
level = nodeLogger.LogDebug
47+
}
3948
}
4049

41-
nodeLogger.Log(level, message)
50+
// Log the complete original message
51+
c.logger.Log(level, newLog)
4252
}
4353

4454
func (c *Core) captureProcessLogs(ctx context.Context, pipe io.Reader) {

‎backend/xray/log_test.go‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package xray
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
nodeLogger "github.com/pasarguard/node/logger"
10+
)
11+
12+
func TestDetectLogType(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
logMessage string
16+
expectedLevel string
17+
expectInError bool // true if should be in error log, false if in access log
18+
}{
19+
{
20+
name: "Error log",
21+
logMessage: "2024/01/15 10:30:45 [Error] failed to connect to server",
22+
expectedLevel: nodeLogger.LogError,
23+
expectInError: true,
24+
},
25+
{
26+
name: "Warning log",
27+
logMessage: "2024/01/15 10:30:45 [Warning] connection timeout",
28+
expectedLevel: nodeLogger.LogWarning,
29+
expectInError: false,
30+
},
31+
{
32+
name: "Info log",
33+
logMessage: "2024/01/15 10:30:45 [Info] server started successfully",
34+
expectedLevel: nodeLogger.LogInfo,
35+
expectInError: false,
36+
},
37+
{
38+
name: "Debug log",
39+
logMessage: "2024/01/15 10:30:45 [Debug] processing request",
40+
expectedLevel: nodeLogger.LogDebug,
41+
expectInError: false,
42+
},
43+
{
44+
name: "No level specified",
45+
logMessage: "some random log without level",
46+
expectedLevel: nodeLogger.LogDebug,
47+
expectInError: false,
48+
},
49+
{
50+
name: "Unknown level",
51+
logMessage: "2024/01/15 10:30:45 [Unknown] some message",
52+
expectedLevel: nodeLogger.LogDebug,
53+
expectInError: false,
54+
},
55+
{
56+
name: "Case insensitive - ERROR",
57+
logMessage: "2024/01/15 10:30:45 [ERROR] critical failure",
58+
expectedLevel: nodeLogger.LogError,
59+
expectInError: true,
60+
},
61+
{
62+
name: "Case insensitive - warning",
63+
logMessage: "2024/01/15 10:30:45 [warning] deprecated API used",
64+
expectedLevel: nodeLogger.LogWarning,
65+
expectInError: false,
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
// Create temporary log files
72+
tmpDir := t.TempDir()
73+
accessLog := filepath.Join(tmpDir, "access.log")
74+
errorLog := filepath.Join(tmpDir, "error.log")
75+
76+
logger := nodeLogger.New(false)
77+
if err := logger.SetLogFile(accessLog, errorLog); err != nil {
78+
t.Fatalf("Failed to set log files: %v", err)
79+
}
80+
defer logger.Close()
81+
82+
core := &Core{
83+
logger: logger,
84+
}
85+
86+
core.detectLogType(tt.logMessage)
87+
88+
// Read the appropriate log file
89+
var logContent []byte
90+
var err error
91+
if tt.expectInError {
92+
logContent, err = os.ReadFile(errorLog)
93+
} else {
94+
logContent, err = os.ReadFile(accessLog)
95+
}
96+
97+
if err != nil {
98+
t.Fatalf("Failed to read log file: %v", err)
99+
}
100+
101+
// Verify the complete message is logged
102+
if !strings.Contains(string(logContent), tt.logMessage) {
103+
t.Errorf("Log file does not contain the expected message.\nExpected: %v\nGot: %v", tt.logMessage, string(logContent))
104+
}
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)