server_env.go 1.7 KB

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