add singbox core(just started)

This commit is contained in:
Yuzuki616
2023-07-28 09:13:11 +08:00
parent 989a9a1ba0
commit 2812b366b3
28 changed files with 1110 additions and 105 deletions

64
common/counter/conn.go Normal file
View File

@@ -0,0 +1,64 @@
package counter
import (
"net"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
)
type ConnCounter struct {
net.Conn
storage *TrafficStorage
}
func NewConnCounter(conn net.Conn, s *TrafficStorage) net.Conn {
return &ConnCounter{
Conn: conn,
storage: s,
}
}
func (c *ConnCounter) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b)
c.storage.DownCounter.Store(int64(n))
return
}
func (c *ConnCounter) Write(b []byte) (n int, err error) {
n, err = c.Conn.Write(b)
c.storage.UpCounter.Store(int64(n))
return
}
type PacketConnCounter struct {
network.PacketConn
storage *TrafficStorage
}
func NewPacketConnCounter(conn network.PacketConn, s *TrafficStorage) network.PacketConn {
return &PacketConnCounter{
PacketConn: conn,
storage: s,
}
}
func (p *PacketConnCounter) ReadPacket(buff *buf.Buffer) (destination M.Socksaddr, err error) {
destination, err = p.PacketConn.ReadPacket(buff)
if err != nil {
return
}
p.storage.DownCounter.Add(int64(buff.Len()))
return
}
func (p *PacketConnCounter) WritePacket(buff *buf.Buffer, destination M.Socksaddr) (err error) {
err = p.PacketConn.WritePacket(buff, destination)
if err != nil {
return
}
p.storage.UpCounter.Add(int64(buff.Len()))
return
}

85
common/counter/traffic.go Normal file
View File

@@ -0,0 +1,85 @@
package counter
import (
"sync"
"sync/atomic"
)
type TrafficCounter struct {
counters map[string]*TrafficStorage
lock sync.RWMutex
}
type TrafficStorage struct {
UpCounter atomic.Int64
DownCounter atomic.Int64
}
func NewTrafficCounter() *TrafficCounter {
return &TrafficCounter{
counters: map[string]*TrafficStorage{},
}
}
func (c *TrafficCounter) GetCounter(id string) *TrafficStorage {
c.lock.RLock()
cts, ok := c.counters[id]
c.lock.RUnlock()
if !ok {
cts = &TrafficStorage{}
c.counters[id] = cts
}
return cts
}
func (c *TrafficCounter) GetUpCount(id string) int64 {
c.lock.RLock()
cts, ok := c.counters[id]
c.lock.RUnlock()
if ok {
return cts.UpCounter.Load()
}
return 0
}
func (c *TrafficCounter) GetDownCount(id string) int64 {
c.lock.RLock()
cts, ok := c.counters[id]
c.lock.RUnlock()
if ok {
return cts.DownCounter.Load()
}
return 0
}
func (c *TrafficCounter) Reset(id string) {
c.lock.RLock()
cts := c.GetCounter(id)
c.lock.RUnlock()
cts.UpCounter.Store(0)
cts.DownCounter.Store(0)
}
func (c *TrafficCounter) Delete(id string) {
c.lock.Lock()
delete(c.counters, id)
c.lock.Unlock()
}
func (c *TrafficCounter) Rx(id string, n int) {
cts := c.GetCounter(id)
cts.DownCounter.Add(int64(n))
}
func (c *TrafficCounter) Tx(id string, n int) {
cts := c.GetCounter(id)
cts.UpCounter.Add(int64(n))
}
func (c *TrafficCounter) IncConn(auth string) {
return
}
func (c *TrafficCounter) DecConn(auth string) {
return
}