全部回复(5)**IIS web.config 规则(只允许中国大陆 + 港澳台访问,国外直接 403)**,不需要改 ASP 页面源码,性能远好于 ASP 脚本调用接口。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="BlockOverSeaIP" stopProcessing="true">
<match url=".*" />
<conditions>
<!-- 这里依赖:CDN/防火墙传入IP地区标记,原生IIS本身不带IP库! -->
<!-- 纯IIS原生是无法仅凭这个配置识别国家,必须搭配支持IP地理的CDN(如阿里云、腾讯云CDN),由CDN传递国家头 -->
<add input="{HTTP_CDN_IP_COUNTRY}" pattern="^(?!CN|HK|MO|TW)$" />
</conditions>
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="仅允许国内访问" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
如果你没有 CDN,只能继续用 ASP 版本,给你【包含港澳台放行版】,直接复制放 ASP 头部:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim ip,xmlhttp,url,json,country
ip = Request.ServerVariables("REMOTE_ADDR")
url = "https://ipapi.co/" & ip & "/json/"
Set xmlhttp = Server.CreateObject("MSXML2.XMLHTTP.6.0")
xmlhttp.Open "GET",url,False
xmlhttp.setRequestHeader "User-Agent","Mozilla/5.0"
On Error Resume Next
xmlhttp.Send
If Err.Number<>0 Then
Set xmlhttp=Nothing
End If
country = ""
If xmlhttp.Status=200 Then
json = xmlhttp.ResponseText
Dim s,e
s = InStr(json,"country_code"":""") + Len("country_code"":""")
e = InStr(s,json,"""")
If s>0 And e>s Then
country = Mid(json,s,e-s)
End If
End If
Set xmlhttp=Nothing
' CN大陆 HK香港 MO澳门 TW台湾 放行,其他国家拦截
Dim allowCode
allowCode = Array("CN","HK","MO","TW")
Dim isAllow,i
isAllow=False
For i=0 To UBound(allowCode)
If UCase(country)=allowCode(i) Then
isAllow=True
Exit For
End If
Next
If Not isAllow Then
Response.Status="403 Forbidden"
Response.Write "<h2>抱歉,仅允许国内及港澳台地区访问</h2>"
Response.End
End If
%>