1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00
v2fly/testing/scenarios/common.go

209 lines
4.6 KiB
Go
Raw Normal View History

2016-12-16 20:39:00 +00:00
package scenarios
import (
2020-06-08 08:16:32 +00:00
"bytes"
2019-01-07 22:27:59 +00:00
"crypto/rand"
2017-03-31 15:12:33 +00:00
"fmt"
2017-01-29 08:02:19 +00:00
"io"
2017-03-31 15:12:33 +00:00
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
"sync"
2018-07-27 10:31:22 +00:00
"syscall"
2016-12-16 20:39:00 +00:00
"time"
2016-12-30 22:12:00 +00:00
"google.golang.org/protobuf/proto"
2021-02-16 20:31:50 +00:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/dispatcher"
"github.com/v2fly/v2ray-core/v4/app/proxyman"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/errors"
"github.com/v2fly/v2ray-core/v4/common/log"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/retry"
"github.com/v2fly/v2ray-core/v4/common/serial"
2016-12-16 20:39:00 +00:00
)
2016-12-30 22:12:00 +00: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 14:36:55 +00:00
b := make([]byte, length)
2016-12-30 22:12:00 +00:00
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
2017-04-03 22:17:29 +00:00
n, err := io.ReadFull(conn, b[:length])
if err != nil {
fmt.Println("Unexpected error from readFrom:", err)
}
2017-01-29 08:02:19 +00:00
return b[:n]
2016-12-30 22:12:00 +00:00
}
2019-01-11 16:17:59 +00: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 22:39:30 +00: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 20:39:00 +00:00
err := BuildV2Ray()
if err != nil {
2017-05-17 22:39:30 +00:00
return nil, err
2016-12-16 20:39:00 +00:00
}
config = withDefaultApps(config)
2016-12-16 20:39:00 +00:00
configBytes, err := proto.Marshal(config)
if err != nil {
2017-05-17 22:39:30 +00:00
return nil, err
2016-12-16 20:39:00 +00:00
}
proc := RunV2RayProtobuf(configBytes)
2017-04-21 11:26:54 +00:00
if err := proc.Start(); err != nil {
2017-05-17 22:39:30 +00:00
return nil, err
2016-12-16 20:39:00 +00:00
}
2017-05-17 22:39:30 +00:00
return proc, nil
2016-12-16 20:39:00 +00:00
}
2017-03-31 15:12:33 +00:00
var (
testBinaryPath string
testBinaryPathGen sync.Once
)
func genTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
2017-04-28 12:48:23 +00:00
common.Must(retry.Timed(5, 100).On(func() error {
2017-03-31 15:12:33 +00:00
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
tempDir = dir
return nil
2017-04-28 12:48:23 +00:00
}))
2017-03-31 15:12:33 +00: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 {
2021-02-16 20:31:50 +00:00
return filepath.Join("github.com", "v2fly", "v2ray-core", "v4", "main")
2017-03-31 15:12:33 +00:00
}
2017-05-17 22:39:30 +00:00
func CloseAllServers(servers []*exec.Cmd) {
2017-12-19 20:28:12 +00:00
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "Closing all servers.",
})
2017-05-17 22:39:30 +00:00
for _, server := range servers {
if runtime.GOOS == "windows" {
server.Process.Kill()
} else {
server.Process.Signal(syscall.SIGTERM)
}
2017-04-16 18:55:48 +00:00
}
2017-05-17 22:39:30 +00:00
for _, server := range servers {
2017-03-31 15:12:33 +00:00
server.Process.Wait()
}
2017-12-19 20:28:12 +00:00
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "All server closed.",
})
2017-03-31 15:12:33 +00: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 22:27:59 +00: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 18:46:53 +00:00
return testTCPConn2(conn, payloadSize, timeout)()
}
}
func testUDPConn(port net.Port, payloadSize int, timeout time.Duration) func() error { // nolint: unparam
2019-02-03 18:46:53 +00: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 22:27:59 +00: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 16:17:59 +00:00
response, err := readFrom2(conn, timeout, payloadSize)
if err != nil {
return err
}
2020-06-08 08:16:32 +00:00
_ = response
if r := bytes.Compare(response, xor(payload)); r != 0 {
2019-01-07 22:27:59 +00:00
return errors.New(r)
}
2020-06-08 08:16:32 +00:00
2019-01-07 22:27:59 +00:00
return nil
}
}