V2bX/core/sing/hook.go

140 lines
3.4 KiB
Go
Raw Normal View History

2023-07-27 21:13:11 -04:00
package sing
import (
"context"
2023-10-13 03:32:06 -04:00
"io"
2023-07-27 21:13:11 -04:00
"net"
2023-07-29 06:47:47 -04:00
"sync"
"github.com/InazumaV/V2bX/common/format"
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/common/rate"
2023-07-29 06:47:47 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/limiter"
2023-07-27 21:13:11 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/common/counter"
2023-10-26 01:06:43 -04:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
2023-07-27 21:13:11 -04:00
N "github.com/sagernet/sing/common/network"
)
2024-12-12 16:22:44 -05:00
var _ adapter.ConnectionTracker = (*HookServer)(nil)
2023-07-27 21:13:11 -04:00
type HookServer struct {
2023-10-13 03:32:06 -04:00
EnableConnClear bool
counter sync.Map
connClears sync.Map
}
type ConnClear struct {
lock sync.RWMutex
conns map[int]io.Closer
}
func (c *ConnClear) AddConn(cn io.Closer) (key int) {
c.lock.Lock()
defer c.lock.Unlock()
key = len(c.conns)
c.conns[key] = cn
return
}
func (c *ConnClear) DelConn(key int) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.conns, key)
}
func (c *ConnClear) ClearConn() {
c.lock.Lock()
defer c.lock.Unlock()
for _, c := range c.conns {
c.Close()
}
2023-07-27 21:13:11 -04:00
}
2023-09-13 14:25:33 -04:00
func (h *HookServer) ModeList() []string {
return nil
}
2024-12-12 16:22:44 -05:00
func NewHookServer(enableClear bool) *HookServer {
server := &HookServer{
2023-10-13 03:32:06 -04:00
EnableConnClear: enableClear,
counter: sync.Map{},
connClears: sync.Map{},
2023-07-27 21:13:11 -04:00
}
return server
2023-07-27 21:13:11 -04:00
}
2024-12-12 16:22:44 -05:00
func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) net.Conn {
l, err := limiter.GetLimiter(m.Inbound)
2023-07-29 06:47:47 -04:00
if err != nil {
2023-10-26 01:06:43 -04:00
log.Warn("get limiter for ", m.Inbound, " error: ", err)
2024-12-12 16:22:44 -05:00
return conn
}
ip := m.Source.Addr.String()
2024-07-24 06:50:55 -04:00
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, true, true); r {
conn.Close()
2023-10-26 01:06:43 -04:00
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
2024-12-12 16:22:44 -05:00
return conn
2023-07-29 06:47:47 -04:00
} else if b != nil {
conn = rate.NewConnRateLimiter(conn, b)
}
2023-10-13 03:32:06 -04:00
if h.EnableConnClear {
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
2024-12-12 16:22:44 -05:00
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User))
2023-07-29 06:47:47 -04:00
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
2024-12-12 16:22:44 -05:00
return counter.NewConnCounter(conn, c.GetCounter(m.User))
2023-07-27 21:13:11 -04:00
}
}
2024-12-12 16:22:44 -05:00
func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) N.PacketConn {
l, err := limiter.GetLimiter(m.Inbound)
if err != nil {
2023-10-26 01:06:43 -04:00
log.Warn("get limiter for ", m.Inbound, " error: ", err)
2024-12-12 16:22:44 -05:00
return conn
}
ip := m.Source.Addr.String()
2024-07-29 12:28:39 -04:00
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, false, false); r {
conn.Close()
2023-10-26 01:06:43 -04:00
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
2024-12-12 16:22:44 -05:00
return conn
} else if b != nil {
//conn = rate.NewPacketConnCounter(conn, b)
}
2023-10-13 03:32:06 -04:00
if h.EnableConnClear {
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
2024-12-12 16:22:44 -05:00
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User))
2023-07-29 06:47:47 -04:00
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
2024-12-12 16:22:44 -05:00
return counter.NewPacketConnCounter(conn, c.GetCounter(m.User))
2023-07-27 21:13:11 -04:00
}
}
2023-10-13 03:32:06 -04:00
func (h *HookServer) ClearConn(inbound string, user string) {
if v, ok := h.connClears.Load(inbound + user); ok {
v.(*ConnClear).ClearConn()
h.connClears.Delete(inbound + user)
}
}