server_env.go 2.1 KB

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