mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-05 05:00:08 +00:00
update sing-box support
This commit is contained in:
@@ -2,6 +2,12 @@ package sing
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Yuzuki616/V2bX/common/rate"
|
||||
|
||||
"github.com/Yuzuki616/V2bX/limiter"
|
||||
|
||||
"github.com/Yuzuki616/V2bX/common/counter"
|
||||
"github.com/inazumav/sing-box/adapter"
|
||||
@@ -17,7 +23,7 @@ func NewHookServer(logger log.Logger) *HookServer {
|
||||
return &HookServer{
|
||||
hooker: &Hooker{
|
||||
logger: logger,
|
||||
counter: make(map[string]*counter.TrafficCounter),
|
||||
counter: sync.Map{},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -40,19 +46,37 @@ func (h *HookServer) Hooker() *Hooker {
|
||||
|
||||
type Hooker struct {
|
||||
logger log.Logger
|
||||
counter map[string]*counter.TrafficCounter
|
||||
counter sync.Map
|
||||
}
|
||||
|
||||
func (h *Hooker) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn {
|
||||
if c, ok := h.counter[inbound]; ok {
|
||||
l, err := limiter.GetLimiter(inbound)
|
||||
if err != nil {
|
||||
log.Error("get limiter for ", inbound, " error: ", err)
|
||||
}
|
||||
ip, _, _ := strings.Cut(conn.RemoteAddr().String(), ":")
|
||||
if b, r := l.CheckLimit(user, ip, true); r {
|
||||
conn.Close()
|
||||
h.logger.Error("[", inbound, "] ", "Limited ", user, " by ip or conn")
|
||||
return conn
|
||||
} else if b != nil {
|
||||
conn = rate.NewConnRateLimiter(conn, b)
|
||||
}
|
||||
if c, ok := h.counter.Load(inbound); ok {
|
||||
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user))
|
||||
} else {
|
||||
c := counter.NewTrafficCounter()
|
||||
h.counter.Store(inbound, c)
|
||||
return counter.NewConnCounter(conn, c.GetCounter(user))
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func (h *Hooker) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn {
|
||||
if c, ok := h.counter[inbound]; ok {
|
||||
if c, ok := h.counter.Load(inbound); ok {
|
||||
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user))
|
||||
} else {
|
||||
c := counter.NewTrafficCounter()
|
||||
h.counter.Store(inbound, c)
|
||||
return counter.NewPacketConnCounter(conn, c.GetCounter(user))
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user