示例#1
0
        static void Main2()
        {
            /*
             * var cli = new TcpClient("httpbin.org", 80);
             * var stream = cli.GetStream2();
             */

            var step1 = new Socks4ClientStream();

            step1.Plug("127.0.0.1", 1080);
            step1.Route("127.0.0.1", 1081);

            var step2 = new Socks4ClientStream(step1, "127.0.0.1", 1082);
            var step3 = new Socks4ClientStream(step2, "httpbin.org", 80);

            var stream = step3;

            void read()
            {
                var buf = new byte[16 * 1024];
                var c   = stream.Read(buf, 0, buf.Length);

                if (c > 0)
                {
                    var str = Encoding.UTF8.GetString(buf, 0, c);
                    Console.WriteLine(str);
                    // Console.WriteLine("\r\n==========<< " + c);
                    Task.Run(delegate
                    {
                        read();
                    });
                }
            }

            Task.Run(delegate
            {
                read();
            });

            var buf2 = Encoding.ASCII.GetBytes("GET /\r\n\r\n");

            stream.Write(buf2, 0, buf2.Length);
            Console.ReadLine();
        }
示例#2
0
        public void Should_socks4_have_a_plug_with_proxy_and_host()
        {
            var server      = new DemoTcpServer();
            var proxyPort   = GetFreePort();
            var proxy       = new SocksServer("socks://0.0.0.0:" + proxyPort);
            var proxyClient = new Socks4ClientStream();

            proxyClient.Plug("127.0.0.1", proxyPort);
            proxyClient.Route("127.0.0.1", server.Port);

            var data = new byte[] { 1, 2, 3, 4 };

            proxyClient.Write(data);
            var buf = new byte[16 * 1024];
            var d   = proxyClient.Read(buf, 0, buf.Length);

            Assert.AreEqual(4, d, "Should read 4 bytes in a single packet");
            // demo server is XOR 37
            CollectionAssert.AreEqual(data.Select(x => (byte)(x ^ 37)).ToArray(), buf.Take(d).ToArray());

            server.Dispose();
            proxy.Dispose();
            proxyClient.Dispose();
        }