From d08e07e6f747c36efa7f59973526757ad6fa0174 Mon Sep 17 00:00:00 2001 From: reshke Date: Mon, 13 Nov 2023 06:22:20 +0000 Subject: [PATCH] cli option to decrypt --- cmd/client/main.go | 4 +++- pkg/proc/client.go | 8 +++++++- pkg/proc/proto.go | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 690e0a8..d462e7a 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -12,6 +12,7 @@ import ( var cfgPath string var logLevel string +var decrypt bool var rootCmd = &cobra.Command{ Use: "", @@ -32,7 +33,7 @@ var rootCmd = &cobra.Command{ } defer con.Close() - msg := proc.ConstructMessage(args[0]) + msg := proc.ConstructMessage(args[0], decrypt) _, err = con.Write(msg) if err != nil { return err @@ -57,6 +58,7 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.PersistentFlags().StringVarP(&cfgPath, "config", "c", "/etc/yproxy/yproxy.yaml", "path to yproxy config file") rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "", "log level") + rootCmd.PersistentFlags().BoolVarP(&decrypt, "decrypt", "d", false, "decrypt external object or not") } func main() { diff --git a/pkg/proc/client.go b/pkg/proc/client.go index c3a716a..10b69cd 100644 --- a/pkg/proc/client.go +++ b/pkg/proc/client.go @@ -23,7 +23,7 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { case MessageTypeCat: // omit first byte name := GetCatName(body[4:]) - ylogger.Zero.Debug().Str("object-path", name).Msg("cat object ") + ylogger.Zero.Debug().Str("object-path", name).Msg("cat object") r, err := s.CatFileFromStorage(name) if err != nil { @@ -34,8 +34,14 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error { 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 { + + _, _ = c.Write([]byte( + fmt.Sprintf("failed to compelete request: %v", err), + )) + return err } } diff --git a/pkg/proc/proto.go b/pkg/proc/proto.go index e13d4fb..25c153f 100644 --- a/pkg/proc/proto.go +++ b/pkg/proc/proto.go @@ -21,8 +21,9 @@ type MessageType byte type RequestEncryption byte const ( - MessageTypeCat = MessageType(42) - DecryptMessage = RequestEncryption(1) + MessageTypeCat = MessageType(42) + DecryptMessage = RequestEncryption(1) + NoDecryptMessage = RequestEncryption(0) ) func (m MessageType) String() string { @@ -71,14 +72,21 @@ func GetCatName(b []byte) string { return buff.String() } -func ConstructMessage(name string) []byte { +func ConstructMessage(name string, decrypt bool) []byte { bt := []byte{ byte(MessageTypeCat), - byte(DecryptMessage), 0, 0, + 0, + } + + if decrypt { + bt[1] = byte(DecryptMessage) + } else { + bt[1] = byte(NoDecryptMessage) } + bt = append(bt, []byte(name)...) bt = append(bt, 0) ln := len(bt)