2018-07-26 00:04:59 +08:00
|
|
|
package tunnel
|
2018-06-17 22:41:32 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// TrafficTrack record traffic of net.Conn
|
2018-06-17 22:41:32 +08:00
|
|
|
type TrafficTrack struct {
|
|
|
|
net.Conn
|
|
|
|
traffic *C.Traffic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *TrafficTrack) Read(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Read(b)
|
|
|
|
tt.traffic.Down() <- int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *TrafficTrack) Write(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Write(b)
|
|
|
|
tt.traffic.Up() <- int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func newTrafficTrack(conn net.Conn, traffic *C.Traffic) *TrafficTrack {
|
2018-06-17 22:41:32 +08:00
|
|
|
return &TrafficTrack{traffic: traffic, Conn: conn}
|
|
|
|
}
|