mirror of https://github.com/v2ray/v2ray-core
apply bitmask to other packages
parent
8c6f73f30b
commit
f1a15e92f5
|
@ -63,7 +63,7 @@ func (w *Writer) writeMetaOnly() error {
|
||||||
|
|
||||||
func (w *Writer) writeData(mb buf.MultiBuffer) error {
|
func (w *Writer) writeData(mb buf.MultiBuffer) error {
|
||||||
meta := w.getNextFrameMeta()
|
meta := w.getNextFrameMeta()
|
||||||
meta.Option.Add(OptionData)
|
meta.Option.Set(OptionData)
|
||||||
|
|
||||||
frame := buf.New()
|
frame := buf.New()
|
||||||
if err := frame.AppendSupplier(meta.AsSupplier()); err != nil {
|
if err := frame.AppendSupplier(meta.AsSupplier()); err != nil {
|
||||||
|
|
|
@ -8,7 +8,7 @@ func (b Byte) Has(bb Byte) bool {
|
||||||
return (b & bb) != 0
|
return (b & bb) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Byte) Add(bb Byte) {
|
func (b *Byte) Set(bb Byte) {
|
||||||
*b |= bb
|
*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 (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/bitmask"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
"v2ray.com/core/common/uuid"
|
"v2ray.com/core/common/uuid"
|
||||||
)
|
)
|
||||||
|
@ -24,31 +25,16 @@ func (c RequestCommand) TransferType() TransferType {
|
||||||
return TransferTypePacket
|
return TransferTypePacket
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestOption is the options of a request.
|
|
||||||
type RequestOption byte
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
|
// 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 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
|
type Security byte
|
||||||
|
|
||||||
func (s Security) Is(t SecurityType) bool {
|
func (s Security) Is(t SecurityType) bool {
|
||||||
|
@ -65,7 +51,7 @@ func NormSecurity(s Security) Security {
|
||||||
type RequestHeader struct {
|
type RequestHeader struct {
|
||||||
Version byte
|
Version byte
|
||||||
Command RequestCommand
|
Command RequestCommand
|
||||||
Option RequestOption
|
Option bitmask.Byte
|
||||||
Security Security
|
Security Security
|
||||||
Port net.Port
|
Port net.Port
|
||||||
Address net.Address
|
Address net.Address
|
||||||
|
@ -79,28 +65,14 @@ func (h *RequestHeader) Destination() net.Destination {
|
||||||
return net.TCPDestination(h.Address, h.Port)
|
return net.TCPDestination(h.Address, h.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseOption byte
|
|
||||||
|
|
||||||
const (
|
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 ResponseCommand interface{}
|
||||||
|
|
||||||
type ResponseHeader struct {
|
type ResponseHeader struct {
|
||||||
Option ResponseOption
|
Option bitmask.Byte
|
||||||
Command ResponseCommand
|
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"
|
"crypto/rand"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/bitmask"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/crypto"
|
"v2ray.com/core/common/crypto"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
|
@ -14,7 +15,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = 1
|
Version = 1
|
||||||
RequestOptionOneTimeAuth = protocol.RequestOption(101)
|
RequestOptionOneTimeAuth bitmask.Byte = 0x01
|
||||||
|
|
||||||
AddrTypeIPv4 = 1
|
AddrTypeIPv4 = 1
|
||||||
AddrTypeIPv6 = 4
|
AddrTypeIPv6 = 4
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
|
|
||||||
"v2ray.com/core/app/log"
|
"v2ray.com/core/app/log"
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/bitmask"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/crypto"
|
"v2ray.com/core/common/crypto"
|
||||||
"v2ray.com/core/common/dice"
|
"v2ray.com/core/common/dice"
|
||||||
|
@ -203,7 +204,7 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
||||||
}
|
}
|
||||||
|
|
||||||
header := &protocol.ResponseHeader{
|
header := &protocol.ResponseHeader{
|
||||||
Option: protocol.ResponseOption(buffer[1]),
|
Option: bitmask.Byte(buffer[1]),
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer[2] != 0 {
|
if buffer[2] != 0 {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
|
|
||||||
"golang.org/x/crypto/chacha20poly1305"
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/bitmask"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/crypto"
|
"v2ray.com/core/common/crypto"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
|
@ -165,7 +166,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||||
s.sessionHistory.add(sid)
|
s.sessionHistory.add(sid)
|
||||||
|
|
||||||
s.responseHeader = buffer[33] // 1 byte
|
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)
|
padingLen := int(buffer[35] >> 4)
|
||||||
request.Security = protocol.NormSecurity(protocol.Security(buffer[35] & 0x0F))
|
request.Security = protocol.NormSecurity(protocol.Security(buffer[35] & 0x0F))
|
||||||
// 1 bytes reserved
|
// 1 bytes reserved
|
||||||
|
|
Loading…
Reference in New Issue