From f1a15e92f553a59b176634999019531ca10e93da Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 21 Oct 2017 21:04:24 +0200 Subject: [PATCH] apply bitmask to other packages --- app/proxyman/mux/writer.go | 2 +- common/bitmask/byte.go | 2 +- common/bitmask/byte_test.go | 27 +++++++++++++++++++++ common/protocol/headers.go | 42 ++++++--------------------------- common/protocol/headers_test.go | 34 -------------------------- proxy/shadowsocks/protocol.go | 5 ++-- proxy/vmess/encoding/client.go | 3 ++- proxy/vmess/encoding/server.go | 5 ++-- 8 files changed, 44 insertions(+), 76 deletions(-) create mode 100644 common/bitmask/byte_test.go delete mode 100644 common/protocol/headers_test.go diff --git a/app/proxyman/mux/writer.go b/app/proxyman/mux/writer.go index 07c7f0816..116af25a1 100644 --- a/app/proxyman/mux/writer.go +++ b/app/proxyman/mux/writer.go @@ -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 { diff --git a/common/bitmask/byte.go b/common/bitmask/byte.go index 724fd9d93..8dcc5c0c8 100644 --- a/common/bitmask/byte.go +++ b/common/bitmask/byte.go @@ -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 } diff --git a/common/bitmask/byte_test.go b/common/bitmask/byte_test.go new file mode 100644 index 000000000..6e7e2e8b2 --- /dev/null +++ b/common/bitmask/byte_test.go @@ -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() +} diff --git a/common/protocol/headers.go b/common/protocol/headers.go index dd541b681..fb64ce712 100644 --- a/common/protocol/headers.go +++ b/common/protocol/headers.go @@ -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 } diff --git a/common/protocol/headers_test.go b/common/protocol/headers_test.go deleted file mode 100644 index 5ecbdd8ab..000000000 --- a/common/protocol/headers_test.go +++ /dev/null @@ -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() -} diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index a9cd686c3..25a360fa8 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -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" @@ -13,8 +14,8 @@ import ( ) const ( - Version = 1 - RequestOptionOneTimeAuth = protocol.RequestOption(101) + Version = 1 + RequestOptionOneTimeAuth bitmask.Byte = 0x01 AddrTypeIPv4 = 1 AddrTypeIPv6 = 4 diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index 0ff3c2bb5..d3eab9eb6 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -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 { diff --git a/proxy/vmess/encoding/server.go b/proxy/vmess/encoding/server.go index 257462c59..eefa9aaaa 100644 --- a/proxy/vmess/encoding/server.go +++ b/proxy/vmess/encoding/server.go @@ -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" @@ -164,8 +165,8 @@ 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 + s.responseHeader = buffer[33] // 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