2020-07-28 11:00:23 -04:00
|
|
|
// +build !confonly
|
|
|
|
|
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EncodeHeaderAddons(buffer *buf.Buffer, addons *Addons) error {
|
|
|
|
|
2020-09-10 23:37:23 -04:00
|
|
|
switch addons.Flow {
|
2020-07-28 11:00:23 -04:00
|
|
|
default:
|
|
|
|
|
|
|
|
if err := buffer.WriteByte(0); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return newError("failed to write addons protobuf length").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeHeaderAddons(buffer *buf.Buffer, reader io.Reader) (*Addons, error) {
|
|
|
|
|
|
|
|
addons := new(Addons)
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 1); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return nil, newError("failed to read addons protobuf length").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if length := int32(buffer.Byte(0)); length != 0 {
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, length); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return nil, newError("failed to read addons protobuf value").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := proto.Unmarshal(buffer.Bytes(), addons); err != nil {
|
2020-08-03 02:13:26 -04:00
|
|
|
return nil, newError("failed to unmarshal addons protobuf value").Base(err)
|
2020-07-28 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verification.
|
2020-09-10 23:37:23 -04:00
|
|
|
switch addons.Flow {
|
2020-07-28 11:00:23 -04:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return addons, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBodyAddons returns a Writer that auto-encrypt content written by caller.
|
|
|
|
func EncodeBodyAddons(writer io.Writer, request *protocol.RequestHeader, addons *Addons) buf.Writer {
|
|
|
|
|
2020-09-10 23:37:23 -04:00
|
|
|
switch addons.Flow {
|
2020-07-28 11:00:23 -04:00
|
|
|
default:
|
|
|
|
|
|
|
|
return buf.NewWriter(writer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBodyAddons returns a Reader from which caller can fetch decrypted body.
|
|
|
|
func DecodeBodyAddons(reader io.Reader, request *protocol.RequestHeader, addons *Addons) buf.Reader {
|
|
|
|
|
2020-09-10 23:37:23 -04:00
|
|
|
switch addons.Flow {
|
2020-07-28 11:00:23 -04:00
|
|
|
default:
|
|
|
|
|
|
|
|
return buf.NewReader(reader)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|