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"
|
2021-09-29 04:25:23 -04:00
|
|
|
"os"
|
2017-03-31 11:12:33 -04:00
|
|
|
"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
|
|
|
|
2020-12-19 21:50:46 -05:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/dispatcher"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/proxyman"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/retry"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/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
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -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 {
|
2021-09-27 18:02:19 -04:00
|
|
|
dir, err := os.MkdirTemp("", "v2ray")
|
2017-03-31 11:12:33 -04:00
|
|
|
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 {
|
2022-01-02 10:16:23 -05:00
|
|
|
return filepath.Join("github.com", "v2fly", "v2ray-core", "v5", "main")
|
2017-03-31 11:12:33 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-07-27 06:41:48 -04:00
|
|
|
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
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
|
2021-09-23 23:59:00 -04:00
|
|
|
func CloseServer(server *exec.Cmd) {
|
|
|
|
log.Record(&log.GeneralMessage{
|
|
|
|
Severity: log.Severity_Info,
|
|
|
|
Content: "Closing server.",
|
|
|
|
})
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
server.Process.Kill()
|
|
|
|
} else {
|
|
|
|
server.Process.Signal(syscall.SIGTERM)
|
|
|
|
}
|
|
|
|
server.Process.Wait()
|
|
|
|
log.Record(&log.GeneralMessage{
|
|
|
|
Severity: log.Severity_Info,
|
|
|
|
Content: "Server closed.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05: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)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 16:05:01 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|