Skip to content

Commit

Permalink
Fix:修复无法使用Sing-box内核urltest出口的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wyx2685 committed Jul 14, 2024
1 parent 8757b95 commit b965456
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
19 changes: 19 additions & 0 deletions core/hy2/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@ 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
}

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)
Expand Down
3 changes: 2 additions & 1 deletion core/hy2/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
18 changes: 15 additions & 3 deletions core/sing/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,19 +61,26 @@ 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 {
return nil
}

func (h *HookServer) Close() error {
h.urlTestHistory.Close()
return nil
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/sing/sing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit b965456

Please sign in to comment.