mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-18 04:54:38 +03:00
update
This commit is contained in:
@@ -2,15 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"neko/pkg/neko_common"
|
||||
"neko/pkg/neko_log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
@@ -27,8 +25,10 @@ var instance_cancel context.CancelFunc
|
||||
// Use sing-box instead of libcore & v2ray
|
||||
|
||||
func setupCore() {
|
||||
neko_log.SetupLog(50*1024, "./neko.log")
|
||||
//
|
||||
log.SetFlags(log.LstdFlags)
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetOutput(neko_log.LogWriter)
|
||||
//
|
||||
neko_common.GetProxyHttpClient = func() *http.Client {
|
||||
return getProxyHttpClient(instance)
|
||||
@@ -67,45 +67,19 @@ func getProxyHttpClient(box *box.Box) *http.Client {
|
||||
return client
|
||||
}
|
||||
|
||||
// TODO move
|
||||
func UrlTestSingBox(box *box.Box, link string, timeout int32) (int32, error) {
|
||||
client := getProxyHttpClient(box)
|
||||
if client == nil {
|
||||
return 0, fmt.Errorf("no client")
|
||||
}
|
||||
|
||||
// Test handshake time
|
||||
var time_start time.Time
|
||||
var times = 1
|
||||
var rtt_times = 1
|
||||
|
||||
// Test RTT "true delay"
|
||||
if link2 := strings.TrimLeft(link, "true"); link != link2 {
|
||||
link = link2
|
||||
times = 3
|
||||
rtt_times = 2
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Millisecond)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", link, nil)
|
||||
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", rand.Int()%84, rand.Int()%2))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for i := 0; i < times; i++ {
|
||||
if i == 1 || times == 1 {
|
||||
time_start = time.Now()
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Url test failed:", err)
|
||||
return 0, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
return int32(time.Since(time_start).Milliseconds() / int64(rtt_times)), nil
|
||||
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
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"neko/gen"
|
||||
"neko/pkg/grpc_server"
|
||||
"neko/pkg/neko_common"
|
||||
"neko/pkg/neko_log"
|
||||
"neko/pkg/speedtest"
|
||||
"nekobox_core/box_main"
|
||||
"net"
|
||||
"time"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
box "github.com/sagernet/sing-box"
|
||||
)
|
||||
@@ -39,6 +42,17 @@ func (s *server) Start(ctx context.Context, in *gen.LoadConfigReq) (out *gen.Err
|
||||
}
|
||||
|
||||
instance, instance_cancel, err = box_main.Create([]byte(in.CoreConfig), true)
|
||||
|
||||
// Logger
|
||||
if instance != nil {
|
||||
logFactory_ := reflect.Indirect(reflect.ValueOf(instance)).FieldByName("logFactory")
|
||||
logFactory_ = reflect.NewAt(logFactory_.Type(), unsafe.Pointer(logFactory_.UnsafeAddr())).Elem() // get unexported logFactory
|
||||
logFactory_ = logFactory_.Elem().Elem() // get struct
|
||||
writer_ := logFactory_.FieldByName("writer")
|
||||
writer_ = reflect.NewAt(writer_.Type(), unsafe.Pointer(writer_.UnsafeAddr())).Elem() // get unexported io.Writer
|
||||
writer_.Set(reflect.ValueOf(neko_log.LogWriter))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -93,27 +107,11 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp,
|
||||
}
|
||||
}
|
||||
// Latency
|
||||
out.Ms, err = UrlTestSingBox(i, in.Url, in.Timeout)
|
||||
out.Ms, err = speedtest.UrlTest(getProxyHttpClient(i), in.Url, in.Timeout)
|
||||
} else if in.Mode == gen.TestMode_TcpPing {
|
||||
host, port, err := net.SplitHostPort(in.Address)
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
return
|
||||
}
|
||||
ip, err := net.ResolveIPAddr("ip", host)
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
return
|
||||
}
|
||||
//
|
||||
startTime := time.Now()
|
||||
_, err = net.DialTimeout("tcp", net.JoinHostPort(ip.String(), port), time.Duration(in.Timeout)*time.Millisecond)
|
||||
endTime := time.Now()
|
||||
if err == nil {
|
||||
out.Ms = int32(endTime.Sub(startTime).Milliseconds())
|
||||
}
|
||||
out.Ms, err = speedtest.TcpPing(in.Address, in.Timeout)
|
||||
} else {
|
||||
// TODO copy
|
||||
err = fmt.Errorf("not available")
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
var instance *libcore.V2RayInstance
|
||||
|
||||
func setupCore() {
|
||||
// TODO del
|
||||
device.IsNekoray = true
|
||||
libcore.SetConfig("", false, true)
|
||||
libcore.InitCore("", "", "", nil, ".", "moe.nekoray.pc:bg", true, 50)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"neko/gen"
|
||||
"neko/pkg/grpc_server"
|
||||
"neko/pkg/neko_common"
|
||||
"neko/pkg/speedtest"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -121,31 +122,15 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp,
|
||||
|
||||
// Latency
|
||||
var t int32
|
||||
t, err = libcore.UrlTestV2ray(i, in.Inbound, in.Url, in.Timeout)
|
||||
t, err = speedtest.UrlTest(getProxyHttpClient(i), in.Address, in.Timeout)
|
||||
out.Ms = t // sn: ms==0 是错误
|
||||
} else if in.Mode == gen.TestMode_TcpPing {
|
||||
host, port, err := net.SplitHostPort(in.Address)
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
return
|
||||
}
|
||||
ip, err := net.ResolveIPAddr("ip", host)
|
||||
if err != nil {
|
||||
out.Error = err.Error()
|
||||
return
|
||||
}
|
||||
//
|
||||
startTime := time.Now()
|
||||
_, err = net.DialTimeout("tcp", net.JoinHostPort(ip.String(), port), time.Duration(in.Timeout)*time.Millisecond)
|
||||
endTime := time.Now()
|
||||
if err == nil {
|
||||
out.Ms = int32(endTime.Sub(startTime).Milliseconds())
|
||||
}
|
||||
out.Ms, err = speedtest.TcpPing(in.Address, in.Timeout)
|
||||
} else if in.Mode == gen.TestMode_FullTest {
|
||||
if in.Config == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO del
|
||||
// Test instance
|
||||
i := libcore.NewV2rayInstance()
|
||||
i.ForTest = true
|
||||
@@ -164,7 +149,7 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp,
|
||||
// Latency
|
||||
var latency string
|
||||
if in.FullLatency {
|
||||
t, _ := libcore.UrlTestV2ray(i, in.Inbound, in.Url, in.Timeout)
|
||||
t, _ := speedtest.UrlTest(getProxyHttpClient(i), in.Address, in.Timeout)
|
||||
out.Ms = t
|
||||
if t > 0 {
|
||||
latency = fmt.Sprint(t, "ms")
|
||||
|
||||
51
go/pkg/neko_log/log.go
Normal file
51
go/pkg/neko_log/log.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package neko_log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var f_neko_log *os.File
|
||||
|
||||
var LogWriter *logWriter
|
||||
|
||||
func SetupLog(maxSize int, path string) {
|
||||
if f_neko_log != nil && LogWriter != nil {
|
||||
return
|
||||
}
|
||||
// mod from libcore, simplify because only 1 proccess.
|
||||
oldBytes, err := os.ReadFile(path)
|
||||
if err == nil && len(oldBytes) > maxSize {
|
||||
if os.Truncate(path, 0) == nil {
|
||||
oldBytes = oldBytes[len(oldBytes)-maxSize:]
|
||||
}
|
||||
}
|
||||
f_neko_log, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
|
||||
if err == nil {
|
||||
f_neko_log.Write(oldBytes)
|
||||
} else {
|
||||
fmt.Println("error open log", err)
|
||||
}
|
||||
//
|
||||
LogWriter = &logWriter{
|
||||
files: []io.Writer{os.Stdout, f_neko_log},
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
72
go/pkg/speedtest/speedtest.go
Normal file
72
go/pkg/speedtest/speedtest.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package speedtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UrlTest(client *http.Client, link string, timeout int32) (int32, error) {
|
||||
if client == nil {
|
||||
return 0, fmt.Errorf("no client")
|
||||
}
|
||||
|
||||
// Test handshake time
|
||||
var time_start time.Time
|
||||
var times = 1
|
||||
var rtt_times = 1
|
||||
|
||||
// Test RTT "true delay"
|
||||
if link2 := strings.TrimLeft(link, "true"); link != link2 {
|
||||
link = link2
|
||||
times = 3
|
||||
rtt_times = 2
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Millisecond)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", link, nil)
|
||||
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", rand.Int()%84, rand.Int()%2))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for i := 0; i < times; i++ {
|
||||
if i == 1 || times == 1 {
|
||||
time_start = time.Now()
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Url test failed:", err)
|
||||
return 0, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
return int32(time.Since(time_start).Milliseconds() / int64(rtt_times)), nil
|
||||
}
|
||||
|
||||
func TcpPing(address string, timeout int32) (ms int32, err error) {
|
||||
host, port, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ip, err := net.ResolveIPAddr("ip", host)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//
|
||||
startTime := time.Now()
|
||||
c, err := net.DialTimeout("tcp", net.JoinHostPort(ip.String(), port), time.Duration(timeout)*time.Millisecond)
|
||||
endTime := time.Now()
|
||||
if err == nil {
|
||||
ms = int32(endTime.Sub(startTime).Milliseconds())
|
||||
c.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user