chore: proxyDialer don't push flow to manager in statistic

This commit is contained in:
gVisor bot 2023-04-11 23:58:56 +08:00
parent 964a13ee55
commit f371813210
3 changed files with 48 additions and 12 deletions

View file

@ -64,7 +64,7 @@ func (p proxyDialer) DialContext(ctx context.Context, network, address string) (
} }
} }
if p.statistic { if p.statistic {
conn = statistic.NewTCPTracker(conn, statistic.DefaultManager, currentMeta, nil, 0, 0) conn = statistic.NewTCPTracker(conn, statistic.DefaultManager, currentMeta, nil, 0, 0, false)
} }
return conn, err return conn, err
} }
@ -100,7 +100,7 @@ func (p proxyDialer) listenPacket(ctx context.Context, currentMeta *C.Metadata)
} }
} }
if p.statistic { if p.statistic {
pc = statistic.NewUDPTracker(pc, statistic.DefaultManager, currentMeta, nil, 0, 0) pc = statistic.NewUDPTracker(pc, statistic.DefaultManager, currentMeta, nil, 0, 0, false)
} }
return pc, nil return pc, nil
} }

View file

@ -32,6 +32,8 @@ type tcpTracker struct {
C.Conn `json:"-"` C.Conn `json:"-"`
*trackerInfo *trackerInfo
manager *Manager manager *Manager
pushToManager bool `json:"-"`
} }
func (tt *tcpTracker) ID() string { func (tt *tcpTracker) ID() string {
@ -41,7 +43,9 @@ func (tt *tcpTracker) ID() string {
func (tt *tcpTracker) Read(b []byte) (int, error) { func (tt *tcpTracker) Read(b []byte) (int, error) {
n, err := tt.Conn.Read(b) n, err := tt.Conn.Read(b)
download := int64(n) download := int64(n)
if tt.pushToManager {
tt.manager.PushDownloaded(download) tt.manager.PushDownloaded(download)
}
tt.DownloadTotal.Add(download) tt.DownloadTotal.Add(download)
return n, err return n, err
} }
@ -49,7 +53,9 @@ func (tt *tcpTracker) Read(b []byte) (int, error) {
func (tt *tcpTracker) ReadBuffer(buffer *buf.Buffer) (err error) { func (tt *tcpTracker) ReadBuffer(buffer *buf.Buffer) (err error) {
err = tt.Conn.ReadBuffer(buffer) err = tt.Conn.ReadBuffer(buffer)
download := int64(buffer.Len()) download := int64(buffer.Len())
if tt.pushToManager {
tt.manager.PushDownloaded(download) tt.manager.PushDownloaded(download)
}
tt.DownloadTotal.Add(download) tt.DownloadTotal.Add(download)
return return
} }
@ -57,7 +63,9 @@ func (tt *tcpTracker) ReadBuffer(buffer *buf.Buffer) (err error) {
func (tt *tcpTracker) Write(b []byte) (int, error) { func (tt *tcpTracker) Write(b []byte) (int, error) {
n, err := tt.Conn.Write(b) n, err := tt.Conn.Write(b)
upload := int64(n) upload := int64(n)
if tt.pushToManager {
tt.manager.PushUploaded(upload) tt.manager.PushUploaded(upload)
}
tt.UploadTotal.Add(upload) tt.UploadTotal.Add(upload)
return n, err return n, err
} }
@ -65,7 +73,9 @@ func (tt *tcpTracker) Write(b []byte) (int, error) {
func (tt *tcpTracker) WriteBuffer(buffer *buf.Buffer) (err error) { func (tt *tcpTracker) WriteBuffer(buffer *buf.Buffer) (err error) {
upload := int64(buffer.Len()) upload := int64(buffer.Len())
err = tt.Conn.WriteBuffer(buffer) err = tt.Conn.WriteBuffer(buffer)
if tt.pushToManager {
tt.manager.PushUploaded(upload) tt.manager.PushUploaded(upload)
}
tt.UploadTotal.Add(upload) tt.UploadTotal.Add(upload)
return return
} }
@ -79,7 +89,7 @@ func (tt *tcpTracker) Upstream() any {
return tt.Conn return tt.Conn
} }
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *tcpTracker { func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *tcpTracker {
if conn != nil { if conn != nil {
if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
metadata.RemoteDst = tcpAddr.IP.String() metadata.RemoteDst = tcpAddr.IP.String()
@ -100,6 +110,16 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R
UploadTotal: atomic.NewInt64(uploadTotal), UploadTotal: atomic.NewInt64(uploadTotal),
DownloadTotal: atomic.NewInt64(downloadTotal), DownloadTotal: atomic.NewInt64(downloadTotal),
}, },
pushToManager: pushToManager,
}
if pushToManager {
if uploadTotal > 0 {
manager.PushUploaded(uploadTotal)
}
if downloadTotal > 0 {
manager.PushDownloaded(downloadTotal)
}
} }
if rule != nil { if rule != nil {
@ -115,6 +135,8 @@ type udpTracker struct {
C.PacketConn `json:"-"` C.PacketConn `json:"-"`
*trackerInfo *trackerInfo
manager *Manager manager *Manager
pushToManager bool `json:"-"`
} }
func (ut *udpTracker) ID() string { func (ut *udpTracker) ID() string {
@ -124,7 +146,9 @@ func (ut *udpTracker) ID() string {
func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) { func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) {
n, addr, err := ut.PacketConn.ReadFrom(b) n, addr, err := ut.PacketConn.ReadFrom(b)
download := int64(n) download := int64(n)
if ut.pushToManager {
ut.manager.PushDownloaded(download) ut.manager.PushDownloaded(download)
}
ut.DownloadTotal.Add(download) ut.DownloadTotal.Add(download)
return n, addr, err return n, addr, err
} }
@ -132,7 +156,9 @@ func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) {
func (ut *udpTracker) WriteTo(b []byte, addr net.Addr) (int, error) { func (ut *udpTracker) WriteTo(b []byte, addr net.Addr) (int, error) {
n, err := ut.PacketConn.WriteTo(b, addr) n, err := ut.PacketConn.WriteTo(b, addr)
upload := int64(n) upload := int64(n)
if ut.pushToManager {
ut.manager.PushUploaded(upload) ut.manager.PushUploaded(upload)
}
ut.UploadTotal.Add(upload) ut.UploadTotal.Add(upload)
return n, err return n, err
} }
@ -142,7 +168,7 @@ func (ut *udpTracker) Close() error {
return ut.PacketConn.Close() return ut.PacketConn.Close()
} }
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64) *udpTracker { func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *udpTracker {
metadata.RemoteDst = conn.RemoteDestination() metadata.RemoteDst = conn.RemoteDestination()
ut := &udpTracker{ ut := &udpTracker{
@ -157,6 +183,16 @@ func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, ru
UploadTotal: atomic.NewInt64(uploadTotal), UploadTotal: atomic.NewInt64(uploadTotal),
DownloadTotal: atomic.NewInt64(downloadTotal), DownloadTotal: atomic.NewInt64(downloadTotal),
}, },
pushToManager: pushToManager,
}
if pushToManager {
if uploadTotal > 0 {
manager.PushUploaded(uploadTotal)
}
if downloadTotal > 0 {
manager.PushDownloaded(downloadTotal)
}
} }
if rule != nil { if rule != nil {

View file

@ -364,7 +364,7 @@ func handleUDPConn(packet C.PacketAdapter) {
} }
pCtx.InjectPacketConn(rawPc) pCtx.InjectPacketConn(rawPc)
pc := statistic.NewUDPTracker(rawPc, statistic.DefaultManager, metadata, rule, 0, 0) pc := statistic.NewUDPTracker(rawPc, statistic.DefaultManager, metadata, rule, 0, 0, true)
switch true { switch true {
case metadata.SpecialProxy != "": case metadata.SpecialProxy != "":
@ -494,7 +494,7 @@ func handleTCPConn(connCtx C.ConnContext) {
return return
} }
remoteConn = statistic.NewTCPTracker(remoteConn, statistic.DefaultManager, metadata, rule, 0, int64(peekLen)) remoteConn = statistic.NewTCPTracker(remoteConn, statistic.DefaultManager, metadata, rule, 0, int64(peekLen), true)
defer func(remoteConn C.Conn) { defer func(remoteConn C.Conn) {
_ = remoteConn.Close() _ = remoteConn.Close()
}(remoteConn) }(remoteConn)