plugin.go 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package core
  2. import (
  3. "os"
  4. "path/filepath"
  5. "plugin"
  6. "strings"
  7. "v2ray.com/core/app/log"
  8. "v2ray.com/core/common/platform"
  9. )
  10. type PluginMetadata struct {
  11. Name string
  12. }
  13. const GetMetadataFuncName = "GetPluginMetadata"
  14. type GetMetadataFunc func() PluginMetadata
  15. func LoadPlugins() error {
  16. pluginPath := platform.GetPluginDirectory()
  17. dir, err := os.Open(pluginPath)
  18. if err != nil {
  19. return err
  20. }
  21. defer dir.Close()
  22. files, err := dir.Readdir(-1)
  23. if err != nil {
  24. return err
  25. }
  26. for _, file := range files {
  27. if !file.IsDir() && strings.HasSuffix(file.Name(), ".so") {
  28. p, err := plugin.Open(filepath.Join(pluginPath, file.Name()))
  29. if err != nil {
  30. return err
  31. }
  32. f, err := p.Lookup(GetMetadataFuncName)
  33. if err != nil {
  34. return err
  35. }
  36. if gmf, ok := f.(GetMetadataFunc); ok {
  37. metadata := gmf()
  38. log.Trace(newError("plugin (", metadata.Name, ") loaded."))
  39. }
  40. }
  41. }
  42. return nil
  43. }