server_env.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package scenarios
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "runtime"
  8. "time"
  9. _ "github.com/v2ray/v2ray-core/app/router/rules"
  10. "github.com/v2ray/v2ray-core/common/log"
  11. // The following are necessary as they register handlers in their init functions.
  12. _ "github.com/v2ray/v2ray-core/proxy/blackhole"
  13. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  14. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  15. _ "github.com/v2ray/v2ray-core/proxy/http"
  16. _ "github.com/v2ray/v2ray-core/proxy/shadowsocks"
  17. _ "github.com/v2ray/v2ray-core/proxy/socks"
  18. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  19. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  20. )
  21. var (
  22. runningServers = make([]*exec.Cmd, 0, 10)
  23. binaryPath string
  24. )
  25. func GetSourcePath() string {
  26. return filepath.Join("github.com", "v2ray", "v2ray-core", "shell", "point", "main")
  27. }
  28. func FillBinaryPath() error {
  29. dir, err := ioutil.TempDir("", "v2ray")
  30. if err != nil {
  31. return err
  32. }
  33. binaryPath = filepath.Join(dir, "v2ray")
  34. if runtime.GOOS == "windows" {
  35. binaryPath += ".exe"
  36. }
  37. return nil
  38. }
  39. func TestFile(filename string) string {
  40. return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
  41. }
  42. func InitializeServerSetOnce(testcase string) error {
  43. if err := InitializeServerServer(testcase); err != nil {
  44. return err
  45. }
  46. if err := InitializeServerClient(testcase); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func InitializeServerServer(testcase string) error {
  52. return InitializeServer(TestFile(testcase + "_server.json"))
  53. }
  54. func InitializeServerClient(testcase string) error {
  55. return InitializeServer(TestFile(testcase + "_client.json"))
  56. }
  57. func InitializeServer(configFile string) error {
  58. err := BuildV2Ray()
  59. if err != nil {
  60. return err
  61. }
  62. proc := RunV2Ray(configFile)
  63. err = proc.Start()
  64. if err != nil {
  65. return err
  66. }
  67. time.Sleep(time.Second)
  68. runningServers = append(runningServers, proc)
  69. return nil
  70. }
  71. func CloseAllServers() {
  72. log.Info("Closing all servers.")
  73. for _, server := range runningServers {
  74. server.Process.Kill()
  75. }
  76. runningServers = make([]*exec.Cmd, 0, 10)
  77. log.Info("All server closed.")
  78. }