Skip to content

Commit

Permalink
Merge pull request #2 from yezzey-gp/tmp
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
reshke authored Nov 13, 2023
2 parents 02f124c + f1cfa1d commit 281441a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 5 additions & 7 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"fmt"
"io"
"net"
"os"

"github.com/spf13/cobra"
"github.com/yezzey-gp/yproxy/config"
Expand Down Expand Up @@ -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
},
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/proc/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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),
))
Expand All @@ -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),
Expand All @@ -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",
))
Expand Down
12 changes: 11 additions & 1 deletion pkg/proc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net"

"github.com/yezzey-gp/yproxy/pkg/ylogger"
)

type ProtoReader struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 281441a

Please sign in to comment.