github.com/xmplusdev/xray-core@v1.8.10/core/functions_test.go (about) 1 package core_test 2 3 import ( 4 "context" 5 "crypto/rand" 6 "io" 7 "testing" 8 "time" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/xmplusdev/xray-core/app/dispatcher" 12 "github.com/xmplusdev/xray-core/app/proxyman" 13 "github.com/xmplusdev/xray-core/common" 14 "github.com/xmplusdev/xray-core/common/net" 15 "github.com/xmplusdev/xray-core/common/serial" 16 core "github.com/xmplusdev/xray-core/core" 17 "github.com/xmplusdev/xray-core/proxy/freedom" 18 "github.com/xmplusdev/xray-core/testing/servers/tcp" 19 "github.com/xmplusdev/xray-core/testing/servers/udp" 20 "google.golang.org/protobuf/proto" 21 ) 22 23 func xor(b []byte) []byte { 24 r := make([]byte, len(b)) 25 for i, v := range b { 26 r[i] = v ^ 'c' 27 } 28 return r 29 } 30 31 func xor2(b []byte) []byte { 32 r := make([]byte, len(b)) 33 for i, v := range b { 34 r[i] = v ^ 'd' 35 } 36 return r 37 } 38 39 func TestXrayDial(t *testing.T) { 40 tcpServer := tcp.Server{ 41 MsgProcessor: xor, 42 } 43 dest, err := tcpServer.Start() 44 common.Must(err) 45 defer tcpServer.Close() 46 47 config := &core.Config{ 48 App: []*serial.TypedMessage{ 49 serial.ToTypedMessage(&dispatcher.Config{}), 50 serial.ToTypedMessage(&proxyman.InboundConfig{}), 51 serial.ToTypedMessage(&proxyman.OutboundConfig{}), 52 }, 53 Outbound: []*core.OutboundHandlerConfig{ 54 { 55 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 56 }, 57 }, 58 } 59 60 cfgBytes, err := proto.Marshal(config) 61 common.Must(err) 62 63 server, err := core.StartInstance("protobuf", cfgBytes) 64 common.Must(err) 65 defer server.Close() 66 67 conn, err := core.Dial(context.Background(), server, dest) 68 common.Must(err) 69 defer conn.Close() 70 71 const size = 10240 * 1024 72 payload := make([]byte, size) 73 common.Must2(rand.Read(payload)) 74 75 if _, err := conn.Write(payload); err != nil { 76 t.Fatal(err) 77 } 78 79 receive := make([]byte, size) 80 if _, err := io.ReadFull(conn, receive); err != nil { 81 t.Fatal("failed to read all response: ", err) 82 } 83 84 if r := cmp.Diff(xor(receive), payload); r != "" { 85 t.Error(r) 86 } 87 } 88 89 func TestXrayDialUDPConn(t *testing.T) { 90 udpServer := udp.Server{ 91 MsgProcessor: xor, 92 } 93 dest, err := udpServer.Start() 94 common.Must(err) 95 defer udpServer.Close() 96 97 config := &core.Config{ 98 App: []*serial.TypedMessage{ 99 serial.ToTypedMessage(&dispatcher.Config{}), 100 serial.ToTypedMessage(&proxyman.InboundConfig{}), 101 serial.ToTypedMessage(&proxyman.OutboundConfig{}), 102 }, 103 Outbound: []*core.OutboundHandlerConfig{ 104 { 105 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 106 }, 107 }, 108 } 109 110 cfgBytes, err := proto.Marshal(config) 111 common.Must(err) 112 113 server, err := core.StartInstance("protobuf", cfgBytes) 114 common.Must(err) 115 defer server.Close() 116 117 conn, err := core.Dial(context.Background(), server, dest) 118 common.Must(err) 119 defer conn.Close() 120 121 const size = 1024 122 payload := make([]byte, size) 123 common.Must2(rand.Read(payload)) 124 125 for i := 0; i < 2; i++ { 126 if _, err := conn.Write(payload); err != nil { 127 t.Fatal(err) 128 } 129 } 130 131 time.Sleep(time.Millisecond * 500) 132 133 receive := make([]byte, size*2) 134 for i := 0; i < 2; i++ { 135 n, err := conn.Read(receive) 136 if err != nil { 137 t.Fatal("expect no error, but got ", err) 138 } 139 if n != size { 140 t.Fatal("expect read size ", size, " but got ", n) 141 } 142 143 if r := cmp.Diff(xor(receive[:n]), payload); r != "" { 144 t.Fatal(r) 145 } 146 } 147 } 148 149 func TestXrayDialUDP(t *testing.T) { 150 udpServer1 := udp.Server{ 151 MsgProcessor: xor, 152 } 153 dest1, err := udpServer1.Start() 154 common.Must(err) 155 defer udpServer1.Close() 156 157 udpServer2 := udp.Server{ 158 MsgProcessor: xor2, 159 } 160 dest2, err := udpServer2.Start() 161 common.Must(err) 162 defer udpServer2.Close() 163 164 config := &core.Config{ 165 App: []*serial.TypedMessage{ 166 serial.ToTypedMessage(&dispatcher.Config{}), 167 serial.ToTypedMessage(&proxyman.InboundConfig{}), 168 serial.ToTypedMessage(&proxyman.OutboundConfig{}), 169 }, 170 Outbound: []*core.OutboundHandlerConfig{ 171 { 172 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 173 }, 174 }, 175 } 176 177 cfgBytes, err := proto.Marshal(config) 178 common.Must(err) 179 180 server, err := core.StartInstance("protobuf", cfgBytes) 181 common.Must(err) 182 defer server.Close() 183 184 conn, err := core.DialUDP(context.Background(), server) 185 common.Must(err) 186 defer conn.Close() 187 188 const size = 1024 189 { 190 payload := make([]byte, size) 191 common.Must2(rand.Read(payload)) 192 193 if _, err := conn.WriteTo(payload, &net.UDPAddr{ 194 IP: dest1.Address.IP(), 195 Port: int(dest1.Port), 196 }); err != nil { 197 t.Fatal(err) 198 } 199 200 receive := make([]byte, size) 201 if _, _, err := conn.ReadFrom(receive); err != nil { 202 t.Fatal(err) 203 } 204 205 if r := cmp.Diff(xor(receive), payload); r != "" { 206 t.Error(r) 207 } 208 } 209 210 { 211 payload := make([]byte, size) 212 common.Must2(rand.Read(payload)) 213 214 if _, err := conn.WriteTo(payload, &net.UDPAddr{ 215 IP: dest2.Address.IP(), 216 Port: int(dest2.Port), 217 }); err != nil { 218 t.Fatal(err) 219 } 220 221 receive := make([]byte, size) 222 if _, _, err := conn.ReadFrom(receive); err != nil { 223 t.Fatal(err) 224 } 225 226 if r := cmp.Diff(xor2(receive), payload); r != "" { 227 t.Error(r) 228 } 229 } 230 }