server_env.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package scenarios
  2. import (
  3. "os"
  4. "path/filepath"
  5. _ "github.com/v2ray/v2ray-core/app/router/json"
  6. _ "github.com/v2ray/v2ray-core/app/router/rules"
  7. _ "github.com/v2ray/v2ray-core/app/router/rules/json"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. "github.com/v2ray/v2ray-core/shell/point"
  10. pointjson "github.com/v2ray/v2ray-core/shell/point/json"
  11. // The following are neccesary as they register handlers in their init functions.
  12. _ "github.com/v2ray/v2ray-core/proxy/blackhole"
  13. _ "github.com/v2ray/v2ray-core/proxy/blackhole/json"
  14. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  15. _ "github.com/v2ray/v2ray-core/proxy/dokodemo/json"
  16. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  17. _ "github.com/v2ray/v2ray-core/proxy/freedom/json"
  18. _ "github.com/v2ray/v2ray-core/proxy/socks"
  19. _ "github.com/v2ray/v2ray-core/proxy/socks/json"
  20. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  21. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
  22. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  23. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
  24. )
  25. var (
  26. runningServers = make([]*point.Point, 0, 10)
  27. )
  28. func TestFile(filename string) string {
  29. return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
  30. }
  31. func InitializeServerSetOnce(testcase string) error {
  32. err := InitializeServer(TestFile(testcase + "_server.json"))
  33. if err != nil {
  34. return err
  35. }
  36. err = InitializeServer(TestFile(testcase + "_client.json"))
  37. if err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func InitializeServer(configFile string) error {
  43. config, err := pointjson.LoadConfig(configFile)
  44. if err != nil {
  45. log.Error("Failed to read config file (%s): %v", configFile, err)
  46. return err
  47. }
  48. vPoint, err := point.NewPoint(config)
  49. if err != nil {
  50. log.Error("Failed to create Point server: %v", err)
  51. return err
  52. }
  53. err = vPoint.Start()
  54. if err != nil {
  55. log.Error("Error starting Point server: %v", err)
  56. return err
  57. }
  58. runningServers = append(runningServers, vPoint)
  59. return nil
  60. }
  61. func CloseAllServers() {
  62. for _, server := range runningServers {
  63. server.Close()
  64. }
  65. runningServers = make([]*point.Point, 0, 10)
  66. }