Пример #1
0
 public static void WriteLine(this Stream stream, string line)
 {
     var writer = new BinaryWriter(stream, Encoding.UTF8, true);
     writer.WriteLine(line);
 }
Пример #2
0
        private static bool CopyHttpStream(Stream fromStream, Stream toStream, string firstLine = "", string sslTunnelDomain="")
        {
            bool keepAlive = false; 
            var encoding = Encoding.UTF8;
            string method;
            string version;
            string url;

            using (var fromStreamReader = new BinaryReader(fromStream, encoding, true))
            using (var toStreamWriter = new BinaryWriter(toStream, encoding, true))
            {
                if (String.IsNullOrEmpty(firstLine))
                    firstLine = fromStream.ReadLine();

                if (!String.IsNullOrEmpty(sslTunnelDomain))
                {
                    //modify the path in the http request to include the domain
                    ParseHttpCommand(firstLine, out method, out url, out version);
                    //modify the forward address so it has complete URL 
                    firstLine = method + ' ' + "https://" + sslTunnelDomain + url + ' ' + version;
                    firstLine += "\r\n";
                    firstLine += "X-Forward-Secure: true";
                }
                LogMessage(string.Format(firstLine));
                toStream.WriteLine(firstLine);
                toStream.Flush();

                string line;
                int contentLength = 0;
                bool chunked = false;

                //copy the headers
                while (!String.IsNullOrEmpty(line = fromStreamReader.ReadLine()))
                {
                    if (line.StartsWith("Content-Length:", true, CultureInfo.CurrentCulture))
                        contentLength = int.Parse(line.Replace("Content-Length:", ""));
                    if (line.StartsWith("Transfer-Encoding: chunked", true, CultureInfo.CurrentCulture))
                        chunked = true;
                    if (line.StartsWith("Proxy-Connection: Keep-Alive", true, CultureInfo.CurrentCulture))
                        keepAlive = true;
                    toStreamWriter.WriteLine(line);
                }
                toStreamWriter.WriteLine();
                if (contentLength > 0)
                    toStreamWriter.Write(fromStreamReader.ReadBytes(contentLength));

                if(chunked)
                {
                    while (!String.IsNullOrEmpty(line = fromStreamReader.ReadLine()))
                    {
                        contentLength = int.Parse(line, System.Globalization.NumberStyles.HexNumber);
                        toStreamWriter.Write(fromStreamReader.ReadBytes(contentLength));
                        fromStreamReader.ReadLine();
                    }
                }
                toStreamWriter.Flush();
            }
            return keepAlive;
        }
Пример #3
0
 public virtual void Save(string basename)
 {
     using (var output = new BinaryWriter(File.Create(basename))) {
         this.InputTokenizer.Save(output);
     }
     using (var output = File.CreateText(basename + ".names")) {
         foreach (var filename in this.FileNames) {
             output.WriteLine (filename);
         }
     }
     using (var output = new BinaryWriter(File.Create(basename + ".seq"))) {
         GenericIO<Sequence>.Save (output, this.Seq);
     }
     using (var output = new BinaryWriter(File.Create(basename + ".voc"))) {
         output.Write ((int)this.Voc.Count);
         foreach (var w in this.Voc) {
             output.Write (w);
         }
     }
 }
Пример #4
0
        private static Stream GetSslTunnelStream(Stream stream, string version = "HTTP/1.1")
        {
            SslStream sslStream = null;
            //Browser wants to create a secure tunnel
            //read and ignore headers
            while (!String.IsNullOrEmpty(stream.ReadLine())) ;
            //tell the client that a tunnel has been established              
            LogMessage(string.Format("Doing CONNECT"));
            var connectStreamWriter = new BinaryWriter(stream);
            connectStreamWriter.WriteLine(version + " 200 Connection established");
            connectStreamWriter.WriteLine(String.Format("Timestamp: {0}", DateTime.Now.ToString()));
            connectStreamWriter.WriteLine("Proxy-agent: buskerproxy");
            connectStreamWriter.WriteLine();
            connectStreamWriter.Flush();

            //open a decrypting stream    
            sslStream = new SslStream(stream, false);
            try
            {
                sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true);
            }
            catch (Exception ex)
            {
                stream.Close();
                sslStream.Close();
                return null;
            }
            return sslStream;
        }