python.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package python
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/google/uuid"
  12. "github.com/langgenius/dify-sandbox/internal/core/runner"
  13. "github.com/langgenius/dify-sandbox/internal/core/runner/types"
  14. "github.com/langgenius/dify-sandbox/internal/static"
  15. )
  16. type PythonRunner struct {
  17. runner.TempDirRunner
  18. }
  19. //go:embed prescript.py
  20. var sandbox_fs []byte
  21. var (
  22. REQUIRED_FS = []string{
  23. path.Join(LIB_PATH, LIB_NAME),
  24. }
  25. )
  26. func (p *PythonRunner) Run(
  27. code string,
  28. timeout time.Duration,
  29. stdin []byte,
  30. preload string,
  31. options *types.RunnerOptions,
  32. ) (chan []byte, chan []byte, chan bool, error) {
  33. configuration := static.GetDifySandboxGlobalConfigurations()
  34. // initialize the environment
  35. untrusted_code_path, err := p.InitializeEnvironment(code, preload, options)
  36. if err != nil {
  37. return nil, nil, nil, err
  38. }
  39. // capture the output
  40. output_handler := runner.NewOutputCaptureRunner()
  41. output_handler.SetTimeout(timeout)
  42. err = p.WithTempDir(LIB_PATH, REQUIRED_FS, func(root_path string) error {
  43. // cleanup
  44. output_handler.SetAfterExitHook(func() {
  45. os.RemoveAll(root_path)
  46. os.Remove(root_path)
  47. })
  48. // create a new process
  49. cmd := exec.Command(
  50. configuration.PythonPath,
  51. untrusted_code_path,
  52. LIB_PATH,
  53. )
  54. cmd.Env = []string{}
  55. if configuration.Proxy.Socks5 != "" {
  56. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTPS_PROXY=%s", configuration.Proxy.Socks5))
  57. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_PROXY=%s", configuration.Proxy.Socks5))
  58. } else if configuration.Proxy.Https != "" || configuration.Proxy.Http != "" {
  59. if configuration.Proxy.Https != "" {
  60. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTPS_PROXY=%s", configuration.Proxy.Https))
  61. }
  62. if configuration.Proxy.Http != "" {
  63. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_PROXY=%s", configuration.Proxy.Http))
  64. }
  65. }
  66. err = output_handler.CaptureOutput(cmd)
  67. if err != nil {
  68. return err
  69. }
  70. return nil
  71. })
  72. if err != nil {
  73. return nil, nil, nil, err
  74. }
  75. return output_handler.GetStdout(), output_handler.GetStderr(), output_handler.GetDone(), nil
  76. }
  77. func (p *PythonRunner) InitializeEnvironment(code string, preload string, options *types.RunnerOptions) (string, error) {
  78. if !checkLibAvaliable() {
  79. // ensure environment is reversed
  80. releaseLibBinary()
  81. }
  82. // create a tmp dir and copy the python script
  83. temp_code_name := strings.ReplaceAll(uuid.New().String(), "-", "_")
  84. temp_code_name = strings.ReplaceAll(temp_code_name, "/", ".")
  85. script := strings.Replace(
  86. string(sandbox_fs),
  87. "{{uid}}", strconv.Itoa(static.SANDBOX_USER_UID), 1,
  88. )
  89. script = strings.Replace(
  90. script,
  91. "{{gid}}", strconv.Itoa(static.SANDBOX_GROUP_ID), 1,
  92. )
  93. if options.EnableNetwork {
  94. script = strings.Replace(
  95. script,
  96. "{{enable_network}}", "1", 1,
  97. )
  98. } else {
  99. script = strings.Replace(
  100. script,
  101. "{{enable_network}}", "0", 1,
  102. )
  103. }
  104. script = strings.Replace(
  105. script,
  106. "{{preload}}",
  107. fmt.Sprintf("%s\n", preload),
  108. 1,
  109. )
  110. code = strings.Replace(
  111. script,
  112. "{{code}}",
  113. code,
  114. 1,
  115. )
  116. untrusted_code_path := fmt.Sprintf("%s/tmp/%s.py", LIB_PATH, temp_code_name)
  117. err := os.MkdirAll(path.Dir(untrusted_code_path), 0755)
  118. if err != nil {
  119. return "", err
  120. }
  121. err = os.WriteFile(untrusted_code_path, []byte(code), 0755)
  122. if err != nil {
  123. return "", err
  124. }
  125. return untrusted_code_path, nil
  126. }