server_env.go 1.9 KB

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