1
0

notifier.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Package notifier defines the implementation for event notifier plugin.
  2. // Notifier plugins allow to receive filesystem events such as file uploads,
  3. // downloads etc. and user events such as add, update, delete.
  4. package notifier
  5. import (
  6. "context"
  7. "github.com/hashicorp/go-plugin"
  8. "google.golang.org/grpc"
  9. "github.com/drakkan/sftpgo/v2/sdk/plugin/notifier/proto"
  10. )
  11. const (
  12. // PluginName defines the name for a notifier plugin
  13. PluginName = "notifier"
  14. )
  15. // Handshake is a common handshake that is shared by plugin and host.
  16. var Handshake = plugin.HandshakeConfig{
  17. ProtocolVersion: 1,
  18. MagicCookieKey: "SFTPGO_NOTIFIER_PLUGIN",
  19. MagicCookieValue: "c499b98b-cd59-4df2-92b3-6268817f4d80",
  20. }
  21. // PluginMap is the map of plugins we can dispense.
  22. var PluginMap = map[string]plugin.Plugin{
  23. PluginName: &Plugin{},
  24. }
  25. // Notifier defines the interface for notifiers plugins
  26. type Notifier interface {
  27. NotifyFsEvent(action, username, fsPath, fsTargetPath, sshCmd, protocol string, fileSize int64, status int) error
  28. NotifyUserEvent(action string, user []byte) error
  29. }
  30. // Plugin defines the implementation to serve/connect to a notifier plugin
  31. type Plugin struct {
  32. plugin.Plugin
  33. Impl Notifier
  34. }
  35. // GRPCServer defines the GRPC server implementation for this plugin
  36. func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  37. proto.RegisterNotifierServer(s, &GRPCServer{
  38. Impl: p.Impl,
  39. })
  40. return nil
  41. }
  42. // GRPCClient defines the GRPC client implementation for this plugin
  43. func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  44. return &GRPCClient{
  45. client: proto.NewNotifierClient(c),
  46. }, nil
  47. }