change v2rayServer to clashServer, and complete limit for sing

This commit is contained in:
yuzuki999 2023-08-04 23:25:27 +08:00
parent c61b4155ce
commit e93706445f
2 changed files with 96 additions and 34 deletions

View File

@ -1,8 +1,9 @@
package sing package sing
import ( import (
"context"
"github.com/inazumav/sing-box/common/urltest"
"net" "net"
"strings"
"sync" "sync"
"github.com/InazumaV/V2bX/common/rate" "github.com/InazumaV/V2bX/common/rate"
@ -16,15 +17,14 @@ import (
) )
type HookServer struct { type HookServer struct {
hooker *Hooker logger log.Logger
counter sync.Map
} }
func NewHookServer(logger log.Logger) *HookServer { func NewHookServer(logger log.Logger) *HookServer {
return &HookServer{ return &HookServer{
hooker: &Hooker{ logger: logger,
logger: logger, counter: sync.Map{},
counter: sync.Map{},
},
} }
} }
@ -36,47 +36,109 @@ func (h *HookServer) Close() error {
return nil return nil
} }
func (h *HookServer) StatsService() adapter.V2RayStatsService { func (h *HookServer) PreStart() error {
return h.hooker return nil
} }
func (h *HookServer) Hooker() *Hooker { func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule) (net.Conn, adapter.Tracker) {
return h.hooker l, err := limiter.GetLimiter(m.Inbound)
}
type Hooker struct {
logger log.Logger
counter sync.Map
}
func (h *Hooker) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn {
l, err := limiter.GetLimiter(inbound)
if err != nil { if err != nil {
log.Error("get limiter for ", inbound, " error: ", err) log.Error("get limiter for ", m.Inbound, " error: ", err)
} }
ip, _, _ := strings.Cut(conn.RemoteAddr().String(), ":") if l.CheckDomainRule(m.Domain) {
if b, r := l.CheckLimit(user, ip, true); r {
conn.Close() conn.Close()
h.logger.Error("[", inbound, "] ", "Limited ", user, " by ip or conn") h.logger.Error("[", m.Inbound, "] ",
return conn "Limited ", m.User, "access to", m.Domain, " by domain rule")
return conn, &Tracker{l: func() {}}
}
if l.CheckProtocolRule(m.Protocol) {
conn.Close()
h.logger.Error("[", m.Inbound, "] ",
"Limited ", m.User, "use", m.Domain, " by protocol rule")
return conn, &Tracker{l: func() {}}
}
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(m.User, ip, true); r {
conn.Close()
h.logger.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn, &Tracker{l: func() {}}
} else if b != nil { } else if b != nil {
conn = rate.NewConnRateLimiter(conn, b) conn = rate.NewConnRateLimiter(conn, b)
} }
if c, ok := h.counter.Load(inbound); ok { t := &Tracker{
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user)) l: func() {
l.ConnLimiter.DelConnCount(m.User, ip)
},
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.Inbound)), t
} else { } else {
c := counter.NewTrafficCounter() c := counter.NewTrafficCounter()
h.counter.Store(inbound, c) h.counter.Store(m.Inbound, c)
return counter.NewConnCounter(conn, c.GetCounter(user)) return counter.NewConnCounter(conn, c.GetCounter(m.Inbound)), t
} }
} }
func (h *Hooker) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn { func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule) (N.PacketConn, adapter.Tracker) {
if c, ok := h.counter.Load(inbound); ok { t := &Tracker{
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user)) l: func() {},
}
l, err := limiter.GetLimiter(m.Inbound)
if err != nil {
log.Error("get limiter for ", m.Inbound, " error: ", err)
}
if l.CheckDomainRule(m.Domain) {
conn.Close()
h.logger.Error("[", m.Inbound, "] ",
"Limited ", m.User, "access to", m.Domain, " by domain rule")
return conn, t
}
if l.CheckProtocolRule(m.Protocol) {
conn.Close()
h.logger.Error("[", m.Inbound, "] ",
"Limited ", m.User, "use", m.Domain, " by protocol rule")
return conn, t
}
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(m.User, ip, true); r {
conn.Close()
h.logger.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn, &Tracker{l: func() {}}
} else if b != nil {
conn = rate.NewPacketConnCounter(conn, b)
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User)), t
} else { } else {
c := counter.NewTrafficCounter() c := counter.NewTrafficCounter()
h.counter.Store(inbound, c) h.counter.Store(m.Inbound, c)
return counter.NewPacketConnCounter(conn, c.GetCounter(user)) return counter.NewPacketConnCounter(conn, c.GetCounter(m.User)), t
} }
} }
// not need
func (h *HookServer) Mode() string {
return ""
}
func (h *HookServer) StoreSelected() bool {
return false
}
func (h *HookServer) CacheFile() adapter.ClashCacheFile {
return nil
}
func (h *HookServer) HistoryStorage() *urltest.HistoryStorage {
return nil
}
func (h *HookServer) StoreFakeIP() bool {
return false
}
type Tracker struct {
l func()
}
func (t *Tracker) Leave() {
t.l()
}

View File

@ -129,7 +129,7 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
if err != nil { if err != nil {
return nil, E.Cause(err, "create v2ray api server") return nil, E.Cause(err, "create v2ray api server")
} }
router.SetV2RayServer(server) router.SetClashServer(server)
return &Box{ return &Box{
router: router, router: router,
inbounds: inMap, inbounds: inMap,