2016-12-16 20:39:00 +00:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2016-12-16 20:39:00 +00:00
|
|
|
"v2ray.com/core"
|
2018-01-10 11:22:37 +00:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-01-16 13:18:13 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-12-19 20:28:12 +00:00
|
|
|
"v2ray.com/core/common/log"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2017-03-31 15:12:33 +00:00
|
|
|
"v2ray.com/core/common/retry"
|
2018-01-10 11:22:37 +00:00
|
|
|
"v2ray.com/core/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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +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 {
|
|
|
|
return filepath.Join("v2ray.com", "core", "main")
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-07-27 10:41:48 +00:00
|
|
|
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
|
|
|
}
|
2018-01-10 11:22:37 +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
|
|
|
|
}
|