diff --git a/db/ConfigBuilder.cpp b/db/ConfigBuilder.cpp index b96d51e..91fa95c 100644 --- a/db/ConfigBuilder.cpp +++ b/db/ConfigBuilder.cpp @@ -853,18 +853,6 @@ namespace NekoRay { routeObj.remove("auto_detect_interface"); } status->result->coreConfig.insert("route", routeObj); - - // api - if (!status->forTest && !status->forExport && dataStore->traffic_loop_interval > 0) { - status->result->coreConfig.insert("experimental", QJsonObject{ - {"v2ray_api", QJsonObject{ - {"listen", "127.0.0.1:" + Int2String(dataStore->inbound_socks_port + 10)}, - {"stats", QJsonObject{ - {"enabled", true}, - {"outbounds", QJsonArray{tagProxy, "bypass"}}, - }}}}, - }); - } } QString WriteVPNSingBoxConfig() { diff --git a/docs/Build_Core.md b/docs/Build_Core.md new file mode 100644 index 0000000..1593479 --- /dev/null +++ b/docs/Build_Core.md @@ -0,0 +1,25 @@ +## 构建 nekoray_core & nekobox_core + +### 简易构建 + +1. 把 `Matsuridayo/v2ray-core` 置于 `../` +2. 进入 `go/cmd/nekoray_core` 文件夹 `go build` 得到 `nekoray_core`。 +3. 进入 `go/cmd/nekobox_core` 文件夹 `go build` 得到 `nekobox_core`。 + +具体编译过程请参考 `libs/get_source.sh` `libs/build_go.sh` + +非官方构建无需编译 `updater` `launcher` + +### 常规构建 + +1. 把 `Matsuridayo/v2ray-core` 置于 `../` +2. `GOOS=windows GOARCH=amd64 bash libs/build_go.sh` + +具体支持的 GOOS 和 GOARCH 请看 `libs/build_go.sh` + +### sing-box tags + +默认使用的 tags 以下变动外,其余均与官方版 sing-box 构建相同。具体使用的 tags 请看 `libs/build_go.sh` + +1. 不包含 `reality_server` +2. `clash_api` 替换为 `v2ray_api` diff --git a/docs/Build_Linux.md b/docs/Build_Linux.md index 9c736ed..50a8828 100644 --- a/docs/Build_Linux.md +++ b/docs/Build_Linux.md @@ -73,12 +73,6 @@ ninja 编译完成后得到 `nekoray` -### Go 部分 +### Go 部分编译 -1. 把 `Matsuridayo/v2ray-core` 置于 `../` -2. 进入 `go/cmd/nekoray_core` 文件夹 `go build` 得到 `nekoray_core`。 -3. 进入 `go/cmd/nekobox_core` 文件夹 `go build` 得到 `nekobox_core`。 - -具体编译过程请参考 `libs/get_source.sh` `libs/build_go.sh` - -非官方构建无需编译 `updater` `launcher` +请看 [Build_Core.md](./Build_Core.md) diff --git a/docs/Build_MacOS.md b/docs/Build_MacOS.md index 24f781a..157e0b2 100644 --- a/docs/Build_MacOS.md +++ b/docs/Build_MacOS.md @@ -35,9 +35,9 @@ cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DNKR_PACKAGE_MACOS=1 .. ninja ``` -## Go 部分 +### Go 部分编译 -同 Build_Linux +请看 [Build_Core.md](./Build_Core.md) ## 打包 diff --git a/docs/Build_Windows.md b/docs/Build_Windows.md index 4c412cf..3335c4e 100644 --- a/docs/Build_Windows.md +++ b/docs/Build_Windows.md @@ -63,4 +63,4 @@ ninja ### Go 部分编译 -同 Linux +请看 [Build_Core.md](./Build_Core.md) diff --git a/go/cmd/nekobox_core/box_main/cmd_check.go b/go/cmd/nekobox_core/box_main/cmd_check.go index 1cb55d0..dada044 100644 --- a/go/cmd/nekobox_core/box_main/cmd_check.go +++ b/go/cmd/nekobox_core/box_main/cmd_check.go @@ -31,7 +31,7 @@ func check() error { return err } ctx, cancel := context.WithCancel(context.Background()) - _, err = box.New(ctx, options) + _, err = box.New(ctx, options, nil) cancel() return err } diff --git a/go/cmd/nekobox_core/box_main/cmd_run.go b/go/cmd/nekobox_core/box_main/cmd_run.go index 60bc7b2..5cd0de5 100644 --- a/go/cmd/nekobox_core/box_main/cmd_run.go +++ b/go/cmd/nekobox_core/box_main/cmd_run.go @@ -74,7 +74,7 @@ func Create(nekoConfigContent []byte, forceDisableColor bool) (*box.Box, context options.Log.DisableColor = true } ctx, cancel := context.WithCancel(context.Background()) - instance, err := box.New(ctx, options) + instance, err := box.New(ctx, options, nil) if err != nil { cancel() return nil, nil, E.Cause(err, "create service") diff --git a/go/cmd/nekobox_core/core_box.go b/go/cmd/nekobox_core/core_box.go index 42cb9f6..fa3aa66 100644 --- a/go/cmd/nekobox_core/core_box.go +++ b/go/cmd/nekobox_core/core_box.go @@ -14,7 +14,6 @@ import ( box "github.com/sagernet/sing-box" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/dialer" - "github.com/sagernet/sing-box/experimental/v2rayapi" "github.com/sagernet/sing/common/metadata" "github.com/sagernet/sing/common/network" ) @@ -22,8 +21,6 @@ import ( var instance *box.Box var instance_cancel context.CancelFunc -var box_v2ray_service *v2rayapi.StatsService - // Use sing-box instead of libcore & v2ray func setupCore() { diff --git a/go/cmd/nekobox_core/go.mod b/go/cmd/nekobox_core/go.mod index 581c2f2..01b5bb6 100644 --- a/go/cmd/nekobox_core/go.mod +++ b/go/cmd/nekobox_core/go.mod @@ -5,10 +5,10 @@ go 1.19 require ( github.com/dustin/go-humanize v1.0.1 github.com/gofrs/uuid v4.4.0+incompatible - github.com/sagernet/sing v0.1.8-0.20230221060643-3401d210384b - github.com/sagernet/sing-box v1.1.6-0.20230221125425-e99741159bcc + github.com/sagernet/sing v0.1.8-0.20230301160041-9fab0a9f4304 + github.com/sagernet/sing-box v1.1.6-0.20230301163156-e8802357e1e7 github.com/sagernet/sing-dns v0.1.4 - github.com/sagernet/sing-tun v0.1.1 + github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9 github.com/spf13/cobra v1.6.1 golang.org/x/sys v0.5.0 neko v1.0.0 @@ -50,16 +50,19 @@ require ( github.com/quic-go/qtls-go1-18 v0.2.0 // indirect github.com/quic-go/qtls-go1-19 v0.2.0 // indirect github.com/quic-go/qtls-go1-20 v0.1.0 // indirect + github.com/sagernet/badhttp v0.0.0-20230228035330-e77eb9a689fd // indirect + github.com/sagernet/badhttp2 v0.0.0-20230228040529-408b0b8e774d // indirect github.com/sagernet/cloudflare-tls v0.0.0-20221031050923-d70792f4c3a0 // indirect github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 // indirect github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // indirect github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 // indirect + github.com/sagernet/reality v0.0.0-20230228045158-d3e085a8e5d1 // indirect github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 // indirect github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587 // indirect github.com/sagernet/sing-vmess v0.1.2 // indirect github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 // indirect github.com/sagernet/tfo-go v0.0.0-20230207095944-549363a7327d // indirect - github.com/sagernet/utls v0.0.0-20230220130002-c08891932056 // indirect + github.com/sagernet/utls v0.0.0-20230225061716-536a007c8b01 // indirect github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e // indirect github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go/cmd/nekobox_core/go.sum b/go/cmd/nekobox_core/go.sum index 436cf2b..8797dd8 100644 --- a/go/cmd/nekobox_core/go.sum +++ b/go/cmd/nekobox_core/go.sum @@ -140,6 +140,10 @@ github.com/quic-go/qtls-go1-19 v0.2.0/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05 github.com/quic-go/qtls-go1-20 v0.1.0 h1:d1PK3ErFy9t7zxKsG3NXBJXZjp/kMLoIb3y/kV54oAI= github.com/quic-go/qtls-go1-20 v0.1.0/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagernet/badhttp v0.0.0-20230228035330-e77eb9a689fd h1:nv3WtVfPGX+i2Ip/TR+Yd3LO1xFSpKUgWmYsXxKJ6vM= +github.com/sagernet/badhttp v0.0.0-20230228035330-e77eb9a689fd/go.mod h1:geEm+9ZyRMZ8THRH0XSexeStaMDtkFBf4J1nMK92mAY= +github.com/sagernet/badhttp2 v0.0.0-20230228040529-408b0b8e774d h1:RmBTGU4SvqxX57SDvpQtrkiQDaCnr4J/DMYMrUBL7OQ= +github.com/sagernet/badhttp2 v0.0.0-20230228040529-408b0b8e774d/go.mod h1:Ag8QdZjLwuy3V2pyOcqlKz4Cdh0wKEOFlYgR3wPUGkI= github.com/sagernet/cloudflare-tls v0.0.0-20221031050923-d70792f4c3a0 h1:KyhtFFt1Jtp5vW2ohNvstvQffTOQ/s5vENuGXzdA+TM= github.com/sagernet/cloudflare-tls v0.0.0-20221031050923-d70792f4c3a0/go.mod h1:D4SFEOkJK+4W1v86ZhX0jPM0rAL498fyQAChqMtes/I= github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 h1:5+m7c6AkmAylhauulqN/c5dnh8/KssrE9c93TQrXldA= @@ -148,28 +152,30 @@ github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6E github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 h1:tztuJB+giOWNRKQEBVY2oI3PsheTooMdh+/yxemYQYY= github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32/go.mod h1:QMCkxXAC3CvBgDZVIJp43NWTuwGBScCzMLVLynjERL8= +github.com/sagernet/reality v0.0.0-20230228045158-d3e085a8e5d1 h1:8mSzchN6DkM26JKLalPwj2KLMIsEjzlp/pYgznlKE2Q= +github.com/sagernet/reality v0.0.0-20230228045158-d3e085a8e5d1/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.1.8-0.20230221060643-3401d210384b h1:Ji2AfGlc4j9AitobOx4k3BCj7eS5nSxL1cgaL81zvlo= -github.com/sagernet/sing v0.1.8-0.20230221060643-3401d210384b/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing-box v1.1.6-0.20230221125425-e99741159bcc h1:9SE5ADb0cTietsRAy0VTne/yfMZmN4EDQtx1VjyVh8g= -github.com/sagernet/sing-box v1.1.6-0.20230221125425-e99741159bcc/go.mod h1:QMFa94JFQl59MCX78HIJZWFRH/GW7J0kER7WktgroFQ= +github.com/sagernet/sing v0.1.8-0.20230301160041-9fab0a9f4304 h1:Yi7wqvHv+ZFLHPQn1DiSAdnMZkb5Cra7Y4s8vzBLrFE= +github.com/sagernet/sing v0.1.8-0.20230301160041-9fab0a9f4304/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= +github.com/sagernet/sing-box v1.1.6-0.20230301163156-e8802357e1e7 h1:HriCIy59evILW5YHnzkprGgeNNY5DTzOYDAZ4IRRn6g= +github.com/sagernet/sing-box v1.1.6-0.20230301163156-e8802357e1e7/go.mod h1:x7krCFYIGdX09BDFXNnwwWy5+ZL+wVGW08jsdTNRg0c= github.com/sagernet/sing-dns v0.1.4 h1:7VxgeoSCiiazDSaXXQVcvrTBxFpOePPq/4XdgnUDN+0= github.com/sagernet/sing-dns v0.1.4/go.mod h1:1+6pCa48B1AI78lD+/i/dLgpw4MwfnsSpZo0Ds8wzzk= github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 h1:qS39eA4C7x+zhEkySbASrtmb6ebdy5v0y2M6mgkmSO0= github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU= github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587 h1:OjIXlHT2bblZfp+ciupM4xY9+Ccpj9FsuHRtKRBv+Pg= github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587/go.mod h1:Kn1VUIprdkwCgkS6SXYaLmIpKzQbqBIKJBMY+RvBhYc= -github.com/sagernet/sing-tun v0.1.1 h1:2Hg3GAyJWzQ7Ua1j74dE+mI06vaqSBO9yD4tkTjggn4= -github.com/sagernet/sing-tun v0.1.1/go.mod h1:WzW/SkT+Nh9uJn/FIYUE2YJHYuPwfbh8sATOzU9QDGw= +github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9 h1:tq1kc0HFj/jfhLfVC1NJI6lex2g6w2W+gVsFu6H2Kis= +github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9/go.mod h1:KnRkwaDHbb06zgeNPu0LQ8A+vA9myMxKEgHN1brCPHg= github.com/sagernet/sing-vmess v0.1.2 h1:RbOZNAId2LrCai8epMoQXlf0XTrou0bfcw08hNBg6lM= github.com/sagernet/sing-vmess v0.1.2/go.mod h1:9NSj8mZTx1JIY/HF9LaYRppUsVkysIN5tEFpNZujXxY= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 h1:5VBIbVw9q7aKbrFdT83mjkyvQ+VaRsQ6yflTepfln38= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195/go.mod h1:yedWtra8nyGJ+SyI+ziwuaGMzBatbB10P1IOOZbbSK8= github.com/sagernet/tfo-go v0.0.0-20230207095944-549363a7327d h1:trP/l6ZPWvQ/5Gv99Z7/t/v8iYy06akDMejxW1sznUk= github.com/sagernet/tfo-go v0.0.0-20230207095944-549363a7327d/go.mod h1:jk6Ii8Y3En+j2KQDLgdgQGwb3M6y7EL567jFnGYhN9g= -github.com/sagernet/utls v0.0.0-20230220130002-c08891932056 h1:gDXi/0uYe8dA48UyUI1LM2la5QYN0IvsDvR2H2+kFnA= -github.com/sagernet/utls v0.0.0-20230220130002-c08891932056/go.mod h1:JKQMZq/O2qnZjdrt+B57olmfgEmLtY9iiSIEYtWvoSM= +github.com/sagernet/utls v0.0.0-20230225061716-536a007c8b01 h1:m4MI13+NRKddIvbdSN0sFHK8w5ROTa60Zi9diZ7EE08= +github.com/sagernet/utls v0.0.0-20230225061716-536a007c8b01/go.mod h1:JKQMZq/O2qnZjdrt+B57olmfgEmLtY9iiSIEYtWvoSM= github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e h1:7uw2njHFGE+VpWamge6o56j2RWk4omF6uLKKxMmcWvs= github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e/go.mod h1:45TUl8+gH4SIKr4ykREbxKWTxkDlSzFENzctB1dVRRY= github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c h1:vK2wyt9aWYHHvNLWniwijBu/n4pySypiKRhN32u/JGo= diff --git a/go/cmd/nekobox_core/grpc_box.go b/go/cmd/nekobox_core/grpc_box.go index 08cde8d..5393c86 100644 --- a/go/cmd/nekobox_core/grpc_box.go +++ b/go/cmd/nekobox_core/grpc_box.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "log" "neko/gen" "neko/pkg/grpc_server" @@ -12,10 +13,10 @@ import ( "neko/pkg/speedtest" "nekobox_core/box_main" "reflect" + "time" "unsafe" box "github.com/sagernet/sing-box" - "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/experimental/v2rayapi" ) @@ -54,13 +55,7 @@ func (s *server) Start(ctx context.Context, in *gen.LoadConfigReq) (out *gen.Err writer_ = reflect.NewAt(writer_.Type(), unsafe.Pointer(writer_.UnsafeAddr())).Elem() // get unexported io.Writer writer_.Set(reflect.ValueOf(neko_log.LogWriter)) // V2ray Service - v2ray_ := reflect.Indirect(reflect.ValueOf(instance)).FieldByName("v2rayServer") - v2ray_ = reflect.NewAt(v2ray_.Type(), unsafe.Pointer(v2ray_.UnsafeAddr())).Elem() - if v2ray, ok := v2ray_.Interface().(adapter.V2RayServer); ok { - if s, ok := v2ray.StatsService().(*v2rayapi.StatsService); ok { - box_v2ray_service = s - } - } + instance.Router().SetV2RayServer(NewSbV2rayServer()) } return @@ -80,11 +75,25 @@ func (s *server) Stop(ctx context.Context, in *gen.EmptyReq) (out *gen.ErrorResp return } - instance_cancel() - instance.Close() // TODO closed failed?? + t := time.NewTimer(time.Second * 2) + c := make(chan struct{}, 1) + go func(cancel context.CancelFunc, closer io.Closer) { + cancel() + closer.Close() + c <- struct{}{} + close(c) + }(instance_cancel, instance) + + select { + case <-t.C: + log.Println("[Warning] sing-box close takes longer than expected.") + case <-c: + } + + t.Stop() instance = nil - box_v2ray_service = nil + return } @@ -130,6 +139,12 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp, func (s *server) QueryStats(ctx context.Context, in *gen.QueryStatsReq) (out *gen.QueryStatsResp, _ error) { out = &gen.QueryStatsResp{} + var box_v2ray_service *sbV2rayStatsService + + if instance != nil && instance.Router().V2RayServer() != nil { + box_v2ray_service, _ = instance.Router().V2RayServer().StatsService().(*sbV2rayStatsService) + } + if box_v2ray_service != nil { req := &v2rayapi.GetStatsRequest{ Name: fmt.Sprintf("outbound>>>%s>>>traffic>>>%s", in.Tag, in.Direct), diff --git a/go/cmd/nekobox_core/v2ray_service.go b/go/cmd/nekobox_core/v2ray_service.go new file mode 100644 index 0000000..009afe8 --- /dev/null +++ b/go/cmd/nekobox_core/v2ray_service.go @@ -0,0 +1,41 @@ +package main + +import ( + "net" + + "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/experimental/v2rayapi" + "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common/network" +) + +type sbV2rayServer struct { + ss *sbV2rayStatsService +} + +func NewSbV2rayServer() adapter.V2RayServer { + options := option.V2RayStatsServiceOptions{ + Enabled: true, + Outbounds: []string{"proxy", "bypass"}, // TODO + } + return &sbV2rayServer{ + ss: &sbV2rayStatsService{v2rayapi.NewStatsService(options)}, + } +} + +func (s *sbV2rayServer) Start() error { return nil } +func (s *sbV2rayServer) Close() error { return nil } +func (s *sbV2rayServer) StatsService() adapter.V2RayStatsService { return s.ss } + +type sbV2rayStatsService struct { + *v2rayapi.StatsService +} + +func (s *sbV2rayStatsService) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn { + // TODO track + return s.StatsService.RoutedConnection(inbound, outbound, user, conn) +} + +func (s *sbV2rayStatsService) RoutedPacketConnection(inbound string, outbound string, user string, conn network.PacketConn) network.PacketConn { + return s.StatsService.RoutedPacketConnection(inbound, outbound, user, conn) +} diff --git a/libs/build_go.sh b/libs/build_go.sh index 805aa92..dbf30aa 100755 --- a/libs/build_go.sh +++ b/libs/build_go.sh @@ -31,5 +31,5 @@ popd #### Go: nekobox_core #### pushd go/cmd/nekobox_core -go build -v -o $DEST -trimpath -ldflags "-w -s -X neko/pkg/neko_common.Version_neko=$version_standalone" -tags "with_gvisor,with_quic,with_wireguard,with_v2ray_api,with_ech,with_utls" +go build -v -o $DEST -trimpath -ldflags "-w -s -X neko/pkg/neko_common.Version_neko=$version_standalone" -tags "with_gvisor,with_quic,with_wireguard,with_utls,with_v2ray_api" popd