예제 #1
0
        public ProxyMiddleware(RequestDelegate next, IOptions <ProxyOptions> options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.Value.Scheme == null)
            {
                throw new ArgumentException("Options parameter must specify scheme.", nameof(options));
            }

            if (!options.Value.Host.HasValue)
            {
                throw new ArgumentException("Options parameter must specify host.", nameof(options));
            }

            _next    = next;
            _options = options.Value;
        }
예제 #2
0
파일: Startup.cs 프로젝트: saithis/Proxy
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddProxy(options =>
     {
         options.GetProxyOptions = request => Task.FromResult(ProxyOptions.FromUri(new Uri("https://example.com")));
         options.PrepareRequest  = (originalRequest, message) =>
         {
             message.Headers.Add("X-Forwarded-Host", originalRequest.Host.Host);
             return(Task.FromResult(0));
         };
     });
 }
예제 #3
0
        /// <summary>
        /// Runs proxy forwarding requests to the server specified by options.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="options"></param>
        public static void RunProxy(this IApplicationBuilder app, ProxyOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            app.UseMiddleware <ProxyMiddleware>(Options.Create(options));
        }
예제 #4
0
        /// <summary>
        /// Creates <see cref="ProxyOptions"/> from a <see cref="Uri"/>
        /// </summary>
        /// <param name="baseUri">The <see cref="Uri"/></param>
        /// <returns>The <see cref="ProxyOptions"/></returns>
        public static ProxyOptions FromUri(Uri baseUri)
        {
            if (baseUri == null)
            {
                throw new ArgumentNullException(nameof(baseUri));
            }

            var options = new ProxyOptions
            {
                Scheme      = baseUri.Scheme,
                Host        = new HostString(baseUri.Authority),
                PathBase    = baseUri.AbsolutePath,
                AppendQuery = new QueryString(baseUri.Query)
            };

            return(options);
        }
예제 #5
0
        public ProxyMiddleware(RequestDelegate next, IOptions <ProxyOptions> options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _next    = next;
            _options = options.Value;

            if (string.IsNullOrEmpty(_options.Host))
            {
                throw new ArgumentException("Options parameter must specify host.", nameof(options));
            }

            // Setting default Port and Scheme if not specified
            if (string.IsNullOrEmpty(_options.Port))
            {
                if (string.Equals(_options.Scheme, "https", StringComparison.OrdinalIgnoreCase))
                {
                    _options.Port = "443";
                }
                else
                {
                    _options.Port = "80";
                }
            }

            if (string.IsNullOrEmpty(_options.Scheme))
            {
                _options.Scheme = "http";
            }

            _httpClient = new HttpClient(_options.BackChannelMessageHandler ?? new HttpClientHandler());
        }
예제 #6
0
        /// <summary>
        /// Runs proxy forwarding requests to the server specified by base uri.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="baseUri">Destination base uri</param>
        public static void RunProxy(this IApplicationBuilder app, Uri baseUri)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (baseUri == null)
            {
                throw new ArgumentNullException(nameof(baseUri));
            }

            var options = new ProxyOptions
            {
                Scheme      = baseUri.Scheme,
                Host        = new Http.HostString(baseUri.Authority),
                PathBase    = baseUri.AbsolutePath,
                AppendQuery = new Http.QueryString(baseUri.Query)
            };

            app.UseMiddleware <ProxyMiddleware>(Options.Create(options));
        }