refactor core

This commit is contained in:
arm64v8a
2023-03-05 12:04:57 +09:00
parent 5db3a30ab1
commit d9668c90fd
4 changed files with 10 additions and 66 deletions

View File

@@ -2,21 +2,13 @@ package main
import ( import (
"context" "context"
"io"
"net"
"net/http" "net/http"
"reflect"
"time"
"unsafe"
"github.com/matsuridayo/libneko/neko_common" "github.com/matsuridayo/libneko/neko_common"
"github.com/matsuridayo/libneko/neko_log" "github.com/matsuridayo/libneko/neko_log"
"github.com/matsuridayo/sing-box-extra/boxapi"
box "github.com/sagernet/sing-box" box "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
) )
var instance *box.Box var instance *box.Box
@@ -28,54 +20,6 @@ func setupCore() {
neko_log.SetupLog(50*1024, "./neko.log") neko_log.SetupLog(50*1024, "./neko.log")
// //
neko_common.GetProxyHttpClient = func() *http.Client { neko_common.GetProxyHttpClient = func() *http.Client {
return getProxyHttpClient(instance) return boxapi.GetProxyHttpClient(instance)
} }
} }
func getProxyHttpClient(box *box.Box) *http.Client {
var d network.Dialer
if box != nil {
router_ := reflect.Indirect(reflect.ValueOf(box)).FieldByName("router")
router_ = reflect.NewAt(router_.Type(), unsafe.Pointer(router_.UnsafeAddr())).Elem()
if router, ok := router_.Interface().(adapter.Router); ok {
d = dialer.NewRouter(router)
}
}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
return d.DialContext(ctx, network, metadata.ParseSocksaddr(addr))
}
transport := &http.Transport{
TLSHandshakeTimeout: time.Second * 3,
ResponseHeaderTimeout: time.Second * 3,
}
if d != nil {
transport.DialContext = dialContext
}
client := &http.Client{
Transport: transport,
}
return client
}
type logWriter struct {
files []io.Writer
}
func (w *logWriter) Write(p []byte) (n int, err error) {
for _, file := range w.files {
if file == nil {
continue
}
n, err = file.Write(p)
if err != nil {
return
}
}
return
}

View File

@@ -3,9 +3,8 @@ module nekobox_core
go 1.19 go 1.19
require ( require (
github.com/matsuridayo/libneko v0.0.0-20230304130313-5ea96b79b3c3 github.com/matsuridayo/libneko v0.0.0-20230305030237-4aefab71d5b4
github.com/matsuridayo/sing-box-extra v0.0.0-20230304131029-37eeda33b862 github.com/matsuridayo/sing-box-extra v0.0.0-20230305030339-e94f0e97ef0c
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-box v1.1.6-0.20230301163156-e8802357e1e7
grpc_server v1.0.0 grpc_server v1.0.0
) )
@@ -55,6 +54,7 @@ require (
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // 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/quic-go v0.0.0-20230202071646-a8c8afb18b32 // indirect
github.com/sagernet/reality v0.0.0-20230228045158-d3e085a8e5d1 // indirect github.com/sagernet/reality v0.0.0-20230228045158-d3e085a8e5d1 // indirect
github.com/sagernet/sing v0.1.8-0.20230301160041-9fab0a9f4304 // indirect
github.com/sagernet/sing-dns v0.1.4 // indirect github.com/sagernet/sing-dns v0.1.4 // indirect
github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 // 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-shadowtls v0.0.0-20230221123345-78e50cd7b587 // indirect

View File

@@ -105,10 +105,10 @@ github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40= github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/matsuridayo/libneko v0.0.0-20230304130313-5ea96b79b3c3 h1:emqnGHIlezKjm24Lz5aos9Lc1bhxFL8Ynp20EprSNJc= github.com/matsuridayo/libneko v0.0.0-20230305030237-4aefab71d5b4 h1:+QFcvCMntMTIG1NQABy7P4Pw7mPqdbPcYnIBswRik1s=
github.com/matsuridayo/libneko v0.0.0-20230304130313-5ea96b79b3c3/go.mod h1:IRO07Queptz/rGFvEW+3Hmwpx7MCup6WiDs4p5jMt4g= github.com/matsuridayo/libneko v0.0.0-20230305030237-4aefab71d5b4/go.mod h1:IRO07Queptz/rGFvEW+3Hmwpx7MCup6WiDs4p5jMt4g=
github.com/matsuridayo/sing-box-extra v0.0.0-20230304131029-37eeda33b862 h1:z8qC1cSVDZrhJbZPgsp1oM8rxnzMb+aU3FxAYG27S+k= github.com/matsuridayo/sing-box-extra v0.0.0-20230305030339-e94f0e97ef0c h1:cPzKBlzhtwQkW9Vtb4MVCXpAO/UDQGyx+cjatJlXLuc=
github.com/matsuridayo/sing-box-extra v0.0.0-20230304131029-37eeda33b862/go.mod h1:5YMikmz+Vpr5P7/rSigF92Yf5pBsCzDq9IpQRjoLb3E= github.com/matsuridayo/sing-box-extra v0.0.0-20230305030339-e94f0e97ef0c/go.mod h1:xZckt28e8/yrsXbh9UXS+QBn3qe6DSXwmzm+0LwzLz4=
github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQobyTjI/tJyq2HG+i/dfSoFUt8/aZCM+GKtmFk/Y= github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQobyTjI/tJyq2HG+i/dfSoFUt8/aZCM+GKtmFk/Y=
github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M=

View File

@@ -130,7 +130,7 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp,
} }
} }
// Latency // Latency
out.Ms, err = speedtest.UrlTest(getProxyHttpClient(i), in.Url, in.Timeout) out.Ms, err = speedtest.UrlTest(boxapi.GetProxyHttpClient(i), in.Url, in.Timeout)
} else if in.Mode == gen.TestMode_TcpPing { } else if in.Mode == gen.TestMode_TcpPing {
out.Ms, err = speedtest.TcpPing(in.Address, in.Timeout) out.Ms, err = speedtest.TcpPing(in.Address, in.Timeout)
} else { } else {