Skip to content

Commit

Permalink
Populate RemoteIpAddress and RemotePort on HttpContext.Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus-L committed Mar 28, 2017
1 parent aa80350 commit 64b2f8b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
Expand Down Expand Up @@ -214,13 +215,14 @@ protected string ErrorReport(Exception e)
}

/// <summary>
/// Convert the JSON document received from API Gateway in the ASP.NET Core IHttpRequestFeature object.
/// IHttpRequestFeature is then passed into IHttpApplication to create the ASP.NET Core request objects.
/// Convert the JSON document received from API Gateway into the InvokeFeatures object.
/// InvokeFeatures is then passed into IHttpApplication to create the ASP.NET Core request objects.
/// </summary>
/// <param name="requestFeatures"></param>
/// <param name="features"></param>
/// <param name="apiGatewayRequest"></param>
protected void MarshallRequest(IHttpRequestFeature requestFeatures, APIGatewayProxyRequest apiGatewayRequest)
protected void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest)
{
var requestFeatures = (IHttpRequestFeature)features;
requestFeatures.Scheme = "https";
requestFeatures.Path = apiGatewayRequest.Path;
requestFeatures.Method = apiGatewayRequest.HttpMethod;
Expand Down Expand Up @@ -273,6 +275,13 @@ protected void MarshallRequest(IHttpRequestFeature requestFeatures, APIGatewayPr
}
requestFeatures.Body = new MemoryStream(binaryBody);
}
// set up connection features
var connectionFeatures = (IHttpConnectionFeature)features;
connectionFeatures.RemoteIpAddress = IPAddress.Parse(apiGatewayRequest.RequestContext.Identity.SourceIp);
if (apiGatewayRequest.Headers?.ContainsKey("X-Forwarded-Port") == true)
{
connectionFeatures.RemotePort = int.Parse(apiGatewayRequest.Headers["X-Forwarded-Port"]);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;
Expand All @@ -14,18 +15,19 @@ namespace Amazon.Lambda.AspNetCoreServer.Internal
{
public class InvokeFeatures : IFeatureCollection,
IHttpRequestFeature,
IHttpResponseFeature
IHttpResponseFeature,
IHttpConnectionFeature
/*
,
IHttpUpgradeFeature,
IHttpConnectionFeature,
IHttpRequestLifetimeFeature*/
{

public InvokeFeatures()
{
_features[typeof(IHttpRequestFeature)] = this;
_features[typeof(IHttpResponseFeature)] = this;
_features[typeof(IHttpConnectionFeature)] = this;
}

#region IFeatureCollection
Expand Down Expand Up @@ -146,5 +148,18 @@ void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
}
#endregion

#region IHttpConnectionFeature

string IHttpConnectionFeature.ConnectionId { get; set; }

IPAddress IHttpConnectionFeature.RemoteIpAddress { get; set; }

IPAddress IHttpConnectionFeature.LocalIpAddress { get; set; }

int IHttpConnectionFeature.RemotePort { get; set; }

int IHttpConnectionFeature.LocalPort { get; set; }

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
Expand Down

0 comments on commit 64b2f8b

Please sign in to comment.