From 1bc310c2e927f2ec25c9cd649d73fb1a363eaffe Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Thu, 13 Feb 2025 14:01:04 +0000 Subject: [PATCH] remove PacketPaddingContainer abstraction --- client/lib/snowflake.go | 3 +- common/packetpadding/conn.go | 8 ++---- common/packetpadding/container.go | 38 +++++++++++++++++++------- common/packetpadding/containerIfce.go | 34 ----------------------- common/packetpadding/container_test.go | 19 ++++++------- server/lib/http.go | 2 +- 6 files changed, 42 insertions(+), 62 deletions(-) delete mode 100644 common/packetpadding/containerIfce.go diff --git a/client/lib/snowflake.go b/client/lib/snowflake.go index 939e593..a68d55d 100644 --- a/client/lib/snowflake.go +++ b/client/lib/snowflake.go @@ -344,8 +344,7 @@ func newSession(snowflakes SnowflakeCollector) (net.PacketConn, *smux.Session, e log.Println("---- Handler: snowflake assigned ----") packetConnWrapper := newPacketConnWrapper(dummyAddr{}, dummyAddr{}, - packetpadding.NewPaddableConnection(conn, - packetpadding.New())) + packetpadding.NewPaddableConnection(conn)) return packetConnWrapper, nil } diff --git a/common/packetpadding/conn.go b/common/packetpadding/conn.go index b214e70..db86a2a 100644 --- a/common/packetpadding/conn.go +++ b/common/packetpadding/conn.go @@ -13,21 +13,19 @@ type PaddableConnection interface { ReadWriteCloserPreservesBoundary } -func NewPaddableConnection(rwc ReadWriteCloserPreservesBoundary, padding PacketPaddingContainer) PaddableConnection { +func NewPaddableConnection(rwc ReadWriteCloserPreservesBoundary) PaddableConnection { return &paddableConnection{ ReadWriteCloserPreservesBoundary: rwc, - padding: padding, } } type paddableConnection struct { ReadWriteCloserPreservesBoundary - padding PacketPaddingContainer } func (c *paddableConnection) Write(p []byte) (n int, err error) { dataLen := len(p) - if _, err = c.ReadWriteCloserPreservesBoundary.Write(c.padding.Pack(p, 0)); err != nil { + if _, err = c.ReadWriteCloserPreservesBoundary.Write(Pack(p, 0)); err != nil { return 0, err } return dataLen, nil @@ -38,7 +36,7 @@ func (c *paddableConnection) Read(p []byte) (n int, err error) { return 0, err } - payload, _ := c.padding.Unpack(p[:n]) + payload, _ := Unpack(p[:n]) if payload != nil { copy(p, payload) } diff --git a/common/packetpadding/container.go b/common/packetpadding/container.go index 318ba2e..01c6246 100644 --- a/common/packetpadding/container.go +++ b/common/packetpadding/container.go @@ -1,22 +1,36 @@ +// Package packetpadding is an package that defines methods to pad packets +// with a given number of bytes, and to unpack the padding from a padded packet. +// The packet format is as follows if the desired output length is greater than +// 2 bytes: +// | data | padding | data length | +// The data length is a 16-bit big-endian integer that represents the length of +// the data in bytes. +// If the desired output length is 2 bytes or less, the packet format is as +// follows: +// | padding | +// No payload will be included in the packet. package packetpadding import "encoding/binary" -func New() PacketPaddingContainer { - return packetPaddingContainer{} -} - -type packetPaddingContainer struct { -} - -func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte { +// Pack pads the given data with the given number of bytes, and appends the +// length of the data to the end of the data. The returned byte slice +// contains the padded data. +// This generates a packet with a length of +// len(data_OWNERSHIP_RELINQUISHED) + padding + 2 +// @param data_OWNERSHIP_RELINQUISHED - The payload, this reference is consumed and should not be used after this call. +// @param padding - The number of padding bytes to add to the data. +func Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte { data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, paddingLength)...) dataLength := len(data_OWNERSHIP_RELINQUISHED) data = binary.BigEndian.AppendUint16(data, uint16(dataLength)) return data } -func (c packetPaddingContainer) Pad(paddingLength int) []byte { +// Pad returns a padding packet of padding length. +// If the padding length is less than 0, nil is returned. +// @param padding - The number of padding bytes to add to the data. +func Pad(paddingLength int) []byte { if assertPaddingLengthIsNotNegative := paddingLength < 0; assertPaddingLengthIsNotNegative { return nil } @@ -33,7 +47,11 @@ func (c packetPaddingContainer) Pad(paddingLength int) []byte { } -func (c packetPaddingContainer) Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) { +// Unpack extracts the data and padding from the given padded data. It +// returns the data and the number of padding bytes. +// the data may be nil. +// @param wrappedData_OWNERSHIP_RELINQUISHED - The packet, this reference is consumed and should not be used after this call. +func Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) { dataLength := len(wrappedData_OWNERSHIP_RELINQUISHED) if dataLength < 2 { return nil, dataLength diff --git a/common/packetpadding/containerIfce.go b/common/packetpadding/containerIfce.go deleted file mode 100644 index 84fac0c..0000000 --- a/common/packetpadding/containerIfce.go +++ /dev/null @@ -1,34 +0,0 @@ -package packetpadding - -// PacketPaddingContainer is an interface that defines methods to pad packets -// with a given number of bytes, and to unpack the padding from a padded packet. -// The packet format is as follows if the desired output length is greater than -// 2 bytes: -// | data | padding | data length | -// The data length is a 16-bit big-endian integer that represents the length of -// the data in bytes. -// If the desired output length is 2 bytes or less, the packet format is as -// follows: -// | padding | -// No payload will be included in the packet. -type PacketPaddingContainer interface { - // Pack pads the given data with the given number of bytes, and appends the - // length of the data to the end of the data. The returned byte slice - // contains the padded data. - // This generates a packet with a length of - // len(data_OWNERSHIP_RELINQUISHED) + padding + 2 - // @param data_OWNERSHIP_RELINQUISHED - The payload, this reference is consumed and should not be used after this call. - // @param padding - The number of padding bytes to add to the data. - Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte - - // Unpack extracts the data and padding from the given padded data. It - // returns the data and the number of padding bytes. - // the data may be nil. - // @param wrappedData_OWNERSHIP_RELINQUISHED - The packet, this reference is consumed and should not be used after this call. - Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) - - // Pad returns a padding packet of padding length. - // If the padding length is less than 0, nil is returned. - // @param padding - The number of padding bytes to add to the data. - Pad(paddingLength int) []byte -} diff --git a/common/packetpadding/container_test.go b/common/packetpadding/container_test.go index e68dcc8..53f57b2 100644 --- a/common/packetpadding/container_test.go +++ b/common/packetpadding/container_test.go @@ -10,12 +10,11 @@ import ( func TestPacketPaddingContainer(t *testing.T) { Convey("Given a PacketPaddingContainer", t, func() { - container := packetpadding.New() Convey("When packing data with padding", func() { data := []byte("testdata") paddingLength := 4 - packedData := container.Pack(data, paddingLength) + packedData := packetpadding.Pack(data, paddingLength) Convey("The packed data should have the correct length", func() { expectedLength := len(data) + paddingLength + 2 @@ -23,7 +22,7 @@ func TestPacketPaddingContainer(t *testing.T) { }) Convey("When unpacking the packed data", func() { - unpackedData, unpackedPaddingLength := container.Unpack(packedData) + unpackedData, unpackedPaddingLength := packetpadding.Unpack(packedData) Convey("The unpacked data should match the original data", func() { So(string(unpackedData), ShouldEqual, string(data)) @@ -38,7 +37,7 @@ func TestPacketPaddingContainer(t *testing.T) { Convey("When packing empty data with padding", func() { data := []byte("") paddingLength := 4 - packedData := container.Pack(data, paddingLength) + packedData := packetpadding.Pack(data, paddingLength) Convey("The packed data should have the correct length", func() { expectedLength := len(data) + paddingLength + 2 @@ -46,7 +45,7 @@ func TestPacketPaddingContainer(t *testing.T) { }) Convey("When unpacking the packed data", func() { - unpackedData, unpackedPaddingLength := container.Unpack(packedData) + unpackedData, unpackedPaddingLength := packetpadding.Unpack(packedData) Convey("The unpacked data should match the original data", func() { So(string(unpackedData), ShouldEqual, string(data)) @@ -61,7 +60,7 @@ func TestPacketPaddingContainer(t *testing.T) { Convey("When packing data with zero padding", func() { data := []byte("testdata") paddingLength := 0 - packedData := container.Pack(data, paddingLength) + packedData := packetpadding.Pack(data, paddingLength) Convey("The packed data should have the correct length", func() { expectedLength := len(data) + paddingLength + 2 @@ -69,7 +68,7 @@ func TestPacketPaddingContainer(t *testing.T) { }) Convey("When unpacking the packed data", func() { - unpackedData, unpackedPaddingLength := container.Unpack(packedData) + unpackedData, unpackedPaddingLength := packetpadding.Unpack(packedData) Convey("The unpacked data should match the original data", func() { So(string(unpackedData), ShouldEqual, string(data)) @@ -84,7 +83,7 @@ func TestPacketPaddingContainer(t *testing.T) { Convey("When padding data", func() { Convey("With a positive padding length", func() { padLength := 3 - padData := container.Pad(padLength) + padData := packetpadding.Pad(padLength) Convey("The padded data should have the correct length", func() { So(len(padData), ShouldEqual, padLength) @@ -93,7 +92,7 @@ func TestPacketPaddingContainer(t *testing.T) { Convey("With a zero padding length", func() { padLength := 0 - padData := container.Pad(padLength) + padData := packetpadding.Pad(padLength) Convey("The padded data should be empty", func() { So(len(padData), ShouldEqual, 0) @@ -102,7 +101,7 @@ func TestPacketPaddingContainer(t *testing.T) { Convey("With a negative padding length", func() { padLength := -1 - padData := container.Pad(padLength) + padData := packetpadding.Pad(padLength) Convey("The padded data should be nil", func() { So(padData, ShouldBeNil) diff --git a/server/lib/http.go b/server/lib/http.go index 26ecaab..85593e2 100644 --- a/server/lib/http.go +++ b/server/lib/http.go @@ -143,7 +143,7 @@ func (handler *httpHandler) turboTunnelUDPLikeMode(conn *websocketconn.Conn, add wg.Add(2) done := make(chan struct{}) - connPaddable := packetpadding.NewPaddableConnection(conn, packetpadding.New()) + connPaddable := packetpadding.NewPaddableConnection(conn) // The remainder of the WebSocket stream consists of packets, one packet // per WebSocket message. We read them one by one and feed them into the