2023-08-21 13:52:06 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-09-20 03:05:44 +02:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-08-21 13:52:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewVirtualClient(recv chan []byte, send chan []byte) VirtualClient {
|
|
|
|
return VirtualClient{
|
|
|
|
recv,
|
|
|
|
send,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type VirtualClient struct {
|
|
|
|
recv chan []byte
|
|
|
|
send chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func virtualReader(recv chan []byte) interface{} {
|
|
|
|
for {
|
2023-09-20 03:05:44 +02:00
|
|
|
var message messages.IPCMessage
|
|
|
|
err := json.Unmarshal(<-recv, &message)
|
2023-08-21 13:52:06 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client VirtualClient) SendToAgent(request interface{}) (interface{}, error) {
|
2023-09-20 03:05:44 +02:00
|
|
|
message, err := messages.IPCMessageFromPayload(request)
|
2023-08-21 13:52:06 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-20 03:05:44 +02:00
|
|
|
messageJson, err := json.Marshal(message)
|
2023-08-21 13:52:06 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.send <- messageJson
|
|
|
|
result := virtualReader(client.recv)
|
2023-09-20 03:05:44 +02:00
|
|
|
return messages.ParsePayload(result.(messages.IPCMessage)), nil
|
2023-08-21 13:52:06 +02:00
|
|
|
}
|