notifier.go 1.8 KB

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