Example #1
0
        public override async Task <ConnectResult> ProtectedConnect(ConnectArgument arg)
        {
            var dest       = arg.Dest;
            var baseResult = await ConnectHelper.Connect(this, server.WithDefaultPort(80), connect_timeout);

            if (!baseResult.Ok)
            {
                return(baseResult);
            }
            try {
                var dataStream = baseResult.Stream;
                var asStream   = MyStream.ToStream(dataStream);
                var sw         = new StringWriter(new StringBuilder(1024));
                var destStr    = dest.ToString();
                HttpClient.WriteHttpRequestHeader(sw, "CONNECT", destStr, new Dictionary <string, string> {
                    ["Host"] = destStr
                });
                await dataStream.WriteAsync(NaiveUtils.GetUTF8Bytes(sw.ToString()));

                var responseStr = await NaiveUtils.ReadStringUntil(asStream, NaiveUtils.DoubleCRLFBytes);

                var sr       = new StringReader(responseStr);
                var response = HttpClient.ReadHttpResponseHeader(sr);
                if (response.StatusCode != "200")
                {
                    throw new Exception($"remote server response '{response.StatusCode} {response.ReasonPhrase}'");
                }
                return(CreateConnectResultWithStream(dataStream));
            } catch (Exception) {
                MyStream.CloseWithTimeout(baseResult.Stream);
                throw;
            }
        }
Example #2
0
 public static Stream ToStream(this IMyStream myStream)
 {
     return(MyStream.ToStream(myStream));
 }