main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "github.com/langgenius/dify-sandbox/internal/static/nodejs_syscall"
  10. )
  11. const (
  12. SYSCALL_NUMS = 1000
  13. )
  14. func run(allowed_syscalls []int) {
  15. os.Chdir("/tmp/sandbox-2c8a41ec-04ae-4209-8ed7-17bd476803a6/tmp/sandbox-nodejs-project/node_temp/node_temp")
  16. nums := []string{}
  17. for _, syscall := range allowed_syscalls {
  18. nums = append(nums, strconv.Itoa(syscall))
  19. }
  20. os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
  21. _, err := exec.Command("node", "test.js", "65537", "1001", "{\"enable_network\":true}").Output()
  22. if err == nil {
  23. } else {
  24. fmt.Println(err.Error())
  25. }
  26. }
  27. func find_syscall(syscall int, syscalls []int) int {
  28. for i, s := range syscalls {
  29. if s == syscall {
  30. return i
  31. }
  32. }
  33. return -1
  34. }
  35. func main() {
  36. original := nodejs_syscall.ALLOW_SYSCALLS
  37. original = append(original, nodejs_syscall.ALLOW_NETWORK_SYSCALLS...)
  38. original = append(original, nodejs_syscall.ALLOW_ERROR_SYSCALLS...)
  39. // generate task list
  40. list := make([][]int, SYSCALL_NUMS)
  41. for i := 0; i < SYSCALL_NUMS; i++ {
  42. list[i] = make([]int, len(original))
  43. copy(list[i], original)
  44. // add i
  45. if find_syscall(i, original) == -1 {
  46. list[i] = append(list[i], i)
  47. }
  48. // for j := 15; j < 16; j++ {
  49. // if find_syscall(j, list[i]) == -1 {
  50. // list[i] = append(list[i], j)
  51. // }
  52. // }
  53. // for j := 24; j < 25; j++ {
  54. // if find_syscall(j, list[i]) == -1 {
  55. // list[i] = append(list[i], j)
  56. // }
  57. // }
  58. // for j := 60; j < 61; j++ {
  59. // if find_syscall(j, list[i]) == -1 {
  60. // list[i] = append(list[i], j)
  61. // }
  62. // }
  63. // for j := 186; j < 187; j++ {
  64. // if find_syscall(j, list[i]) == -1 {
  65. // list[i] = append(list[i], j)
  66. // }
  67. // }
  68. // for j := 204; j < 205; j++ {
  69. // if find_syscall(j, list[i]) == -1 {
  70. // list[i] = append(list[i], j)
  71. // }
  72. // }
  73. // for j := 273; j < 274; j++ {
  74. // if find_syscall(j, list[i]) == -1 {
  75. // list[i] = append(list[i], j)
  76. // }
  77. // }
  78. // for j := 334; j < 335; j++ {
  79. // if find_syscall(j, list[i]) == -1 {
  80. // list[i] = append(list[i], j)
  81. // }
  82. // }
  83. // for j := 435; j < 436; j++ {
  84. // if find_syscall(j, list[i]) == -1 {
  85. // list[i] = append(list[i], j)
  86. // }
  87. // }
  88. }
  89. lock := sync.Mutex{}
  90. wg := sync.WaitGroup{}
  91. i := 0
  92. // run 4 tasks concurrently
  93. for j := 0; j < 10; j++ {
  94. wg.Add(1)
  95. go func() {
  96. defer wg.Done()
  97. for {
  98. lock.Lock()
  99. if i >= len(list) {
  100. lock.Unlock()
  101. return
  102. }
  103. task := list[i]
  104. i++
  105. lock.Unlock()
  106. run(task)
  107. }
  108. }()
  109. }
  110. // wait for all tasks to finish
  111. wg.Wait()
  112. }