2024-10-17 20:20:42 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-10-19 23:05:34 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-10-17 20:20:42 +00:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/plugin/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Plugin struct {
|
|
|
|
conn net.Conn
|
|
|
|
md domain.InitPluginRequest
|
|
|
|
}
|
2024-10-17 22:24:05 +00:00
|
|
|
|
2024-10-19 23:05:34 +00:00
|
|
|
func (p *Plugin) Init(initPayload any) error {
|
|
|
|
r := &domain.Ping{
|
|
|
|
Payload: initPayload,
|
|
|
|
}
|
|
|
|
|
|
|
|
pingData, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = p.conn.Write(pingData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pongData := make([]byte, 0)
|
|
|
|
if _, err := p.conn.Read(pongData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(pongData, pingData) {
|
|
|
|
return fmt.Errorf("ping-pong payload assertion error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-17 22:24:05 +00:00
|
|
|
func (p *Plugin) Close() error {
|
|
|
|
return p.conn.Close()
|
|
|
|
}
|