Fix:修复无法使用Sing-box内核urltest出口的问题

This commit is contained in:
wyx2685
2024-07-15 00:33:54 +09:00
parent 8757b955a6
commit b96545649b
6 changed files with 49 additions and 17 deletions

View File

@@ -4,10 +4,14 @@ import (
"sync"
"github.com/InazumaV/V2bX/common/counter"
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/limiter"
"go.uber.org/zap"
)
type HookServer struct {
Tag string
logger *zap.Logger
Counter sync.Map
}
@@ -15,6 +19,21 @@ func (h *HookServer) LogTraffic(id string, tx, rx uint64) (ok bool) {
var c interface{}
var exists bool
limiterinfo, err := limiter.GetLimiter(h.Tag)
if err != nil {
h.logger.Error("Get limiter error", zap.String("tag", h.Tag), zap.Error(err))
return false
}
userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(h.Tag, id))
if ok {
userlimitInfo := userLimit.(*limiter.UserLimitInfo)
if userlimitInfo.OverLimit {
userlimitInfo.OverLimit = false
return false
}
}
if c, exists = h.Counter.Load(h.Tag); !exists {
c = counter.NewTrafficCounter()
h.Counter.Store(h.Tag, c)

View File

@@ -38,7 +38,8 @@ func (h *Hysteria2) AddNode(tag string, info *panel.NodeInfo, config *conf.Optio
logger: h.Logger,
},
TrafficLogger: &HookServer{
Tag: tag,
Tag: tag,
logger: h.Logger,
},
}

View File

@@ -17,9 +17,14 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
)
var _ adapter.ClashServer = (*HookServer)(nil)
type HookServer struct {
ctx context.Context
urlTestHistory *urltest.HistoryStorage
EnableConnClear bool
counter sync.Map
connClears sync.Map
@@ -56,12 +61,18 @@ func (h *HookServer) ModeList() []string {
return nil
}
func NewHookServer(enableClear bool) *HookServer {
return &HookServer{
func NewHookServer(ctx context.Context, enableClear bool) *HookServer {
server := &HookServer{
ctx: ctx,
EnableConnClear: enableClear,
counter: sync.Map{},
connClears: sync.Map{},
}
server.urlTestHistory = service.PtrFromContext[urltest.HistoryStorage](ctx)
if server.urlTestHistory == nil {
server.urlTestHistory = urltest.NewHistoryStorage()
}
return server
}
func (h *HookServer) Start() error {
@@ -69,6 +80,7 @@ func (h *HookServer) Start() error {
}
func (h *HookServer) Close() error {
h.urlTestHistory.Close()
return nil
}
@@ -193,7 +205,7 @@ func (h *HookServer) CacheFile() adapter.CacheFile {
return nil
}
func (h *HookServer) HistoryStorage() *urltest.HistoryStorage {
return nil
return h.urlTestHistory
}
func (h *HookServer) StoreFakeIP() bool {

View File

@@ -69,7 +69,7 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
if err != nil {
return nil, err
}
hs := NewHookServer(c.SingConfig.EnableConnClear)
hs := NewHookServer(b.Router().GetCtx(), c.SingConfig.EnableConnClear)
b.Router().SetClashServer(hs)
return &Sing{
ctx: b.Router().GetCtx(),