grpc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package notifier
  2. import (
  3. "context"
  4. "time"
  5. "google.golang.org/protobuf/types/known/emptypb"
  6. "github.com/drakkan/sftpgo/v2/sdk/plugin/notifier/proto"
  7. )
  8. const (
  9. rpcTimeout = 20 * time.Second
  10. )
  11. // GRPCClient is an implementation of Notifier interface that talks over RPC.
  12. type GRPCClient struct {
  13. client proto.NotifierClient
  14. }
  15. // NotifyFsEvent implements the Notifier interface
  16. func (c *GRPCClient) NotifyFsEvent(timestamp int64, action, username, fsPath, fsTargetPath, sshCmd, protocol, ip,
  17. virtualPath, virtualTargetPath, sessionID string, fileSize int64, status int,
  18. ) error {
  19. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  20. defer cancel()
  21. _, err := c.client.SendFsEvent(ctx, &proto.FsEvent{
  22. Timestamp: timestamp,
  23. Action: action,
  24. Username: username,
  25. FsPath: fsPath,
  26. FsTargetPath: fsTargetPath,
  27. SshCmd: sshCmd,
  28. FileSize: fileSize,
  29. Protocol: protocol,
  30. Ip: ip,
  31. Status: int32(status),
  32. VirtualPath: virtualPath,
  33. VirtualTargetPath: virtualTargetPath,
  34. SessionId: sessionID,
  35. })
  36. return err
  37. }
  38. // NotifyProviderEvent implements the Notifier interface
  39. func (c *GRPCClient) NotifyProviderEvent(timestamp int64, action, username, objectType, objectName, ip string, object []byte) error {
  40. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  41. defer cancel()
  42. _, err := c.client.SendProviderEvent(ctx, &proto.ProviderEvent{
  43. Timestamp: timestamp,
  44. Action: action,
  45. ObjectType: objectType,
  46. Username: username,
  47. Ip: ip,
  48. ObjectName: objectName,
  49. ObjectData: object,
  50. })
  51. return err
  52. }
  53. // GRPCServer defines the gRPC server that GRPCClient talks to.
  54. type GRPCServer struct {
  55. Impl Notifier
  56. }
  57. // SendFsEvent implements the serve side fs notify method
  58. func (s *GRPCServer) SendFsEvent(ctx context.Context, req *proto.FsEvent) (*emptypb.Empty, error) {
  59. event := &FsEvent{
  60. Action: req.Action,
  61. Username: req.Username,
  62. Path: req.FsPath,
  63. TargetPath: req.FsTargetPath,
  64. VirtualPath: req.VirtualPath,
  65. SSHCmd: req.SshCmd,
  66. FileSize: req.FileSize,
  67. Status: int(req.Status),
  68. Protocol: req.Protocol,
  69. IP: req.Ip,
  70. SessionID: req.SessionId,
  71. Timestamp: req.Timestamp,
  72. }
  73. err := s.Impl.NotifyFsEvent(event)
  74. return &emptypb.Empty{}, err
  75. }
  76. // SendProviderEvent implements the serve side provider event notify method
  77. func (s *GRPCServer) SendProviderEvent(ctx context.Context, req *proto.ProviderEvent) (*emptypb.Empty, error) {
  78. event := &ProviderEvent{
  79. Action: req.Action,
  80. Username: req.Username,
  81. ObjectType: req.ObjectType,
  82. ObjectName: req.ObjectName,
  83. IP: req.Ip,
  84. ObjectData: req.ObjectData,
  85. Timestamp: req.Timestamp,
  86. }
  87. err := s.Impl.NotifyProviderEvent(event)
  88. return &emptypb.Empty{}, err
  89. }