Skip to content

Commit

Permalink
refactor nekoray_core
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Feb 12, 2023
1 parent fd93963 commit 4191394
Show file tree
Hide file tree
Showing 24 changed files with 669 additions and 269 deletions.
2 changes: 1 addition & 1 deletion core_commit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c47c63a07486c7a3b6b186404ba9ef95b0161e5d
8134d3cc23aa6b8e2a056887addf22d7d22bd969
2 changes: 1 addition & 1 deletion docs/Build_Linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ninja

### Go 部分

1.`Matsuridayo/Matsuri` `Matsuridayo/v2ray-core` 置于 `../`
1.`Matsuridayo/v2ray-core` 置于 `../`
2. 进入 `go/cmd/nekoray_core` 文件夹 `go build` 得到 `nekoray_core`
3. 进入 `go/cmd/nekobox_core` 文件夹 `go build` 得到 `nekobox_core`

Expand Down
4 changes: 0 additions & 4 deletions go/cmd/nekobox_core/core_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"io"
"log"
"neko/pkg/neko_common"
"neko/pkg/neko_log"
"net"
Expand All @@ -30,9 +29,6 @@ var box_v2ray_service *v2rayapi.StatsService
func setupCore() {
neko_log.SetupLog(50*1024, "./neko.log")
//
log.SetFlags(log.LstdFlags)
log.SetOutput(neko_log.LogWriter)
//
neko_common.GetProxyHttpClient = func() *http.Client {
return getProxyHttpClient(instance)
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/nekobox_core/grpc_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *server) Stop(ctx context.Context, in *gen.EmptyReq) (out *gen.ErrorResp
}

instance_cancel()
instance.Close() // xx closed
instance.Close() // TODO closed failed??

instance = nil
box_v2ray_service = nil
Expand Down
45 changes: 32 additions & 13 deletions go/cmd/nekoray_core/core_ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,61 @@ package main
import (
"context"
"fmt"
"libcore"
"libcore/device"
"log"
"neko/pkg/neko_common"
"neko/pkg/neko_log"
"net"
"net/http"
"os"
"time"

"github.com/sirupsen/logrus"
core "github.com/v2fly/v2ray-core/v5"
appLog "github.com/v2fly/v2ray-core/v5/app/log"
commonLog "github.com/v2fly/v2ray-core/v5/common/log"
v2rayNet "github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/features/dns"
"github.com/v2fly/v2ray-core/v5/features/dns/localdns"
)

var instance *libcore.V2RayInstance
var instance *NekoV2RayInstance
var getNekorayTunIndex = func() int { return 0 } // Windows only
var underlyingNetDialer *net.Dialer // NKR_VPN_LEGACY_DNS only
var underlyingNetDialer *net.Dialer

type v2rayLogWriter struct {
}

func (w *v2rayLogWriter) Write(s string) error {
log.Println(s)
return nil
}

func (w *v2rayLogWriter) Close() error {
return nil
}

func setupCore() {
// TODO del
device.IsNekoray = true
libcore.SetConfig("", false, true)
libcore.InitCore("", "", "", nil, ".", "moe.nekoray.pc:bg", true, 50)
// core setup
os.Setenv("v2ray.conf.geoloader", "memconservative")
neko_log.SetupLog(50*1024, "./neko.log")
_ = appLog.RegisterHandlerCreator(appLog.LogType_Console, func(lt appLog.LogType,
options appLog.HandlerCreatorOptions) (commonLog.Handler, error) {
return commonLog.NewLogger(func() commonLog.Writer {
return &v2rayLogWriter{}
}), nil
})
// localdns setup
resolver_def := &net.Resolver{PreferGo: false}
resolver_go := &net.Resolver{PreferGo: true}
if underlyingNetDialer != nil && os.Getenv("NKR_VPN_LEGACY_DNS") == "1" {
resolver_def.Dial = underlyingNetDialer.DialContext
resolver_go.Dial = underlyingNetDialer.DialContext
logrus.Println("using NKR_VPN_LEGACY_DNS")
log.Println("using NKR_VPN_LEGACY_DNS")
}
localdns.SetLookupFunc(func(network string, host string) (ips []net.IP, err error) {
// fix old sekai
defer func() {
if len(ips) == 0 {
logrus.Println("LookupIP error:", network, host, err)
log.Println("LookupIP error:", network, host, err)
err = dns.ErrEmptyResponse
}
}()
Expand All @@ -57,13 +76,13 @@ func setupCore() {

// PROXY

func getProxyHttpClient(_instance *libcore.V2RayInstance) *http.Client {
func getProxyHttpClient(_instance *NekoV2RayInstance) *http.Client {
dailContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := v2rayNet.ParseDestination(fmt.Sprintf("%s:%s", network, addr))
if err != nil {
return nil, err
}
return _instance.DialContext(ctx, dest)
return core.Dial(ctx, _instance.Instance, dest)
}

transport := &http.Transport{
Expand Down
36 changes: 13 additions & 23 deletions go/cmd/nekoray_core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ module nekoray_core
go 1.19

require (
github.com/Dreamacro/clash v1.9.0
github.com/jsimonetti/rtnetlink v1.2.2
github.com/sirupsen/logrus v1.8.1
github.com/v2fly/v2ray-core/v5 v5.0.0
kernel.org/pub/linux/libs/security/libcap/cap v1.2.65
libcore v1.0.0
neko v1.0.0
)

require (
github.com/Dreamacro/clash v1.9.0 // indirect
github.com/Dreamacro/go-shadowsocks2 v0.1.7 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
Expand All @@ -24,7 +22,6 @@ require (
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -34,13 +31,9 @@ require (
github.com/klauspost/compress v1.15.12 // indirect
github.com/klauspost/cpuid v1.2.3 // indirect
github.com/klauspost/reedsolomon v1.9.3 // indirect
github.com/lucas-clemente/quic-go v0.31.1 // indirect
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.3 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.1 // indirect
github.com/mdlayher/netlink v1.6.0 // indirect
github.com/mdlayher/socket v0.1.1 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/mustafaturan/bus v1.0.2 // indirect
github.com/mustafaturan/monoton v1.0.0 // indirect
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
Expand All @@ -50,41 +43,38 @@ require (
github.com/pion/sctp v1.7.6 // indirect
github.com/pires/go-proxyproto v0.6.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
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/quic-go/quic-go v0.32.0 // indirect
github.com/refraction-networking/utls v1.2.0 // indirect
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
github.com/sagernet/libping v0.1.1 // indirect
github.com/secure-io/siv-go v0.0.0-20180922214919-5ff40651e2c4 // indirect
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/v2fly/BrowserBridge v0.0.0-20210430233438-0570fc1d7d08 // indirect
github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e // indirect
github.com/xiaokangwang/VLite v0.0.0-20220418190619-cff95160a432 // indirect
github.com/xtaci/smux v1.5.16 // indirect
go.starlark.net v0.0.0-20220817180228-f738f5508c12 // indirect
go.uber.org/automaxprocs v1.4.0 // indirect
go4.org/netipx v0.0.0-20220925034521-797b0c90d8ab // indirect
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
golang.org/x/tools v0.1.12 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gvisor.dev/gvisor v0.0.0 // indirect
kernel.org/pub/linux/libs/security/libcap/psx v1.2.65 // indirect
)

replace neko v1.0.0 => ../../

replace libcore v1.0.0 => ../../../../Matsuri/libcore

replace github.com/v2fly/v2ray-core/v5 v5.0.0 => ../../../../v2ray-core

replace gvisor.dev/gvisor => github.com/sagernet/gvisor v0.0.0-20220402114650-763d12dc953e
Loading

0 comments on commit 4191394

Please sign in to comment.