Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new query types #9

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import (
var cfgPath string
var logLevel string
var decrypt bool
var encrypt bool

var rootCmd = &cobra.Command{
Use: "",
Short: "",
}

var catCmd = &cobra.Command{
Use: "cat",
Short: "cat",
RunE: func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
Expand All @@ -34,7 +40,7 @@ var rootCmd = &cobra.Command{
}

defer con.Close()
msg := proc.ConstructMessage(args[0], decrypt)
msg := proc.NewCatMessage(args[0], decrypt).Encode()
_, err = con.Write(msg)
if err != nil {
return err
Expand All @@ -51,10 +57,63 @@ var rootCmd = &cobra.Command{
},
}

var putCmd = &cobra.Command{
Use: "put",
Short: "put",
RunE: func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
if err != nil {
return err
}

instanceCnf := config.InstanceConfig()

con, err := net.Dial("unix", instanceCnf.SocketPath)

if err != nil {
return err
}

defer con.Close()
msg := proc.NewPutMessage(args[0], encrypt).Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed message")

_, err = io.Copy(os.Stdin, con)
if err != nil {
return err
}

msg = proc.NewCommandCompleteMessage().Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

msg = proc.NewReadyForQueryMessage().Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

return nil
},
}

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")

catCmd.PersistentFlags().BoolVarP(&decrypt, "decrypt", "d", false, "decrypt external object or not")
rootCmd.AddCommand(catCmd)

putCmd.PersistentFlags().BoolVarP(&encrypt, "encrypt", "e", false, "encrypt external object before put")
rootCmd.AddCommand(putCmd)
}

func main() {
Expand Down
55 changes: 55 additions & 0 deletions pkg/proc/cat_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package proc

import (
"bytes"
"encoding/binary"
)

type CatMessage struct {
ProtoMessage
Decrypt bool
Name string
}

func NewCatMessage(name string, decrypt bool) *CatMessage {
return &CatMessage{
Name: name,
Decrypt: decrypt,
}
}

func (c *CatMessage) Encode() []byte {
bt := []byte{
byte(MessageTypeCat),
0,
0,
0,
}

if c.Decrypt {
bt[1] = byte(DecryptMessage)
} else {
bt[1] = byte(NoDecryptMessage)
}

bt = append(bt, []byte(c.Name)...)
bt = append(bt, 0)
ln := len(bt) + 8

bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(ln))
return append(bs, bt...)
}

func GetCatName(b []byte) string {
buff := bytes.NewBufferString("")

for i := 0; i < len(b); i++ {
if b[i] == 0 {
break
}
buff.WriteByte(b[i])
}

return buff.String()
}
25 changes: 25 additions & 0 deletions pkg/proc/command_complete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package proc

import "encoding/binary"

type CommandCompleteMessage struct {
}

func NewCommandCompleteMessage() *CommandCompleteMessage {
return &CommandCompleteMessage{}
}

func (cc *CommandCompleteMessage) Encode() []byte {
bt := []byte{
byte(MessageTypeCommandComplete),
0,
0,
0,
}

ln := len(bt) + 8

bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(ln))
return append(bs, bt...)
}
31 changes: 31 additions & 0 deletions pkg/proc/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package proc

type ProtoMessage interface {
Decode([]byte)
Encode() []byte
}

type MessageType byte

type RequestEncryption byte

const (
MessageTypeCat = MessageType(42)
MessageTypePut = MessageType(43)
MessageTypeCommandComplete = MessageType(44)
MessageTypeReadyForQuery = MessageType(45)

DecryptMessage = RequestEncryption(1)
NoDecryptMessage = RequestEncryption(0)

EncryptMessage = RequestEncryption(1)
NoEncryptMessage = RequestEncryption(0)
)

func (m MessageType) String() string {
switch m {
case MessageTypeCat:
return "CAT"
}
return "UNKNOWN"
}
56 changes: 0 additions & 56 deletions pkg/proc/proto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proc

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand All @@ -21,24 +20,6 @@ func NewProtoReader(ycl *client.YClient) *ProtoReader {
}
}

type MessageType byte

type RequestEncryption byte

const (
MessageTypeCat = MessageType(42)
DecryptMessage = RequestEncryption(1)
NoDecryptMessage = RequestEncryption(0)
)

func (m MessageType) String() string {
switch m {
case MessageTypeCat:
return "CAT"
}
return "UNKNOWN"
}

const maxMsgLen = 1 << 20

func (r *ProtoReader) ReadPacket() (MessageType, []byte, error) {
Expand Down Expand Up @@ -71,40 +52,3 @@ func (r *ProtoReader) ReadPacket() (MessageType, []byte, error) {
msgType := MessageType(data[0])
return msgType, data, nil
}

func GetCatName(b []byte) string {
buff := bytes.NewBufferString("")

for i := 0; i < len(b); i++ {
if b[i] == 0 {
break
}
buff.WriteByte(b[i])
}

return buff.String()
}

func ConstructMessage(name string, decrypt bool) []byte {

bt := []byte{
byte(MessageTypeCat),
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) + 8

bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(ln))
return append(bs, bt...)
}
39 changes: 39 additions & 0 deletions pkg/proc/put_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package proc

import "encoding/binary"

type PutMessage struct {
ProtoMessage
Encrypt bool
Name string
}

func NewPutMessage(name string, encrypt bool) *PutMessage {
return &PutMessage{
Name: name,
Encrypt: encrypt,
}
}

func (c *PutMessage) Encode() []byte {
bt := []byte{
byte(MessageTypeCat),
0,
0,
0,
}

if c.Encrypt {
bt[1] = byte(EncryptMessage)
} else {
bt[1] = byte(NoEncryptMessage)
}

bt = append(bt, []byte(c.Name)...)
bt = append(bt, 0)
ln := len(bt) + 8

bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(ln))
return append(bs, bt...)
}
25 changes: 25 additions & 0 deletions pkg/proc/ready_for_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package proc

import "encoding/binary"

type ReadyForQueryMessage struct {
}

func NewReadyForQueryMessage() *ReadyForQueryMessage {
return &ReadyForQueryMessage{}
}

func (cc *ReadyForQueryMessage) Encode() []byte {
bt := []byte{
byte(MessageTypeReadyForQuery),
0,
0,
0,
}

ln := len(bt) + 8

bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(ln))
return append(bs, bt...)
}