From f9277958a5f813fb8aa15ec82a78f26843e7b99b Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 25 May 2018 17:16:15 +0200 Subject: [PATCH] DTLS header for MKCP --- main/distro/all/all.go | 1 + transport/internet/headers/tls/config.pb.go | 69 +++++++++++++++++++++ transport/internet/headers/tls/config.proto | 10 +++ transport/internet/headers/tls/dtls.go | 48 ++++++++++++++ transport/internet/headers/tls/dtls_test.go | 26 ++++++++ 5 files changed, 154 insertions(+) create mode 100644 transport/internet/headers/tls/config.pb.go create mode 100644 transport/internet/headers/tls/config.proto create mode 100644 transport/internet/headers/tls/dtls.go create mode 100644 transport/internet/headers/tls/dtls_test.go diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 0a70e6b53..f4a31439f 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -44,6 +44,7 @@ import ( _ "v2ray.com/core/transport/internet/headers/http" _ "v2ray.com/core/transport/internet/headers/noop" _ "v2ray.com/core/transport/internet/headers/srtp" + _ "v2ray.com/core/transport/internet/headers/tls" _ "v2ray.com/core/transport/internet/headers/utp" _ "v2ray.com/core/transport/internet/headers/wechat" diff --git a/transport/internet/headers/tls/config.pb.go b/transport/internet/headers/tls/config.pb.go new file mode 100644 index 000000000..b613db189 --- /dev/null +++ b/transport/internet/headers/tls/config.pb.go @@ -0,0 +1,69 @@ +package tls + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type PacketConfig struct { + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PacketConfig) Reset() { *m = PacketConfig{} } +func (m *PacketConfig) String() string { return proto.CompactTextString(m) } +func (*PacketConfig) ProtoMessage() {} +func (*PacketConfig) Descriptor() ([]byte, []int) { + return fileDescriptor_config_49e6316d1b9b007a, []int{0} +} +func (m *PacketConfig) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_PacketConfig.Unmarshal(m, b) +} +func (m *PacketConfig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_PacketConfig.Marshal(b, m, deterministic) +} +func (dst *PacketConfig) XXX_Merge(src proto.Message) { + xxx_messageInfo_PacketConfig.Merge(dst, src) +} +func (m *PacketConfig) XXX_Size() int { + return xxx_messageInfo_PacketConfig.Size(m) +} +func (m *PacketConfig) XXX_DiscardUnknown() { + xxx_messageInfo_PacketConfig.DiscardUnknown(m) +} + +var xxx_messageInfo_PacketConfig proto.InternalMessageInfo + +func init() { + proto.RegisterType((*PacketConfig)(nil), "v2ray.core.transport.internet.headers.tls.PacketConfig") +} + +func init() { + proto.RegisterFile("v2ray.com/core/transport/internet/headers/tls/config.proto", fileDescriptor_config_49e6316d1b9b007a) +} + +var fileDescriptor_config_49e6316d1b9b007a = []byte{ + // 163 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xb2, 0x2a, 0x33, 0x2a, 0x4a, + 0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x2f, 0x29, 0x4a, 0xcc, 0x2b, + 0x2e, 0xc8, 0x2f, 0x2a, 0xd1, 0xcf, 0xcc, 0x2b, 0x49, 0x2d, 0xca, 0x4b, 0x2d, 0xd1, 0xcf, 0x48, + 0x4d, 0x4c, 0x49, 0x2d, 0x2a, 0xd6, 0x2f, 0xc9, 0x29, 0xd6, 0x4f, 0xce, 0xcf, 0x4b, 0xcb, 0x4c, + 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xd2, 0x84, 0xe9, 0x2d, 0x4a, 0xd5, 0x83, 0xeb, 0xd3, + 0x83, 0xe9, 0xd3, 0x83, 0xea, 0xd3, 0x2b, 0xc9, 0x29, 0x56, 0xe2, 0xe3, 0xe2, 0x09, 0x48, 0x4c, + 0xce, 0x4e, 0x2d, 0x71, 0x06, 0x1b, 0xe0, 0x94, 0xc4, 0xa5, 0x9b, 0x9c, 0x9f, 0xab, 0x47, 0xb4, + 0x01, 0x01, 0x8c, 0x51, 0xcc, 0x25, 0x39, 0xc5, 0xab, 0x98, 0x34, 0xc3, 0x8c, 0x82, 0x12, 0x2b, + 0xf5, 0x9c, 0x41, 0x5a, 0x42, 0xe0, 0x5a, 0x3c, 0x61, 0x5a, 0x3c, 0xa0, 0x5a, 0x42, 0x72, 0x8a, + 0x93, 0xd8, 0xc0, 0xae, 0x34, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xd5, 0x21, 0xb5, 0x48, 0xe3, + 0x00, 0x00, 0x00, +} diff --git a/transport/internet/headers/tls/config.proto b/transport/internet/headers/tls/config.proto new file mode 100644 index 000000000..7bd8b5e8e --- /dev/null +++ b/transport/internet/headers/tls/config.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package v2ray.core.transport.internet.headers.tls; +option csharp_namespace = "V2Ray.Core.Transport.Internet.Headers.Tls"; +option go_package = "tls"; +option java_package = "com.v2ray.core.transport.internet.headers.tls"; +option java_multiple_files = true; + +message PacketConfig { +} diff --git a/transport/internet/headers/tls/dtls.go b/transport/internet/headers/tls/dtls.go new file mode 100644 index 000000000..4412191da --- /dev/null +++ b/transport/internet/headers/tls/dtls.go @@ -0,0 +1,48 @@ +package tls + +import ( + "context" + + "v2ray.com/core/common" + "v2ray.com/core/common/dice" +) + +// DTLS writes header as DTLS. See https://tools.ietf.org/html/rfc6347 +type DTLS struct { + epoch uint16 + sequence uint32 +} + +// Size implements PacketHeader. +func (*DTLS) Size() int32 { + return 1 + 2 + 2 + 3 + 2 +} + +// Write implements PacketHeader. +func (d *DTLS) Write(b []byte) (int, error) { + b[0] = 23 // application data + b[1] = 254 + b[2] = 253 + b[3] = byte(d.epoch >> 8) + b[4] = byte(d.epoch) + b[5] = byte(d.sequence >> 16) + b[6] = byte(d.sequence >> 8) + b[7] = byte(d.sequence) + d.sequence++ + l := dice.RollUint16() + b[8] = byte(l >> 8) + b[9] = byte(l) + return 10, nil +} + +// New creates a new UTP header for the given config. +func New(ctx context.Context, config interface{}) (interface{}, error) { + return &DTLS{ + epoch: dice.RollUint16(), + sequence: 0, + }, nil +} + +func init() { + common.Must(common.RegisterConfig((*PacketConfig)(nil), New)) +} diff --git a/transport/internet/headers/tls/dtls_test.go b/transport/internet/headers/tls/dtls_test.go new file mode 100644 index 000000000..b02cb841a --- /dev/null +++ b/transport/internet/headers/tls/dtls_test.go @@ -0,0 +1,26 @@ +package tls_test + +import ( + "context" + "testing" + + "v2ray.com/core/common/buf" + . "v2ray.com/core/transport/internet/headers/tls" + . "v2ray.com/ext/assert" +) + +func TestDTLSWrite(t *testing.T) { + assert := With(t) + + content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} + dtlsRaw, err := New(context.Background(), &PacketConfig{}) + assert(err, IsNil) + + dtls := dtlsRaw.(*DTLS) + + payload := buf.New() + payload.AppendSupplier(dtls.Write) + payload.Write(content) + + assert(payload.Len(), Equals, int32(len(content))+dtls.Size()) +}