common.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package scenarios
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "sync"
  11. "time"
  12. "github.com/golang/protobuf/proto"
  13. "v2ray.com/core"
  14. "v2ray.com/core/app/dispatcher"
  15. "v2ray.com/core/app/proxyman"
  16. "v2ray.com/core/common"
  17. "v2ray.com/core/common/log"
  18. "v2ray.com/core/common/net"
  19. "v2ray.com/core/common/retry"
  20. "v2ray.com/core/common/serial"
  21. )
  22. func xor(b []byte) []byte {
  23. r := make([]byte, len(b))
  24. for i, v := range b {
  25. r[i] = v ^ 'c'
  26. }
  27. return r
  28. }
  29. func readFrom(conn net.Conn, timeout time.Duration, length int) []byte {
  30. b := make([]byte, length)
  31. deadline := time.Now().Add(timeout)
  32. conn.SetReadDeadline(deadline)
  33. n, err := io.ReadFull(conn, b[:length])
  34. if err != nil {
  35. fmt.Println("Unexpected error from readFrom:", err)
  36. }
  37. return b[:n]
  38. }
  39. func InitializeServerConfigs(configs ...*core.Config) ([]*exec.Cmd, error) {
  40. servers := make([]*exec.Cmd, 0, 10)
  41. for _, config := range configs {
  42. server, err := InitializeServerConfig(config)
  43. if err != nil {
  44. CloseAllServers(servers)
  45. return nil, err
  46. }
  47. servers = append(servers, server)
  48. }
  49. time.Sleep(time.Second * 2)
  50. return servers, nil
  51. }
  52. func InitializeServerConfig(config *core.Config) (*exec.Cmd, error) {
  53. err := BuildV2Ray()
  54. if err != nil {
  55. return nil, err
  56. }
  57. config = withDefaultApps(config)
  58. configBytes, err := proto.Marshal(config)
  59. if err != nil {
  60. return nil, err
  61. }
  62. proc := RunV2RayProtobuf(configBytes)
  63. if err := proc.Start(); err != nil {
  64. return nil, err
  65. }
  66. return proc, nil
  67. }
  68. var (
  69. testBinaryPath string
  70. testBinaryPathGen sync.Once
  71. )
  72. func genTestBinaryPath() {
  73. testBinaryPathGen.Do(func() {
  74. var tempDir string
  75. common.Must(retry.Timed(5, 100).On(func() error {
  76. dir, err := ioutil.TempDir("", "v2ray")
  77. if err != nil {
  78. return err
  79. }
  80. tempDir = dir
  81. return nil
  82. }))
  83. file := filepath.Join(tempDir, "v2ray.test")
  84. if runtime.GOOS == "windows" {
  85. file += ".exe"
  86. }
  87. testBinaryPath = file
  88. fmt.Printf("Generated binary path: %s\n", file)
  89. })
  90. }
  91. func GetSourcePath() string {
  92. return filepath.Join("v2ray.com", "core", "main")
  93. }
  94. func CloseAllServers(servers []*exec.Cmd) {
  95. log.Record(&log.GeneralMessage{
  96. Severity: log.Severity_Info,
  97. Content: "Closing all servers.",
  98. })
  99. for _, server := range servers {
  100. server.Process.Signal(os.Interrupt)
  101. }
  102. for _, server := range servers {
  103. server.Process.Wait()
  104. }
  105. log.Record(&log.GeneralMessage{
  106. Severity: log.Severity_Info,
  107. Content: "All server closed.",
  108. })
  109. }
  110. func withDefaultApps(config *core.Config) *core.Config {
  111. config.App = append(config.App, serial.ToTypedMessage(&dispatcher.Config{}))
  112. config.App = append(config.App, serial.ToTypedMessage(&proxyman.InboundConfig{}))
  113. config.App = append(config.App, serial.ToTypedMessage(&proxyman.OutboundConfig{}))
  114. return config
  115. }