You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/testing/scenarios/common.go

133 lines
2.7 KiB

8 years ago
package scenarios
import (
"fmt"
8 years ago
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
8 years ago
"time"
"github.com/golang/protobuf/proto"
8 years ago
"v2ray.com/core"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
"v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/common/serial"
8 years ago
)
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 {
b := make([]byte, length)
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
8 years ago
n, err := io.ReadFull(conn, b[:length])
if err != nil {
fmt.Println("Unexpected error from readFrom:", err)
}
8 years ago
return b[:n]
}
8 years ago
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) {
8 years ago
err := BuildV2Ray()
if err != nil {
8 years ago
return nil, err
8 years ago
}
config = withDefaultApps(config)
8 years ago
configBytes, err := proto.Marshal(config)
if err != nil {
8 years ago
return nil, err
8 years ago
}
proc := RunV2RayProtobuf(configBytes)
8 years ago
if err := proc.Start(); err != nil {
8 years ago
return nil, err
8 years ago
}
8 years ago
return proc, nil
8 years ago
}
var (
testBinaryPath string
testBinaryPathGen sync.Once
)
func genTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
8 years ago
common.Must(retry.Timed(5, 100).On(func() error {
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
tempDir = dir
return nil
8 years ago
}))
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")
}
8 years ago
func CloseAllServers(servers []*exec.Cmd) {
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "Closing all servers.",
})
8 years ago
for _, server := range servers {
server.Process.Signal(os.Interrupt)
}
8 years ago
for _, server := range servers {
server.Process.Wait()
}
log.Record(&log.GeneralMessage{
Severity: log.Severity_Info,
Content: "All server closed.",
})
}
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
}