apply bitmask to other packages

pull/642/merge
Darien Raymond 7 years ago
parent 8c6f73f30b
commit f1a15e92f5

@ -63,7 +63,7 @@ func (w *Writer) writeMetaOnly() error {
func (w *Writer) writeData(mb buf.MultiBuffer) error {
meta := w.getNextFrameMeta()
meta.Option.Add(OptionData)
meta.Option.Set(OptionData)
frame := buf.New()
if err := frame.AppendSupplier(meta.AsSupplier()); err != nil {

@ -8,7 +8,7 @@ func (b Byte) Has(bb Byte) bool {
return (b & bb) != 0
}
func (b *Byte) Add(bb Byte) {
func (b *Byte) Set(bb Byte) {
*b |= bb
}

@ -0,0 +1,27 @@
package bitmask_test
import (
"testing"
. "v2ray.com/core/common/bitmask"
"v2ray.com/core/testing/assert"
)
func TestBitmaskByte(t *testing.T) {
assert := assert.On(t)
b := Byte(0)
b.Set(Byte(1))
assert.Bool(b.Has(1)).IsTrue()
b.Set(Byte(2))
assert.Bool(b.Has(2)).IsTrue()
assert.Bool(b.Has(1)).IsTrue()
b.Clear(Byte(1))
assert.Bool(b.Has(2)).IsTrue()
assert.Bool(b.Has(1)).IsFalse()
b.Toggle(Byte(2))
assert.Bool(b.Has(2)).IsFalse()
}

@ -3,6 +3,7 @@ package protocol
import (
"runtime"
"v2ray.com/core/common/bitmask"
"v2ray.com/core/common/net"
"v2ray.com/core/common/uuid"
)
@ -24,31 +25,16 @@ func (c RequestCommand) TransferType() TransferType {
return TransferTypePacket
}
// RequestOption is the options of a request.
type RequestOption byte
const (
// RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
RequestOptionChunkStream = RequestOption(0x01)
RequestOptionChunkStream bitmask.Byte = 0x01
// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
RequestOptionConnectionReuse = RequestOption(0x02)
RequestOptionConnectionReuse bitmask.Byte = 0x02
RequestOptionChunkMasking = RequestOption(0x04)
RequestOptionChunkMasking bitmask.Byte = 0x04
)
func (o RequestOption) Has(option RequestOption) bool {
return (o & option) == option
}
func (o *RequestOption) Set(option RequestOption) {
*o = (*o | option)
}
func (o *RequestOption) Clear(option RequestOption) {
*o = (*o & (^option))
}
type Security byte
func (s Security) Is(t SecurityType) bool {
@ -65,7 +51,7 @@ func NormSecurity(s Security) Security {
type RequestHeader struct {
Version byte
Command RequestCommand
Option RequestOption
Option bitmask.Byte
Security Security
Port net.Port
Address net.Address
@ -79,28 +65,14 @@ func (h *RequestHeader) Destination() net.Destination {
return net.TCPDestination(h.Address, h.Port)
}
type ResponseOption byte
const (
ResponseOptionConnectionReuse = ResponseOption(0x01)
ResponseOptionConnectionReuse bitmask.Byte = 0x01
)
func (o *ResponseOption) Set(option ResponseOption) {
*o = (*o | option)
}
func (o ResponseOption) Has(option ResponseOption) bool {
return (o & option) == option
}
func (o *ResponseOption) Clear(option ResponseOption) {
*o = (*o & (^option))
}
type ResponseCommand interface{}
type ResponseHeader struct {
Option ResponseOption
Option bitmask.Byte
Command ResponseCommand
}

@ -1,34 +0,0 @@
package protocol_test
import (
"testing"
. "v2ray.com/core/common/protocol"
"v2ray.com/core/testing/assert"
)
func TestRequestOptionSet(t *testing.T) {
assert := assert.On(t)
var option RequestOption
assert.Bool(option.Has(RequestOptionChunkStream)).IsFalse()
option.Set(RequestOptionChunkStream)
assert.Bool(option.Has(RequestOptionChunkStream)).IsTrue()
option.Set(RequestOptionChunkMasking)
assert.Bool(option.Has(RequestOptionChunkMasking)).IsTrue()
assert.Bool(option.Has(RequestOptionChunkStream)).IsTrue()
}
func TestRequestOptionClear(t *testing.T) {
assert := assert.On(t)
var option RequestOption
option.Set(RequestOptionChunkStream)
option.Set(RequestOptionChunkMasking)
option.Clear(RequestOptionChunkStream)
assert.Bool(option.Has(RequestOptionChunkStream)).IsFalse()
assert.Bool(option.Has(RequestOptionChunkMasking)).IsTrue()
}

@ -5,6 +5,7 @@ import (
"crypto/rand"
"io"
"v2ray.com/core/common/bitmask"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/net"
@ -14,7 +15,7 @@ import (
const (
Version = 1
RequestOptionOneTimeAuth = protocol.RequestOption(101)
RequestOptionOneTimeAuth bitmask.Byte = 0x01
AddrTypeIPv4 = 1
AddrTypeIPv6 = 4

@ -12,6 +12,7 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/bitmask"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/dice"
@ -203,7 +204,7 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
}
header := &protocol.ResponseHeader{
Option: protocol.ResponseOption(buffer[1]),
Option: bitmask.Byte(buffer[1]),
}
if buffer[2] != 0 {

@ -12,6 +12,7 @@ import (
"golang.org/x/crypto/chacha20poly1305"
"v2ray.com/core/common"
"v2ray.com/core/common/bitmask"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/net"
@ -165,7 +166,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
s.sessionHistory.add(sid)
s.responseHeader = buffer[33] // 1 byte
request.Option = protocol.RequestOption(buffer[34]) // 1 byte
request.Option = bitmask.Byte(buffer[34]) // 1 byte
padingLen := int(buffer[35] >> 4)
request.Security = protocol.NormSecurity(protocol.Security(buffer[35] & 0x0F))
// 1 bytes reserved

Loading…
Cancel
Save