2016-12-16 15:39:00 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2017-04-08 19:43:25 -04:00
|
|
|
"errors"
|
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"
|
2017-01-13 18:27:45 -05:00
|
|
|
"net"
|
2017-03-31 11:12:33 -04:00
|
|
|
"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"
|
2017-03-31 11:12:33 -04:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-16 08:18:13 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-16 15:39:00 -05:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2017-03-31 11:12:33 -04:00
|
|
|
"v2ray.com/core/common/retry"
|
2016-12-16 15:39:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func pickPort() v2net.Port {
|
2017-01-16 08:18:13 -05:00
|
|
|
listener, err := net.Listen("tcp4", ":0")
|
|
|
|
common.Must(err)
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
addr := listener.Addr().(*net.TCPAddr)
|
|
|
|
return v2net.Port(addr.Port)
|
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
|
|
|
}
|
|
|
|
|
2016-12-16 15:39:00 -05:00
|
|
|
func InitializeServerConfig(config *core.Config) error {
|
|
|
|
err := BuildV2Ray()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configBytes, err := proto.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
proc := RunV2RayProtobuf(configBytes)
|
|
|
|
|
|
|
|
err = proc.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
runningServers = append(runningServers, proc)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-31 11:12:33 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
runningServers = make([]*exec.Cmd, 0, 10)
|
|
|
|
testBinaryPath string
|
|
|
|
testBinaryPathGen sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func genTestBinaryPath() {
|
|
|
|
testBinaryPathGen.Do(func() {
|
|
|
|
var tempDir string
|
|
|
|
err := retry.Timed(5, 100).On(func() error {
|
|
|
|
dir, err := ioutil.TempDir("", "v2ray")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tempDir = dir
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func CloseAllServers() {
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("Closing all servers."))
|
2017-03-31 11:12:33 -04:00
|
|
|
for _, server := range runningServers {
|
|
|
|
server.Process.Signal(os.Interrupt)
|
|
|
|
server.Process.Wait()
|
|
|
|
}
|
|
|
|
runningServers = make([]*exec.Cmd, 0, 10)
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("All server closed."))
|
2017-03-31 11:12:33 -04:00
|
|
|
}
|