1
0

utils.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package plugins
  2. import (
  3. "errors"
  4. "math/rand"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "imuslab.com/zoraxy/mod/netutils"
  9. )
  10. /*
  11. Check if the folder contains a valid plugin in either one of the forms
  12. 1. Contain a file that have the same name as its parent directory, either executable or .exe on Windows
  13. 2. Contain a start.sh or start.bat file
  14. Return the path of the plugin entry point if found
  15. */
  16. func (m *Manager) GetPluginEntryPoint(folderpath string) (string, error) {
  17. info, err := os.Stat(folderpath)
  18. if err != nil {
  19. return "", err
  20. }
  21. if !info.IsDir() {
  22. return "", errors.New("path is not a directory")
  23. }
  24. expectedBinaryPath := filepath.Join(folderpath, filepath.Base(folderpath))
  25. if runtime.GOOS == "windows" {
  26. expectedBinaryPath += ".exe"
  27. }
  28. if _, err := os.Stat(expectedBinaryPath); err == nil {
  29. return expectedBinaryPath, nil
  30. }
  31. if _, err := os.Stat(filepath.Join(folderpath, "start.sh")); err == nil {
  32. return filepath.Join(folderpath, "start.sh"), nil
  33. }
  34. if _, err := os.Stat(filepath.Join(folderpath, "start.bat")); err == nil {
  35. return filepath.Join(folderpath, "start.bat"), nil
  36. }
  37. return "", errors.New("No valid entry point found")
  38. }
  39. // Log logs a message with an optional error
  40. func (m *Manager) Log(message string, err error) {
  41. m.Options.Logger.PrintAndLog("plugin-manager", message, err)
  42. }
  43. // getRandomPortNumber generates a random port number between 49152 and 65535
  44. func getRandomPortNumber() int {
  45. portNo := rand.Intn(65535-49152) + 49152
  46. //Check if the port is already in use
  47. for netutils.CheckIfPortOccupied(portNo) {
  48. portNo = rand.Intn(65535-49152) + 49152
  49. }
  50. return portNo
  51. }