get actual IP from connection
This commit is contained in:
		
							
								
								
									
										24
									
								
								MareSynchronosServer/MareSynchronosServer/Extensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								MareSynchronosServer/MareSynchronosServer/Extensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| using Microsoft.AspNetCore.Http; | ||||
|  | ||||
| namespace MareSynchronosServer | ||||
| { | ||||
|     public static class Extensions | ||||
|     { | ||||
|         public static string GetIpAddress(this IHttpContextAccessor accessor) | ||||
|         { | ||||
|             if (!string.IsNullOrEmpty(accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"])) | ||||
|                 return accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"]; | ||||
|  | ||||
|             var ipAddress = accessor.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR"); | ||||
|  | ||||
|             if (!string.IsNullOrEmpty(ipAddress)) | ||||
|             { | ||||
|                 var addresses = ipAddress.Split(','); | ||||
|                 if (addresses.Length != 0) | ||||
|                     return addresses.Last(); | ||||
|             } | ||||
|  | ||||
|             return accessor.HttpContext.Connection.RemoteIpAddress.ToString(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -8,6 +8,7 @@ using MareSynchronosServer.Authentication; | ||||
| using MareSynchronosServer.Data; | ||||
| using MareSynchronosServer.Metrics; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.AspNetCore.Http.Features; | ||||
| using Microsoft.AspNetCore.SignalR; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| @@ -33,7 +34,7 @@ namespace MareSynchronosServer.Hubs | ||||
|         } | ||||
|  | ||||
|         [HubMethodName(Api.InvokeHeartbeat)] | ||||
|         public async Task<ConnectionDto> Heartbeat(string? characterIdentification) | ||||
|         public async Task<ConnectionDto> Heartbeat(string characterIdentification) | ||||
|         { | ||||
|             MareMetrics.InitializedConnections.Inc(); | ||||
|  | ||||
| @@ -86,8 +87,8 @@ namespace MareSynchronosServer.Hubs | ||||
|  | ||||
|         public override Task OnConnectedAsync() | ||||
|         { | ||||
|             var feature = Context.Features.Get<IHttpConnectionFeature>(); | ||||
|             _logger.LogInformation("Connection from " + feature.RemoteIpAddress); | ||||
|             var feature = Context.Features.Get<IHttpContextAccessor>(); | ||||
|             _logger.LogInformation("Connection from " + feature.GetIpAddress()); | ||||
|             MareMetrics.Connections.Inc(); | ||||
|             return base.OnConnectedAsync(); | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Stanley Dimant
					Stanley Dimant