2016-12-16 15:39:00 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2016-12-30 17:12:00 -05:00
|
|
|
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2016-12-16 15:39:00 -05:00
|
|
|
"v2ray.com/core"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-01-08 16:32:41 -05:00
|
|
|
port uint32 = 40000
|
2016-12-16 15:39:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func pickPort() v2net.Port {
|
|
|
|
return v2net.Port(atomic.AddUint32(&port, 1))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
b := make([]byte, 2048)
|
|
|
|
totalBytes := 0
|
|
|
|
deadline := time.Now().Add(timeout)
|
|
|
|
conn.SetReadDeadline(deadline)
|
|
|
|
for totalBytes < length {
|
|
|
|
if time.Now().After(deadline) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n, err := conn.Read(b[totalBytes:])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
totalBytes += n
|
|
|
|
}
|
|
|
|
return b[:totalBytes]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|