V2bX/common/counter/conn.go

142 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-07-27 21:13:11 -04:00
package counter
import (
2023-07-29 06:47:47 -04:00
"io"
2023-07-27 21:13:11 -04:00
"net"
2023-07-29 06:47:47 -04:00
"github.com/sagernet/sing/common/bufio"
2023-07-27 21:13:11 -04:00
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
)
type ConnCounter struct {
2023-07-29 06:47:47 -04:00
network.ExtendedConn
storage *TrafficStorage
readFunc network.CountFunc
writeFunc network.CountFunc
2023-07-27 21:13:11 -04:00
}
func NewConnCounter(conn net.Conn, s *TrafficStorage) net.Conn {
return &ConnCounter{
2023-07-29 06:47:47 -04:00
ExtendedConn: bufio.NewExtendedConn(conn),
storage: s,
readFunc: func(n int64) {
2023-08-27 13:56:01 -04:00
s.UpCounter.Add(n)
2023-07-29 06:47:47 -04:00
},
writeFunc: func(n int64) {
2023-08-27 13:56:01 -04:00
s.DownCounter.Add(n)
2023-07-29 06:47:47 -04:00
},
2023-07-27 21:13:11 -04:00
}
}
func (c *ConnCounter) Read(b []byte) (n int, err error) {
2023-07-29 06:47:47 -04:00
n, err = c.ExtendedConn.Read(b)
2023-08-27 13:56:01 -04:00
c.storage.UpCounter.Store(int64(n))
2023-07-27 21:13:11 -04:00
return
}
func (c *ConnCounter) Write(b []byte) (n int, err error) {
2023-07-29 06:47:47 -04:00
n, err = c.ExtendedConn.Write(b)
2023-08-27 13:56:01 -04:00
c.storage.DownCounter.Store(int64(n))
2023-07-27 21:13:11 -04:00
return
}
2023-07-29 06:47:47 -04:00
func (c *ConnCounter) ReadBuffer(buffer *buf.Buffer) error {
err := c.ExtendedConn.ReadBuffer(buffer)
if err != nil {
return err
}
if buffer.Len() > 0 {
2023-08-27 13:56:01 -04:00
c.storage.UpCounter.Add(int64(buffer.Len()))
2023-07-29 06:47:47 -04:00
}
return nil
}
func (c *ConnCounter) WriteBuffer(buffer *buf.Buffer) error {
dataLen := int64(buffer.Len())
err := c.ExtendedConn.WriteBuffer(buffer)
if err != nil {
return err
}
if dataLen > 0 {
2023-08-27 13:56:01 -04:00
c.storage.DownCounter.Add(dataLen)
2023-07-29 06:47:47 -04:00
}
return nil
}
func (c *ConnCounter) UnwrapReader() (io.Reader, []network.CountFunc) {
return c.ExtendedConn, []network.CountFunc{
c.readFunc,
}
}
func (c *ConnCounter) UnwrapWriter() (io.Writer, []network.CountFunc) {
return c.ExtendedConn, []network.CountFunc{
c.writeFunc,
}
}
func (c *ConnCounter) Upstream() any {
return c.ExtendedConn
}
2023-07-27 21:13:11 -04:00
type PacketConnCounter struct {
network.PacketConn
2023-07-29 06:47:47 -04:00
storage *TrafficStorage
readFunc network.CountFunc
writeFunc network.CountFunc
2023-07-27 21:13:11 -04:00
}
func NewPacketConnCounter(conn network.PacketConn, s *TrafficStorage) network.PacketConn {
return &PacketConnCounter{
PacketConn: conn,
storage: s,
2023-07-29 06:47:47 -04:00
readFunc: func(n int64) {
2023-08-27 13:56:01 -04:00
s.UpCounter.Add(n)
2023-07-29 06:47:47 -04:00
},
writeFunc: func(n int64) {
2023-08-27 13:56:01 -04:00
s.DownCounter.Add(n)
2023-07-29 06:47:47 -04:00
},
2023-07-27 21:13:11 -04:00
}
}
func (p *PacketConnCounter) ReadPacket(buff *buf.Buffer) (destination M.Socksaddr, err error) {
destination, err = p.PacketConn.ReadPacket(buff)
if err != nil {
return
}
2023-08-27 13:56:01 -04:00
p.storage.UpCounter.Add(int64(buff.Len()))
2023-07-27 21:13:11 -04:00
return
}
func (p *PacketConnCounter) WritePacket(buff *buf.Buffer, destination M.Socksaddr) (err error) {
2023-07-29 06:47:47 -04:00
n := buff.Len()
2023-07-27 21:13:11 -04:00
err = p.PacketConn.WritePacket(buff, destination)
if err != nil {
return
}
2023-07-29 06:47:47 -04:00
if n > 0 {
2023-08-27 13:56:01 -04:00
p.storage.DownCounter.Add(int64(n))
2023-07-29 06:47:47 -04:00
}
2023-07-27 21:13:11 -04:00
return
}
2023-07-29 06:47:47 -04:00
func (p *PacketConnCounter) UnwrapPacketReader() (network.PacketReader, []network.CountFunc) {
return p.PacketConn, []network.CountFunc{
p.readFunc,
}
}
func (p *PacketConnCounter) UnwrapPacketWriter() (network.PacketWriter, []network.CountFunc) {
return p.PacketConn, []network.CountFunc{
p.writeFunc,
}
}
func (p *PacketConnCounter) Upstream() any {
return p.PacketConn
}