Exemplo n.º 1
0
 public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                      ProxyConnectOptions options, ref BufStruct buf)
 {
     if ((options & ProxyConnectOptions.ContentIsRawHttp) == 0)
     {
         throw new NotImplementedException();
     }
 }
Exemplo n.º 2
0
        public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
            ProxyConnectOptions options, ref BufStruct buf)
        {
            BufStruct forwardBufStruct = default(BufStruct);
            host.Connect(socket, DnsPriority.IPv4ThenIPv6, ProxyConnectOptions.None, ref forwardBufStruct);
            if (forwardBufStruct.contentLength > 0)
            {
                throw new NotImplementedException();
            }

            if (endPoint.stringIsAnIP)
            {
                ProxyConnectTcpUsingIP(socket, endPoint.ipEndPoint.Address, endPoint.port);
            }
            else
            {
                ProxyConnectTcpUsingHost(socket, endPoint.ipOrHost, endPoint.port);
            }
        }
Exemplo n.º 3
0
 public override Boolean ProxyConnectAsyncTcp(Socket socket, ref StringEndPoint endPoint,
                                              ProxyConnectOptions options, ref BufStruct buf)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 4
0
        public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                             ProxyConnectOptions options, ref BufStruct buf)
        {
            throw new NotImplementedException();

            /*
             * //
             * // Check if the proxy end point string is valid
             * //
             * socket.Send(Encoding.UTF8.GetBytes(String.Format(
             *  "CONNECT {0} HTTP/1.1\r\nHost: {0}:{1}\r\n\r\n",
             *  endpoint.unparsedIPOrHost, endpoint.port)));
             *
             * NetworkStream stream = new NetworkStream(socket);
             *
             * //
             * // Process first line
             * //
             * for (int i = 0; i < 9; i++)
             * {
             *  Int32 nextByte = stream.ReadByte();
             *  if ((nextByte & 0xFF) != nextByte) throw new SocketException();
             * }
             *
             * //
             * // Get response code
             * //
             * Char[] responseCodeChars = new Char[3];
             * responseCodeChars[0] = (Char)stream.ReadByte();
             * responseCodeChars[1] = (Char)stream.ReadByte();
             * responseCodeChars[2] = (Char)stream.ReadByte();
             * String responseCodeString = new String(responseCodeChars);
             *
             * Int32 responseCode;
             * if (!Int32.TryParse(responseCodeString, out responseCode))
             *  throw new InvalidOperationException(String.Format("First line of HTTP Connect response was not formatted correctly (Expected response code but got '{0}')", responseCodeString));
             *
             * if (responseCode != 200) throw new InvalidOperationException(String.Format("Expected response code 200 but got {0}", responseCode));
             *
             * //
             * // Read until end of response
             * //
             * Int32 lineLength = 12;
             *
             * while (true)
             * {
             *  Int32 nextByte = stream.ReadByte();
             *  //Console.WriteLine("[HttpsProxyDebug] Got Char '{0}'", (Char)nextByte);
             *  if ((nextByte & 0xFF) != nextByte) throw new SocketException();
             *
             *  if (nextByte != '\r')
             *  {
             *      lineLength++;
             *  }
             *  else
             *  {
             *      nextByte = stream.ReadByte();
             *      if ((nextByte & 0xFF) != nextByte) throw new SocketException();
             *      if (nextByte != '\n') throw new InvalidOperationException(String.Format(
             *           "Received '\\r' and expected '\\n' next but got (Char)'{0}' (Int32)'{1}'",
             *           (Char)nextByte, nextByte));
             *
             *
             *      if (lineLength <= 0) break;
             *
             *      lineLength = 0;
             *  }
             * }
             */
        }
Exemplo n.º 5
0
 public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                      ProxyConnectOptions options, ref BufStruct buf)
 {
     endPoint.ForceIPResolution(DnsPriority.IPv4ThenIPv6);
     socket.Connect(endPoint.ipEndPoint);
 }
Exemplo n.º 6
0
 public abstract Boolean ProxyConnectAsyncTcp(Socket socket, ref StringEndPoint endPoint,
                                              ProxyConnectOptions options, ref BufStruct buf);
Exemplo n.º 7
0
        /*
         * public String ConnectorString(StringEndPoint targetEndPoint)
         * {
         *  return String.Format("{0}:{1}:{2}%{3}", Type,
         *      ipOrHost, endPoint.Port, targetEndPoint);
         * }
         * public override String ToString()
         * {
         *  return String.Format("{0}:{1}:{2}", Type, ipOrHost, endPoint.Port);
         * }
         */

        // Design Note: The ProxyConnect method could also connect the socket, however,
        //              in some cases the socket will already be connected, so, this api
        //              doesn't wrap connecting the socket.  Note that this means typically
        //              this call will always follow immediately after calling socket.Connect()
        /// <summary>
        /// Setup the proxy connection. The given socket must already be connected.
        /// The endpoint should have been retrieved from the proxy's CreateEndpoint method.
        ///
        /// Once the method is done, any leftover data from the socket will be in the given buffer
        /// </summary>
        /// <param name="socket">A connected tcp socket</param>
        /// <param name="ipEndPoint">The final destination, retreived from calling proxy.CreateEndpoint(...).</param>
        public abstract void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                             ProxyConnectOptions options, ref BufStruct buf);
