-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
61 lines (49 loc) · 1.03 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
package scenarios
import (
"io"
"net"
"time"
"github.com/golang/protobuf/proto"
"v2ray.com/core"
"v2ray.com/core/common"
v2net "v2ray.com/core/common/net"
)
func pickPort() v2net.Port {
listener, err := net.Listen("tcp4", ":0")
common.Must(err)
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return v2net.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, 2048)
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
n, _ := io.ReadFull(conn, b[:length])
return b[:n]
}
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
}