From f1cfa1df3b21959918d6d641c146b66e64e82314 Mon Sep 17 00:00:00 2001 From: reshke Date: Mon, 13 Nov 2023 09:16:33 +0000 Subject: [PATCH] Fixes --- cmd/client/main.go | 12 +++++------- pkg/proc/interaction.go | 12 ++++++++++++ pkg/proc/proto.go | 12 +++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index d462e7a..78e3ab9 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -1,8 +1,9 @@ package main import ( - "fmt" + "io" "net" + "os" "github.com/spf13/cobra" "github.com/yezzey-gp/yproxy/config" @@ -41,16 +42,13 @@ var rootCmd = &cobra.Command{ ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed message") - reply := make([]byte, 1024) - - _, err = con.Read(reply) - + _, err = io.Copy(os.Stdout, con) if err != nil { return err } - fmt.Println("reply:", string(reply)) - + // fmt.Println("reply:", string(reply)) + // return nil }, } diff --git a/pkg/proc/interaction.go b/pkg/proc/interaction.go index 10b69cd..143db38 100644 --- a/pkg/proc/interaction.go +++ b/pkg/proc/interaction.go @@ -14,6 +14,12 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { pr := NewProtoReader(c) tp, body, err := pr.ReadPacket() if err != nil { + ylogger.Zero.Debug().Err(err).Msg("failed to compelete request") + + _, _ = c.Write([]byte( + fmt.Sprintf("failed to compelete request: %v", err), + )) + return err } @@ -27,6 +33,8 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { r, err := s.CatFileFromStorage(name) if err != nil { + ylogger.Zero.Debug().Err(err).Msg("failed to compelete request") + _, _ = c.Write([]byte( fmt.Sprintf("failed to compelete request: %v", err), )) @@ -37,6 +45,7 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { ylogger.Zero.Debug().Str("object-path", name).Msg("decrypt object ") r, err = cr.Decrypt(r) if err != nil { + ylogger.Zero.Debug().Err(err).Msg("failed to compelete request") _, _ = c.Write([]byte( fmt.Sprintf("failed to compelete request: %v", err), @@ -48,6 +57,9 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { io.Copy(c, r) default: + + ylogger.Zero.Debug().Int("type", int(tp)).Msg("wrong request type") + _, err := c.Write([]byte( "wrong request type", )) diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index 25c153f..a264e75 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -6,6 +6,8 @@ import ( "fmt" "io" "net" + + "github.com/yezzey-gp/yproxy/pkg/ylogger" ) type ProtoReader struct { @@ -49,6 +51,14 @@ func (r *ProtoReader) ReadPacket() (MessageType, []byte, error) { return 0, nil, fmt.Errorf("message too big") } + if dataLen <= 8 { + return 0, nil, fmt.Errorf("message empty") + } + + dataLen -= 8 + + ylogger.Zero.Debug().Uint64("size", dataLen).Msg("requested packet") + data := make([]byte, dataLen) _, err = io.ReadFull(r.c, data) if err != nil { @@ -89,7 +99,7 @@ func ConstructMessage(name string, decrypt bool) []byte { bt = append(bt, []byte(name)...) bt = append(bt, 0) - ln := len(bt) + ln := len(bt) + 8 bs := make([]byte, 8) binary.BigEndian.PutUint64(bs, uint64(ln))