From 86ceb1135d5c920f17b3c1a4671852e2b0dc3cd2 Mon Sep 17 00:00:00 2001 From: Vladimir Rachkin Date: Thu, 31 Oct 2024 17:44:33 +0300 Subject: [PATCH 1/5] Add error forwarding --- pkg/client/client.go | 19 ++++++++++++++++--- pkg/message/message.go | 3 +++ pkg/proc/proto.go | 5 +++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 9bb698d..51400f2 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -5,6 +5,7 @@ import ( "io" "net" "reflect" + "encoding/binary" "github.com/yezzey-gp/yproxy/pkg/ylogger" ) @@ -76,9 +77,21 @@ func NewYClient(c net.Conn) YproxyClient { func (y *YClient) ReplyError(err error, msg string) error { ylogger.Zero.Error().Err(err).Msg(msg) - _, _ = y.Conn.Write([]byte( - fmt.Sprintf("%s: %v", msg, err), - )) + bt := []byte{ + byte(55), + 0, + 0, + 0, + } + + bt = append(bt, []byte(fmt.Sprintf("%s: %v", msg, err))...) + bt = append(bt, 0) + ln := len(bt) + 8 + + bs := make([]byte, 8) + binary.BigEndian.PutUint64(bs, uint64(ln)) + + _, _ = y.Conn.Write(append(bs, bt...)) return nil } diff --git a/pkg/message/message.go b/pkg/message/message.go index 4392171..f2f2daa 100644 --- a/pkg/message/message.go +++ b/pkg/message/message.go @@ -23,6 +23,7 @@ const ( MessageTypeGool = MessageType(52) MessageTypePutV2 = MessageType(53) MessageTypeCatV2 = MessageType(54) + MessageTypeError = MessageType(55) DecryptMessage = RequestEncryption(1) NoDecryptMessage = RequestEncryption(0) @@ -59,6 +60,8 @@ func (m MessageType) String() string { return "COPY" case MessageTypeGool: return "GOOL" + case MessageTypeError: + return "ERROR" } return "UNKNOWN" } diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index af534dc..b3c1d95 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -51,5 +51,10 @@ func (r *ProtoReader) ReadPacket() (message.MessageType, []byte, error) { } msgType := message.MessageType(data[0]) + + if (msgType == message.MessageTypeError) { + return msgType, data, fmt.Errorf("proxy error: %s", data) + } + return msgType, data, nil } From 1f778c83121594e68fde793d7dc82e5ab828ff7c Mon Sep 17 00:00:00 2001 From: Vladimir Rachkin Date: Tue, 5 Nov 2024 10:00:05 +0300 Subject: [PATCH 2/5] Add ErrorMessage type of message --- pkg/client/client.go | 19 ++------------ pkg/message/error_message.go | 49 ++++++++++++++++++++++++++++++++++++ pkg/proc/proto.go | 8 ++++-- 3 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 pkg/message/error_message.go diff --git a/pkg/client/client.go b/pkg/client/client.go index 51400f2..418ba13 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1,12 +1,11 @@ package client import ( - "fmt" "io" "net" "reflect" - "encoding/binary" + "github.com/yezzey-gp/yproxy/pkg/message" "github.com/yezzey-gp/yproxy/pkg/ylogger" ) @@ -77,21 +76,7 @@ func NewYClient(c net.Conn) YproxyClient { func (y *YClient) ReplyError(err error, msg string) error { ylogger.Zero.Error().Err(err).Msg(msg) - bt := []byte{ - byte(55), - 0, - 0, - 0, - } - - bt = append(bt, []byte(fmt.Sprintf("%s: %v", msg, err))...) - bt = append(bt, 0) - ln := len(bt) + 8 - - bs := make([]byte, 8) - binary.BigEndian.PutUint64(bs, uint64(ln)) - - _, _ = y.Conn.Write(append(bs, bt...)) + _, _ = y.Conn.Write(message.NewErrorMessage(err, msg).Encode()) return nil } diff --git a/pkg/message/error_message.go b/pkg/message/error_message.go new file mode 100644 index 0000000..c483e79 --- /dev/null +++ b/pkg/message/error_message.go @@ -0,0 +1,49 @@ +package message + +import ( + "encoding/binary" +) + +type ErrorMessage struct { + Error string + Message string +} + +var _ ProtoMessage = &ListMessage{} + +func NewErrorMessage(err error, msg string) *ErrorMessage { + return &ErrorMessage{ + Error: err.Error(), + Message: msg, + } +} + +func (c *ErrorMessage) Encode() []byte { + encodedMessage := []byte{ + byte(MessageTypeError), + 0, + 0, + 0, + } + + byteError := []byte(c.Error) + byteLen := make([]byte, 8) + binary.BigEndian.PutUint64(byteLen, uint64(len(byteError))) + encodedMessage = append(encodedMessage, byteLen...) + encodedMessage = append(encodedMessage, byteError...) + + byteMessage := []byte(c.Error) + binary.BigEndian.PutUint64(byteLen, uint64(len(byteMessage))) + encodedMessage = append(encodedMessage, byteLen...) + encodedMessage = append(encodedMessage, byteMessage...) + + binary.BigEndian.PutUint64(byteLen, uint64(len(encodedMessage)+8)) + return append(byteLen, encodedMessage...) +} + +func (c *ErrorMessage) Decode(data []byte) { + errorLen := binary.BigEndian.Uint64(data[4:12]) + c.Error = string(data[12 : 12+errorLen]) + messageLen := binary.BigEndian.Uint64(data[12+errorLen : 12+errorLen+8]) + c.Message = string(data[12+errorLen+8 : 12+errorLen+8+messageLen]) +} diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index b3c1d95..f62f99c 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -52,8 +52,12 @@ func (r *ProtoReader) ReadPacket() (message.MessageType, []byte, error) { msgType := message.MessageType(data[0]) - if (msgType == message.MessageTypeError) { - return msgType, data, fmt.Errorf("proxy error: %s", data) + if msgType == message.MessageTypeError { + errorMessage := message.ErrorMessage{} + + errorMessage.Decode(data) + + return msgType, data, fmt.Errorf("proxy error: %s", errorMessage.Error) } return msgType, data, nil From 87aa2a369ffd36d9a59a0d5010c71040d2d1aef5 Mon Sep 17 00:00:00 2001 From: Vladimir Rachkin Date: Tue, 5 Nov 2024 10:13:10 +0300 Subject: [PATCH 3/5] Add test for ErrorMessage --- pkg/message/error_message.go | 2 +- pkg/message/message_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/message/error_message.go b/pkg/message/error_message.go index c483e79..4cc6abc 100644 --- a/pkg/message/error_message.go +++ b/pkg/message/error_message.go @@ -32,7 +32,7 @@ func (c *ErrorMessage) Encode() []byte { encodedMessage = append(encodedMessage, byteLen...) encodedMessage = append(encodedMessage, byteError...) - byteMessage := []byte(c.Error) + byteMessage := []byte(c.Message) binary.BigEndian.PutUint64(byteLen, uint64(len(byteMessage))) encodedMessage = append(encodedMessage, byteLen...) encodedMessage = append(encodedMessage, byteMessage...) diff --git a/pkg/message/message_test.go b/pkg/message/message_test.go index 2ea1c85..3aa6e49 100644 --- a/pkg/message/message_test.go +++ b/pkg/message/message_test.go @@ -2,6 +2,7 @@ package message_test import ( "encoding/binary" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -303,3 +304,22 @@ func TestDeleteMsg(t *testing.T) { assert.True(msg2.Confirm) assert.True(msg2.Garbage) } + +func TestErrorMsg(t *testing.T) { + assert := assert.New(t) + + errString := "could not list objects: NoCredentialProviders: no valid providers in chain." + messageString := "Deprecated. For verbose messaging see aws.Config.CredentialsChainVerboseErrors" + err := fmt.Errorf("%s", errString) + + msg := message.NewErrorMessage(err, messageString) + body := msg.Encode() + + assert.Equal(body[8], byte(message.MessageTypeError)) + + msg2 := message.ErrorMessage{} + msg2.Decode(body[8:]) + + assert.Equal(errString, msg2.Error) + assert.Equal(messageString, msg2.Message) +} From 58186ea1b593a629ffaac0d9bb50e1f9696ebae3 Mon Sep 17 00:00:00 2001 From: Vladimir Rachkin Date: Tue, 5 Nov 2024 12:31:31 +0300 Subject: [PATCH 4/5] Remove error check from ReadPacket() --- pkg/proc/proto.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index f62f99c..351c938 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -52,13 +52,5 @@ func (r *ProtoReader) ReadPacket() (message.MessageType, []byte, error) { msgType := message.MessageType(data[0]) - if msgType == message.MessageTypeError { - errorMessage := message.ErrorMessage{} - - errorMessage.Decode(data) - - return msgType, data, fmt.Errorf("proxy error: %s", errorMessage.Error) - } - return msgType, data, nil } From 1f0e95a70a3c0f9fd99b7a4d9ce8f5a9ed992d81 Mon Sep 17 00:00:00 2001 From: reshke Date: Tue, 5 Nov 2024 22:25:32 +0500 Subject: [PATCH 5/5] Update proto.go --- pkg/proc/proto.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index 351c938..156bf73 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -50,7 +50,5 @@ func (r *ProtoReader) ReadPacket() (message.MessageType, []byte, error) { return 0, nil, err } - msgType := message.MessageType(data[0]) - - return msgType, data, nil + return message.MessageType(data[0]), data, nil }