server_env.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. serverup = make(map[string]bool)
  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. if up, found := serverup[testcase]; found && up {
  33. return nil
  34. }
  35. err := InitializeServer(TestFile(testcase + "_server.json"))
  36. if err != nil {
  37. return err
  38. }
  39. err = InitializeServer(TestFile(testcase + "_client.json"))
  40. if err != nil {
  41. return err
  42. }
  43. serverup[testcase] = true
  44. return nil
  45. }
  46. func InitializeServer(configFile string) error {
  47. config, err := pointjson.LoadConfig(configFile)
  48. if err != nil {
  49. log.Error("Failed to read config file (%s): %v", configFile, err)
  50. return err
  51. }
  52. vPoint, err := point.NewPoint(config)
  53. if err != nil {
  54. log.Error("Failed to create Point server: %v", err)
  55. return err
  56. }
  57. err = vPoint.Start()
  58. if err != nil {
  59. log.Error("Error starting Point server: %v", err)
  60. return err
  61. }
  62. return nil
  63. }