Skip to content

Commit af0846e

Browse files
committed
Enhance user synchronization and error logging in Inbound
- Improved error logging for user account creation in syncUsers method, providing clearer context on failures. - Added logging for the number of users synced per inbound configuration, aiding in debugging and monitoring. - Updated Core's Start method to always generate a config file for debugging, regardless of debug mode, and added checks for empty config before startup. - Introduced logging for config size during startup to assist in diagnosing issues.
1 parent e029115 commit af0846e

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

backend/xray/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ func (i *Inbound) syncUsers(users []*common.User) {
130130
if slices.Contains(user.Inbounds, i.Tag) {
131131
account, err := api.NewVmessAccount(user)
132132
if err != nil {
133-
log.Println("error for user", user.GetEmail(), ":", err)
133+
log.Printf("error creating vmess account for user %s in inbound %s: %v", user.GetEmail(), i.Tag, err)
134134
continue
135135
}
136136
i.clients[user.GetEmail()] = account
137137
}
138138
}
139+
log.Printf("inbound %s (%s): synced %d users", i.Tag, i.Protocol, len(i.clients))
139140

140141
case Vless:
141142
for _, user := range users {
@@ -145,13 +146,14 @@ func (i *Inbound) syncUsers(users []*common.User) {
145146
if slices.Contains(user.Inbounds, i.Tag) {
146147
account, err := api.NewVlessAccount(user)
147148
if err != nil {
148-
log.Println("error for user", user.GetEmail(), ":", err)
149+
log.Printf("error creating vless account for user %s in inbound %s: %v", user.GetEmail(), i.Tag, err)
149150
continue
150151
}
151152
newAccount := checkVless(i, *account)
152153
i.clients[user.GetEmail()] = &newAccount
153154
}
154155
}
156+
log.Printf("inbound %s (%s): synced %d users", i.Tag, i.Protocol, len(i.clients))
155157

156158
case Trojan:
157159
for _, user := range users {
@@ -162,6 +164,7 @@ func (i *Inbound) syncUsers(users []*common.User) {
162164
i.clients[user.GetEmail()] = api.NewTrojanAccount(user)
163165
}
164166
}
167+
log.Printf("inbound %s (%s): synced %d users", i.Tag, i.Protocol, len(i.clients))
165168

166169
case Shadowsocks:
167170
method, methodOk := i.Settings["method"].(string)
@@ -186,6 +189,7 @@ func (i *Inbound) syncUsers(users []*common.User) {
186189
}
187190
}
188191
}
192+
log.Printf("inbound %s (%s): synced %d users", i.Tag, i.Protocol, len(i.clients))
189193
}
190194
}
191195

backend/xray/core.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ func (c *Core) Start(xConfig *Config, debugMode bool) error {
118118
accessFile, errorFile := xConfig.RemoveLogFiles()
119119

120120
bytesConfig, err := xConfig.ToBytes()
121-
if debugMode {
122-
if err = c.GenerateConfigFile(bytesConfig); err != nil {
123-
return err
124-
}
121+
if err != nil {
122+
return fmt.Errorf("failed to serialize config: %w", err)
123+
}
124+
125+
// Always write config file for debugging, not just in debug mode
126+
// This helps diagnose issues when users are missing
127+
if err = c.GenerateConfigFile(bytesConfig); err != nil {
128+
log.Printf("warning: failed to write config file: %v", err)
129+
// Don't fail startup if config file write fails, Xray reads from stdin anyway
125130
}
126131

127132
c.mu.Lock()
@@ -167,6 +172,14 @@ func (c *Core) Start(xConfig *Config, debugMode bool) error {
167172
return err
168173
}
169174

175+
// Verify config has content before starting
176+
if len(bytesConfig) == 0 {
177+
return errors.New("config is empty, cannot start xray")
178+
}
179+
180+
// Log config size for debugging
181+
log.Printf("starting xray with config size: %d bytes", len(bytesConfig))
182+
170183
cmd.Stdin = bytes.NewBuffer(bytesConfig)
171184
if err = cmd.Start(); err != nil {
172185
return err
@@ -258,6 +271,10 @@ func (c *Core) Restart(config *Config, debugMode bool) error {
258271

259272
log.Println("restarting Xray core...")
260273
c.Stop()
274+
275+
// Small delay to ensure previous process is fully terminated
276+
time.Sleep(time.Millisecond * 100)
277+
261278
if err := c.Start(config, debugMode); err != nil {
262279
return err
263280
}

backend/xray/user.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ func (x *Xray) SyncUser(ctx context.Context, user *common.User) error {
165165

166166
func (x *Xray) SyncUsers(_ context.Context, users []*common.User) error {
167167
x.config.syncUsers(users)
168+
169+
// Verify users were synced before restarting
170+
totalClients := 0
171+
for _, inbound := range x.config.InboundConfigs {
172+
if !inbound.exclude && inbound.clients != nil {
173+
totalClients += len(inbound.clients)
174+
}
175+
}
176+
log.Printf("syncing %d users, total clients in config: %d", len(users), totalClients)
177+
168178
if err := x.Restart(); err != nil {
169179
return err
170180
}
@@ -210,6 +220,16 @@ func (x *Xray) UpdateUsers(ctx context.Context, users []*common.User) error {
210220

211221
func (x *Xray) UpdateUsersAndRestart(_ context.Context, users []*common.User) error {
212222
x.config.updateUsers(users)
223+
224+
// Verify users were updated before restarting
225+
totalClients := 0
226+
for _, inbound := range x.config.InboundConfigs {
227+
if !inbound.exclude && inbound.clients != nil {
228+
totalClients += len(inbound.clients)
229+
}
230+
}
231+
log.Printf("updating %d users, total clients in config: %d", len(users), totalClients)
232+
213233
if err := x.Restart(); err != nil {
214234
return err
215235
}

0 commit comments

Comments
 (0)