Skip to content

Commit ddb7b98

Browse files
committed
add IPv6 resolution
1 parent 6a09fb2 commit ddb7b98

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

FastWebApi/JwsIntegration.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#region <USINGs>
33

4+
using System.Net;
45
using System.Threading.Tasks;
56
using Microsoft.Owin;
67
using Owin;
@@ -21,7 +22,7 @@ public class JwsIntegration : OwinMiddleware
2122
/// <summary>
2223
/// 下一个“中间件”对象
2324
/// </summary>
24-
OwinMiddleware _next;
25+
readonly OwinMiddleware _next;
2526

2627
/// <summary>
2728
/// 构造函数,第一个参数必须为 OwinMiddleware对象
@@ -49,16 +50,17 @@ public override Task Invoke(IOwinContext owinContext)
4950
if (headers != null && headers.ContainsKey("X-Original-For"))
5051
{
5152
var ipaddAdndPort = headers["X-Original-For"];
52-
var dot = ipaddAdndPort.IndexOf(":");
53+
var colon = ipaddAdndPort.LastIndexOf(":");
5354
var ip = ipaddAdndPort;
5455
var port = 0;
55-
if (dot > 0)
56+
57+
if (colon > 0)
5658
{
57-
ip = ipaddAdndPort.Substring(0, dot);
58-
port = int.Parse(ipaddAdndPort.Substring(dot + 1));
59+
ip = ipaddAdndPort.Substring(0, colon).Trim(new[] { '[', ']', '\x20' });
60+
port = int.Parse(ipaddAdndPort.Substring(colon + 1));
5961
}
6062

61-
owinContext.Request.RemoteIpAddress = System.Net.IPAddress.Parse(ip).ToString();
63+
owinContext.Request.RemoteIpAddress = IPAddress.Parse(ip).ToString();
6264
if (port != 0) owinContext.Request.RemotePort = port;
6365
}
6466

0 commit comments

Comments
 (0)