server_env.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package scenarios
  2. import (
  3. "os"
  4. "path/filepath"
  5. _ "github.com/v2ray/v2ray-core/app/router/config/json"
  6. _ "github.com/v2ray/v2ray-core/app/router/rules"
  7. _ "github.com/v2ray/v2ray-core/app/router/rules/config/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/config/json"
  14. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  15. _ "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json"
  16. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  17. _ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
  18. _ "github.com/v2ray/v2ray-core/proxy/socks"
  19. _ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  20. _ "github.com/v2ray/v2ray-core/proxy/vmess"
  21. _ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
  22. )
  23. var (
  24. serverup = make(map[string]bool)
  25. )
  26. func TestFile(filename string) string {
  27. return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
  28. }
  29. func InitializeServerSetOnce(testcase string) error {
  30. if up, found := serverup[testcase]; found && up {
  31. return nil
  32. }
  33. err := InitializeServer(TestFile(testcase + "_server.json"))
  34. if err != nil {
  35. return err
  36. }
  37. err = InitializeServer(TestFile(testcase + "_client.json"))
  38. if err != nil {
  39. return err
  40. }
  41. serverup[testcase] = true
  42. return nil
  43. }
  44. func InitializeServer(configFile string) error {
  45. config, err := pointjson.LoadConfig(configFile)
  46. if err != nil {
  47. log.Error("Failed to read config file (%s): %v", configFile, err)
  48. return err
  49. }
  50. vPoint, err := point.NewPoint(config)
  51. if err != nil {
  52. log.Error("Failed to create Point server: %v", err)
  53. return err
  54. }
  55. err = vPoint.Start()
  56. if err != nil {
  57. log.Error("Error starting Point server: %v", err)
  58. return err
  59. }
  60. return nil
  61. }