server_env.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/dokodemo"
  14. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  15. _ "github.com/v2ray/v2ray-core/proxy/socks"
  16. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  17. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  18. )
  19. var (
  20. runningServers = make([]*point.Point, 0, 10)
  21. )
  22. func TestFile(filename string) string {
  23. return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
  24. }
  25. func InitializeServerSetOnce(testcase string) error {
  26. err := InitializeServer(TestFile(testcase + "_server.json"))
  27. if err != nil {
  28. return err
  29. }
  30. err = InitializeServer(TestFile(testcase + "_client.json"))
  31. if err != nil {
  32. return err
  33. }
  34. return nil
  35. }
  36. func InitializeServer(configFile string) error {
  37. config, err := pointjson.LoadConfig(configFile)
  38. if err != nil {
  39. log.Error("Failed to read config file (%s): %v", configFile, err)
  40. return err
  41. }
  42. vPoint, err := point.NewPoint(config)
  43. if err != nil {
  44. log.Error("Failed to create Point server: %v", err)
  45. return err
  46. }
  47. err = vPoint.Start()
  48. if err != nil {
  49. log.Error("Error starting Point server: %v", err)
  50. return err
  51. }
  52. runningServers = append(runningServers, vPoint)
  53. return nil
  54. }
  55. func CloseAllServers() {
  56. for _, server := range runningServers {
  57. server.Close()
  58. }
  59. runningServers = make([]*point.Point, 0, 10)
  60. }