Fix: CounterConnection with ReadV/WriteV (#720)

Co-authored-by: JimhHan <50871214+JimhHan@users.noreply.github.com>
This commit is contained in:
Arthur Morgan 2021-09-20 20:11:21 +08:00 committed by GitHub
parent f2cb13a8ec
commit 24b637cd5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 247 additions and 128 deletions

View file

@ -5,6 +5,8 @@ import (
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/dice"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/session"
@ -17,14 +19,14 @@ import (
// Dialer is the interface for dialing outbound connections.
type Dialer interface {
// Dial dials a system connection to the given destination.
Dial(ctx context.Context, destination net.Destination) (Connection, error)
Dial(ctx context.Context, destination net.Destination) (stat.Connection, error)
// Address returns the address used by this Dialer. Maybe nil if not known.
Address() net.Address
}
// dialFunc is an interface to dial network connection to a specific destination.
type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)
type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (stat.Connection, error)
var (
transportDialerCache = make(map[string]dialFunc)
@ -40,7 +42,7 @@ func RegisterTransportDialer(protocol string, dialer dialFunc) error {
}
// Dial dials a internet connection towards the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (stat.Connection, error) {
if dest.Network == net.Network_TCP {
if streamSettings == nil {
s, err := ToMemoryStreamConfig(nil)

View file

@ -6,6 +6,8 @@ package domainsocket
import (
"context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
@ -13,7 +15,7 @@ import (
"github.com/xtls/xray-core/transport/internet/xtls"
)
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
settings := streamSettings.ProtocolSettings.(*Config)
addr, err := settings.GetUnixAddr()
if err != nil {

View file

@ -9,6 +9,8 @@ import (
"os"
"strings"
"github.com/xtls/xray-core/transport/internet/stat"
goxtls "github.com/xtls/go"
"golang.org/x/sys/unix"
@ -98,7 +100,7 @@ func (ln *Listener) run() {
conn = xtls.Server(conn, ln.xtlsConfig)
}
ln.addConn(internet.Connection(conn))
ln.addConn(stat.Connection(conn))
}
}

View file

@ -8,6 +8,8 @@ import (
"runtime"
"testing"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net"
@ -23,7 +25,7 @@ func TestListen(t *testing.T) {
Path: "/tmp/ts3",
},
}
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn internet.Connection) {
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn stat.Connection) {
defer conn.Close()
b := buf.New()
@ -64,7 +66,7 @@ func TestListenAbstract(t *testing.T) {
Abstract: true,
},
}
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn internet.Connection) {
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn stat.Connection) {
defer conn.Close()
b := buf.New()

View file

@ -17,17 +17,18 @@ import (
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/grpc/encoding"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/transport/internet/tls"
)
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := dialgRPC(ctx, dest, streamSettings)
if err != nil {
return nil, newError("failed to dial gRPC").Base(err)
}
return internet.Connection(conn), nil
return stat.Connection(conn), nil
}
func init() {

View file

@ -151,7 +151,7 @@ func (w *HeaderWriter) Write(writer io.Writer) error {
if w.header == nil {
return nil
}
err := buf.WriteAllBytes(writer, w.header.Bytes())
err := buf.WriteAllBytes(writer, w.header.Bytes(), nil)
w.header.Release()
w.header = nil
return err

View file

@ -8,6 +8,8 @@ import (
"sync"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net"
@ -110,7 +112,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
}
// Dial dials a new TCP connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
httpSettings := streamSettings.ProtocolSettings.(*Config)
client, err := getHTTPClient(ctx, dest, streamSettings)
if err != nil {

View file

@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/google/go-cmp/cmp"
"github.com/xtls/xray-core/common"
@ -28,7 +30,7 @@ func TestHTTPConnection(t *testing.T) {
SecuritySettings: &tls.Config{
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("www.example.com")))},
},
}, func(conn internet.Connection) {
}, func(conn stat.Connection) {
go func() {
defer conn.Close()

View file

@ -5,6 +5,8 @@ import (
"io"
"sync/atomic"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/dice"
@ -46,7 +48,7 @@ func fetchInput(_ context.Context, input io.Reader, reader PacketReader, conn *C
}
// DialKCP dials a new KCP connections to the specific destination.
func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
dest.Network = net.Network_UDP
newError("dialing mKCP to ", dest).WriteToLog()
@ -84,7 +86,7 @@ func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet
go fetchInput(ctx, rawConn, reader, session)
var iConn internet.Connection = session
var iConn stat.Connection = session
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))

View file

@ -7,6 +7,8 @@ import (
"testing"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
@ -21,8 +23,8 @@ func TestDialAndListen(t *testing.T) {
listerner, err := NewListener(context.Background(), net.LocalHostIP, net.Port(0), &internet.MemoryStreamConfig{
ProtocolName: "mkcp",
ProtocolSettings: &Config{},
}, func(conn internet.Connection) {
go func(c internet.Connection) {
}, func(conn stat.Connection) {
go func(c stat.Connection) {
payload := make([]byte, 4096)
for {
nBytes, err := c.Read(payload)

View file

@ -6,6 +6,8 @@ import (
gotls "crypto/tls"
"sync"
"github.com/xtls/xray-core/transport/internet/stat"
goxtls "github.com/xtls/go"
"github.com/xtls/xray-core/common"
@ -134,7 +136,7 @@ func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination) {
Security: l.security,
Writer: writer,
}, writer, l.config)
var netConn internet.Connection = conn
var netConn stat.Connection = conn
if l.tlsConfig != nil {
netConn = tls.Server(conn, l.tlsConfig)
} else if l.xtlsConfig != nil {

View file

@ -5,6 +5,8 @@ import (
"sync"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/lucas-clemente/quic-go"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
@ -114,7 +116,7 @@ func (s *clientSessions) cleanSessions() error {
return nil
}
func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (internet.Connection, error) {
func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (stat.Connection, error) {
s.access.Lock()
defer s.access.Unlock()
@ -182,7 +184,7 @@ func init() {
common.Must(client.cleanup.Start())
}
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil {
tlsConfig = &tls.Config{

View file

@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/google/go-cmp/cmp"
"github.com/xtls/xray-core/common"
@ -37,7 +39,7 @@ func TestQuicConnection(t *testing.T) {
),
},
},
}, func(conn internet.Connection) {
}, func(conn stat.Connection) {
go func() {
defer conn.Close()
@ -100,7 +102,7 @@ func TestQuicConnectionWithoutTLS(t *testing.T) {
listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
ProtocolName: "quic",
ProtocolSettings: &quic.Config{},
}, func(conn internet.Connection) {
}, func(conn stat.Connection) {
go func() {
defer conn.Close()
@ -164,7 +166,7 @@ func TestQuicConnectionAuthHeader(t *testing.T) {
Type: protocol.SecurityType_AES128_GCM,
},
},
}, func(conn internet.Connection) {
}, func(conn stat.Connection) {
go func() {
defer conn.Close()

View file

@ -1,4 +1,4 @@
package internet
package stat
import (
"net"
@ -10,13 +10,13 @@ type Connection interface {
net.Conn
}
type StatCouterConnection struct {
type CounterConnection struct {
Connection
ReadCounter stats.Counter
WriteCounter stats.Counter
}
func (c *StatCouterConnection) Read(b []byte) (int, error) {
func (c *CounterConnection) Read(b []byte) (int, error) {
nBytes, err := c.Connection.Read(b)
if c.ReadCounter != nil {
c.ReadCounter.Add(int64(nBytes))
@ -25,7 +25,7 @@ func (c *StatCouterConnection) Read(b []byte) (int, error) {
return nBytes, err
}
func (c *StatCouterConnection) Write(b []byte) (int, error) {
func (c *CounterConnection) Write(b []byte) (int, error) {
nBytes, err := c.Connection.Write(b)
if c.WriteCounter != nil {
c.WriteCounter.Add(int64(nBytes))

View file

@ -3,6 +3,8 @@ package tcp
import (
"context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
@ -12,7 +14,7 @@ import (
)
// Dial dials a new TCP connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
@ -46,7 +48,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
}
conn = auth.Client(conn)
}
return internet.Connection(conn), nil
return stat.Connection(conn), nil
}
func init() {

View file

@ -6,6 +6,8 @@ import (
"strings"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
goxtls "github.com/xtls/go"
"github.com/xtls/xray-core/common"
@ -120,7 +122,7 @@ func (v *Listener) keepAccepting() {
conn = v.authConfig.Server(conn)
}
v.addConn(internet.Connection(conn))
v.addConn(stat.Connection(conn))
}
}

View file

@ -5,10 +5,11 @@ package tcp
import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
)
// GetOriginalDestination from tcp conn
func GetOriginalDestination(conn internet.Connection) (net.Destination, error) {
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
la := conn.LocalAddr()
ra := conn.RemoteAddr()
ip, port, err := internet.OriginalDst(la, ra)

View file

@ -6,13 +6,14 @@ import (
"syscall"
"unsafe"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
)
const SO_ORIGINAL_DST = 80
func GetOriginalDestination(conn internet.Connection) (net.Destination, error) {
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
sysrawconn, f := conn.(syscall.Conn)
if !f {
return net.Destination{}, newError("unable to get syscall.Conn")

View file

@ -4,9 +4,9 @@ package tcp
import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
)
func GetOriginalDestination(conn internet.Connection) (net.Destination, error) {
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
return net.Destination{}, nil
}

View file

@ -3,6 +3,8 @@ package internet
import (
"context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/net"
)
@ -18,7 +20,7 @@ func RegisterTransportListener(protocol string, listener ListenFunc) error {
return nil
}
type ConnHandler func(Connection)
type ConnHandler func(stat.Connection)
type ListenFunc func(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error)

View file

@ -3,6 +3,8 @@ package udp
import (
"context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
@ -10,7 +12,7 @@ import (
func init() {
common.Must(internet.RegisterTransportDialer(protocolName,
func(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
var sockopt *internet.SocketConfig
if streamSettings != nil {
sockopt = streamSettings.SocketSettings
@ -20,6 +22,6 @@ func init() {
return nil, err
}
// TODO: handle dialer options
return internet.Connection(conn), nil
return stat.Connection(conn), nil
}))
}

View file

@ -10,6 +10,8 @@ import (
"os"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/gorilla/websocket"
"github.com/xtls/xray-core/common"
@ -41,7 +43,7 @@ func init() {
}
// Dial dials a WebSocket connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
var conn net.Conn
if streamSettings.ProtocolSettings.(*Config).Ed > 0 {
@ -59,7 +61,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
return nil, newError("failed to dial WebSocket").Base(err)
}
}
return internet.Connection(conn), nil
return stat.Connection(conn), nil
}
func init() {

View file

@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol/tls/cert"
@ -20,8 +22,8 @@ func Test_listenWSAndDial(t *testing.T) {
ProtocolSettings: &Config{
Path: "ws",
},
}, func(conn internet.Connection) {
go func(c internet.Connection) {
}, func(conn stat.Connection) {
go func(c stat.Connection) {
defer c.Close()
var b [1024]byte
@ -75,8 +77,8 @@ func TestDialWithRemoteAddr(t *testing.T) {
ProtocolSettings: &Config{
Path: "ws",
},
}, func(conn internet.Connection) {
go func(c internet.Connection) {
}, func(conn stat.Connection) {
go func(c stat.Connection) {
defer c.Close()
var b [1024]byte
@ -129,7 +131,7 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
},
}
listen, err := ListenWS(context.Background(), net.LocalHostIP, 13143, streamSettings, func(conn internet.Connection) {
listen, err := ListenWS(context.Background(), net.LocalHostIP, 13143, streamSettings, func(conn stat.Connection) {
go func() {
_ = conn.Close()
}()