Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 6ec4233

Browse files
committed
Provide RemoteIPAddress in Frame
1 parent c1b21b8 commit 6ec4233

File tree

11 files changed

+356
-36
lines changed

11 files changed

+356
-36
lines changed

KestrelHttpServer.sln

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Server.Kes
3737
EndProject
3838
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Server.Kestrel.FunctionalTests", "test\Microsoft.AspNet.Server.Kestrel.FunctionalTests\Microsoft.AspNet.Server.Kestrel.FunctionalTests.xproj", "{9559A5F1-080C-4909-B6CF-7E4B3DC55748}"
3939
EndProject
40+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Hosting", "..\Hosting\src\Microsoft.AspNet.Hosting\Microsoft.AspNet.Hosting.xproj", "{3944F036-7E75-47E8-AA52-C4B89A64EC3A}"
41+
EndProject
42+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Hosting.Abstractions", "..\Hosting\src\Microsoft.AspNet.Hosting.Abstractions\Microsoft.AspNet.Hosting.Abstractions.xproj", "{BB780FBB-7842-4759-8DE7-96FA2E5571C1}"
43+
EndProject
44+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Hosting.Server.Abstractions", "..\Hosting\src\Microsoft.AspNet.Hosting.Server.Abstractions\Microsoft.AspNet.Hosting.Server.Abstractions.xproj", "{FDBBA081-5248-4FC0-9E08-B46BEF3FA438}"
45+
EndProject
46+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Http.Abstractions", "..\HttpAbstractions\src\Microsoft.AspNet.Http.Abstractions\Microsoft.AspNet.Http.Abstractions.xproj", "{22071333-15BA-4D16-A1D5-4D5B1A83FBDD}"
47+
EndProject
48+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Http", "..\HttpAbstractions\src\Microsoft.AspNet.Http\Microsoft.AspNet.Http.xproj", "{BCF0F967-8753-4438-BD07-AADCA9CE509A}"
49+
EndProject
50+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Http.Features", "..\HttpAbstractions\src\Microsoft.AspNet.Http.Features\Microsoft.AspNet.Http.Features.xproj", "{D9128247-8F97-48B8-A863-F1F21A029FCE}"
51+
EndProject
52+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.WebEncoders.Core", "..\HttpAbstractions\src\Microsoft.Extensions.WebEncoders.Core\Microsoft.Extensions.WebEncoders.Core.xproj", "{BE9112CB-D87D-4080-9CC3-24492D49CBE6}"
53+
EndProject
54+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Http.Extensions", "..\HttpAbstractions\src\Microsoft.AspNet.Http.Extensions\Microsoft.AspNet.Http.Extensions.xproj", "{CCC4363E-81E2-4058-94DD-00494E9E992A}"
55+
EndProject
56+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.WebUtilities", "..\HttpAbstractions\src\Microsoft.AspNet.WebUtilities\Microsoft.AspNet.WebUtilities.xproj", "{A2FB7838-0031-4FAD-BA3E-83C30B3AF406}"
57+
EndProject
58+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Net.Http.Headers", "..\HttpAbstractions\src\Microsoft.Net.Http.Headers\Microsoft.Net.Http.Headers.xproj", "{60AA2FDB-8121-4826-8D00-9A143FEFAF66}"
59+
EndProject
4060
Global
4161
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4262
Debug|Any CPU = Debug|Any CPU
@@ -79,6 +99,46 @@ Global
7999
{9559A5F1-080C-4909-B6CF-7E4B3DC55748}.Debug|Any CPU.Build.0 = Debug|Any CPU
80100
{9559A5F1-080C-4909-B6CF-7E4B3DC55748}.Release|Any CPU.ActiveCfg = Release|Any CPU
81101
{9559A5F1-080C-4909-B6CF-7E4B3DC55748}.Release|Any CPU.Build.0 = Release|Any CPU
102+
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
105+
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Release|Any CPU.Build.0 = Release|Any CPU
106+
{BB780FBB-7842-4759-8DE7-96FA2E5571C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107+
{BB780FBB-7842-4759-8DE7-96FA2E5571C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
108+
{BB780FBB-7842-4759-8DE7-96FA2E5571C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
109+
{BB780FBB-7842-4759-8DE7-96FA2E5571C1}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{FDBBA081-5248-4FC0-9E08-B46BEF3FA438}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{FDBBA081-5248-4FC0-9E08-B46BEF3FA438}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{FDBBA081-5248-4FC0-9E08-B46BEF3FA438}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{FDBBA081-5248-4FC0-9E08-B46BEF3FA438}.Release|Any CPU.Build.0 = Release|Any CPU
114+
{22071333-15BA-4D16-A1D5-4D5B1A83FBDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115+
{22071333-15BA-4D16-A1D5-4D5B1A83FBDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
116+
{22071333-15BA-4D16-A1D5-4D5B1A83FBDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
117+
{22071333-15BA-4D16-A1D5-4D5B1A83FBDD}.Release|Any CPU.Build.0 = Release|Any CPU
118+
{BCF0F967-8753-4438-BD07-AADCA9CE509A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119+
{BCF0F967-8753-4438-BD07-AADCA9CE509A}.Debug|Any CPU.Build.0 = Debug|Any CPU
120+
{BCF0F967-8753-4438-BD07-AADCA9CE509A}.Release|Any CPU.ActiveCfg = Release|Any CPU
121+
{BCF0F967-8753-4438-BD07-AADCA9CE509A}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{D9128247-8F97-48B8-A863-F1F21A029FCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123+
{D9128247-8F97-48B8-A863-F1F21A029FCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
124+
{D9128247-8F97-48B8-A863-F1F21A029FCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
125+
{D9128247-8F97-48B8-A863-F1F21A029FCE}.Release|Any CPU.Build.0 = Release|Any CPU
126+
{BE9112CB-D87D-4080-9CC3-24492D49CBE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127+
{BE9112CB-D87D-4080-9CC3-24492D49CBE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
128+
{BE9112CB-D87D-4080-9CC3-24492D49CBE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
129+
{BE9112CB-D87D-4080-9CC3-24492D49CBE6}.Release|Any CPU.Build.0 = Release|Any CPU
130+
{CCC4363E-81E2-4058-94DD-00494E9E992A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131+
{CCC4363E-81E2-4058-94DD-00494E9E992A}.Debug|Any CPU.Build.0 = Debug|Any CPU
132+
{CCC4363E-81E2-4058-94DD-00494E9E992A}.Release|Any CPU.ActiveCfg = Release|Any CPU
133+
{CCC4363E-81E2-4058-94DD-00494E9E992A}.Release|Any CPU.Build.0 = Release|Any CPU
134+
{A2FB7838-0031-4FAD-BA3E-83C30B3AF406}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135+
{A2FB7838-0031-4FAD-BA3E-83C30B3AF406}.Debug|Any CPU.Build.0 = Debug|Any CPU
136+
{A2FB7838-0031-4FAD-BA3E-83C30B3AF406}.Release|Any CPU.ActiveCfg = Release|Any CPU
137+
{A2FB7838-0031-4FAD-BA3E-83C30B3AF406}.Release|Any CPU.Build.0 = Release|Any CPU
138+
{60AA2FDB-8121-4826-8D00-9A143FEFAF66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
139+
{60AA2FDB-8121-4826-8D00-9A143FEFAF66}.Debug|Any CPU.Build.0 = Debug|Any CPU
140+
{60AA2FDB-8121-4826-8D00-9A143FEFAF66}.Release|Any CPU.ActiveCfg = Release|Any CPU
141+
{60AA2FDB-8121-4826-8D00-9A143FEFAF66}.Release|Any CPU.Build.0 = Release|Any CPU
82142
EndGlobalSection
83143
GlobalSection(SolutionProperties) = preSolution
84144
HideSolutionNode = FALSE

samples/SampleApp/Startup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IAp
5252
context.Request.Path,
5353
context.Request.QueryString);
5454

55+
var connectionFeature = context.Connection;
56+
Console.WriteLine($"Peer: {connectionFeature.RemoteIpAddress?.ToString()} {connectionFeature.RemotePort}");
57+
Console.WriteLine($"Sock: {connectionFeature.LocalIpAddress?.ToString()} {connectionFeature.LocalPort}");
58+
Console.WriteLine($"IsLocal: {connectionFeature.IsLocal}");
59+
5560
context.Response.ContentLength = 11;
5661
context.Response.ContentType = "text/plain";
5762
await context.Response.WriteAsync("Hello world");

src/Microsoft.AspNet.Server.Kestrel/Http/Connection.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Net;
56
using System.Threading;
67
using Microsoft.AspNet.Server.Kestrel.Filter;
78
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
@@ -28,6 +29,9 @@ public class Connection : ConnectionContext, IConnectionControl
2829
private readonly object _stateLock = new object();
2930
private ConnectionState _connectionState;
3031

32+
private IPEndPoint _remoteEndPoint;
33+
private IPEndPoint _localEndPoint;
34+
3135
public Connection(ListenerContext context, UvStreamHandle socket) : base(context)
3236
{
3337
_socket = socket;
@@ -46,13 +50,20 @@ public void Start()
4650
// Start socket prior to applying the ConnectionFilter
4751
_socket.ReadStart(_allocCallback, _readCallback, this);
4852

53+
var tcpHandle = _socket as UvTcpHandle;
54+
if (tcpHandle != null)
55+
{
56+
_remoteEndPoint = tcpHandle.GetPeerIPEndPoint();
57+
_localEndPoint = tcpHandle.GetSockIPEndPoint();
58+
}
59+
4960
// Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
5061
if (ConnectionFilter == null)
5162
{
5263
SocketInput = _rawSocketInput;
5364
SocketOutput = _rawSocketOutput;
5465

55-
_frame = new Frame(this);
66+
_frame = CreateFrame();
5667
_frame.Start();
5768
}
5869
else
@@ -94,7 +105,7 @@ private void ApplyConnectionFilter()
94105
SocketInput = filteredStreamAdapter.SocketInput;
95106
SocketOutput = filteredStreamAdapter.SocketOutput;
96107

97-
_frame = new Frame(this);
108+
_frame = CreateFrame();
98109
_frame.Start();
99110
}
100111

@@ -142,6 +153,11 @@ private void OnRead(UvStreamHandle handle, int status)
142153
_rawSocketInput.IncomingComplete(readCount, error);
143154
}
144155

156+
private Frame CreateFrame()
157+
{
158+
return new Frame(this, _remoteEndPoint, _localEndPoint);
159+
}
160+
145161
void IConnectionControl.Pause()
146162
{
147163
Log.ConnectionPause(_connectionId);

src/Microsoft.AspNet.Server.Kestrel/Http/Frame.FeatureCollection.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Linq;
9+
using System.Net;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNet.Http;
1112
using Microsoft.AspNet.Http.Features;
1213
using Microsoft.Extensions.Primitives;
1314

1415
namespace Microsoft.AspNet.Server.Kestrel.Http
1516
{
16-
public partial class Frame : IFeatureCollection, IHttpRequestFeature, IHttpResponseFeature, IHttpUpgradeFeature
17+
public partial class Frame : IFeatureCollection,
18+
IHttpRequestFeature,
19+
IHttpResponseFeature,
20+
IHttpUpgradeFeature,
21+
IHttpConnectionFeature
1722
{
1823
// NOTE: When feature interfaces are added to or removed from this Frame class implementation,
1924
// then the list of `implementedFeatures` in the generated code project MUST also be updated.
@@ -246,6 +251,16 @@ bool IHttpUpgradeFeature.IsUpgradableRequest
246251

247252
int IFeatureCollection.Revision => _featureRevision;
248253

254+
IPAddress IHttpConnectionFeature.RemoteIpAddress { get; set; }
255+
256+
IPAddress IHttpConnectionFeature.LocalIpAddress { get; set; }
257+
258+
int IHttpConnectionFeature.RemotePort { get; set; }
259+
260+
int IHttpConnectionFeature.LocalPort { get; set; }
261+
262+
bool IHttpConnectionFeature.IsLocal { get; set; }
263+
249264
object IFeatureCollection.this[Type key]
250265
{
251266
get { return FastFeatureGet(key); }

src/Microsoft.AspNet.Server.Kestrel/Http/Frame.Generated.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ private void FastReset()
4545
_currentIHttpRequestFeature = this;
4646
_currentIHttpResponseFeature = this;
4747
_currentIHttpUpgradeFeature = this;
48+
_currentIHttpConnectionFeature = this;
4849

4950
_currentIHttpRequestIdentifierFeature = null;
5051
_currentIServiceProvidersFeature = null;
5152
_currentIHttpRequestLifetimeFeature = null;
52-
_currentIHttpConnectionFeature = null;
5353
_currentIHttpAuthenticationFeature = null;
5454
_currentIQueryFeature = null;
5555
_currentIFormFeature = null;

src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using System.Net;
89
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.AspNet.Http;
13+
using Microsoft.AspNet.Http.Features;
1214
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
1315
using Microsoft.Extensions.Logging;
1416
using Microsoft.Extensions.Primitives;
@@ -44,12 +46,25 @@ public partial class Frame : FrameContext, IFrameControl
4446
private bool _autoChunk;
4547
private Exception _applicationException;
4648

47-
public Frame(ConnectionContext context) : base(context)
49+
private readonly IPEndPoint _localEndPoint;
50+
private readonly IPEndPoint _remoteEndPoint;
51+
52+
public Frame(ConnectionContext context)
53+
: base(context)
4854
{
4955
FrameControl = this;
5056
Reset();
5157
}
5258

59+
public Frame(ConnectionContext context,
60+
IPEndPoint remoteEndPoint,
61+
IPEndPoint localEndPoint)
62+
: this(context)
63+
{
64+
_remoteEndPoint = remoteEndPoint;
65+
_localEndPoint = localEndPoint;
66+
}
67+
5368
public string Method { get; set; }
5469
public string RequestUri { get; set; }
5570
public string Path { get; set; }
@@ -99,6 +114,26 @@ public void Reset()
99114
ResponseBody = null;
100115
DuplexStream = null;
101116

117+
var httpConnectionFeature = this as IHttpConnectionFeature;
118+
if (httpConnectionFeature != null)
119+
{
120+
if (_remoteEndPoint != null)
121+
{
122+
httpConnectionFeature.RemoteIpAddress = _remoteEndPoint.Address;
123+
httpConnectionFeature.RemotePort = _remoteEndPoint.Port;
124+
}
125+
126+
if (_localEndPoint != null)
127+
{
128+
httpConnectionFeature.LocalIpAddress = _localEndPoint.Address;
129+
httpConnectionFeature.LocalPort = _localEndPoint.Port;
130+
}
131+
132+
if (_remoteEndPoint != null && _localEndPoint != null)
133+
{
134+
httpConnectionFeature.IsLocal = _remoteEndPoint.Address.Equals(_localEndPoint.Address);
135+
}
136+
}
102137
}
103138

104139
public void ResetResponseHeaders()

0 commit comments

Comments
 (0)