Skip to content

Commit

Permalink
add JumpToCN complex call interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHe404 committed Aug 22, 2019
1 parent e847fa3 commit d00a987
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 57 deletions.
8 changes: 5 additions & 3 deletions HeXuShi.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSample", "sample\WebSamp
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demo", "demo", "{A633B722-1F9E-493E-9473-4CB6BD220788}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpSearchDemo", "demo\IpSearchDemo\IpSearchDemo.csproj", "{DFFFEB0E-1082-4E51-A78A-D9BFECDA847B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IpSearchDemo", "demo\IpSearchDemo\IpSearchDemo.csproj", "{DFFFEB0E-1082-4E51-A78A-D9BFECDA847B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeXuShi.Extensions.IsChinaIp", "src\HeXuShi.Extensions.IsChinaIp\HeXuShi.Extensions.IsChinaIp.csproj", "{95CD1BE9-3FA5-4EB8-9527-73AC9492E74F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HeXuShi.Extensions.IsChinaIp", "src\HeXuShi.Extensions.IsChinaIp\HeXuShi.Extensions.IsChinaIp.csproj", "{95CD1BE9-3FA5-4EB8-9527-73AC9492E74F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestChinaIp", "sample\TestChinaIp\TestChinaIp.csproj", "{A86DAC6D-3EA7-485F-B83A-8355F8B31F8E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestChinaIp", "sample\TestChinaIp\TestChinaIp.csproj", "{A86DAC6D-3EA7-485F-B83A-8355F8B31F8E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{DAE97445-BF2C-46C8-ACF7-EC0773FBBED6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>change xx.com or xx.anything to xx.cn,help you jump to cn(china) domain suffix.</Description>
<Copyright>Copyright 2019</Copyright>
<RootNamespace>HeXuShi.Extensions</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/HeXuShi.Extensions.JumpToCN/JumpOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace HeXuShi.Extensions
{
//JumpToCNState
//ToCNState
//JumpToState
public enum JumpOption
{
OnlyTo_SpecSuffix,//Only jump to specify suffix
OnlyTo_SpecDomain,//Only jump to specify domain
SuffixTo_SpecSuffix,//specify suffix to specify suffix,
DomainTo_SpecDomain,//specify domain to specify domain,
}
}
14 changes: 13 additions & 1 deletion src/HeXuShi.Extensions.JumpToCN/JumpToCNPolicyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ public static IApplicationBuilder JumpToCN(this IApplicationBuilder app, string
{
throw new ArgumentNullException(nameof(app));
}
app.UseMiddleware<JumpToCNMiddleware>(noCNSuffix ?? string.Empty);
if(noCNSuffix == null)
app.UseMiddleware<JumpToCNMiddleware>(".cn", string.Empty, JumpOption.OnlyTo_SpecSuffix);
else
app.UseMiddleware<JumpToCNMiddleware>(".cn", noCNSuffix, JumpOption.SuffixTo_SpecSuffix);
return app;
}
public static IApplicationBuilder ComplexJumpToCN(this IApplicationBuilder app, string first, string second, JumpOption option)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
app.UseMiddleware<JumpToCNMiddleware>(first, second, option);
return app;
}
}
Expand Down
195 changes: 195 additions & 0 deletions src/HeXuShi.Extensions.JumpToCN/Middleware/HandleRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace HeXuShi.Extensions.Middleware
{
internal class HandleRequest
{
private JumpOption _option;
private string _first;
private string _second;
public HandleRequest(
string first,
string second,
JumpOption option)
{
_option = option;
_first = first;
_second = second;
}
public Tuple<bool, string> Handle(HttpContext context)
{
switch(_option)
{
case JumpOption.OnlyTo_SpecSuffix:
return OnlyTo_SpecSuffix(context);
case JumpOption.OnlyTo_SpecDomain:
return OnlyTo_SpecDomain(context);
case JumpOption.SuffixTo_SpecSuffix:
return SuffixTo_SpecSuffix(context);
case JumpOption.DomainTo_SpecDomain:
return DomainTo_SpecDomain(context);
default:
break;
}
throw new InvalidOperationException("jump to cn error handle request range");
}
private Tuple<bool, string> OnlyTo_SpecSuffix(HttpContext context)
{
var result = new Tuple<bool, string>(false, string.Empty);
var index = context.Request.Host.Value.LastIndexOf(".");
if (index == -1 || context.Connection.RemoteIpAddress.ToString().Length < 6)
return result;

var suffix = context.Request.Host.Value.Substring(index);
if (suffix == _first)
return result;

try
{
IsChinaIp.Setup();
bool isChinaIp = false;
switch (context.Connection.RemoteIpAddress.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
isChinaIp = IsChinaIp.VerifyIPv4(context.Connection.RemoteIpAddress.ToString());
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
isChinaIp = IsChinaIp.VerifyIPv6(context.Connection.RemoteIpAddress.ToString());
break;
default:
return result;
}
if (isChinaIp)
{
var domain = context.Request.Host.Value;
domain = domain.Remove(index, domain.Length - index);
domain += _first;
return new Tuple<bool, string>(true, domain);
}
else
return result;
}
catch
{
return result;
}
}
public Tuple<bool, string> OnlyTo_SpecDomain(HttpContext context)
{
var result = new Tuple<bool, string>(false, string.Empty);
if (context.Request.Host.Value == _first)
return result;

try
{
IsChinaIp.Setup();
bool isChinaIp = false;
switch (context.Connection.RemoteIpAddress.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
isChinaIp = IsChinaIp.VerifyIPv4(context.Connection.RemoteIpAddress.ToString());
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
isChinaIp = IsChinaIp.VerifyIPv6(context.Connection.RemoteIpAddress.ToString());
break;
default:
return result;
}
if (isChinaIp)
{
return new Tuple<bool, string>(true, _first);
}
else
return result;
}
catch
{
return result;
}
}
public Tuple<bool, string> SuffixTo_SpecSuffix(HttpContext context)
{
var result = new Tuple<bool, string>(false, string.Empty);
var index = context.Request.Host.Value.LastIndexOf(".");
if (index == -1 || context.Connection.RemoteIpAddress.ToString().Length < 6)
return result;

var suffix = context.Request.Host.Value.Substring(index);

try
{
IsChinaIp.Setup();
bool isChinaIp = false;
switch (context.Connection.RemoteIpAddress.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
isChinaIp = IsChinaIp.VerifyIPv4(context.Connection.RemoteIpAddress.ToString());
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
isChinaIp = IsChinaIp.VerifyIPv6(context.Connection.RemoteIpAddress.ToString());
break;
default:
return result;
}
if (suffix != _first && isChinaIp)
{
var domain = context.Request.Host.Value;
domain = domain.Remove(index, domain.Length - index);
domain += _first;
return new Tuple<bool, string>(true, domain);
}
else if (suffix != _second && !isChinaIp)
{
var domain = context.Request.Host.Value;
domain = domain.Remove(index, domain.Length - index);
domain += _second;
return new Tuple<bool, string>(true, domain);
}
else
return result;
}
catch
{
return result;
}
}
public Tuple<bool, string> DomainTo_SpecDomain(HttpContext context)
{
var result = new Tuple<bool, string>(false, string.Empty);

try
{
IsChinaIp.Setup();
bool isChinaIp = false;
switch (context.Connection.RemoteIpAddress.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
isChinaIp = IsChinaIp.VerifyIPv4(context.Connection.RemoteIpAddress.ToString());
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
isChinaIp = IsChinaIp.VerifyIPv6(context.Connection.RemoteIpAddress.ToString());
break;
default:
return result;
}
if (context.Request.Host.Value != _first && isChinaIp)
{
return new Tuple<bool, string>(true, _first);
}
else if (context.Request.Host.Value != _second && !isChinaIp)
{
return new Tuple<bool, string>(true, _second);
}
else
return result;
}
catch
{
return result;
}
}
}
}
63 changes: 10 additions & 53 deletions src/HeXuShi.Extensions.JumpToCN/Middleware/JumpToCNMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IP2Region;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Net.Http.Headers;
Expand All @@ -13,67 +12,25 @@ namespace HeXuShi.Extensions.Middleware
public class JumpToCNMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostingEnvironment _hostingEnvironment;
private string _webRootPath;
private string _noCNSuffix;
private HandleRequest _handleRequest;
public JumpToCNMiddleware(
RequestDelegate next,
string noCNSuffix,
IHostingEnvironment hostingEnvironment
string first,
string second,
JumpOption option
)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_hostingEnvironment = hostingEnvironment;
_webRootPath = _hostingEnvironment.WebRootPath;
_noCNSuffix = noCNSuffix;
}
public HostString changeDomainSuffix(string domain, int index, string newSuffix)
{
domain = domain.Remove(index, domain.Length - index);
return new HostString(domain + newSuffix);
_handleRequest = new HandleRequest(first, second, option);
}

public Task Invoke(HttpContext context)
{
var index = context.Request.Host.Value.LastIndexOf(".");
if (index == -1 || context.Connection.RemoteIpAddress.ToString().Length < 6)
var result = _handleRequest.Handle(context);
if (!result.Item1)
return _next(context);

string cnSuffix = ".cn";
var suffix = context.Request.Host.Value.Substring(index);
if (_noCNSuffix == string.Empty && suffix == cnSuffix)
return _next(context);

HostString newHost;
try
{
IsChinaIp.Setup();
bool isChinaIp = false;
switch (context.Connection.RemoteIpAddress.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
isChinaIp = IsChinaIp.VerifyIPv4(context.Connection.RemoteIpAddress.ToString());
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
isChinaIp = IsChinaIp.VerifyIPv6(context.Connection.RemoteIpAddress.ToString());
break;
default:
return _next(context);
}
if (suffix != cnSuffix && isChinaIp)
{
newHost = changeDomainSuffix(context.Request.Host.Value, index, cnSuffix);
}
else if (suffix != _noCNSuffix && !isChinaIp && _noCNSuffix != string.Empty)
{
newHost = changeDomainSuffix(context.Request.Host.Value, index, _noCNSuffix);
}
else
return _next(context);
}
catch
{
return _next(context);
}
var newHost = new HostString(result.Item2);
var request = context.Request;
var redirectUrl = UriHelper.BuildAbsolute(
context.Request.Scheme,
Expand Down

0 comments on commit d00a987

Please sign in to comment.