2023-08-21 14:52:06 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-09-20 04:05:44 +03:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-08-21 14:52:06 +03: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 04:05:44 +03:00
|
|
|
var message messages.IPCMessage
|
|
|
|
err := json.Unmarshal(<-recv, &message)
|
2023-08-21 14:52:06 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client VirtualClient) SendToAgent(request interface{}) (interface{}, error) {
|
2023-09-20 04:05:44 +03:00
|
|
|
message, err := messages.IPCMessageFromPayload(request)
|
2023-08-21 14:52:06 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-20 04:05:44 +03:00
|
|
|
messageJson, err := json.Marshal(message)
|
2023-08-21 14:52:06 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.send <- messageJson
|
|
|
|
result := virtualReader(client.recv)
|
2023-09-20 04:05:44 +03:00
|
|
|
return messages.ParsePayload(result.(messages.IPCMessage)), nil
|
2023-08-21 14:52:06 +03:00
|
|
|
}
|