2017-02-07 15:11:47 -05:00
|
|
|
package mux
|
|
|
|
|
2017-03-26 19:47:01 -04:00
|
|
|
import (
|
2017-11-07 16:31:47 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-03-26 19:47:01 -04:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-03-31 18:53:01 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-05-02 16:23:07 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2017-03-26 19:47:01 -04:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
2017-02-07 15:11:47 -05:00
|
|
|
|
2017-04-02 07:43:24 -04:00
|
|
|
type Writer struct {
|
2017-05-02 16:23:07 -04:00
|
|
|
dest net.Destination
|
|
|
|
writer buf.Writer
|
2017-10-21 14:21:51 -04:00
|
|
|
id uint16
|
2017-05-02 16:23:07 -04:00
|
|
|
followup bool
|
2018-04-04 15:33:33 -04:00
|
|
|
hasError bool
|
2017-05-02 16:23:07 -04:00
|
|
|
transferType protocol.TransferType
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func NewWriter(id uint16, dest net.Destination, writer buf.Writer, transferType protocol.TransferType) *Writer {
|
2017-04-02 07:43:24 -04:00
|
|
|
return &Writer{
|
2017-05-02 16:23:07 -04:00
|
|
|
id: id,
|
|
|
|
dest: dest,
|
|
|
|
writer: writer,
|
|
|
|
followup: false,
|
|
|
|
transferType: transferType,
|
2017-04-02 03:48:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func NewResponseWriter(id uint16, writer buf.Writer, transferType protocol.TransferType) *Writer {
|
2017-04-03 06:55:46 -04:00
|
|
|
return &Writer{
|
2017-05-02 16:23:07 -04:00
|
|
|
id: id,
|
|
|
|
writer: writer,
|
|
|
|
followup: true,
|
|
|
|
transferType: transferType,
|
2017-04-03 06:55:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:00:11 -04:00
|
|
|
func (w *Writer) getNextFrameMeta() FrameMetadata {
|
2017-03-31 18:53:01 -04:00
|
|
|
meta := FrameMetadata{
|
|
|
|
SessionID: w.id,
|
|
|
|
Target: w.dest,
|
|
|
|
}
|
2018-04-04 15:33:33 -04:00
|
|
|
|
2017-03-31 18:53:01 -04:00
|
|
|
if w.followup {
|
|
|
|
meta.SessionStatus = SessionStatusKeep
|
|
|
|
} else {
|
|
|
|
w.followup = true
|
|
|
|
meta.SessionStatus = SessionStatusNew
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:00:11 -04:00
|
|
|
return meta
|
|
|
|
}
|
2017-04-19 05:20:08 -04:00
|
|
|
|
2017-04-19 12:00:11 -04:00
|
|
|
func (w *Writer) writeMetaOnly() error {
|
|
|
|
meta := w.getNextFrameMeta()
|
|
|
|
b := buf.New()
|
2018-02-23 18:57:54 -05:00
|
|
|
if err := meta.WriteTo(b); err != nil {
|
2017-04-19 12:00:11 -04:00
|
|
|
return err
|
2017-03-31 18:53:01 -04:00
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
return w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
|
2017-04-19 12:00:11 -04:00
|
|
|
}
|
|
|
|
|
2018-08-31 09:15:40 -04:00
|
|
|
func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuffer) error {
|
2017-03-31 18:53:01 -04:00
|
|
|
frame := buf.New()
|
2018-02-23 18:57:54 -05:00
|
|
|
if err := meta.WriteTo(frame); err != nil {
|
2017-04-19 12:00:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-08-31 09:15:40 -04:00
|
|
|
if err := frame.AppendSupplier(serial.WriteUint16(uint16(data.Len()))); err != nil {
|
2017-04-19 12:00:11 -04:00
|
|
|
return err
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2017-04-19 12:00:11 -04:00
|
|
|
|
2018-08-31 09:15:40 -04:00
|
|
|
mb2 := buf.NewMultiBufferCap(int32(len(data)) + 1)
|
2017-04-19 15:27:21 -04:00
|
|
|
mb2.Append(frame)
|
2018-08-31 09:15:40 -04:00
|
|
|
mb2.AppendMulti(data)
|
|
|
|
return writer.WriteMultiBuffer(mb2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) writeData(mb buf.MultiBuffer) error {
|
|
|
|
meta := w.getNextFrameMeta()
|
|
|
|
meta.Option.Set(OptionData)
|
|
|
|
|
|
|
|
return writeMetaWithFrame(w.writer, meta, mb)
|
2017-03-31 18:53:01 -04:00
|
|
|
}
|
2017-02-07 15:11:47 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
// WriteMultiBuffer implements buf.Writer.
|
|
|
|
func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
2017-11-07 05:58:43 -05:00
|
|
|
defer mb.Release()
|
|
|
|
|
2017-04-19 12:00:11 -04:00
|
|
|
if mb.IsEmpty() {
|
|
|
|
return w.writeMetaOnly()
|
|
|
|
}
|
|
|
|
|
2017-11-07 05:58:43 -05:00
|
|
|
for !mb.IsEmpty() {
|
|
|
|
var chunk buf.MultiBuffer
|
|
|
|
if w.transferType == protocol.TransferTypeStream {
|
|
|
|
chunk = mb.SliceBySize(8 * 1024)
|
|
|
|
} else {
|
|
|
|
chunk = buf.NewMultiBufferValue(mb.SplitFirst())
|
2017-05-02 16:23:07 -04:00
|
|
|
}
|
2017-11-07 05:58:43 -05:00
|
|
|
if err := w.writeData(chunk); err != nil {
|
|
|
|
return err
|
2017-04-19 04:14:52 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-02 16:23:07 -04:00
|
|
|
|
2017-04-19 04:14:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-26 10:32:55 -04:00
|
|
|
// Close implements common.Closable.
|
2018-02-08 09:39:46 -05:00
|
|
|
func (w *Writer) Close() error {
|
2017-03-31 18:53:01 -04:00
|
|
|
meta := FrameMetadata{
|
|
|
|
SessionID: w.id,
|
|
|
|
SessionStatus: SessionStatusEnd,
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2018-04-04 15:33:33 -04:00
|
|
|
if w.hasError {
|
|
|
|
meta.Option.Set(OptionError)
|
2018-04-04 11:20:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
frame := buf.New()
|
|
|
|
common.Must(meta.WriteTo(frame))
|
|
|
|
|
2018-05-26 10:32:55 -04:00
|
|
|
w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(frame)) // nolint: errcheck
|
2018-04-04 11:20:45 -04:00
|
|
|
return nil
|
|
|
|
}
|