package plugin import ( "context" "net" "runtime" "strconv" "strings" "github.com/Neur0toxine/sshpoke/pkg/convert" "github.com/Neur0toxine/sshpoke/pkg/dto" "github.com/Neur0toxine/sshpoke/pkg/plugin/pb" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/protobuf/types/known/emptypb" ) type Client struct { parent pb.PluginServiceClient token string close func() error } func NewClient(addr, token string) (*Client, error) { conn, err := grpc.Dial(normalizeAddr(addr), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err } c := &Client{ parent: pb.NewPluginServiceClient(conn), token: token, close: conn.Close, } runtime.SetFinalizer(c, connCloser) return c, nil } func (c *Client) Event(ctx context.Context) (*Stream, error) { stream, err := c.parent.Event(ctx, &emptypb.Empty{}) if err != nil { return nil, err } return &Stream{stream: stream}, nil } func (c *Client) EventStatus(ctx context.Context, status dto.EventStatus) error { _, err := c.parent.EventStatus(ctx, convert.AppEventStatusToMessage(status)) return err } func connCloser(c *Client) { _ = c.close() } func normalizeAddr(addr string) string { addr = strings.TrimSpace(addr) if strings.HasPrefix(addr, "grpc://") { addr = addr[7:] } host, port, err := net.SplitHostPort(addr) if err != nil && err.Error() == "missing port in address" { host, port, err = net.SplitHostPort(addr + ":" + strconv.Itoa(DefaultPort)) } if err != nil { return "" } return host + ":" + port }