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

@ -6,6 +6,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/app/proxyman" "github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
@ -54,7 +56,7 @@ func getTProxyType(s *internet.MemoryStreamConfig) internet.SocketConfig_TProxyM
return s.SocketSettings.Tproxy return s.SocketSettings.Tproxy
} }
func (w *tcpWorker) callback(conn internet.Connection) { func (w *tcpWorker) callback(conn stat.Connection) {
ctx, cancel := context.WithCancel(w.ctx) ctx, cancel := context.WithCancel(w.ctx)
sid := session.NewID() sid := session.NewID()
ctx = session.ContextWithID(ctx, sid) ctx = session.ContextWithID(ctx, sid)
@ -80,7 +82,7 @@ func (w *tcpWorker) callback(conn internet.Connection) {
} }
if w.uplinkCounter != nil || w.downlinkCounter != nil { if w.uplinkCounter != nil || w.downlinkCounter != nil {
conn = &internet.StatCouterConnection{ conn = &stat.CounterConnection{
Connection: conn, Connection: conn,
ReadCounter: w.uplinkCounter, ReadCounter: w.uplinkCounter,
WriteCounter: w.downlinkCounter, WriteCounter: w.downlinkCounter,
@ -117,7 +119,7 @@ func (w *tcpWorker) Proxy() proxy.Inbound {
func (w *tcpWorker) Start() error { func (w *tcpWorker) Start() error {
ctx := context.Background() ctx := context.Background()
hub, err := internet.ListenTCP(ctx, w.address, w.port, w.stream, func(conn internet.Connection) { hub, err := internet.ListenTCP(ctx, w.address, w.port, w.stream, func(conn stat.Connection) {
go w.callback(conn) go w.callback(conn)
}) })
if err != nil { if err != nil {
@ -436,13 +438,13 @@ type dsWorker struct {
ctx context.Context ctx context.Context
} }
func (w *dsWorker) callback(conn internet.Connection) { func (w *dsWorker) callback(conn stat.Connection) {
ctx, cancel := context.WithCancel(w.ctx) ctx, cancel := context.WithCancel(w.ctx)
sid := session.NewID() sid := session.NewID()
ctx = session.ContextWithID(ctx, sid) ctx = session.ContextWithID(ctx, sid)
if w.uplinkCounter != nil || w.downlinkCounter != nil { if w.uplinkCounter != nil || w.downlinkCounter != nil {
conn = &internet.StatCouterConnection{ conn = &stat.CounterConnection{
Connection: conn, Connection: conn,
ReadCounter: w.uplinkCounter, ReadCounter: w.uplinkCounter,
WriteCounter: w.downlinkCounter, WriteCounter: w.downlinkCounter,
@ -482,7 +484,7 @@ func (w *dsWorker) Port() net.Port {
} }
func (w *dsWorker) Start() error { func (w *dsWorker) Start() error {
ctx := context.Background() ctx := context.Background()
hub, err := internet.ListenUnix(ctx, w.address, w.stream, func(conn internet.Connection) { hub, err := internet.ListenUnix(ctx, w.address, w.stream, func(conn stat.Connection) {
go w.callback(conn) go w.callback(conn)
}) })
if err != nil { if err != nil {

View file

@ -3,6 +3,8 @@ package outbound
import ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/app/proxyman" "github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/mux" "github.com/xtls/xray-core/common/mux"
@ -158,7 +160,7 @@ func (h *Handler) Address() net.Address {
} }
// Dial implements internet.Dialer. // Dial implements internet.Dialer.
func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) { func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connection, error) {
if h.senderSettings != nil { if h.senderSettings != nil {
if h.senderSettings.ProxySettings.HasTag() { if h.senderSettings.ProxySettings.HasTag() {
tag := h.senderSettings.ProxySettings.Tag tag := h.senderSettings.ProxySettings.Tag
@ -201,9 +203,9 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Conn
return h.getStatCouterConnection(conn), err return h.getStatCouterConnection(conn), err
} }
func (h *Handler) getStatCouterConnection(conn internet.Connection) internet.Connection { func (h *Handler) getStatCouterConnection(conn stat.Connection) stat.Connection {
if h.uplinkCounter != nil || h.downlinkCounter != nil { if h.uplinkCounter != nil || h.downlinkCounter != nil {
return &internet.StatCouterConnection{ return &stat.CounterConnection{
Connection: conn, Connection: conn,
ReadCounter: h.downlinkCounter, ReadCounter: h.downlinkCounter,
WriteCounter: h.uplinkCounter, WriteCounter: h.uplinkCounter,

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"testing" "testing"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/app/policy" "github.com/xtls/xray-core/app/policy"
. "github.com/xtls/xray-core/app/proxyman/outbound" . "github.com/xtls/xray-core/app/proxyman/outbound"
"github.com/xtls/xray-core/app/stats" "github.com/xtls/xray-core/app/stats"
@ -12,7 +14,6 @@ import (
core "github.com/xtls/xray-core/core" core "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/outbound" "github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/proxy/freedom" "github.com/xtls/xray-core/proxy/freedom"
"github.com/xtls/xray-core/transport/internet"
) )
func TestInterfaces(t *testing.T) { func TestInterfaces(t *testing.T) {
@ -44,9 +45,9 @@ func TestOutboundWithoutStatCounter(t *testing.T) {
ProxySettings: serial.ToTypedMessage(&freedom.Config{}), ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
}) })
conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
_, ok := conn.(*internet.StatCouterConnection) _, ok := conn.(*stat.CounterConnection)
if ok { if ok {
t.Errorf("Expected conn to not be StatCouterConnection") t.Errorf("Expected conn to not be CounterConnection")
} }
} }
@ -73,8 +74,8 @@ func TestOutboundWithStatCounter(t *testing.T) {
ProxySettings: serial.ToTypedMessage(&freedom.Config{}), ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
}) })
conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
_, ok := conn.(*internet.StatCouterConnection) _, ok := conn.(*stat.CounterConnection)
if !ok { if !ok {
t.Errorf("Expected conn to be StatCouterConnection") t.Errorf("Expected conn to be CounterConnection")
} }
} }

View file

@ -6,6 +6,9 @@ import (
"os" "os"
"syscall" "syscall"
"time" "time"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/transport/internet/stat"
) )
// Reader extends io.Reader with MultiBuffer. // Reader extends io.Reader with MultiBuffer.
@ -29,9 +32,17 @@ type Writer interface {
} }
// WriteAllBytes ensures all bytes are written into the given writer. // WriteAllBytes ensures all bytes are written into the given writer.
func WriteAllBytes(writer io.Writer, payload []byte) error { func WriteAllBytes(writer io.Writer, payload []byte, c stats.Counter) error {
wc := 0
defer func() {
if c != nil {
c.Add(int64(wc))
}
}()
for len(payload) > 0 { for len(payload) > 0 {
n, err := writer.Write(payload) n, err := writer.Write(payload)
wc += n
if err != nil { if err != nil {
return err return err
} }
@ -60,12 +71,18 @@ func NewReader(reader io.Reader) Reader {
_, isFile := reader.(*os.File) _, isFile := reader.(*os.File)
if !isFile && useReadv { if !isFile && useReadv {
var counter stats.Counter
if statConn, ok := reader.(*stat.CounterConnection); ok {
reader = statConn.Connection
counter = statConn.ReadCounter
}
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 {
return NewReadVReader(reader, rawConn) return NewReadVReader(reader, rawConn, counter)
} }
} }
} }
@ -104,13 +121,24 @@ func NewWriter(writer io.Writer) Writer {
return mw return mw
} }
if isPacketWriter(writer) { var iConn = writer
if statConn, ok := writer.(*stat.CounterConnection); ok {
iConn = statConn.Connection
}
if isPacketWriter(iConn) {
return &SequentialWriter{ return &SequentialWriter{
Writer: writer, Writer: writer,
} }
} }
var counter stats.Counter
if statConn, ok := writer.(*stat.CounterConnection); ok {
counter = statConn.WriteCounter
}
return &BufferToBytesWriter{ return &BufferToBytesWriter{
Writer: writer, Writer: iConn,
counter: counter,
} }
} }

View file

@ -6,6 +6,8 @@ import (
"io" "io"
"syscall" "syscall"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/common/platform" "github.com/xtls/xray-core/common/platform"
) )
@ -53,17 +55,19 @@ type ReadVReader struct {
rawConn syscall.RawConn rawConn syscall.RawConn
mr multiReader mr multiReader
alloc allocStrategy alloc allocStrategy
counter stats.Counter
} }
// NewReadVReader creates a new ReadVReader. // NewReadVReader creates a new ReadVReader.
func NewReadVReader(reader io.Reader, rawConn syscall.RawConn) *ReadVReader { func NewReadVReader(reader io.Reader, rawConn syscall.RawConn, counter stats.Counter) *ReadVReader {
return &ReadVReader{ return &ReadVReader{
Reader: reader, Reader: reader,
rawConn: rawConn, rawConn: rawConn,
alloc: allocStrategy{ alloc: allocStrategy{
current: 1, current: 1,
}, },
mr: newMultiReader(), mr: newMultiReader(),
counter: counter,
} }
} }
@ -122,10 +126,16 @@ func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error) {
if b.IsFull() { if b.IsFull() {
r.alloc.Adjust(1) r.alloc.Adjust(1)
} }
if r.counter != nil && b != nil {
r.counter.Add(int64(b.Len()))
}
return MultiBuffer{b}, err return MultiBuffer{b}, err
} }
mb, err := r.readMulti() mb, err := r.readMulti()
if r.counter != nil && mb != nil {
r.counter.Add(int64(mb.Len()))
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -50,7 +50,7 @@ func TestReadvReader(t *testing.T) {
rawConn, err := conn.(*net.TCPConn).SyscallConn() rawConn, err := conn.(*net.TCPConn).SyscallConn()
common.Must(err) common.Must(err)
reader := NewReadVReader(conn, rawConn) reader := NewReadVReader(conn, rawConn, nil)
var rmb MultiBuffer var rmb MultiBuffer
for { for {
mb, err := reader.ReadMultiBuffer() mb, err := reader.ReadMultiBuffer()

View file

@ -5,6 +5,8 @@ import (
"net" "net"
"sync" "sync"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
) )
@ -13,7 +15,8 @@ import (
type BufferToBytesWriter struct { type BufferToBytesWriter struct {
io.Writer io.Writer
cache [][]byte counter stats.Counter
cache [][]byte
} }
// WriteMultiBuffer implements Writer. This method takes ownership of the given buffer. // WriteMultiBuffer implements Writer. This method takes ownership of the given buffer.
@ -26,7 +29,7 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
} }
if len(mb) == 1 { if len(mb) == 1 {
return WriteAllBytes(w.Writer, mb[0].Bytes()) return WriteAllBytes(w.Writer, mb[0].Bytes(), w.counter)
} }
if cap(w.cache) < len(mb) { if cap(w.cache) < len(mb) {
@ -45,9 +48,15 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
}() }()
nb := net.Buffers(bs) nb := net.Buffers(bs)
wc := int64(0)
defer func() {
if w.counter != nil {
w.counter.Add(wc)
}
}()
for size > 0 { for size > 0 {
n, err := nb.WriteTo(w.Writer) n, err := nb.WriteTo(w.Writer)
wc += n
if err != nil { if err != nil {
return err return err
} }
@ -173,7 +182,7 @@ func (w *BufferedWriter) flushInternal() error {
w.buffer = nil w.buffer = nil
if writer, ok := w.writer.(io.Writer); ok { if writer, ok := w.writer.(io.Writer); ok {
err := WriteAllBytes(writer, b.Bytes()) err := WriteAllBytes(writer, b.Bytes(), nil)
b.Release() b.Release()
return err return err
} }

View file

@ -50,7 +50,7 @@ func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
func (w *CryptionWriter) Write(data []byte) (int, error) { func (w *CryptionWriter) Write(data []byte) (int, error) {
w.stream.XORKeyStream(data, data) w.stream.XORKeyStream(data, data)
if err := buf.WriteAllBytes(w.writer, data); err != nil { if err := buf.WriteAllBytes(w.writer, data, nil); err != nil {
return 0, err return 0, err
} }
return len(data), nil return len(data), nil

View file

@ -5,6 +5,8 @@ import (
"io" "io"
"sync" "sync"
"github.com/xtls/xray-core/transport/internet/stat"
"golang.org/x/net/dns/dnsmessage" "golang.org/x/net/dns/dnsmessage"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
@ -104,7 +106,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
newError("handling DNS traffic to ", dest).WriteToLog(session.ExportIDToError(ctx)) newError("handling DNS traffic to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn := &outboundConn{ conn := &outboundConn{
dialer: func() (internet.Connection, error) { dialer: func() (stat.Connection, error) {
return d.Dial(ctx, dest) return d.Dial(ctx, dest)
}, },
connReady: make(chan struct{}, 1), connReady: make(chan struct{}, 1),
@ -266,7 +268,7 @@ func (h *Handler) handleIPQuery(id uint16, qType dnsmessage.Type, domain string,
type outboundConn struct { type outboundConn struct {
access sync.Mutex access sync.Mutex
dialer func() (internet.Connection, error) dialer func() (stat.Connection, error)
conn net.Conn conn net.Conn
connReady chan struct{} connReady chan struct{}

View file

@ -7,6 +7,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/log"
@ -18,7 +20,6 @@ import (
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet"
) )
func init() { func init() {
@ -76,7 +77,7 @@ type hasHandshakeAddress interface {
} }
// Process implements proxy.Inbound. // Process implements proxy.Inbound.
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog(session.ExportIDToError(ctx)) newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog(session.ExportIDToError(ctx))
dest := net.Destination{ dest := net.Destination{
Network: network, Network: network,

View file

@ -6,6 +6,8 @@ import (
"context" "context"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/dice" "github.com/xtls/xray-core/common/dice"
@ -121,7 +123,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
input := link.Reader input := link.Reader
output := link.Writer output := link.Writer
var conn internet.Connection var conn stat.Connection
err := retry.ExponentialBackoff(5, 100).On(func() error { err := retry.ExponentialBackoff(5, 100).On(func() error {
dialDest := destination dialDest := destination
if h.config.useIP() && dialDest.Address.Family().IsDomain() { if h.config.useIP() && dialDest.Address.Family().IsDomain() {
@ -194,7 +196,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
func NewPacketReader(conn net.Conn, UDPOverride net.Destination) buf.Reader { func NewPacketReader(conn net.Conn, UDPOverride net.Destination) buf.Reader {
iConn := conn iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }
@ -238,7 +240,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride net.Destination) buf.Writer { func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride net.Destination) buf.Writer {
iConn := conn iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }

View file

@ -9,6 +9,8 @@ import (
"net/url" "net/url"
"sync" "sync"
"github.com/xtls/xray-core/transport/internet/stat"
"golang.org/x/net/http2" "golang.org/x/net/http2"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
@ -77,7 +79,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
} }
var user *protocol.MemoryUser var user *protocol.MemoryUser
var conn internet.Connection var conn stat.Connection
mbuf, _ := link.Reader.ReadMultiBuffer() mbuf, _ := link.Reader.ReadMultiBuffer()
len := mbuf.Len() len := mbuf.Len()
@ -101,7 +103,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
return err return err
} }
} }
conn = internet.Connection(netConn) conn = stat.Connection(netConn)
} }
return err return err
}); err != nil { }); err != nil {
@ -231,7 +233,7 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u
} }
iConn := rawConn iConn := rawConn
if statConn, ok := iConn.(*internet.StatCouterConnection); ok { if statConn, ok := iConn.(*stat.CounterConnection); ok {
iConn = statConn.Connection iConn = statConn.Connection
} }

View file

@ -9,6 +9,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -22,7 +24,6 @@ import (
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet"
) )
// Server is an HTTP proxy server. // Server is an HTTP proxy server.
@ -82,7 +83,7 @@ type readerOnly struct {
io.Reader io.Reader
} }
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
inbound := session.InboundFromContext(ctx) inbound := session.InboundFromContext(ctx)
if inbound != nil { if inbound != nil {
inbound.User = &protocol.MemoryUser{ inbound.User = &protocol.MemoryUser{
@ -157,7 +158,7 @@ Start:
return err return err
} }
func (s *Server) handleConnect(ctx context.Context, _ *http.Request, reader *bufio.Reader, conn internet.Connection, dest net.Destination, dispatcher routing.Dispatcher, inbound *session.Inbound) error { func (s *Server) handleConnect(ctx context.Context, _ *http.Request, reader *bufio.Reader, conn stat.Connection, dest net.Destination, dispatcher routing.Dispatcher, inbound *session.Inbound) error {
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) _, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil { if err != nil {
return newError("failed to write back OK response").Base(err) return newError("failed to write back OK response").Base(err)

View file

@ -5,6 +5,8 @@ import (
"context" "context"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/crypto" "github.com/xtls/xray-core/common/crypto"
@ -16,7 +18,6 @@ import (
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet"
) )
var ( var (
@ -76,7 +77,7 @@ func isValidConnectionType(c [4]byte) bool {
return false return false
} }
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
sPolicy := s.policy.ForLevel(s.user.Level) sPolicy := s.policy.ForLevel(s.user.Level)
if err := conn.SetDeadline(time.Now().Add(sPolicy.Timeouts.Handshake)); err != nil { if err := conn.SetDeadline(time.Now().Add(sPolicy.Timeouts.Handshake)); err != nil {

View file

@ -8,6 +8,8 @@ package proxy
import ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
@ -21,7 +23,7 @@ type Inbound interface {
Network() []net.Network Network() []net.Network
// Process processes a connection of given network. If necessary, the Inbound can dispatch the connection to an Outbound. // Process processes a connection of given network. If necessary, the Inbound can dispatch the connection to an Outbound.
Process(context.Context, net.Network, internet.Connection, routing.Dispatcher) error Process(context.Context, net.Network, stat.Connection, routing.Dispatcher) error
} }
// An Outbound process outbound connections. // An Outbound process outbound connections.

View file

@ -3,6 +3,8 @@ package shadowsocks
import ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -55,7 +57,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
network := destination.Network network := destination.Network
var server *protocol.ServerSpec var server *protocol.ServerSpec
var conn internet.Connection var conn stat.Connection
err := retry.ExponentialBackoff(5, 100).On(func() error { err := retry.ExponentialBackoff(5, 100).On(func() error {
server = c.serverPicker.PickServer() server = c.serverPicker.PickServer()

View file

@ -173,7 +173,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize()) iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv)) common.Must2(rand.Read(iv))
if err := buf.WriteAllBytes(writer, iv); err != nil { if err := buf.WriteAllBytes(writer, iv, nil); err != nil {
return nil, newError("failed to write IV") return nil, newError("failed to write IV")
} }
} }
@ -218,7 +218,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize()) iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv)) common.Must2(rand.Read(iv))
if err := buf.WriteAllBytes(writer, iv); err != nil { if err := buf.WriteAllBytes(writer, iv, nil); err != nil {
return nil, newError("failed to write IV.").Base(err) return nil, newError("failed to write IV.").Base(err)
} }
} }

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/log"
@ -16,7 +18,6 @@ import (
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/udp" "github.com/xtls/xray-core/transport/internet/udp"
) )
@ -70,7 +71,7 @@ func (s *Server) Network() []net.Network {
return list return list
} }
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
switch network { switch network {
case net.Network_TCP: case net.Network_TCP:
return s.handleConnection(ctx, conn, dispatcher) return s.handleConnection(ctx, conn, dispatcher)
@ -81,7 +82,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
} }
} }
func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) handleUDPPayload(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher) error {
udpServer := udp.NewDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) { udpServer := udp.NewDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) {
request := protocol.RequestHeaderFromContext(ctx) request := protocol.RequestHeaderFromContext(ctx)
if request == nil { if request == nil {
@ -185,7 +186,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,
return nil return nil
} }
func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) handleConnection(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher) error {
sessionPolicy := s.policyManager.ForLevel(0) sessionPolicy := s.policyManager.ForLevel(0)
if err := conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil { if err := conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil {
return newError("unable to set read deadline").Base(err).AtWarning() return newError("unable to set read deadline").Base(err).AtWarning()

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -59,7 +61,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
// Outbound server's destination. // Outbound server's destination.
var dest net.Destination var dest net.Destination
// Connection to the outbound server. // Connection to the outbound server.
var conn internet.Connection var conn stat.Connection
if err := retry.ExponentialBackoff(5, 100).On(func() error { if err := retry.ExponentialBackoff(5, 100).On(func() error {
server = c.serverPicker.PickServer() server = c.serverPicker.PickServer()

View file

@ -293,7 +293,7 @@ func hasAuthMethod(expectedAuth byte, authCandidates []byte) bool {
} }
func writeSocks5AuthenticationResponse(writer io.Writer, version byte, auth byte) error { func writeSocks5AuthenticationResponse(writer io.Writer, version byte, auth byte) error {
return buf.WriteAllBytes(writer, []byte{version, auth}) return buf.WriteAllBytes(writer, []byte{version, auth}, nil)
} }
func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error { func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error {
@ -305,7 +305,7 @@ func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, po
return err return err
} }
return buf.WriteAllBytes(writer, buffer.Bytes()) return buf.WriteAllBytes(writer, buffer.Bytes(), nil)
} }
func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error { func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error {
@ -317,7 +317,7 @@ func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, po
portBytes := buffer.Extend(2) portBytes := buffer.Extend(2)
binary.BigEndian.PutUint16(portBytes, port.Value()) binary.BigEndian.PutUint16(portBytes, port.Value())
common.Must2(buffer.Write(address.IP())) common.Must2(buffer.Write(address.IP()))
return buf.WriteAllBytes(writer, buffer.Bytes()) return buf.WriteAllBytes(writer, buffer.Bytes(), nil)
} }
func DecodeUDPPacket(packet *buf.Buffer) (*protocol.RequestHeader, error) { func DecodeUDPPacket(packet *buf.Buffer) (*protocol.RequestHeader, error) {
@ -422,7 +422,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
defer b.Release() defer b.Release()
common.Must2(b.Write([]byte{socks5Version, 0x01, authByte})) common.Must2(b.Write([]byte{socks5Version, 0x01, authByte}))
if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil { if err := buf.WriteAllBytes(writer, b.Bytes(), nil); err != nil {
return nil, err return nil, err
} }
@ -446,7 +446,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
common.Must2(b.WriteString(account.Username)) common.Must2(b.WriteString(account.Username))
common.Must(b.WriteByte(byte(len(account.Password)))) common.Must(b.WriteByte(byte(len(account.Password))))
common.Must2(b.WriteString(account.Password)) common.Must2(b.WriteString(account.Password))
if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil { if err := buf.WriteAllBytes(writer, b.Bytes(), nil); err != nil {
return nil, err return nil, err
} }
@ -474,7 +474,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
} }
} }
if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil { if err := buf.WriteAllBytes(writer, b.Bytes(), nil); err != nil {
return nil, err return nil, err
} }

View file

@ -5,6 +5,8 @@ import (
"io" "io"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/log"
@ -18,7 +20,6 @@ import (
"github.com/xtls/xray-core/features" "github.com/xtls/xray-core/features"
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/udp" "github.com/xtls/xray-core/transport/internet/udp"
) )
@ -62,7 +63,7 @@ func (s *Server) Network() []net.Network {
} }
// Process implements proxy.Inbound. // Process implements proxy.Inbound.
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
if inbound := session.InboundFromContext(ctx); inbound != nil { if inbound := session.InboundFromContext(ctx); inbound != nil {
inbound.User = &protocol.MemoryUser{ inbound.User = &protocol.MemoryUser{
Level: s.config.UserLevel, Level: s.config.UserLevel,
@ -79,7 +80,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
} }
} }
func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher) error {
plcy := s.policy() plcy := s.policy()
if err := conn.SetReadDeadline(time.Now().Add(plcy.Timeouts.Handshake)); err != nil { if err := conn.SetReadDeadline(time.Now().Add(plcy.Timeouts.Handshake)); err != nil {
newError("failed to set deadline").Base(err).WriteToLog(session.ExportIDToError(ctx)) newError("failed to set deadline").Base(err).WriteToLog(session.ExportIDToError(ctx))
@ -191,7 +192,7 @@ func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ
return nil return nil
} }
func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) handleUDPPayload(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher) error {
udpServer := udp.NewDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) { udpServer := udp.NewDispatcher(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) {
payload := packet.Payload payload := packet.Payload
newError("writing back UDP response with ", payload.Len(), " bytes").AtDebug().WriteToLog(session.ExportIDToError(ctx)) newError("writing back UDP response with ", payload.Len(), " bytes").AtDebug().WriteToLog(session.ExportIDToError(ctx))

View file

@ -5,6 +5,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -61,7 +63,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
network := destination.Network network := destination.Network
var server *protocol.ServerSpec var server *protocol.ServerSpec
var conn internet.Connection var conn stat.Connection
err := retry.ExponentialBackoff(5, 100).On(func() error { err := retry.ExponentialBackoff(5, 100).On(func() error {
server = c.serverPicker.PickServer() server = c.serverPicker.PickServer()
@ -81,7 +83,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
defer conn.Close() defer conn.Close()
iConn := conn iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }

View file

@ -8,6 +8,8 @@ import (
"runtime" "runtime"
"syscall" "syscall"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -15,7 +17,6 @@ import (
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/signal" "github.com/xtls/xray-core/common/signal"
"github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/xtls" "github.com/xtls/xray-core/transport/internet/xtls"
) )
@ -298,7 +299,7 @@ func ReadV(reader buf.Reader, writer buf.Writer, timer signal.ActivityUpdater, c
if sctx != nil { if sctx != nil {
if inbound := session.InboundFromContext(sctx); inbound != nil && inbound.Conn != nil { if inbound := session.InboundFromContext(sctx); inbound != nil && inbound.Conn != nil {
iConn := inbound.Conn iConn := inbound.Conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }
@ -325,7 +326,7 @@ func ReadV(reader buf.Reader, writer buf.Writer, timer signal.ActivityUpdater, c
//panic("XTLS Splice: nil inbound or nil inbound.Conn") //panic("XTLS Splice: nil inbound or nil inbound.Conn")
} }
} }
reader = buf.NewReadVReader(conn.Connection, rawConn) reader = buf.NewReadVReader(conn.Connection, rawConn, nil)
ct = counter ct = counter
if conn.SHOW { if conn.SHOW {
fmt.Println(conn.MARK, "ReadV") fmt.Println(conn.MARK, "ReadV")

View file

@ -9,6 +9,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -25,7 +27,6 @@ import (
"github.com/xtls/xray-core/features/policy" "github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/udp" "github.com/xtls/xray-core/transport/internet/udp"
"github.com/xtls/xray-core/transport/internet/xtls" "github.com/xtls/xray-core/transport/internet/xtls"
) )
@ -141,11 +142,11 @@ func (s *Server) Network() []net.Network {
} }
// Process implements proxy.Inbound.Process(). // Process implements proxy.Inbound.Process().
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
sid := session.ExportIDToError(ctx) sid := session.ExportIDToError(ctx)
iConn := conn iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }
@ -343,7 +344,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Session, func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Session,
destination net.Destination, destination net.Destination,
clientReader buf.Reader, clientReader buf.Reader,
clientWriter buf.Writer, dispatcher routing.Dispatcher, iConn internet.Connection, rawConn syscall.RawConn, statConn *internet.StatCouterConnection) error { clientWriter buf.Writer, dispatcher routing.Dispatcher, iConn stat.Connection, rawConn syscall.RawConn, statConn *stat.CounterConnection) error {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle) timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
ctx = policy.ContextWithBufferPolicy(ctx, sessionPolicy.Buffer) ctx = policy.ContextWithBufferPolicy(ctx, sessionPolicy.Buffer)
@ -391,7 +392,7 @@ func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Sess
return nil return nil
} }
func (s *Server) fallback(ctx context.Context, sid errors.ExportOption, err error, sessionPolicy policy.Session, connection internet.Connection, iConn internet.Connection, napfb map[string]map[string]map[string]*Fallback, first *buf.Buffer, firstLen int64, reader buf.Reader) error { func (s *Server) fallback(ctx context.Context, sid errors.ExportOption, err error, sessionPolicy policy.Session, connection stat.Connection, iConn stat.Connection, napfb map[string]map[string]map[string]*Fallback, first *buf.Buffer, firstLen int64, reader buf.Reader) error {
if err := connection.SetReadDeadline(time.Time{}); err != nil { if err := connection.SetReadDeadline(time.Time{}); err != nil {
newError("unable to set back read deadline").Base(err).AtWarning().WriteToLog(sid) newError("unable to set back read deadline").Base(err).AtWarning().WriteToLog(sid)
} }

View file

@ -9,6 +9,8 @@ import (
"runtime" "runtime"
"syscall" "syscall"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -17,7 +19,6 @@ import (
"github.com/xtls/xray-core/common/signal" "github.com/xtls/xray-core/common/signal"
"github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy/vless" "github.com/xtls/xray-core/proxy/vless"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/xtls" "github.com/xtls/xray-core/transport/internet/xtls"
) )
@ -185,7 +186,7 @@ func ReadV(reader buf.Reader, writer buf.Writer, timer signal.ActivityUpdater, c
if sctx != nil { if sctx != nil {
if inbound := session.InboundFromContext(sctx); inbound != nil && inbound.Conn != nil { if inbound := session.InboundFromContext(sctx); inbound != nil && inbound.Conn != nil {
iConn := inbound.Conn iConn := inbound.Conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }
@ -212,7 +213,7 @@ func ReadV(reader buf.Reader, writer buf.Writer, timer signal.ActivityUpdater, c
//panic("XTLS Splice: nil inbound or nil inbound.Conn") //panic("XTLS Splice: nil inbound or nil inbound.Conn")
} }
} }
reader = buf.NewReadVReader(conn.Connection, rawConn) reader = buf.NewReadVReader(conn.Connection, rawConn, nil)
ct = counter ct = counter
if conn.SHOW { if conn.SHOW {
fmt.Println(conn.MARK, "ReadV") fmt.Println(conn.MARK, "ReadV")

View file

@ -10,6 +10,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -29,7 +31,6 @@ import (
"github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy/vless" "github.com/xtls/xray-core/proxy/vless"
"github.com/xtls/xray-core/proxy/vless/encoding" "github.com/xtls/xray-core/proxy/vless/encoding"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/internet/tls"
"github.com/xtls/xray-core/transport/internet/xtls" "github.com/xtls/xray-core/transport/internet/xtls"
) )
@ -172,11 +173,11 @@ func (*Handler) Network() []net.Network {
} }
// Process implements proxy.Inbound.Process(). // Process implements proxy.Inbound.Process().
func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher routing.Dispatcher) error { func (h *Handler) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
sid := session.ExportIDToError(ctx) sid := session.ExportIDToError(ctx)
iConn := connection iConn := connection
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }

View file

@ -7,6 +7,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -77,7 +79,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
// Process implements proxy.Outbound.Process(). // Process implements proxy.Outbound.Process().
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error { func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
var rec *protocol.ServerSpec var rec *protocol.ServerSpec
var conn internet.Connection var conn stat.Connection
if err := retry.ExponentialBackoff(5, 200).On(func() error { if err := retry.ExponentialBackoff(5, 200).On(func() error {
rec = h.serverPicker.PickServer() rec = h.serverPicker.PickServer()
@ -93,7 +95,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
defer conn.Close() defer conn.Close()
iConn := conn iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*stat.CounterConnection)
if ok { if ok {
iConn = statConn.Connection iConn = statConn.Connection
} }

View file

@ -9,6 +9,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -26,7 +28,6 @@ import (
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/proxy/vmess" "github.com/xtls/xray-core/proxy/vmess"
"github.com/xtls/xray-core/proxy/vmess/encoding" "github.com/xtls/xray-core/proxy/vmess/encoding"
"github.com/xtls/xray-core/transport/internet"
) )
var ( var (
@ -220,14 +221,14 @@ func isInsecureEncryption(s protocol.SecurityType) bool {
} }
// Process implements proxy.Inbound.Process(). // Process implements proxy.Inbound.Process().
func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher routing.Dispatcher) error { func (h *Handler) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
sessionPolicy := h.policyManager.ForLevel(0) sessionPolicy := h.policyManager.ForLevel(0)
if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil { if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil {
return newError("unable to set read deadline").Base(err).AtWarning() return newError("unable to set read deadline").Base(err).AtWarning()
} }
iConn := connection iConn := connection
if statConn, ok := iConn.(*internet.StatCouterConnection); ok { if statConn, ok := iConn.(*stat.CounterConnection); ok {
iConn = statConn.Connection iConn = statConn.Connection
} }
_, isDrain := iConn.(*net.TCPConn) _, isDrain := iConn.(*net.TCPConn)

View file

@ -6,6 +6,8 @@ import (
"context" "context"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -57,7 +59,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
// Process implements proxy.Outbound.Process(). // Process implements proxy.Outbound.Process().
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error { func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
var rec *protocol.ServerSpec var rec *protocol.ServerSpec
var conn internet.Connection var conn stat.Connection
err := retry.ExponentialBackoff(5, 200).On(func() error { err := retry.ExponentialBackoff(5, 200).On(func() error {
rec = h.serverPicker.PickServer() rec = h.serverPicker.PickServer()

View file

@ -6,12 +6,14 @@ package mocks
import ( import (
context "context" context "context"
reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
net "github.com/xtls/xray-core/common/net" net "github.com/xtls/xray-core/common/net"
routing "github.com/xtls/xray-core/features/routing" routing "github.com/xtls/xray-core/features/routing"
transport "github.com/xtls/xray-core/transport" transport "github.com/xtls/xray-core/transport"
internet "github.com/xtls/xray-core/transport/internet" internet "github.com/xtls/xray-core/transport/internet"
reflect "reflect" "github.com/xtls/xray-core/transport/internet/stat"
) )
// ProxyInbound is a mock of Inbound interface // ProxyInbound is a mock of Inbound interface
@ -52,7 +54,7 @@ func (mr *ProxyInboundMockRecorder) Network() *gomock.Call {
} }
// Process mocks base method // Process mocks base method
func (m *ProxyInbound) Process(arg0 context.Context, arg1 net.Network, arg2 internet.Connection, arg3 routing.Dispatcher) error { func (m *ProxyInbound) Process(arg0 context.Context, arg1 net.Network, arg2 stat.Connection, arg3 routing.Dispatcher) error {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Process", arg0, arg1, arg2, arg3) ret := m.ctrl.Call(m, "Process", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].(error) ret0, _ := ret[0].(error)

View file

@ -5,6 +5,8 @@ import (
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/dice" "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"
"github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
@ -17,14 +19,14 @@ import (
// Dialer is the interface for dialing outbound connections. // Dialer is the interface for dialing outbound connections.
type Dialer interface { type Dialer interface {
// Dial dials a system connection to the given destination. // 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 returns the address used by this Dialer. Maybe nil if not known.
Address() net.Address Address() net.Address
} }
// dialFunc is an interface to dial network connection to a specific destination. // 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 ( var (
transportDialerCache = make(map[string]dialFunc) 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. // 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 dest.Network == net.Network_TCP {
if streamSettings == nil { if streamSettings == nil {
s, err := ToMemoryStreamConfig(nil) s, err := ToMemoryStreamConfig(nil)

View file

@ -6,6 +6,8 @@ package domainsocket
import ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
@ -13,7 +15,7 @@ import (
"github.com/xtls/xray-core/transport/internet/xtls" "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) settings := streamSettings.ProtocolSettings.(*Config)
addr, err := settings.GetUnixAddr() addr, err := settings.GetUnixAddr()
if err != nil { if err != nil {

View file

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

View file

@ -8,6 +8,8 @@ import (
"runtime" "runtime"
"testing" "testing"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -23,7 +25,7 @@ func TestListen(t *testing.T) {
Path: "/tmp/ts3", 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() defer conn.Close()
b := buf.New() b := buf.New()
@ -64,7 +66,7 @@ func TestListenAbstract(t *testing.T) {
Abstract: true, 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() defer conn.Close()
b := buf.New() b := buf.New()

View file

@ -17,17 +17,18 @@ import (
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/grpc/encoding" "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" "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)) newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := dialgRPC(ctx, dest, streamSettings) conn, err := dialgRPC(ctx, dest, streamSettings)
if err != nil { if err != nil {
return nil, newError("failed to dial gRPC").Base(err) return nil, newError("failed to dial gRPC").Base(err)
} }
return internet.Connection(conn), nil return stat.Connection(conn), nil
} }
func init() { func init() {

View file

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

View file

@ -8,6 +8,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "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. // 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) httpSettings := streamSettings.ProtocolSettings.(*Config)
client, err := getHTTPClient(ctx, dest, streamSettings) client, err := getHTTPClient(ctx, dest, streamSettings)
if err != nil { if err != nil {

View file

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

View file

@ -5,6 +5,8 @@ import (
"io" "io"
"sync/atomic" "sync/atomic"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/dice" "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. // 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 dest.Network = net.Network_UDP
newError("dialing mKCP to ", dest).WriteToLog() 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) go fetchInput(ctx, rawConn, reader, session)
var iConn internet.Connection = session var iConn stat.Connection = session
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil { if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest))) iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))

View file

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

View file

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

View file

@ -5,6 +5,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -114,7 +116,7 @@ func (s *clientSessions) cleanSessions() error {
return nil 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() s.access.Lock()
defer s.access.Unlock() defer s.access.Unlock()
@ -182,7 +184,7 @@ func init() {
common.Must(client.cleanup.Start()) 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) tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil { if tlsConfig == nil {
tlsConfig = &tls.Config{ tlsConfig = &tls.Config{

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,9 +4,9 @@ package tcp
import ( import (
"github.com/xtls/xray-core/common/net" "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 return net.Destination{}, nil
} }

View file

@ -3,6 +3,8 @@ package internet
import ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
) )
@ -18,7 +20,7 @@ func RegisterTransportListener(protocol string, listener ListenFunc) error {
return nil 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) 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 ( import (
"context" "context"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
@ -10,7 +12,7 @@ import (
func init() { func init() {
common.Must(internet.RegisterTransportDialer(protocolName, 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 var sockopt *internet.SocketConfig
if streamSettings != nil { if streamSettings != nil {
sockopt = streamSettings.SocketSettings sockopt = streamSettings.SocketSettings
@ -20,6 +22,6 @@ func init() {
return nil, err return nil, err
} }
// TODO: handle dialer options // TODO: handle dialer options
return internet.Connection(conn), nil return stat.Connection(conn), nil
})) }))
} }

View file

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

View file

@ -6,6 +6,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol/tls/cert" "github.com/xtls/xray-core/common/protocol/tls/cert"
@ -20,8 +22,8 @@ func Test_listenWSAndDial(t *testing.T) {
ProtocolSettings: &Config{ ProtocolSettings: &Config{
Path: "ws", Path: "ws",
}, },
}, func(conn internet.Connection) { }, func(conn stat.Connection) {
go func(c internet.Connection) { go func(c stat.Connection) {
defer c.Close() defer c.Close()
var b [1024]byte var b [1024]byte
@ -75,8 +77,8 @@ func TestDialWithRemoteAddr(t *testing.T) {
ProtocolSettings: &Config{ ProtocolSettings: &Config{
Path: "ws", Path: "ws",
}, },
}, func(conn internet.Connection) { }, func(conn stat.Connection) {
go func(c internet.Connection) { go func(c stat.Connection) {
defer c.Close() defer c.Close()
var b [1024]byte 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")))}, 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() { go func() {
_ = conn.Close() _ = conn.Close()
}() }()