mirror of https://github.com/v2ray/v2ray-core
commit
cb49b2b961
|
@ -3,6 +3,7 @@ package buf
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -57,22 +58,17 @@ func NewReader(reader io.Reader) Reader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if useReadv {
|
_, isFile := reader.(*os.File)
|
||||||
|
if !isFile && useReadv {
|
||||||
if sc, ok := reader.(syscall.Conn); ok {
|
if sc, ok := reader.(syscall.Conn); ok {
|
||||||
rawConn, err := sc.SyscallConn()
|
rawConn, err := sc.SyscallConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
newError("failed to get sysconn").Base(err).WriteToLog()
|
newError("failed to get sysconn").Base(err).WriteToLog()
|
||||||
} else {
|
} else {
|
||||||
/*
|
|
||||||
Check if ReadVReader Can be used on this reader first
|
|
||||||
Fix https://github.com/v2ray/v2ray-core/issues/1666
|
|
||||||
*/
|
|
||||||
if ok, _ := checkReadVConstraint(rawConn); ok {
|
|
||||||
return NewReadVReader(reader, rawConn)
|
return NewReadVReader(reader, rawConn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return &SingleReader{
|
return &SingleReader{
|
||||||
Reader: reader,
|
Reader: reader,
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
. "v2ray.com/core/common/buf"
|
. "v2ray.com/core/common/buf"
|
||||||
|
@ -98,6 +100,7 @@ func TestMultiBufferSplitFirst(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiBufferReadAllToByte(t *testing.T) {
|
func TestMultiBufferReadAllToByte(t *testing.T) {
|
||||||
|
{
|
||||||
lb := make([]byte, 8*1024)
|
lb := make([]byte, 8*1024)
|
||||||
common.Must2(io.ReadFull(rand.Reader, lb))
|
common.Must2(io.ReadFull(rand.Reader, lb))
|
||||||
rd := bytes.NewBuffer(lb)
|
rd := bytes.NewBuffer(lb)
|
||||||
|
@ -107,6 +110,24 @@ func TestMultiBufferReadAllToByte(t *testing.T) {
|
||||||
if l := len(b); l != 8*1024 {
|
if l := len(b); l != 8*1024 {
|
||||||
t.Error("unexpceted length from ReadAllToBytes", l)
|
t.Error("unexpceted length from ReadAllToBytes", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const dat = "data/test_MultiBufferReadAllToByte.dat"
|
||||||
|
f, err := os.Open(dat)
|
||||||
|
common.Must(err)
|
||||||
|
|
||||||
|
buf2, err := ReadAllToBytes(f)
|
||||||
|
common.Must(err)
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
cnt, err := ioutil.ReadFile(dat)
|
||||||
|
common.Must(err)
|
||||||
|
|
||||||
|
if d := cmp.Diff(buf2, cnt); d != "" {
|
||||||
|
t.Error("fail to read from file: ", d)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiBufferCopy(t *testing.T) {
|
func TestMultiBufferCopy(t *testing.T) {
|
||||||
|
|
|
@ -3,12 +3,9 @@ package buf_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
. "v2ray.com/core/common/buf"
|
. "v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/transport/pipe"
|
"v2ray.com/core/transport/pipe"
|
||||||
|
@ -92,23 +89,6 @@ func TestReadBuffer(t *testing.T) {
|
||||||
buf.Release()
|
buf.Release()
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
const dat = "data/test_ReadBuffer.dat"
|
|
||||||
f, err := os.Open(dat)
|
|
||||||
common.Must(err)
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
buf2, err := ReadBuffer(f)
|
|
||||||
common.Must(err)
|
|
||||||
|
|
||||||
cnt, err := ioutil.ReadFile(dat)
|
|
||||||
common.Must(err)
|
|
||||||
|
|
||||||
if cmp.Diff(buf2.Bytes(), cnt) != "" {
|
|
||||||
t.Error("fail to read from file")
|
|
||||||
}
|
|
||||||
buf2.Release()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadAtMost(t *testing.T) {
|
func TestReadAtMost(t *testing.T) {
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package buf
|
|
||||||
|
|
||||||
import "syscall"
|
|
||||||
|
|
||||||
func checkReadVConstraint(conn syscall.RawConn) (bool, error) {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
|
@ -1,37 +0,0 @@
|
||||||
// +build windows
|
|
||||||
package buf
|
|
||||||
|
|
||||||
import (
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
func checkReadVConstraint(conn syscall.RawConn) (bool, error) {
|
|
||||||
var isSocketReady = false
|
|
||||||
var reason error
|
|
||||||
/*
|
|
||||||
In Windows, WSARecv system call only support socket connection.
|
|
||||||
|
|
||||||
It it required to check if the given fd is of a socket type
|
|
||||||
|
|
||||||
Fix https://github.com/v2ray/v2ray-core/issues/1666
|
|
||||||
|
|
||||||
Additional Information:
|
|
||||||
https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-wsarecv
|
|
||||||
https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-getsockopt
|
|
||||||
https://docs.microsoft.com/en-us/windows/desktop/WinSock/sol-socket-socket-options
|
|
||||||
|
|
||||||
*/
|
|
||||||
err := conn.Control(func(fd uintptr) {
|
|
||||||
var val [4]byte
|
|
||||||
var le = int32(len(val))
|
|
||||||
err := syscall.Getsockopt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, &val[0], &le)
|
|
||||||
if err != nil {
|
|
||||||
isSocketReady = false
|
|
||||||
} else {
|
|
||||||
isSocketReady = true
|
|
||||||
}
|
|
||||||
reason = err
|
|
||||||
})
|
|
||||||
|
|
||||||
return isSocketReady, err
|
|
||||||
}
|
|
|
@ -162,6 +162,7 @@ func TestUDPDNSTunnel(t *testing.T) {
|
||||||
m1.Question[0] = dns.Question{"ipv4only.google.com.", dns.TypeAAAA, dns.ClassINET}
|
m1.Question[0] = dns.Question{"ipv4only.google.com.", dns.TypeAAAA, dns.ClassINET}
|
||||||
|
|
||||||
c := new(dns.Client)
|
c := new(dns.Client)
|
||||||
|
c.Timeout = 10 * time.Second
|
||||||
in, _, err := c.Exchange(m1, "127.0.0.1:"+strconv.Itoa(int(serverPort)))
|
in, _, err := c.Exchange(m1, "127.0.0.1:"+strconv.Itoa(int(serverPort)))
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
|
|
||||||
|
|
|
@ -810,10 +810,10 @@ func TestVMessKCPLarge(t *testing.T) {
|
||||||
Protocol: internet.TransportProtocol_MKCP,
|
Protocol: internet.TransportProtocol_MKCP,
|
||||||
Settings: serial.ToTypedMessage(&kcp.Config{
|
Settings: serial.ToTypedMessage(&kcp.Config{
|
||||||
ReadBuffer: &kcp.ReadBuffer{
|
ReadBuffer: &kcp.ReadBuffer{
|
||||||
Size: 4096,
|
Size: 512 * 1024,
|
||||||
},
|
},
|
||||||
WriteBuffer: &kcp.WriteBuffer{
|
WriteBuffer: &kcp.WriteBuffer{
|
||||||
Size: 4096,
|
Size: 512 * 1024,
|
||||||
},
|
},
|
||||||
UplinkCapacity: &kcp.UplinkCapacity{
|
UplinkCapacity: &kcp.UplinkCapacity{
|
||||||
Value: 20,
|
Value: 20,
|
||||||
|
@ -897,10 +897,10 @@ func TestVMessKCPLarge(t *testing.T) {
|
||||||
Protocol: internet.TransportProtocol_MKCP,
|
Protocol: internet.TransportProtocol_MKCP,
|
||||||
Settings: serial.ToTypedMessage(&kcp.Config{
|
Settings: serial.ToTypedMessage(&kcp.Config{
|
||||||
ReadBuffer: &kcp.ReadBuffer{
|
ReadBuffer: &kcp.ReadBuffer{
|
||||||
Size: 4096,
|
Size: 512 * 1024,
|
||||||
},
|
},
|
||||||
WriteBuffer: &kcp.WriteBuffer{
|
WriteBuffer: &kcp.WriteBuffer{
|
||||||
Size: 4096,
|
Size: 512 * 1024,
|
||||||
},
|
},
|
||||||
UplinkCapacity: &kcp.UplinkCapacity{
|
UplinkCapacity: &kcp.UplinkCapacity{
|
||||||
Value: 20,
|
Value: 20,
|
||||||
|
|
Loading…
Reference in New Issue