From b96545649bdf7575c33e634d1e646f35fd6ab89e Mon Sep 17 00:00:00 2001 From: wyx2685 Date: Mon, 15 Jul 2024 00:33:54 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8Sing-box=E5=86=85=E6=A0=B8urltest=E5=87=BA?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/hy2/hook.go | 19 +++++++++++++++++++ core/hy2/node.go | 3 ++- core/sing/hook.go | 18 +++++++++++++++--- core/sing/sing.go | 2 +- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/core/hy2/hook.go b/core/hy2/hook.go index 4e4480f7..26f61315 100644 --- a/core/hy2/hook.go +++ b/core/hy2/hook.go @@ -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) diff --git a/core/hy2/node.go b/core/hy2/node.go index 2bfb9885..601ca2e2 100644 --- a/core/hy2/node.go +++ b/core/hy2/node.go @@ -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, }, } diff --git a/core/sing/hook.go b/core/sing/hook.go index ceb1a1e8..0b5e537f 100644 --- a/core/sing/hook.go +++ b/core/sing/hook.go @@ -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 { diff --git a/core/sing/sing.go b/core/sing/sing.go index 97b93f01..6ef16107 100644 --- a/core/sing/sing.go +++ b/core/sing/sing.go @@ -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(), diff --git a/go.mod b/go.mod index 19782daa..e187d3a6 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.22 toolchain go1.22.5 require ( - github.com/apernet/hysteria/core/v2 v2.5.1-0.20240702222552-458ee1386cc4 - github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240702222552-458ee1386cc4 + github.com/apernet/hysteria/core/v2 v2.5.1-0.20240710201643-b563f3981fc6 + github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240710201643-b563f3981fc6 github.com/beevik/ntp v1.2.0 github.com/fsnotify/fsnotify v1.7.0 github.com/go-acme/lego/v4 v4.17.4 @@ -19,7 +19,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.15.0 - github.com/xtls/xray-core v1.8.17-0.20240708033233-ce637c0c232c + github.com/xtls/xray-core v1.8.17 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.25.0 golang.org/x/sys v0.22.0 @@ -237,7 +237,7 @@ require ( github.com/vishvananda/netlink v1.2.1-beta.2.0.20230316163032-ced5aaba43e3 // indirect github.com/vishvananda/netns v0.0.4 // indirect github.com/vultr/govultr/v2 v2.17.2 // indirect - github.com/xtls/reality v0.0.0-20240429224917-ecc4401070cc // indirect + github.com/xtls/reality v0.0.0-20240712055506-48f0b2d5ed6d // indirect github.com/yandex-cloud/go-genproto v0.0.0-20240318083951-4fe6125f286e // indirect github.com/yandex-cloud/go-sdk v0.0.0-20240318084659-dfa50323a0b4 // indirect github.com/zeebo/blake3 v0.2.3 // indirect diff --git a/go.sum b/go.sum index ed1bf79c..75be7550 100644 --- a/go.sum +++ b/go.sum @@ -112,10 +112,10 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1 github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/apernet/hysteria/core/v2 v2.5.1-0.20240702222552-458ee1386cc4 h1:Dmxsp/nPY3ssoqkUO3OeEHE1zyE6hSix0lHtPirhAsg= -github.com/apernet/hysteria/core/v2 v2.5.1-0.20240702222552-458ee1386cc4/go.mod h1:swZ3kYbiPIYNW5amiJdrUCfn+JTzGlICDpQHJZtG/qc= -github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240702222552-458ee1386cc4 h1:OLvBwyUl9auGo6lXAO83rfIzNVrEZafid2q3XkYylwY= -github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240702222552-458ee1386cc4/go.mod h1:vUBc13ojigSWAaiwUGbGqbNGXv8PtmiRk5JmsLC0dbA= +github.com/apernet/hysteria/core/v2 v2.5.1-0.20240710201643-b563f3981fc6 h1:8JJ2Q9n3U49rHhMPfu4sAGMH4ppMh1ER4XErtF+1rH0= +github.com/apernet/hysteria/core/v2 v2.5.1-0.20240710201643-b563f3981fc6/go.mod h1:swZ3kYbiPIYNW5amiJdrUCfn+JTzGlICDpQHJZtG/qc= +github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240710201643-b563f3981fc6 h1:IDAE/lE/cc0ituffxHPmrlN02ibiRpi/TbQ1nLDnLD0= +github.com/apernet/hysteria/extras/v2 v2.5.1-0.20240710201643-b563f3981fc6/go.mod h1:vUBc13ojigSWAaiwUGbGqbNGXv8PtmiRk5JmsLC0dbA= github.com/apernet/quic-go v0.45.2-0.20240702221538-ed74cfbe8b6e h1:KBs8aBfKl5AKPKGpfn3bl0joDJXDq5fnH+AjFODiU+A= github.com/apernet/quic-go v0.45.2-0.20240702221538-ed74cfbe8b6e/go.mod h1:MjGWpXA31DZZWESdX3/PjIpSWIT1fOm8FNCqyXXFZFU= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= @@ -947,10 +947,10 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2 github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -github.com/xtls/reality v0.0.0-20240429224917-ecc4401070cc h1:0Nj8T1n7F7+v4vRVroaJIvY6R0vNABLfPH+lzPHRJvI= -github.com/xtls/reality v0.0.0-20240429224917-ecc4401070cc/go.mod h1:dm4y/1QwzjGaK17ofi0Vs6NpKAHegZky8qk6J2JJZAE= -github.com/xtls/xray-core v1.8.17-0.20240708033233-ce637c0c232c h1:HqAL1uLn/MdQGzgc7DQBYGJ0w8SelFRYnaCjoXMCsKE= -github.com/xtls/xray-core v1.8.17-0.20240708033233-ce637c0c232c/go.mod h1:OJ7TLCJp8If5yZVDIaOapGDe0ljBTYzwNpB9MA0ZIDY= +github.com/xtls/reality v0.0.0-20240712055506-48f0b2d5ed6d h1:+B97uD9uHLgAAulhigmys4BVwZZypzK7gPN3WtpgRJg= +github.com/xtls/reality v0.0.0-20240712055506-48f0b2d5ed6d/go.mod h1:dm4y/1QwzjGaK17ofi0Vs6NpKAHegZky8qk6J2JJZAE= +github.com/xtls/xray-core v1.8.17 h1:T+A/hkBB33P0sEGXDiuKrQMZUOYxbZ84JlsYfyRiK8w= +github.com/xtls/xray-core v1.8.17/go.mod h1:qEVGJD2suPN7EArG3r5EX6pYGV0QLiSRTlDMn0paJkc= github.com/yandex-cloud/go-genproto v0.0.0-20240318083951-4fe6125f286e h1:jLIqA7M9qY31g/Nw/5htVD0DFbxmLnlFZcHKJiG3osI= github.com/yandex-cloud/go-genproto v0.0.0-20240318083951-4fe6125f286e/go.mod h1:HEUYX/p8966tMUHHT+TsS0hF/Ca/NYwqprC5WXSDMfE= github.com/yandex-cloud/go-sdk v0.0.0-20240318084659-dfa50323a0b4 h1:wtzLQJmghkSUb1YkeFphIh7ST7NNVDaVOJZSAJcjMdw=