예제 #1
0
        public void Should_socks4_have_a_ctor_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("127.0.0.1", proxyPort, "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();
        }
예제 #2
0
        static void MainClient()
        {
            var client     = new Socks4ClientStream("127.0.0.1", 1080, "www.google.com", 80);
            var readBuf    = new byte[16 * 1024];
            int readBufPos = 0;

            client.BeginRead(readBuf, 0, readBuf.Length, Read, null);
            // bool found = false;
            void Read(IAsyncResult ar)
            {
                var c = client.EndRead(ar);

                Profiling.Stamp(TraceCategory.Test, "Test Read Done = " + c);
                if (c == 0)
                {
                    Console.WriteLine("Disconnected");
                    return;
                }
                var line = Encoding.UTF8.GetString(readBuf, readBufPos, c);

                Console.Write(line);
                // readBufPos += c;
                // var line = Encoding.UTF8.GetString(readBuf, 0, c);
                // Console.WriteLine(">>> " + line);
                // Profiling.Stamp(TraceCategory.Test, "Test Read...");
                client.BeginRead(readBuf, readBufPos, readBuf.Length - readBufPos, Read, null);
            }

            string q = null;

            while (q != "q")
            {
                var request = Encoding.ASCII.GetBytes($"GET /ncr HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n");
                client.Write(request, 0, request.Length);

                q = Console.ReadLine();
            }
        }