예제 #1
0
func get(connid int, uri, serverAddr string, rawAddr []byte, cipher ss.Cipher, done chan []time.Duration) {
	reqDone := 0
	reqTime := make([]time.Duration, config.nreq)
	defer func() {
		done <- reqTime[:reqDone]
	}()
	tr := &http.Transport{
		Dial: func(_, _ string) (net.Conn, error) {
			return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy())
		},
	}

	buf := make([]byte, 8192)
	client := &http.Client{Transport: tr}
	for ; reqDone < config.nreq; reqDone++ {
		start := time.Now()
		if err := doOneRequest(client, uri, buf); err != nil {
			return
		}
		reqTime[reqDone] = time.Now().Sub(start)

		if (reqDone+1)%1000 == 0 {
			fmt.Printf("conn %d finished %d get requests\n", connid, reqDone+1)
		}
	}
}
예제 #2
0
// select one server to connect in round robin order
func createServerConn(rawaddr []byte, addr string) (remote *ss.Conn, err error) {
	n := len(servers.srvenc)
	if n == 1 {
		se := servers.srvenc[0]
		debug.Printf("connecting to %s via %s\n", addr, se.server)
		return ss.DialWithRawAddr(rawaddr, se.server, se.enctbl)
	}

	id := servers.idx
	servers.idx++ // it's ok for concurrent update
	for i := 0; i < n; i++ {
		se := servers.srvenc[(int(id)+i)%n]
		remote, err = ss.DialWithRawAddr(rawaddr, se.server, se.enctbl)
		if err == nil {
			debug.Printf("connected to %s via %s\n", addr, se.server)
			return
		} else {
			log.Println("error connecting to shadowsocks server:", err)
		}
	}
	return
}
예제 #3
0
func connectToServer(serverId int, rawaddr []byte, addr string) (remote *ss.Conn, err error) {
	se := servers.srvCipher[serverId]
	remote, err = ss.DialWithRawAddr(rawaddr, se.server, se.cipher.Copy())
	if err != nil {
		log.Println("error connecting to shadowsocks server:", err)
		const maxFailCnt = 30
		if servers.failCnt[serverId] < maxFailCnt {
			servers.failCnt[serverId]++
		}
		return nil, err
	}
	debug.Printf("connected to %s via %s\n", addr, se.server)
	servers.failCnt[serverId] = 0
	return
}
예제 #4
0
func TestShadowsocksTCP(t *testing.T) {
	v2testing.Current(t)

	tcpServer := &tcp.Server{
		Port: v2net.Port(50052),
		MsgProcessor: func(data []byte) []byte {
			buffer := make([]byte, 0, 2048)
			buffer = append(buffer, []byte("Processed: ")...)
			buffer = append(buffer, data...)
			return buffer
		},
	}
	_, err := tcpServer.Start()
	assert.Error(err).IsNil()
	defer tcpServer.Close()

	assert.Error(InitializeServerServer("test_6")).IsNil()

	cipher, err := ssclient.NewCipher("aes-256-cfb", "v2ray-password")
	assert.Error(err).IsNil()

	rawAddr := []byte{1, 127, 0, 0, 1, 0xc3, 0x84} // 127.0.0.1:50052
	conn, err := ssclient.DialWithRawAddr(rawAddr, "127.0.0.1:50051", cipher)
	assert.Error(err).IsNil()

	payload := "shadowsocks request."
	nBytes, err := conn.Write([]byte(payload))
	assert.Error(err).IsNil()
	assert.Int(nBytes).Equals(len(payload))

	conn.Conn.(*net.TCPConn).CloseWrite()

	response := make([]byte, 1024)
	nBytes, err = conn.Read(response)
	assert.Error(err).IsNil()
	assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
	conn.Close()

	CloseAllServers()
}