1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/testing/scenarios/common.go

133 lines
2.7 KiB
Go
Raw Normal View History

2016-12-16 15:39:00 -05:00
package scenarios
import (
2017-03-31 11:12:33 -04:00
"fmt"
2017-01-29 03:02:19 -05:00
"io"
2017-03-31 11:12:33 -04:00
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
2016-12-16 15:39:00 -05:00
"time"
2016-12-30 17:12:00 -05:00
"github.com/golang/protobuf/proto"
2016-12-16 15:39:00 -05:00
"v2ray.com/core"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
2017-01-16 08:18:13 -05:00
"v2ray.com/core/common"
2017-12-19 15:28:12 -05:00
"v2ray.com/core/common/log"
"v2ray.com/core/common/net"
2017-03-31 11:12:33 -04:00
"v2ray.com/core/common/retry"
"v2ray.com/core/common/serial"
2016-12-16 15:39:00 -05:00
)
2016-12-30 17:12:00 -05:00
func xor(b []byte) []byte {
r := make([]byte, len(b))
for i, v := range b {
r[i] = v ^ 'c'
}
return r
}
func readFrom(conn net.Conn, timeout time.Duration, length int) []byte {
2017-02-06 09:36:55 -05:00
b := make([]byte, length)
2016-12-30 17:12:00 -05:00
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
2017-04-03 18:17:29 -04:00
n, err := io.ReadFull(conn, b[:length])
if err != nil {
fmt.Println("Unexpected error from readFrom:", err)
}
2017-01-29 03:02:19 -05:00
return b[:n]
2016-12-30 17:12:00 -05:00
}
2017-05-17 18:39:30 -04:00
func InitializeServerConfigs(configs ...*core.Config) ([]*exec.Cmd, error) {
servers := make([]*exec.Cmd, 0, 10)
for _, config := range configs {
server, err := InitializeServerConfig(config)
if err != nil {
CloseAllServers(servers)
return nil, err
}
servers = append(servers, server)
}
time.Sleep(time.Second * 2)
return servers, nil
}
func InitializeServerConfig(config *core.Config) (*exec.Cmd, error) {
2016-12-16 15:39:00 -05:00
err := BuildV2Ray()
if err != nil {
2017-05-17 18:39:30 -04:00
return nil, err
2016-12-16 15:39:00 -05:00
}
config = withDefaultApps(config)
2016-12-16 15:39:00 -05:00
configBytes, err := proto.Marshal(config)
if err != nil {
2017-05-17 18:39:30 -04:00
return nil, err
2016-12-16 15:39:00 -05:00
}
proc := RunV2RayProtobuf(configBytes)
2017-04-21 07:26:54 -04:00
if err := proc.Start(); err != nil {
2017-05-17 18:39:30 -04:00
return nil, err
2016-12-16 15:39:00 -05:00
}
2017-05-17 18:39:30 -04:00
return proc, nil
2016-12-16 15:39:00 -05:00
}
2017-03-31 11:12:33 -04:00
var (
testBinaryPath string
testBinaryPathGen sync.Once
)
func genTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
2017-04-28 08:48:23 -04:00
common.Must(retry.Timed(5, 100).On(func() error {
2017-03-31 11:12:33 -04:00
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
tempDir = dir
return nil
2017-04-28 08:48:23 -04:00
}))
2017-03-31 11:12:33 -04:00
file := filepath.Join(tempDir, "v2ray.test")
if runtime.GOOS == "windows" {
file += ".exe"
}
testBinaryPath = file
fmt.Printf("Generated binary path: %s\n", file)
})
}
func GetSourcePath() string {
return filepath.Join("v2ray.com", "core", "main")
}
2017-05-17 18:39:30 -04:00
func CloseAllServers(servers []*exec.Cmd) {
2017-12-19 15:28:12 -05:00
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "Closing all servers.",
})
2017-05-17 18:39:30 -04:00
for _, server := range servers {
2017-03-31 11:12:33 -04:00
server.Process.Signal(os.Interrupt)
2017-04-16 14:55:48 -04:00
}
2017-05-17 18:39:30 -04:00
for _, server := range servers {
2017-03-31 11:12:33 -04:00
server.Process.Wait()
}
2017-12-19 15:28:12 -05:00
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "All server closed.",
})
2017-03-31 11:12:33 -04:00
}
func withDefaultApps(config *core.Config) *core.Config {
config.App = append(config.App, serial.ToTypedMessage(&dispatcher.Config{}))
config.App = append(config.App, serial.ToTypedMessage(&proxyman.InboundConfig{}))
config.App = append(config.App, serial.ToTypedMessage(&proxyman.OutboundConfig{}))
return config
}