@@ -196,6 +196,47 @@ func (d *DefaultDispatcher) getLink(ctx context.Context) (*transport.Link, *tran
196196 return inboundLink , outboundLink
197197}
198198
199+ func (d * DefaultDispatcher ) WrapLink (ctx context.Context , link * transport.Link ) * transport.Link {
200+ sessionInbound := session .InboundFromContext (ctx )
201+ var user * protocol.MemoryUser
202+ if sessionInbound != nil {
203+ user = sessionInbound .User
204+ }
205+
206+ link .Reader = & buf.TimeoutWrapperReader {Reader : link .Reader }
207+
208+ if user != nil && len (user .Email ) > 0 {
209+ p := d .policy .ForLevel (user .Level )
210+ if p .Stats .UserUplink {
211+ name := "user>>>" + user .Email + ">>>traffic>>>uplink"
212+ if c , _ := stats .GetOrRegisterCounter (d .stats , name ); c != nil {
213+ link .Reader .(* buf.TimeoutWrapperReader ).Counter = c
214+ }
215+ }
216+ if p .Stats .UserDownlink {
217+ name := "user>>>" + user .Email + ">>>traffic>>>downlink"
218+ if c , _ := stats .GetOrRegisterCounter (d .stats , name ); c != nil {
219+ link .Writer = & SizeStatWriter {
220+ Counter : c ,
221+ Writer : link .Writer ,
222+ }
223+ }
224+ }
225+ if p .Stats .UserOnline {
226+ name := "user>>>" + user .Email + ">>>online"
227+ if om , _ := stats .GetOrRegisterOnlineMap (d .stats , name ); om != nil {
228+ sessionInbounds := session .InboundFromContext (ctx )
229+ userIP := sessionInbounds .Source .Address .String ()
230+ om .AddIP (userIP )
231+ // log Online user with ips
232+ // errors.LogDebug(ctx, "user>>>" + user.Email + ">>>online", om.Count(), om.List())
233+ }
234+ }
235+ }
236+
237+ return link
238+ }
239+
199240func (d * DefaultDispatcher ) shouldOverride (ctx context.Context , result SniffResult , request session.SniffingRequest , destination net.Destination ) bool {
200241 domain := result .Domain ()
201242 if domain == "" {
@@ -316,6 +357,7 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
316357 content = new (session.Content )
317358 ctx = session .ContextWithContent (ctx , content )
318359 }
360+ outbound = d .WrapLink (ctx , outbound )
319361 sniffingRequest := content .SniffingRequest
320362 if ! sniffingRequest .Enabled {
321363 d .routedDispatch (ctx , outbound , destination )
0 commit comments