萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 取得用戶真實 IP:穿透 Proxy、Load Balancer、CDN 的多層檢測

IP 取得優先順序

HTTP 請求經過多層代理 (CDN、Load Balancer、Reverse Proxy) 時,原始客戶端 IP 會被附加在特定 Header 中。標準檢測順序:

  1. HTTP_X_FORWARDED_FOR (可能包含多個 IP,逗號分隔,**最左邊為原始客戶端**)
  2. HTTP_X_REAL_IP (Nginx/Apache 常用)
  3. HTTP_CLIENT_IP (部分 Proxy)
  4. HTTP_X_CLUSTER_CLIENT_IP (AWS ALB 等)
  5. REMOTE_ADDR (直接連線或最後一層 Proxy IP)

完整取得真實 IP 函數

<%
Function GetRealIP()
  Dim ip, forwarded, realIP, clientIP, clusterIP, remoteAddr, ips, i
  
  ' 1. X-Forwarded-For (標準)
  forwarded = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
  If forwarded <> "" Then
    ips = Split(forwarded, ",")
    For i = 0 To UBound(ips)
      ip = Trim(ips(i))
      If IsValidPublicIP(ip) Then
        GetRealIP = ip
        Exit Function
      End If
    Next
  End If
  
  ' 2. X-Real-IP (Nginx proxy_set_header)
  realIP = Request.ServerVariables("HTTP_X_REAL_IP")
  If realIP <> "" And IsValidPublicIP(realIP) Then
    GetRealIP = realIP
    Exit Function
  End If
  
  ' 3. Client-IP
  clientIP = Request.ServerVariables("HTTP_CLIENT_IP")
  If clientIP <> "" And IsValidPublicIP(clientIP) Then
    GetRealIP = clientIP
    Exit Function
  End If
  
  ' 4. X-Cluster-Client-IP (AWS ALB)
  clusterIP = Request.ServerVariables("HTTP_X_CLUSTER_CLIENT_IP")
  If clusterIP <> "" And IsValidPublicIP(clusterIP) Then
    GetRealIP = clusterIP
    Exit Function
  End If
  
  ' 5. 回退至 REMOTE_ADDR
  remoteAddr = Request.ServerVariables("REMOTE_ADDR")
  If IsValidPublicIP(remoteAddr) Then
    GetRealIP = remoteAddr
  Else
    GetRealIP = "0.0.0.0" ' 未知
  End If
End Function

' 驗證是否為合法公網 IPv4 (簡易版,排除私有/保留區段)
Function IsValidPublicIP(ip)
  Dim octets, o1, o2
  If Not IsIPv4Format(ip) Then IsValidPublicIP = False: Exit Function
  
  octets = Split(ip, ".")
  o1 = CInt(octets(0))
  o2 = CInt(octets(1))
  
  ' 私有網段 (RFC 1918)
  If o1 = 10 Then IsValidPublicIP = False: Exit Function
  If o1 = 172 And o2 >= 16 And o2 <= 31 Then IsValidPublicIP = False: Exit Function
  If o1 = 192 And o2 = 168 Then IsValidPublicIP = False: Exit Function
  ' 回環/保留
  If o1 = 127 Then IsValidPublicIP = False: Exit Function
  If o1 = 169 And o2 = 254 Then IsValidPublicIP = False: Exit Function ' Link-local
  If o1 >= 224 Then IsValidPublicIP = False: Exit Function ' Multicast/保留
  
  IsValidPublicIP = True
End Function

Function IsIPv4Format(ip)
  Dim re, matches
  Set re = New RegExp
  re.Pattern = "^(\d{1,3}\.){3}\d{1,3}$"
  IsIPv4Format = re.Test(ip)
  If IsIPv4Format Then
    Dim parts, p
    parts = Split(ip, ".")
    For Each p In parts
      If CInt(p) > 255 Then IsIPv4Format = False: Exit Function
    Next
  End If
End Function
%>

使用範例

<%
Dim realIP
realIP = GetRealIP()
Response.Write "真實 IP: " & realIP & "
" Response.Write "REMOTE_ADDR: " & Request.ServerVariables("REMOTE_ADDR") & "
" Response.Write "X-Forwarded-For: " & Request.ServerVariables("HTTP_X_FORWARDED_FOR") & "
" %>

搭配地理位置查詢 (IP2Location / MaxMind GeoIP)

<%
' 假設已安裝 IP2Location COM 元件
Dim ip2loc, record
Set ip2loc = Server.CreateObject("IP2Location.Component")
ip2loc.Open Server.MapPath("/bin/IP2LOCATION.BIN")

Set record = ip2loc.IPQuery(GetRealIP())
If Not record Is Nothing Then
  Response.Write "國家: " & record.CountryLong & "
" Response.Write "城市: " & record.City & "
" Response.Write "ISP: " & record.ISP & "
" End If %>

部署架構對應設定

架構層需設定的 Header範例 (Nginx)
CDN (Cloudflare)CF-Connecting-IP自動添加,ASP 讀取 HTTP_CF_CONNECTING_IP
Load Balancer (AWS ALB)X-Forwarded-For, X-Forwarded-Proto自動添加
Reverse Proxy (Nginx)proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
在 location / 區塊設定
IIS ARRX-Forwarded-ForServer Farm → Proxy → 勾選「使用 X-Forwarded-For」

安全性警示

X-Forwarded-For 可被用戶端偽造!
僅信任**你直接控制的那一層 Proxy** 添加的 Header。例如:Nginx 設定 `proxy_set_header X-Real-IP $remote_addr;` 後,ASP 只信任 `HTTP_X_REAL_IP`;不信任客戶端直接送來的 `X-Forwarded-For`。
最佳做法:在最邊緣 (CDN/Load Balancer) 將真實 IP 寫入**自訂 Header** (如 `X-Original-Client-IP`),並刪除用戶端送來的同名 Header,後端只讀取該自訂 Header。

copyright © 萬盛學電腦網 all rights reserved