Пример #1
0
        /// <summary>
        ///     HTTP代理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <Status> HttpProxy(ProxyContext context)
        {
            if (context.Header == null)
            {
                return(Status.Exiting);
            }
            if (context.CurrentHostAddress == null || !Equals(context.Header.Host, context.CurrentHostAddress))
            {
                return(Status.HostDeciding);
            }

            if (context.Host == null)
            {
                await Send404(context);

                return(Status.Exiting);
            }

            //异步转发主机收到的数据到客户端
            Forward(context.Host, context.Client, context).GetAwaiter();


            var buffer = new byte[context.Configuration.BufferSize];
            int bytesRead;

            //转发客户端的数据到主机
            do
            {
                context.Header ??= await ProxyHttpHeaders.Parse(context.Client); //刷新请求头

                if (context.Header == null)
                {
                    return(Status.Exiting);
                }
                //主机更换,需要重新连接
                if (context.CurrentHostAddress == null || !Equals(context.Header.Host, context.CurrentHostAddress))
                {
                    return(Status.HostDeciding);
                }

                var header = (context.Redirection == null
                        ? context.Header.HeadersRaw
                        : context.Header.HeadersRaw.Replace(context.Redirection?.Item1 ?? "!",
                                                            context.Redirection?.Item2))
                             .ToAsciiBytes();

                //转发请求头
                await context.Host?.WriteBytesAsync
                    (header, context.Token) !;

                //转发请求体 && 其它请求头等等
                bytesRead = header.Length;
                if (context.Header.ContentLength > 0)
                {
                    var cl = context.Header.ContentLength;
                    do
                    {
                        bytesRead = await context.Client.ReadAsync(buffer, 0,
                                                                   (int)Math.Min(cl, context.Configuration.BufferSize), context.Token);

                        await context.Host.WriteAsync(buffer, 0, bytesRead, context.Token);

                        cl -= bytesRead;
                    } while (bytesRead > 0 && cl > 0);
                }

                context.Header = null;
            } while (bytesRead > 0);

            return(Status.Exiting);
        }
Пример #2
0
        /// <summary>
        ///     获取请求头部
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task <Status> GetHeaders(ProxyContext context)
        {
            context.Header = await ProxyHttpHeaders.Parse(context.Client);

            return(context.Header != null ? Status.HeaderGot : Status.Exiting);
        }