python.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package python
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/google/uuid"
  11. "github.com/langgenius/dify-sandbox/internal/core/runner"
  12. python_dependencies "github.com/langgenius/dify-sandbox/internal/core/runner/python/dependencies"
  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 python_sandbox_fs []byte
  21. var (
  22. PYTHON_REQUIRED_FS = []string{
  23. "/tmp/sandbox-python/python.so",
  24. "/etc/ssl/certs/ca-certificates.crt",
  25. "/usr/local/lib/python3.10/site-packages/certifi/cacert.pem",
  26. "/usr/local/lib/python3.10/dist-packages/certifi/cacert.pem",
  27. "/etc/nsswitch.conf",
  28. "/etc/resolv.conf",
  29. "/run/systemd/resolve/stub-resolv.conf",
  30. "/run/resolvconf/resolv.conf",
  31. "/etc/hosts",
  32. }
  33. )
  34. func (p *PythonRunner) Run(
  35. code string,
  36. timeout time.Duration,
  37. stdin []byte,
  38. preload string,
  39. options *types.RunnerOptions,
  40. ) (chan []byte, chan []byte, chan bool, error) {
  41. configuration := static.GetDifySandboxGlobalConfigurations()
  42. // initialize the environment
  43. untrusted_code_path, err := p.InitializeEnvironment(code, preload, options)
  44. if err != nil {
  45. return nil, nil, nil, err
  46. }
  47. // capture the output
  48. output_handler := runner.NewOutputCaptureRunner()
  49. output_handler.SetTimeout(timeout)
  50. err = p.WithTempDir(PYTHON_REQUIRED_FS, func(root_path string) error {
  51. // cleanup
  52. output_handler.SetAfterExitHook(func() {
  53. os.RemoveAll(root_path)
  54. os.Remove(root_path)
  55. })
  56. // create a new process
  57. cmd := exec.Command(
  58. configuration.PythonPath,
  59. untrusted_code_path,
  60. )
  61. cmd.Env = []string{}
  62. if configuration.Proxy.Socks5 != "" {
  63. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTPS_PROXY=socks5://%s", configuration.Proxy.Socks5))
  64. cmd.Env = append(cmd.Env, fmt.Sprintf("HTTP_PROXY=socks5://%s", configuration.Proxy.Socks5))
  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. // create a tmp dir and copy the python script
  79. temp_code_name := strings.ReplaceAll(uuid.New().String(), "-", "_")
  80. temp_code_name = strings.ReplaceAll(temp_code_name, "/", ".")
  81. packages_preload := make([]string, len(options.Dependencies))
  82. for i, dependency := range options.Dependencies {
  83. packages_preload[i] = python_dependencies.GetDependencies(dependency.Name, dependency.Version)
  84. }
  85. script := strings.Replace(
  86. string(python_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%s", preload, strings.Join(packages_preload, "\n")),
  108. 1,
  109. )
  110. code = strings.Replace(
  111. script,
  112. "{{code}}",
  113. code,
  114. 1,
  115. )
  116. untrusted_code_path := fmt.Sprintf("/tmp/code/%s.py", temp_code_name)
  117. err := os.MkdirAll("/tmp/code", 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. }