Skip to content

Commit 6f20eb4

Browse files
committed
fix: record only successful requests
1 parent 0986bed commit 6f20eb4

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

backend/xray/api/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type VlessAccount struct {
6161
}
6262

6363
func (va *VlessAccount) Message() (*serial.TypedMessage, error) {
64-
return ToTypedMessage(&vless.Account{Id: va.ID.String(), Flow: va.Flow})
64+
return ToTypedMessage(&vless.Account{Id: va.ID.String(), Flow: va.Flow, Reverse: nil})
6565
}
6666

6767
func NewVlessAccount(user *common.User) (*VlessAccount, error) {

controller/controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,21 @@ func (c *Controller) Backend() backend.Backend {
113113
}
114114

115115
func (c *Controller) keepAliveTracker(ctx context.Context, keepAlive time.Duration) {
116+
ticker := time.NewTicker(5 * time.Second)
117+
defer ticker.Stop()
118+
116119
for {
117120
select {
118121
case <-ctx.Done():
119122
return
120-
default:
123+
case <-ticker.C:
121124
c.mu.RLock()
122125
lastRequest := c.lastRequest
123126
c.mu.RUnlock()
124127
if time.Since(lastRequest) >= keepAlive {
125128
log.Println("disconnect automatically due to keep alive timeout")
126129
c.Disconnect()
127130
}
128-
time.Sleep(5 * time.Second)
129131
}
130132
}
131133
}

controller/rest/middleware.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func (s *Service) validateApiKey(next http.Handler) http.Handler {
2929
return
3030
}
3131

32-
s.NewRequest()
3332
next.ServeHTTP(w, r)
3433
})
3534
}
@@ -61,3 +60,16 @@ func LogRequest(next http.Handler) http.Handler {
6160
log.Println(fmt.Sprintf("[API] %s, %s, %s, %d", r.RemoteAddr, r.Method, r.URL.Path, ww.Status()))
6261
})
6362
}
63+
64+
func (s *Service) trackSuccessfulRequest(next http.Handler) http.Handler {
65+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
67+
68+
next.ServeHTTP(ww, r)
69+
70+
// Only track successful requests (status codes 200-299)
71+
if status := ww.Status(); status >= 200 && status < 300 {
72+
s.NewRequest()
73+
}
74+
})
75+
}

controller/rest/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (s *Service) setRouter() {
2828
// Api Handlers
2929
router.Use(LogRequest)
3030
router.Use(s.validateApiKey)
31+
router.Use(s.trackSuccessfulRequest)
3132
router.Use(middleware.Recoverer)
3233

3334
router.Post("/start", s.Start)

controller/rpc/middleware.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func validateApiKey(ctx context.Context, s *Service) error {
4040
return status.Errorf(codes.PermissionDenied, "api key mismatch")
4141
}
4242

43-
s.NewRequest()
4443
return nil
4544
}
4645

@@ -135,22 +134,29 @@ func logRequest(ctx context.Context, method string, err error) {
135134
}
136135
}
137136

138-
func LoggingInterceptor(
139-
ctx context.Context,
140-
req interface{},
141-
info *grpc.UnaryServerInfo,
142-
handler grpc.UnaryHandler,
143-
) (interface{}, error) {
144-
// Handle the request
145-
resp, err := handler(ctx, req)
137+
func LoggingInterceptor(s *Service) grpc.UnaryServerInterceptor {
138+
return func(
139+
ctx context.Context,
140+
req interface{},
141+
info *grpc.UnaryServerInfo,
142+
handler grpc.UnaryHandler,
143+
) (interface{}, error) {
144+
// Handle the request
145+
resp, err := handler(ctx, req)
146146

147-
// Log the request
148-
logRequest(ctx, info.FullMethod, err)
147+
// Log the request
148+
logRequest(ctx, info.FullMethod, err)
149+
150+
// Track successful requests
151+
if err == nil {
152+
s.NewRequest()
153+
}
149154

150-
return resp, err
155+
return resp, err
156+
}
151157
}
152158

153-
func LoggingStreamInterceptor() grpc.StreamServerInterceptor {
159+
func LoggingStreamInterceptor(s *Service) grpc.StreamServerInterceptor {
154160
return func(
155161
srv interface{},
156162
ss grpc.ServerStream,
@@ -169,6 +175,11 @@ func LoggingStreamInterceptor() grpc.StreamServerInterceptor {
169175
// Log the request
170176
logRequest(ss.Context(), info.FullMethod, err)
171177

178+
// Track successful requests
179+
if err == nil {
180+
s.NewRequest()
181+
}
182+
172183
return err
173184
}
174185
}
@@ -194,7 +205,7 @@ func ConditionalMiddleware(s *Service) grpc.UnaryServerInterceptor {
194205
) (interface{}, error) {
195206
var interceptors []grpc.UnaryServerInterceptor
196207

197-
interceptors = append(interceptors, LoggingInterceptor)
208+
interceptors = append(interceptors, LoggingInterceptor(s))
198209

199210
interceptors = append(interceptors, validateApiKeyMiddleware(s))
200211

@@ -216,7 +227,7 @@ func ConditionalStreamMiddleware(s *Service) grpc.StreamServerInterceptor {
216227
) error {
217228
var interceptors []grpc.StreamServerInterceptor
218229

219-
interceptors = append(interceptors, LoggingStreamInterceptor())
230+
interceptors = append(interceptors, LoggingStreamInterceptor(s))
220231

221232
interceptors = append(interceptors, validateApiKeyStreamMiddleware(s))
222233

0 commit comments

Comments
 (0)