From 59bc881d705801475b096bd42f9d04d6e9059885 Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 25 Feb 2016 17:14:49 +0100 Subject: [PATCH] move timestamp generator to protocol --- common/protocol/time.go | 11 ++++++++ .../protocol/time_test.go | 7 ++--- proxy/vmess/outbound/outbound.go | 2 +- proxy/vmess/protocol/rand.go | 28 ------------------- proxy/vmess/protocol/vmess.go | 4 +-- proxy/vmess/protocol/vmess_test.go | 14 ++++------ 6 files changed, 23 insertions(+), 43 deletions(-) rename proxy/vmess/protocol/rand_test.go => common/protocol/time_test.go (65%) delete mode 100644 proxy/vmess/protocol/rand.go diff --git a/common/protocol/time.go b/common/protocol/time.go index bb3d61a0..6777d493 100644 --- a/common/protocol/time.go +++ b/common/protocol/time.go @@ -1,6 +1,8 @@ package protocol import ( + "math/rand" + "github.com/v2ray/v2ray-core/common/serial" ) @@ -9,3 +11,12 @@ type Timestamp int64 func (this Timestamp) Bytes() []byte { return serial.Int64Literal(this).Bytes() } + +type TimestampGenerator func() Timestamp + +func NewTimestampGenerator(base Timestamp, delta int) TimestampGenerator { + return func() Timestamp { + rangeInDelta := rand.Intn(delta*2) - delta + return base + Timestamp(rangeInDelta) + } +} diff --git a/proxy/vmess/protocol/rand_test.go b/common/protocol/time_test.go similarity index 65% rename from proxy/vmess/protocol/rand_test.go rename to common/protocol/time_test.go index 137e8b35..d72ed10e 100644 --- a/proxy/vmess/protocol/rand_test.go +++ b/common/protocol/time_test.go @@ -4,8 +4,7 @@ import ( "testing" "time" - "github.com/v2ray/v2ray-core/common/protocol" - . "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + . "github.com/v2ray/v2ray-core/common/protocol" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -15,10 +14,10 @@ func TestGenerateRandomInt64InRange(t *testing.T) { base := time.Now().Unix() delta := 100 - generator := NewRandomTimestampGenerator(protocol.Timestamp(base), delta) + generator := NewTimestampGenerator(Timestamp(base), delta) for i := 0; i < 100; i++ { - v := int64(generator.Next()) + v := int64(generator()) assert.Int64(v).AtMost(base + int64(delta)) assert.Int64(v).AtLeast(base - int64(delta)) } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 58138ff1..b9f65029 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -107,7 +107,7 @@ func (this *VMessOutboundHandler) handleRequest(conn net.Conn, request *protocol buffer := alloc.NewBuffer().Clear() defer buffer.Release() - buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(proto.Timestamp(time.Now().Unix()), 30), buffer) + buffer, err = request.ToBytes(proto.NewTimestampGenerator(proto.Timestamp(time.Now().Unix()), 30), buffer) if err != nil { log.Error("VMessOut: Failed to serialize VMess request: ", err) return diff --git a/proxy/vmess/protocol/rand.go b/proxy/vmess/protocol/rand.go deleted file mode 100644 index ba6f75be..00000000 --- a/proxy/vmess/protocol/rand.go +++ /dev/null @@ -1,28 +0,0 @@ -package protocol - -import ( - "math/rand" - - "github.com/v2ray/v2ray-core/common/protocol" -) - -type RandomTimestampGenerator interface { - Next() protocol.Timestamp -} - -type RealRandomTimestampGenerator struct { - base protocol.Timestamp - delta int -} - -func NewRandomTimestampGenerator(base protocol.Timestamp, delta int) RandomTimestampGenerator { - return &RealRandomTimestampGenerator{ - base: base, - delta: delta, - } -} - -func (this *RealRandomTimestampGenerator) Next() protocol.Timestamp { - rangeInDelta := rand.Intn(this.delta*2) - this.delta - return this.base + protocol.Timestamp(rangeInDelta) -} diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index d569985a..216d3a51 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -189,12 +189,12 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { } // ToBytes returns a VMessRequest in the form of byte array. -func (this *VMessRequest) ToBytes(timestampGenerator RandomTimestampGenerator, buffer *alloc.Buffer) (*alloc.Buffer, error) { +func (this *VMessRequest) ToBytes(timestampGenerator proto.TimestampGenerator, buffer *alloc.Buffer) (*alloc.Buffer, error) { if buffer == nil { buffer = alloc.NewSmallBuffer().Clear() } - timestamp := timestampGenerator.Next() + timestamp := timestampGenerator() idHash := IDHash(this.User.AnyValidID().Bytes()) idHash.Write(timestamp.Bytes()) diff --git a/proxy/vmess/protocol/vmess_test.go b/proxy/vmess/protocol/vmess_test.go index 34f706a1..c0da2b27 100644 --- a/proxy/vmess/protocol/vmess_test.go +++ b/proxy/vmess/protocol/vmess_test.go @@ -16,12 +16,10 @@ import ( "github.com/v2ray/v2ray-core/testing/assert" ) -type FakeTimestampGenerator struct { - timestamp proto.Timestamp -} - -func (this *FakeTimestampGenerator) Next() proto.Timestamp { - return this.timestamp +func newStaticTimestampGenerator(t proto.Timestamp) proto.TimestampGenerator { + return func() proto.Timestamp { + return t + } } func TestVMessSerialization(t *testing.T) { @@ -56,7 +54,7 @@ func TestVMessSerialization(t *testing.T) { mockTime := proto.Timestamp(1823730) - buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil) + buffer, err := request.ToBytes(newStaticTimestampGenerator(mockTime), nil) if err != nil { t.Fatal(err) } @@ -114,6 +112,6 @@ func BenchmarkVMessRequestWriting(b *testing.B) { request.Port = v2net.Port(80) for i := 0; i < b.N; i++ { - request.ToBytes(NewRandomTimestampGenerator(proto.Timestamp(time.Now().Unix()), 30), nil) + request.ToBytes(proto.NewTimestampGenerator(proto.Timestamp(time.Now().Unix()), 30), nil) } }