V2bX/core/sing/hook.go
wyx2685 2f1362067b
Some checks failed
Build and Release / build (386, freebsd) (push) Has been cancelled
Build and Release / build (386, linux) (push) Has been cancelled
Build and Release / build (386, windows) (push) Has been cancelled
Build and Release / build (amd64, darwin) (push) Has been cancelled
Build and Release / build (amd64, freebsd) (push) Has been cancelled
Build and Release / build (amd64, linux) (push) Has been cancelled
Build and Release / build (amd64, windows) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
sing内核支持面板rule规则
2025-01-10 16:33:05 +09:00

126 lines
3.2 KiB
Go

package sing
import (
"context"
"fmt"
"net"
"sync"
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/common/rate"
"github.com/InazumaV/V2bX/limiter"
"github.com/InazumaV/V2bX/common/counter"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.ConnectionTracker = (*HookServer)(nil)
type HookServer struct {
counter sync.Map
}
func (h *HookServer) ModeList() []string {
return nil
}
func NewHookServer() *HookServer {
server := &HookServer{
counter: sync.Map{},
}
return server
}
func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) net.Conn {
l, err := limiter.GetLimiter(m.Inbound)
if err != nil {
log.Warn("get limiter for ", m.Inbound, " error: ", err)
return conn
}
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, true, true); r {
conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn
} else if b != nil {
conn = rate.NewConnRateLimiter(conn, b)
}
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Destination.Network()
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User))
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
return counter.NewConnCounter(conn, c.GetCounter(m.User))
}
}
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 {
log.Warn("get limiter for ", m.Inbound, " error: ", err)
return conn
}
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, false, false); r {
conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn
} else if b != nil {
//conn = rate.NewPacketConnCounter(conn, b)
}
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Destination.Network()
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User))
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
return counter.NewPacketConnCounter(conn, c.GetCounter(m.User))
}
}