nodejs.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package nodejs
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "strconv"
  9. "time"
  10. "github.com/langgenius/dify-sandbox/internal/core/runner"
  11. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  12. "github.com/langgenius/dify-sandbox/internal/static"
  13. )
  14. type NodeJsRunner struct {
  15. runner.TempDirRunner
  16. }
  17. //go:embed prescript.js
  18. var nodejs_sandbox_fs []byte
  19. var (
  20. REQUIRED_FS = []string{
  21. path.Join(LIB_PATH, PROJECT_NAME, "node_temp"),
  22. path.Join(LIB_PATH, LIB_NAME),
  23. "/etc/ssl/certs/ca-certificates.crt",
  24. "/etc/nsswitch.conf",
  25. "/etc/resolv.conf",
  26. "/run/systemd/resolve/stub-resolv.conf",
  27. "/etc/hosts",
  28. }
  29. )
  30. func (p *NodeJsRunner) Run(
  31. code string,
  32. timeout time.Duration,
  33. stdin []byte,
  34. preload string,
  35. options *types.RunnerOptions,
  36. ) (chan []byte, chan []byte, chan bool, error) {
  37. // capture the output
  38. output_handler := runner.NewOutputCaptureRunner()
  39. output_handler.SetTimeout(timeout)
  40. err := p.WithTempDir(REQUIRED_FS, func(root_path string) error {
  41. output_handler.SetAfterExitHook(func() {
  42. os.RemoveAll(root_path)
  43. os.Remove(root_path)
  44. })
  45. // initialize the environment
  46. script_path, err := p.InitializeEnvironment(code, preload, root_path)
  47. if err != nil {
  48. return err
  49. }
  50. // create a new process
  51. cmd := exec.Command(
  52. static.GetDifySandboxGlobalConfigurations().NodejsPath,
  53. script_path,
  54. strconv.Itoa(static.SANDBOX_USER_UID),
  55. strconv.Itoa(static.SANDBOX_GROUP_ID),
  56. options.Json(),
  57. )
  58. cmd.Env = []string{}
  59. // capture the output
  60. err = output_handler.CaptureOutput(cmd)
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. })
  66. if err != nil {
  67. return nil, nil, nil, err
  68. }
  69. return output_handler.GetStdout(), output_handler.GetStderr(), output_handler.GetDone(), nil
  70. }
  71. func (p *NodeJsRunner) InitializeEnvironment(code string, preload string, root_path string) (string, error) {
  72. if !checkLibAvaliable() {
  73. releaseLibBinary()
  74. }
  75. node_sandbox_file := string(nodejs_sandbox_fs)
  76. if preload != "" {
  77. node_sandbox_file = fmt.Sprintf("%s\n%s", preload, node_sandbox_file)
  78. }
  79. // join nodejs_sandbox_fs and code
  80. code = node_sandbox_file + code
  81. // override root_path/tmp/sandbox-nodejs-project/prescript.js
  82. script_path := path.Join(root_path, LIB_PATH, PROJECT_NAME, "node_temp/node_temp/test.js")
  83. err := os.WriteFile(script_path, []byte(code), 0755)
  84. if err != nil {
  85. return "", err
  86. }
  87. return script_path, nil
  88. }