mirror of https://github.com/XTLS/Xray-core
Collect stats and possible padding (previously Vision Reader Writer) to all traffic
parent
cba71f8cdc
commit
51234fbe53
|
@ -143,8 +143,8 @@ type OutboundState struct {
|
||||||
UplinkWriterDirectCopy bool
|
UplinkWriterDirectCopy bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTrafficState(userUUID []byte) *TrafficState {
|
func NewTrafficState(userUUID []byte, flow string) *TrafficState {
|
||||||
return &TrafficState{
|
var state = TrafficState{
|
||||||
UserUUID: userUUID,
|
UserUUID: userUUID,
|
||||||
StartTime: time.Time{},
|
StartTime: time.Time{},
|
||||||
ByteSent: 0,
|
ByteSent: 0,
|
||||||
|
@ -158,26 +158,29 @@ func NewTrafficState(userUUID []byte) *TrafficState {
|
||||||
Cipher: 0,
|
Cipher: 0,
|
||||||
RemainingServerHello: -1,
|
RemainingServerHello: -1,
|
||||||
Inbound: InboundState{
|
Inbound: InboundState{
|
||||||
WithinPaddingBuffers: true,
|
|
||||||
UplinkReaderDirectCopy: false,
|
UplinkReaderDirectCopy: false,
|
||||||
RemainingCommand: -1,
|
RemainingCommand: -1,
|
||||||
RemainingContent: -1,
|
RemainingContent: -1,
|
||||||
RemainingPadding: -1,
|
RemainingPadding: -1,
|
||||||
CurrentCommand: 0,
|
CurrentCommand: 0,
|
||||||
IsPadding: true,
|
|
||||||
DownlinkWriterDirectCopy: false,
|
DownlinkWriterDirectCopy: false,
|
||||||
},
|
},
|
||||||
Outbound: OutboundState{
|
Outbound: OutboundState{
|
||||||
WithinPaddingBuffers: true,
|
|
||||||
DownlinkReaderDirectCopy: false,
|
DownlinkReaderDirectCopy: false,
|
||||||
RemainingCommand: -1,
|
RemainingCommand: -1,
|
||||||
RemainingContent: -1,
|
RemainingContent: -1,
|
||||||
RemainingPadding: -1,
|
RemainingPadding: -1,
|
||||||
CurrentCommand: 0,
|
CurrentCommand: 0,
|
||||||
IsPadding: true,
|
|
||||||
UplinkWriterDirectCopy: false,
|
UplinkWriterDirectCopy: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if len(flow) > 0 {
|
||||||
|
state.Inbound.IsPadding = true;
|
||||||
|
state.Outbound.IsPadding = true;
|
||||||
|
state.Inbound.WithinPaddingBuffers = true;
|
||||||
|
state.Outbound.WithinPaddingBuffers = true;
|
||||||
|
}
|
||||||
|
return &state
|
||||||
}
|
}
|
||||||
|
|
||||||
// VisionReader is used to read xtls vision protocol
|
// VisionReader is used to read xtls vision protocol
|
||||||
|
|
|
@ -58,21 +58,20 @@ func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) {
|
||||||
|
|
||||||
// EncodeBodyAddons returns a Writer that auto-encrypt content written by caller.
|
// EncodeBodyAddons returns a Writer that auto-encrypt content written by caller.
|
||||||
func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, requestAddons *Addons, state *proxy.TrafficState, isUplink bool, context context.Context, conn net.Conn, ob *session.Outbound) buf.Writer {
|
func EncodeBodyAddons(writer buf.Writer, request *protocol.RequestHeader, requestAddons *Addons, state *proxy.TrafficState, isUplink bool, context context.Context, conn net.Conn, ob *session.Outbound) buf.Writer {
|
||||||
|
w := proxy.NewVisionWriter(writer, state, isUplink, context, conn, ob)
|
||||||
if request.Command == protocol.RequestCommandUDP {
|
if request.Command == protocol.RequestCommandUDP {
|
||||||
return NewMultiLengthPacketWriter(writer)
|
return NewMultiLengthPacketWriter(w)
|
||||||
}
|
|
||||||
if requestAddons.Flow == vless.XRV {
|
|
||||||
return proxy.NewVisionWriter(writer, state, isUplink, context, conn, ob)
|
|
||||||
}
|
}
|
||||||
return writer
|
return writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeBodyAddons returns a Reader from which caller can fetch decrypted body.
|
// DecodeBodyAddons returns a Reader from which caller can fetch decrypted body.
|
||||||
func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons) buf.Reader {
|
func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons, state *proxy.TrafficState, isUplink bool, context context.Context, conn net.Conn, input *bytes.Reader, rawInput *bytes.Buffer, ob *session.Outbound) buf.Reader {
|
||||||
|
r := proxy.NewVisionReader(buf.NewReader(reader), state, isUplink, context, conn, input, rawInput, ob)
|
||||||
if request.Command == protocol.RequestCommandUDP {
|
if request.Command == protocol.RequestCommandUDP {
|
||||||
return NewLengthPacketReader(reader)
|
return NewLengthPacketReader(&buf.BufferedReader{Reader: r})
|
||||||
}
|
}
|
||||||
return buf.NewReader(reader)
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMultiLengthPacketWriter(writer buf.Writer) *MultiLengthPacketWriter {
|
func NewMultiLengthPacketWriter(writer buf.Writer) *MultiLengthPacketWriter {
|
||||||
|
|
|
@ -556,11 +556,8 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
||||||
ctx = session.ContextWithAllowedNetwork(ctx, net.Network_UDP)
|
ctx = session.ContextWithAllowedNetwork(ctx, net.Network_UDP)
|
||||||
}
|
}
|
||||||
|
|
||||||
trafficState := proxy.NewTrafficState(userSentID)
|
trafficState := proxy.NewTrafficState(userSentID, account.Flow)
|
||||||
clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons)
|
clientReader := encoding.DecodeBodyAddons(reader, request, requestAddons, trafficState, true, ctx, connection, input, rawInput, nil)
|
||||||
if requestAddons.Flow == vless.XRV {
|
|
||||||
clientReader = proxy.NewVisionReader(clientReader, trafficState, true, ctx, connection, input, rawInput, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
bufferWriter := buf.NewBufferedWriter(buf.NewWriter(connection))
|
bufferWriter := buf.NewBufferedWriter(buf.NewWriter(connection))
|
||||||
if err := encoding.EncodeResponseHeader(bufferWriter, request, responseAddons); err != nil {
|
if err := encoding.EncodeResponseHeader(bufferWriter, request, responseAddons); err != nil {
|
||||||
|
|
|
@ -210,7 +210,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
||||||
|
|
||||||
clientReader := link.Reader // .(*pipe.Reader)
|
clientReader := link.Reader // .(*pipe.Reader)
|
||||||
clientWriter := link.Writer // .(*pipe.Writer)
|
clientWriter := link.Writer // .(*pipe.Writer)
|
||||||
trafficState := proxy.NewTrafficState(account.ID.Bytes())
|
trafficState := proxy.NewTrafficState(account.ID.Bytes(), account.Flow)
|
||||||
if request.Command == protocol.RequestCommandUDP && (requestAddons.Flow == vless.XRV || (h.cone && request.Port != 53 && request.Port != 443)) {
|
if request.Command == protocol.RequestCommandUDP && (requestAddons.Flow == vless.XRV || (h.cone && request.Port != 53 && request.Port != 443)) {
|
||||||
request.Command = protocol.RequestCommandMux
|
request.Command = protocol.RequestCommandMux
|
||||||
request.Address = net.DomainAddress("v1.mux.cool")
|
request.Address = net.DomainAddress("v1.mux.cool")
|
||||||
|
@ -286,16 +286,9 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
|
||||||
}
|
}
|
||||||
|
|
||||||
// default: serverReader := buf.NewReader(conn)
|
// default: serverReader := buf.NewReader(conn)
|
||||||
serverReader := encoding.DecodeBodyAddons(conn, request, responseAddons)
|
serverReader := encoding.DecodeBodyAddons(conn, request, responseAddons, trafficState, false, ctx, conn, input, rawInput, ob)
|
||||||
if requestAddons.Flow == vless.XRV {
|
|
||||||
serverReader = proxy.NewVisionReader(serverReader, trafficState, false, ctx, conn, input, rawInput, ob)
|
|
||||||
}
|
|
||||||
if request.Command == protocol.RequestCommandMux && request.Port == 666 {
|
if request.Command == protocol.RequestCommandMux && request.Port == 666 {
|
||||||
if requestAddons.Flow == vless.XRV {
|
|
||||||
serverReader = xudp.NewPacketReader(&buf.BufferedReader{Reader: serverReader})
|
serverReader = xudp.NewPacketReader(&buf.BufferedReader{Reader: serverReader})
|
||||||
} else {
|
|
||||||
serverReader = xudp.NewPacketReader(conn)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if requestAddons.Flow == vless.XRV {
|
if requestAddons.Flow == vless.XRV {
|
||||||
|
|
Loading…
Reference in New Issue