Skip to content

Commit

Permalink
Merge pull request #6 from yezzey-gp/tmp
Browse files Browse the repository at this point in the history
Refactor and support external storage prefix setting
  • Loading branch information
reshke authored Nov 13, 2023
2 parents 8b82392 + da973a4 commit 2c6c465
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
2 changes: 0 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ var rootCmd = &cobra.Command{
return err
}

// fmt.Println("reply:", string(reply))
//
return nil
},
}
Expand Down
23 changes: 20 additions & 3 deletions pkg/client/request.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}
38 changes: 9 additions & 29 deletions pkg/proc/interaction.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -32,42 +28,26 @@ 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
}
if body[1] == byte(DecryptMessage) {
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
Expand Down
7 changes: 5 additions & 2 deletions pkg/proc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
"io"
"net"

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

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
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"io"
"path"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 2c6c465

Please sign in to comment.