1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00
v2fly/testing/scenarios/common.go

208 lines
4.4 KiB
Go
Raw Normal View History

2016-12-16 15:39:00 -05:00
package scenarios
import (
2020-06-08 04:16:32 -04:00
"bytes"
2019-01-07 17:27:59 -05:00
"crypto/rand"
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/exec"
"path/filepath"
"runtime"
"sync"
2018-07-27 06:31:22 -04:00
"syscall"
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"
2019-01-07 17:27:59 -05:00
"v2ray.com/core/common/errors"
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
}
2019-01-11 11:17:59 -05:00
func readFrom2(conn net.Conn, timeout time.Duration, length int) ([]byte, error) {
b := make([]byte, length)
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
n, err := io.ReadFull(conn, b[:length])
if err != nil {
return nil, err
}
return b[:n], nil
}
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 {
if runtime.GOOS == "windows" {
server.Process.Kill()
} else {
server.Process.Signal(syscall.SIGTERM)
}
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
}
2019-01-07 17:27:59 -05:00
func testTCPConn(port net.Port, payloadSize int, timeout time.Duration) func() error {
return func() error {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
if err != nil {
return err
}
defer conn.Close()
2019-02-03 13:46:53 -05:00
return testTCPConn2(conn, payloadSize, timeout)()
}
}
func testUDPConn(port net.Port, payloadSize int, timeout time.Duration) func() error { // nolint: unparam
2019-02-03 13:46:53 -05:00
return func() error {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
if err != nil {
return err
}
defer conn.Close()
return testTCPConn2(conn, payloadSize, timeout)()
}
}
func testTCPConn2(conn net.Conn, payloadSize int, timeout time.Duration) func() error {
return func() error {
2019-01-07 17:27:59 -05:00
payload := make([]byte, payloadSize)
common.Must2(rand.Read(payload))
nBytes, err := conn.Write(payload)
if err != nil {
return err
}
if nBytes != len(payload) {
return errors.New("expect ", len(payload), " written, but actually ", nBytes)
}
2019-01-11 11:17:59 -05:00
response, err := readFrom2(conn, timeout, payloadSize)
if err != nil {
return err
}
2020-06-08 04:16:32 -04:00
_ = response
if r := bytes.Compare(response, xor(payload)); r != 0 {
2019-01-07 17:27:59 -05:00
return errors.New(r)
}
2020-06-08 04:16:32 -04:00
2019-01-07 17:27:59 -05:00
return nil
}
}