diff --git a/cmd/client/main.go b/cmd/client/main.go index 78e3ab9..9ba62f2 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -47,8 +47,6 @@ var rootCmd = &cobra.Command{ return err } - // fmt.Println("reply:", string(reply)) - // return nil }, } diff --git a/pkg/client/request.go b/pkg/client/request.go index 3df47b5..c5c4a21 100644 --- a/pkg/client/request.go +++ b/pkg/client/request.go @@ -1,11 +1,28 @@ package client -import "net" +import ( + "fmt" + "net" + + "github.com/yezzey-gp/yproxy/pkg/ylogger" +) type YClient struct { - conn net.Conn + Conn net.Conn +} + +func NewYClient(c net.Conn) *YClient { + return &YClient{ + Conn: c, + } } -func ReplyError(err error) error { +func (y *YClient) ReplyError(err error, msg string) error { + + ylogger.Zero.Debug().Err(err).Msg(msg) + + _, _ = y.Conn.Write([]byte( + fmt.Sprintf("%s: %v", msg, err), + )) return nil } diff --git a/pkg/core/core.go b/pkg/core/core.go index 2eec6f4..c22da0f 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -9,6 +9,7 @@ import ( "time" "github.com/yezzey-gp/yproxy/config" + "github.com/yezzey-gp/yproxy/pkg/client" "github.com/yezzey-gp/yproxy/pkg/crypt" "github.com/yezzey-gp/yproxy/pkg/proc" "github.com/yezzey-gp/yproxy/pkg/sdnotifier" @@ -91,6 +92,6 @@ func (i *Instance) Run(instanceCnf *config.Instance) error { ylogger.Zero.Error().Err(err).Msg("failed to accept connection") } ylogger.Zero.Debug().Str("addr", clConn.LocalAddr().String()).Msg("accepted client connection") - go proc.ProcConn(s, cr, clConn) + go proc.ProcConn(s, cr, client.NewYClient(clConn)) } } diff --git a/pkg/proc/interaction.go b/pkg/proc/interaction.go index 143db38..fa5b3a5 100644 --- a/pkg/proc/interaction.go +++ b/pkg/proc/interaction.go @@ -1,24 +1,20 @@ package proc import ( - "fmt" "io" - "net" + "github.com/yezzey-gp/yproxy/pkg/client" "github.com/yezzey-gp/yproxy/pkg/crypt" "github.com/yezzey-gp/yproxy/pkg/storage" "github.com/yezzey-gp/yproxy/pkg/ylogger" ) -func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { - pr := NewProtoReader(c) +func ProcConn(s storage.StorageReader, cr crypt.Crypter, ycl *client.YClient) error { + pr := NewProtoReader(ycl) 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), - )) + _ = ycl.ReplyError(err, "failed to compelete request") return err } @@ -32,12 +28,7 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { ylogger.Zero.Debug().Str("object-path", name).Msg("cat object") 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), - )) + _ = ycl.ReplyError(err, "failed to compelete request") return err } @@ -45,29 +36,18 @@ 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), - )) + _ = ycl.ReplyError(err, "failed to compelete request") return err } } - io.Copy(c, r) + io.Copy(ycl.Conn, r) default: - ylogger.Zero.Debug().Int("type", int(tp)).Msg("wrong request type") - - _, err := c.Write([]byte( - "wrong request type", - )) - if err != nil { - return err - } + _ = ycl.ReplyError(nil, "wrong request type") - return c.Close() + return ycl.Conn.Close() } return nil diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index a264e75..21f5c8a 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -7,6 +7,7 @@ import ( "io" "net" + "github.com/yezzey-gp/yproxy/pkg/client" "github.com/yezzey-gp/yproxy/pkg/ylogger" ) @@ -14,8 +15,10 @@ type ProtoReader struct { c net.Conn } -func NewProtoReader(c net.Conn) *ProtoReader { - return &ProtoReader{c} +func NewProtoReader(ycl *client.YClient) *ProtoReader { + return &ProtoReader{ + c: ycl.Conn, + } } type MessageType byte diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index fdc5a69..b00ecc8 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -2,6 +2,7 @@ package storage import ( "io" + "path" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/s3" @@ -32,7 +33,7 @@ func (s *S3StorageReader) CatFileFromStorage(name string) (io.Reader, error) { return nil, err } - objectPath := name + objectPath := path.Join(s.cnf.StoragePrefix, name) input := &s3.GetObjectInput{ Bucket: &s.cnf.StorageBucket, Key: aws.String(objectPath),