Skip to content

Commit

Permalink
prefix name change "v" to "x".
Browse files Browse the repository at this point in the history
change remote ip address support ip6 to ip4
  • Loading branch information
nameofSEOKWONHONG committed Aug 14, 2024
1 parent 3f74c78 commit c8cdaad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# eXtensionSharp.AspNet
eXtensionSharp for asp.net web application

* v0.0.3
* prefix name change "v" to "x".
* support remote ip address ip6 to ip4. (xGetRemoteIpAddress)

* v0.0.2
* Impl vGetMethod
* Impl vGetControllerName
Expand Down
67 changes: 53 additions & 14 deletions src/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,57 @@ public static class HttpContextExtensions
/// get remote address
/// </summary>
/// <param name="context"></param>
/// <param name="isOrigin">true:It doesn't attempt to convert IP6 to IP4. (default. false)</param>
/// <returns></returns>
public static string xGetRemoteIpAddress(this HttpContext context)
public static string xGetRemoteIpAddress(this HttpContext context, bool isOrigin = false)
{
return context.Connection.RemoteIpAddress?.ToString();
if (context.xIsEmpty()) return string.Empty;
if (context.Connection.xIsEmpty()) return string.Empty;
if (context.Connection.RemoteIpAddress.xIsEmpty()) return string.Empty;

var remoteIp = context.Connection.RemoteIpAddress;

if (remoteIp == null)
{
return string.Empty;
}

if (isOrigin) return remoteIp.ToString();

// Check if it's an IPv4-mapped IPv6 address
if (remoteIp.IsIPv4MappedToIPv6)
{
remoteIp = remoteIp.MapToIPv4();
}

return remoteIp.ToString();
}

/// <summary>
/// get remote port
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string xGetRemotePort(this HttpContext context)
public static int? xGetRemotePort(this HttpContext context)
{
return context.Connection.RemotePort.xValue<string>();
if (context.xIsEmpty()) return null;
if (context.Connection.xIsEmpty()) return null;
if (context.Connection.RemoteIpAddress.xIsEmpty()) return null;

return context.Connection.RemotePort;
}

/// <summary>
/// get remote full ip addresss
/// get remote ip:port addresss
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string xGetRemoteFullIpAddress(this HttpContext context)
{
return $"{context.Connection.RemoteIpAddress}:{context.Connection.RemotePort}";
var ip = context.xGetRemoteIpAddress();
var port = context.xGetRemotePort();

return $"{ip}:{port}";
}

/// <summary>
Expand Down Expand Up @@ -173,8 +200,11 @@ public static string xGetActionName(this HttpContext context)
/// <returns></returns>
public static string xGetUserAgent(this HttpContext context)
{
context.xTryGetRequestHeader(HttpContextHeaderName.UserAgent, out var v);
return v;
if (context.xTryGetRequestHeader(HttpContextHeaderName.UserAgent, out var v))
{
return v;
}
return string.Empty;
}

/// <summary>
Expand All @@ -184,8 +214,11 @@ public static string xGetUserAgent(this HttpContext context)
/// <returns></returns>
public static string xGetAcceptEncoding(this HttpContext context)
{
context.xTryGetRequestHeader(HttpContextHeaderName.AcceptEncoding, out var v);
return v;
if (context.xTryGetRequestHeader(HttpContextHeaderName.AcceptEncoding, out var v))
{
return v;
}
return string.Empty;
}

/// <summary>
Expand All @@ -195,8 +228,11 @@ public static string xGetAcceptEncoding(this HttpContext context)
/// <returns></returns>
public static string xGetAcceptLanguage(this HttpContext context)
{
context.xTryGetRequestHeader(HttpContextHeaderName.AcceptLanguage, out var v);
return v;
if (context.xTryGetRequestHeader(HttpContextHeaderName.AcceptLanguage, out var v))
{
return v;
}
return string.Empty;
}

/// <summary>
Expand All @@ -206,7 +242,10 @@ public static string xGetAcceptLanguage(this HttpContext context)
/// <returns></returns>
public static string xGetReferer(this HttpContext context)
{
context.xTryGetRequestHeader(HttpContextHeaderName.Referer, out var v);
return v;
if (context.xTryGetRequestHeader(HttpContextHeaderName.Referer, out var v))
{
return v;
}
return string.Empty;
}
}
2 changes: 1 addition & 1 deletion src/eXtensionSharp.AspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<Copyright>seokwon hong</Copyright>
<Product>eXtensionSharp.AspNet</Product>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<Title>eXtensionSharp.AspNet</Title>
<Description>asp.net extensions</Description>
<PackageProjectUrl>https://github.com/nameofSEOKWONHONG/eXtensionSharp.AspNet</PackageProjectUrl>
Expand Down

0 comments on commit c8cdaad

Please sign in to comment.