This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathOverrideHeaderMiddleware.cs
107 lines (96 loc) · 4.1 KB
/
OverrideHeaderMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.HttpOverrides
{
public class OverrideHeaderMiddleware
{
private const string XForwardedForHeaderName = "X-Forwarded-For";
private const string XForwardedHostHeaderName = "X-Forwarded-Host";
private const string XForwardedProtoHeaderName = "X-Forwarded-Proto";
private const string XOriginalForName = "X-Original-For";
private const string XOriginalHostName = "X-Original-Host";
private const string XOriginalProtoName = "X-Original-Proto";
private readonly OverrideHeaderOptions _options;
private readonly RequestDelegate _next;
public OverrideHeaderMiddleware(RequestDelegate next, IOptions<OverrideHeaderOptions> options)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_options = options.Value;
_next = next;
}
public Task Invoke(HttpContext context)
{
UpdateRemoteIp(context);
UpdateHost(context);
UpdateScheme(context);
return _next(context);
}
private void UpdateRemoteIp(HttpContext context)
{
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedFor) != 0)
{
var xForwardedForHeaderValue = context.Request.Headers.GetCommaSeparatedValues(XForwardedForHeaderName);
if (xForwardedForHeaderValue != null && xForwardedForHeaderValue.Length > 0)
{
IPEndPoint endpoint;
if (IPEndPointParser.TryParse(xForwardedForHeaderValue[0], out endpoint))
{
var connection = context.Connection;
var remoteIP = connection.RemoteIpAddress;
if (remoteIP != null)
{
var remoteIPString = new IPEndPoint(remoteIP, connection.RemotePort).ToString();
context.Request.Headers[XOriginalForName] = remoteIPString;
}
connection.RemoteIpAddress = endpoint.Address;
connection.RemotePort = endpoint.Port;
}
}
}
}
private void UpdateHost(HttpContext context)
{
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedHost) != 0)
{
var xForwardHostHeaderValue = context.Request.Headers[XForwardedHostHeaderName];
if (!string.IsNullOrEmpty(xForwardHostHeaderValue))
{
var hostString = context.Request.Host.ToString();
if (!string.IsNullOrEmpty(hostString))
{
context.Request.Headers[XOriginalHostName] = hostString;
}
context.Request.Host = HostString.FromUriComponent(xForwardHostHeaderValue);
}
}
}
private void UpdateScheme(HttpContext context)
{
if ((_options.ForwardedOptions & ForwardedHeaders.XForwardedProto) != 0)
{
var xForwardProtoHeaderValue = context.Request.Headers[XForwardedProtoHeaderName];
if (!string.IsNullOrEmpty(xForwardProtoHeaderValue))
{
if (!string.IsNullOrEmpty(context.Request.Scheme))
{
context.Request.Headers[XOriginalProtoName] = context.Request.Scheme;
}
context.Request.Scheme = xForwardProtoHeaderValue;
}
}
}
}
}