Exemplo n.º 8
0
 public void Connect(Socket socket, PriorityQuery <IPAddress> dnsPriorityQuery, ProxyConnectOptions proxyOptions, ref BufStruct buf)
 {
     if (proxy == null)
     {
         targetEndPoint.ForceIPResolution(dnsPriorityQuery);
         socket.Connect(targetEndPoint.ipEndPoint);
     }
     else
     {
         proxy.ProxyConnectTcp(socket, ref targetEndPoint, proxyOptions, ref buf);
     }
 }
Exemplo n.º 9
0
        public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                             ProxyConnectOptions options, ref BufStruct forwardBufStruct)
        {
            host.Connect(socket, DnsPriority.IPv4ThenIPv6, ProxyConnectOptions.None, ref forwardBufStruct);
            if (forwardBufStruct.contentLength > 0)
            {
                throw new NotImplementedException();
            }

            if ((options & ProxyConnectOptions.ContentIsRawHttp) != 0)
            {
                // do nothing
            }
            else
            {
                // send an HTTP CONNECT method
                unsafe
                {
                    AsciiPartsBuilder builder = new AsciiPartsBuilder(CONNECT_REQUEST_PARTS,
                                                                      endPoint.ipOrHost, endPoint.ipOrHost, endPoint.port.ToString());
                    UInt32 contentLength = builder.PrecalculateLength();
                    Byte[] buffer        = new Byte[(int)contentLength];
                    builder.BuildInto(buffer);

                    /*
                     * Console.Write("Request\n--------------------------\n");
                     * Console.Write(Encoding.ASCII.GetString(buffer));
                     * Console.Write("\n--------------------------\n");
                     */

                    socket.SendFullSize(buffer);

                    ByteBuilder responseBuilder   = new ByteBuilder(200);
                    int         endOfHeadersIndex = 0;
                    while (true)
                    {
                        int lastReceived = socket.Receive(buffer);
                        if (lastReceived <= 0)
                        {
                            /*
                             * Console.Write("Response\n--------------------------\n");
                             * Console.Write(Encoding.ASCII.GetString(responseBuilder.bytes, 0, (int)responseBuilder.contentLength));
                             * Console.Write("\n--------------------------\n");
                             */
                            throw new InvalidOperationException(String.Format("socket Receive returned {0} (received {1} bytes total)",
                                                                              lastReceived, responseBuilder.contentLength));
                        }
                        responseBuilder.Append(buffer, 0, (uint)lastReceived);
                        bool foundEndOfHeaders = false;
                        for (; endOfHeadersIndex + 3 < responseBuilder.contentLength; endOfHeadersIndex++)
                        {
                            if (responseBuilder.bytes[endOfHeadersIndex] == '\r' &&
                                responseBuilder.bytes[endOfHeadersIndex + 1] == '\n' &&
                                responseBuilder.bytes[endOfHeadersIndex + 2] == '\r' &&
                                responseBuilder.bytes[endOfHeadersIndex + 3] == '\n')
                            {
                                endOfHeadersIndex += 4;
                                foundEndOfHeaders  = true;
                                break;
                            }
                        }
                        if (foundEndOfHeaders)
                        {
                            break;
                        }
                        // rewind 3 characters in case we got a partial double newline
                        if (endOfHeadersIndex >= 3)
                        {
                            endOfHeadersIndex -= 3;
                        }
                    }

                    if (!responseBuilder.bytes.Equals(0, HTTP_VERSION_RESPONSE, 0, (uint)HTTP_VERSION_RESPONSE.Length))
                    {
                        throw new InvalidOperationException(String.Format(
                                                                "invalid HTTP response header (starts with \"{0}\")",
                                                                Encoding.ASCII.GetString(responseBuilder.bytes, 0,
                                                                                         (HTTP_VERSION_RESPONSE.Length <= responseBuilder.contentLength) ? HTTP_VERSION_RESPONSE.Length : (int)responseBuilder.contentLength)));
                    }
                    if (HTTP_VERSION_RESPONSE.Length + 3 > responseBuilder.contentLength)
                    {
                        throw new InvalidOperationException("invalid HTTP response header (it's too short)");
                    }
                    ResponseCode responseCode;
                    responseCode.a = (char)responseBuilder.bytes[HTTP_VERSION_RESPONSE.Length + 0];
                    responseCode.b = (char)responseBuilder.bytes[HTTP_VERSION_RESPONSE.Length + 1];
                    responseCode.c = (char)responseBuilder.bytes[HTTP_VERSION_RESPONSE.Length + 2];
                    if (responseCode.a != '2' && responseCode.b != '0' && responseCode.c != '0')
                    {
                        throw new InvalidOperationException(String.Format("expected response code 200 but got {0}{1}{2}",
                                                                          responseCode.a, responseCode.b, responseCode.c));
                    }

                    // TODO: receive the entire response if there is a content length!!
                    Console.Write("Response\n--------------------------\n");
                    Console.Write(Encoding.ASCII.GetString(responseBuilder.bytes, 0, (int)responseBuilder.contentLength));
                    Console.Write("\n--------------------------\n");
                    throw new NotImplementedException();
                }
            }
        }
Exemplo n.º 10
0
 public override void ProxyConnectTcp(Socket socket, ref StringEndPoint endPoint,
                                      ProxyConnectOptions options, ref BufStruct buf)
 {
     host.Connect(socket, DnsPriority.IPv4ThenIPv6, options, ref buf);
 }