notifier.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // FsEvent defines a file system event
  27. type FsEvent struct {
  28. Action string `json:"action"`
  29. Username string `json:"username"`
  30. Path string `json:"path"`
  31. TargetPath string `json:"target_path,omitempty"`
  32. VirtualPath string `json:"virtual_path"`
  33. VirtualTargetPath string `json:"virtual_target_path,omitempty"`
  34. SSHCmd string `json:"ssh_cmd,omitempty"`
  35. FileSize int64 `json:"file_size,omitempty"`
  36. FsProvider int `json:"fs_provider"`
  37. Bucket string `json:"bucket,omitempty"`
  38. Endpoint string `json:"endpoint,omitempty"`
  39. Status int `json:"status"`
  40. Protocol string `json:"protocol"`
  41. IP string `json:"ip"`
  42. SessionID string `json:"session_id"`
  43. Timestamp int64 `json:"timestamp"`
  44. OpenFlags int `json:"open_flags,omitempty"`
  45. }
  46. // ProviderEvent defines a provider event
  47. type ProviderEvent struct {
  48. Action string
  49. Username string
  50. ObjectType string
  51. ObjectName string
  52. IP string
  53. ObjectData []byte
  54. Timestamp int64
  55. }
  56. // Notifier defines the interface for notifiers plugins
  57. type Notifier interface {
  58. NotifyFsEvent(event *FsEvent) error
  59. NotifyProviderEvent(event *ProviderEvent) error
  60. }
  61. // Plugin defines the implementation to serve/connect to a notifier plugin
  62. type Plugin struct {
  63. plugin.Plugin
  64. Impl Notifier
  65. }
  66. // GRPCServer defines the GRPC server implementation for this plugin
  67. func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  68. proto.RegisterNotifierServer(s, &GRPCServer{
  69. Impl: p.Impl,
  70. })
  71. return nil
  72. }
  73. // GRPCClient defines the GRPC client implementation for this plugin
  74. func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  75. return &GRPCClient{
  76. client: proto.NewNotifierClient(c),
  77. }, nil
  78. }