server_env.go 1.6 KB

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