From 267025fe66f6bb963c41ab5fec08450d743b497e Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 12 Jan 2017 12:59:57 +0100 Subject: [PATCH] proxy over kcp --- testing/scenarios/feature_test.go | 150 ++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/testing/scenarios/feature_test.go b/testing/scenarios/feature_test.go index 18a5b817..f605dcf0 100644 --- a/testing/scenarios/feature_test.go +++ b/testing/scenarios/feature_test.go @@ -231,6 +231,156 @@ func TestProxy(t *testing.T) { CloseAllServers() } +func TestProxyOverKCP(t *testing.T) { + assert := assert.On(t) + + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + assert.Error(err).IsNil() + defer tcpServer.Close() + + serverUserID := protocol.NewID(uuid.New()) + serverPort := pickPort() + serverConfig := &core.Config{ + Inbound: []*core.InboundConnectionConfig{ + { + PortRange: v2net.SinglePortRange(serverPort), + ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP), + Settings: serial.ToTypedMessage(&inbound.Config{ + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: serverUserID.String(), + }), + }, + }, + }), + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_MKCP, + }, + }, + }, + Outbound: []*core.OutboundConnectionConfig{ + { + Settings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + proxyUserID := protocol.NewID(uuid.New()) + proxyPort := pickPort() + proxyConfig := &core.Config{ + Inbound: []*core.InboundConnectionConfig{ + { + PortRange: v2net.SinglePortRange(proxyPort), + ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP), + Settings: serial.ToTypedMessage(&inbound.Config{ + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: proxyUserID.String(), + }), + }, + }, + }), + }, + }, + Outbound: []*core.OutboundConnectionConfig{ + { + Settings: serial.ToTypedMessage(&freedom.Config{}), + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_MKCP, + }, + }, + }, + } + + clientPort := pickPort() + clientConfig := &core.Config{ + Inbound: []*core.InboundConnectionConfig{ + { + PortRange: v2net.SinglePortRange(clientPort), + ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP), + Settings: serial.ToTypedMessage(&dokodemo.Config{ + Address: v2net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + NetworkList: &v2net.NetworkList{ + Network: []v2net.Network{v2net.Network_TCP}, + }, + }), + }, + }, + Outbound: []*core.OutboundConnectionConfig{ + { + Settings: serial.ToTypedMessage(&outbound.Config{ + Receiver: []*protocol.ServerEndpoint{ + { + Address: v2net.NewIPOrDomain(v2net.LocalHostIP), + Port: uint32(serverPort), + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: serverUserID.String(), + }), + }, + }, + }, + }, + }), + ProxySettings: &internet.ProxyConfig{ + Tag: "proxy", + }, + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_MKCP, + }, + }, + { + Tag: "proxy", + Settings: serial.ToTypedMessage(&outbound.Config{ + Receiver: []*protocol.ServerEndpoint{ + { + Address: v2net.NewIPOrDomain(v2net.LocalHostIP), + Port: uint32(proxyPort), + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: proxyUserID.String(), + }), + }, + }, + }, + }, + }), + }, + }, + } + + assert.Error(InitializeServerConfig(serverConfig)).IsNil() + assert.Error(InitializeServerConfig(proxyConfig)).IsNil() + assert.Error(InitializeServerConfig(clientConfig)).IsNil() + + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(clientPort), + }) + assert.Error(err).IsNil() + + payload := "dokodemo request." + nBytes, err := conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + response := make([]byte, 1024) + nBytes, err = conn.Read(response) + assert.Error(err).IsNil() + assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert.Error(conn.Close()).IsNil() + + CloseAllServers() +} + func TestBlackhole(t *testing.T) { assert := assert.On(t)