ipfilter.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package plugin
  15. import (
  16. "fmt"
  17. "github.com/hashicorp/go-hclog"
  18. "github.com/hashicorp/go-plugin"
  19. "github.com/sftpgo/sdk/plugin/ipfilter"
  20. "github.com/drakkan/sftpgo/v2/internal/logger"
  21. )
  22. type ipFilterPlugin struct {
  23. config Config
  24. filter ipfilter.Filter
  25. client *plugin.Client
  26. }
  27. func newIPFilterPlugin(config Config) (*ipFilterPlugin, error) {
  28. p := &ipFilterPlugin{
  29. config: config,
  30. }
  31. if err := p.initialize(); err != nil {
  32. logger.Warn(logSender, "", "unable to create IP filter plugin: %v, config %+v", err, config)
  33. return nil, err
  34. }
  35. return p, nil
  36. }
  37. func (p *ipFilterPlugin) exited() bool {
  38. return p.client.Exited()
  39. }
  40. func (p *ipFilterPlugin) cleanup() {
  41. p.client.Kill()
  42. }
  43. func (p *ipFilterPlugin) initialize() error {
  44. logger.Debug(logSender, "", "create new IP filter plugin %q", p.config.Cmd)
  45. killProcess(p.config.Cmd)
  46. secureConfig, err := p.config.getSecureConfig()
  47. if err != nil {
  48. return err
  49. }
  50. client := plugin.NewClient(&plugin.ClientConfig{
  51. HandshakeConfig: ipfilter.Handshake,
  52. Plugins: ipfilter.PluginMap,
  53. Cmd: p.config.getCommand(),
  54. SkipHostEnv: true,
  55. AllowedProtocols: []plugin.Protocol{
  56. plugin.ProtocolGRPC,
  57. },
  58. AutoMTLS: p.config.AutoMTLS,
  59. SecureConfig: secureConfig,
  60. Managed: false,
  61. Logger: &logger.HCLogAdapter{
  62. Logger: hclog.New(&hclog.LoggerOptions{
  63. Name: fmt.Sprintf("%v.%v", logSender, ipfilter.PluginName),
  64. Level: pluginsLogLevel,
  65. DisableTime: true,
  66. }),
  67. },
  68. })
  69. rpcClient, err := client.Client()
  70. if err != nil {
  71. logger.Debug(logSender, "", "unable to get rpc client for plugin %q: %v", p.config.Cmd, err)
  72. return err
  73. }
  74. raw, err := rpcClient.Dispense(ipfilter.PluginName)
  75. if err != nil {
  76. logger.Debug(logSender, "", "unable to get plugin %v from rpc client for command %q: %v",
  77. ipfilter.PluginName, p.config.Cmd, err)
  78. return err
  79. }
  80. p.client = client
  81. p.filter = raw.(ipfilter.Filter)
  82. return nil
  83. }