server_env.go 1.9 KB

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