From b38137bd137103d43d932744687b0764f7596c73 Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 8 Aug 2016 21:23:07 +0200 Subject: [PATCH] utp header --- shell/point/main/main.go | 1 + transport/internet/authenticators/utp/utp.go | 46 +++++++++++++++++++ .../internet/authenticators/utp/utp_test.go | 21 +++++++++ 3 files changed, 68 insertions(+) create mode 100644 transport/internet/authenticators/utp/utp.go create mode 100644 transport/internet/authenticators/utp/utp_test.go diff --git a/shell/point/main/main.go b/shell/point/main/main.go index eaf5160f8..d5a29dbbe 100644 --- a/shell/point/main/main.go +++ b/shell/point/main/main.go @@ -28,6 +28,7 @@ import ( _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/noop" _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/srtp" + _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/utp" ) var ( diff --git a/transport/internet/authenticators/utp/utp.go b/transport/internet/authenticators/utp/utp.go new file mode 100644 index 000000000..761e3a05d --- /dev/null +++ b/transport/internet/authenticators/utp/utp.go @@ -0,0 +1,46 @@ +package utp + +import ( + "math/rand" + + "github.com/v2ray/v2ray-core/common/alloc" + "github.com/v2ray/v2ray-core/transport/internet" +) + +type Config struct { + Version byte +} + +type UTP struct { + header byte + extension byte + connectionId uint16 +} + +func (this *UTP) Overhead() int { + return 4 +} + +func (this *UTP) Open(payload *alloc.Buffer) bool { + payload.SliceFrom(this.Overhead()) + return true +} + +func (this *UTP) Seal(payload *alloc.Buffer) { + payload.PrependUint16(this.connectionId) + payload.PrependBytes(this.header, this.extension) +} + +type UTPFactory struct{} + +func (this UTPFactory) Create(rawSettings internet.AuthenticatorConfig) internet.Authenticator { + return &UTP{ + header: 1, + extension: 0, + connectionId: uint16(rand.Intn(65536)), + } +} + +func init() { + internet.RegisterAuthenticator("utp", UTPFactory{}, func() interface{} { return new(Config) }) +} diff --git a/transport/internet/authenticators/utp/utp_test.go b/transport/internet/authenticators/utp/utp_test.go new file mode 100644 index 000000000..effc469d7 --- /dev/null +++ b/transport/internet/authenticators/utp/utp_test.go @@ -0,0 +1,21 @@ +package utp_test + +import ( + "testing" + + "github.com/v2ray/v2ray-core/common/alloc" + "github.com/v2ray/v2ray-core/testing/assert" + . "github.com/v2ray/v2ray-core/transport/internet/authenticators/utp" +) + +func TestUTPOpenSeal(t *testing.T) { + assert := assert.On(t) + + content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} + payload := alloc.NewLocalBuffer(2048).Clear().Append(content) + utp := UTP{} + utp.Seal(payload) + assert.Int(payload.Len()).GreaterThan(len(content)) + assert.Bool(utp.Open(payload)).IsTrue() + assert.Bytes(content).Equals(payload.Bytes()) +}