forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 29
/
common.go
137 lines (117 loc) · 2.61 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package scenarios
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/golang/protobuf/proto"
"v2ray.com/core"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
)
func pickPort() net.Port {
listener, err := net.Listen("tcp4", ":0")
common.Must(err)
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return net.Port(addr.Port)
}
func pickUDPPort() net.Port {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.LocalHostIP.IP(),
Port: 0,
})
common.Must(err)
defer conn.Close()
addr := conn.LocalAddr().(*net.UDPAddr)
return net.Port(addr.Port)
}
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)
n, err := io.ReadFull(conn, b[:length])
if err != nil {
fmt.Println("Unexpected error from readFrom:", err)
}
return b[:n]
}
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) {
err := BuildV2Ray()
if err != nil {
return nil, err
}
configBytes, err := proto.Marshal(config)
if err != nil {
return nil, err
}
proc := RunV2RayProtobuf(configBytes)
if err := proc.Start(); err != nil {
return nil, err
}
return proc, nil
}
var (
testBinaryPath string
testBinaryPathGen sync.Once
)
func genTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
common.Must(retry.Timed(5, 100).On(func() error {
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
tempDir = dir
return nil
}))
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(servers []*exec.Cmd) {
log.Trace(errors.New("Closing all servers."))
for _, server := range servers {
server.Process.Signal(os.Interrupt)
}
for _, server := range servers {
server.Process.Wait()
}
log.Trace(errors.New("All server closed."))
}