nodejs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "github.com/langgenius/dify-sandbox/internal/utils/log"
  14. )
  15. type NodeJsRunner struct {
  16. runner.TempDirRunner
  17. }
  18. //go:embed prescript.js
  19. var nodejs_sandbox_fs []byte
  20. //go:embed nodejs.so
  21. var nodejs_lib []byte
  22. //go:embed dependens
  23. var nodejs_dependens embed.FS // it's a directory
  24. func init() {
  25. log.Info("initializing nodejs runner environment...")
  26. os.RemoveAll("/tmp/sandbox-nodejs")
  27. os.Remove("/tmp/sandbox-nodejs")
  28. err := os.MkdirAll("/tmp/sandbox-nodejs", 0755)
  29. if err != nil {
  30. log.Panic("failed to create /tmp/sandbox-nodejs")
  31. }
  32. err = os.WriteFile("/tmp/sandbox-nodejs/nodejs.so", nodejs_lib, 0755)
  33. if err != nil {
  34. log.Panic("failed to write /tmp/sandbox-nodejs/nodejs.so")
  35. }
  36. // remove /tmp/sandbox-nodejs-project
  37. os.RemoveAll("/tmp/sandbox-nodejs-project")
  38. os.Remove("/tmp/sandbox-nodejs-project")
  39. // copy the nodejs project into /tmp/sandbox-nodejs-project
  40. err = os.MkdirAll("/tmp/sandbox-nodejs-project", 0755)
  41. if err != nil {
  42. log.Panic("failed to create /tmp/sandbox-nodejs-project")
  43. }
  44. // copy the nodejs project into /tmp/sandbox-nodejs-project
  45. var recursively_copy func(src string, dst string) error
  46. recursively_copy = func(src string, dst string) error {
  47. entries, err := nodejs_dependens.ReadDir(src)
  48. if err != nil {
  49. return err
  50. }
  51. for _, entry := range entries {
  52. src_path := src + "/" + entry.Name()
  53. dst_path := dst + "/" + entry.Name()
  54. if entry.IsDir() {
  55. err = os.Mkdir(dst_path, 0755)
  56. if err != nil {
  57. return err
  58. }
  59. err = recursively_copy(src_path, dst_path)
  60. if err != nil {
  61. return err
  62. }
  63. } else {
  64. data, err := nodejs_dependens.ReadFile(src_path)
  65. if err != nil {
  66. return err
  67. }
  68. err = os.WriteFile(dst_path, data, 0755)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. }
  74. return nil
  75. }
  76. err = recursively_copy("dependens", "/tmp/sandbox-nodejs-project")
  77. if err != nil {
  78. log.Panic("failed to copy nodejs project")
  79. }
  80. log.Info("nodejs runner environment initialized")
  81. }
  82. var (
  83. NODEJS_REQUIRED_FS = []string{
  84. "/tmp/sandbox-nodejs-project/node_temp",
  85. "/tmp/sandbox-nodejs/nodejs.so",
  86. }
  87. )
  88. func (p *NodeJsRunner) Run(
  89. code string,
  90. timeout time.Duration,
  91. stdin []byte,
  92. preload string,
  93. options types.RunnerOptions,
  94. ) (chan []byte, chan []byte, chan bool, error) {
  95. // capture the output
  96. output_handler := runner.NewOutputCaptureRunner()
  97. output_handler.SetTimeout(timeout)
  98. err := p.WithTempDir(NODEJS_REQUIRED_FS, func(root_path string) error {
  99. output_handler.SetAfterExitHook(func() {
  100. os.RemoveAll(root_path)
  101. os.Remove(root_path)
  102. })
  103. // initialize the environment
  104. script_path, err := p.InitializeEnvironment(code, preload, root_path)
  105. if err != nil {
  106. return err
  107. }
  108. // create a new process
  109. cmd := exec.Command(
  110. static.GetDifySandboxGlobalConfigurations().NodejsPath,
  111. script_path,
  112. strconv.Itoa(static.SANDBOX_USER_UID),
  113. strconv.Itoa(static.SANDBOX_GROUP_ID),
  114. options.Json(),
  115. )
  116. cmd.Env = []string{}
  117. // capture the output
  118. err = output_handler.CaptureOutput(cmd)
  119. if err != nil {
  120. return err
  121. }
  122. return nil
  123. })
  124. if err != nil {
  125. return nil, nil, nil, err
  126. }
  127. return output_handler.GetStdout(), output_handler.GetStderr(), output_handler.GetDone(), nil
  128. }
  129. func (p *NodeJsRunner) InitializeEnvironment(code string, preload string, root_path string) (string, error) {
  130. node_sandbox_file := string(nodejs_sandbox_fs)
  131. if preload != "" {
  132. node_sandbox_file = fmt.Sprintf("%s\n%s", preload, node_sandbox_file)
  133. }
  134. // join nodejs_sandbox_fs and code
  135. code = node_sandbox_file + code
  136. // override root_path/tmp/sandbox-nodejs-project/prescript.js
  137. script_path := path.Join(root_path, "tmp/sandbox-nodejs-project/node_temp/node_temp/test.js")
  138. err := os.WriteFile(script_path, []byte(code), 0755)
  139. if err != nil {
  140. return "", err
  141. }
  142. return script_path, nil
  143. }