在反向代理、CDN、負載平衡器架構下,Request.ServerVariables("REMOTE_ADDR") 只能取得代理伺服器 IP。本文提供生產級的真實 IP 取得函數,支援多層代理、IPv6、私有 IP 過濾與安全性驗證。
<%
Function GetRealIP()
Dim ip, forwardedFor, realIP, ips, i, clientIP
' 1. 優先檢查 X-Forwarded-For 標頭(可能包含多個 IP,逗號分隔)
forwardedFor = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If forwardedFor <> "" Then
' 取第一個非私有 IP(最接近用戶端的)
ips = Split(forwardedFor, ",")
For i = 0 To UBound(ips)
clientIP = Trim(ips(i))
If IsPublicIP(clientIP) Then
realIP = clientIP
Exit For
End If
Next
End If
' 2. 回退:X-Real-IP(Nginx 常用)
If realIP = "" Then
realIP = Request.ServerVariables("HTTP_X_REAL_IP")
If realIP <> "" And Not IsPublicIP(realIP) Then realIP = ""
End If
' 3. 回退:CF-Connecting-IP(Cloudflare)
If realIP = "" Then
realIP = Request.ServerVariables("HTTP_CF_CONNECTING_IP")
End If
' 4. 最後回退:REMOTE_ADDR
If realIP = "" Then
realIP = Request.ServerVariables("REMOTE_ADDR")
End If
' 5. IPv6 回環地址轉換
If realIP = "::1" Then realIP = "127.0.0.1"
GetRealIP = realIP
End Function
' 判斷公開 IP(過濾私有/保留範圍)
Function IsPublicIP(ip)
Dim parts, firstOctet, secondOctet
If ip = "" Then IsPublicIP = False : Exit Function
' IPv6 簡易檢查
If InStr(ip, ":") > 0 Then
' 這裡簡化處理,實務建議用正則或專用庫
If ip = "::1" Or Left(ip, 4) = "fe80" Or Left(ip, 7) = "fc00::" Then
IsPublicIP = False
Else
IsPublicIP = True
End If
Exit Function
End If
' IPv4 私有範圍檢查
parts = Split(ip, ".")
If UBound(parts) = 3 Then
firstOctet = CLng(parts(0))
secondOctet = CLng(parts(1))
Select Case firstOctet
Case 10
IsPublicIP = False ' 10.0.0.0/8
Case 172
If secondOctet >= 16 And secondOctet <= 31 Then
IsPublicIP = False ' 172.16.0.0/12
Else
IsPublicIP = True
End If
Case 192
If secondOctet = 168 Then
IsPublicIP = False ' 192.168.0.0/16
Else
IsPublicIP = True
End If
Case 127
IsPublicIP = False ' 回環
Case 169
If secondOctet = 254 Then IsPublicIP = False ' Link-local 169.254.0.0/16
Case 0, 224 To 255
IsPublicIP = False ' 保留/多播
Case Else
IsPublicIP = True
End Select
Else
IsPublicIP = False
End If
End Function
%>
<%
Dim clientIP
clientIP = GetRealIP()
Response.Write "用戶真實 IP:" & clientIP & "
"
' 記錄至資料庫
Dim sql : sql = "INSERT INTO AccessLog (IP, URL, UserAgent, AccessTime) VALUES (?, ?, ?, Now())"
ExecuteNonQuery sql, Array(clientIP, Request.ServerVariables("URL"), Request.ServerVariables("HTTP_USER_AGENT"))
%>
Nginx 反向代理設定:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
IIS ARR (Application Request Routing) 設定:
HTTP_X_FORWARDED_FOR 伺服器變數允許<%
' 風險:用戶可偽造 X-Forwarded-For 標頭
' 防禦:只信任已知代理 IP 範圍內的請求
Function GetTrustedRealIP()
Dim trustedProxies, clientIP, forwardedFor, ips, i
' 受信任代理 IP 範圍(CIDR 或清單)
trustedProxies = Array("10.0.0.0/8", "192.168.1.100", "172.16.0.0/12")
clientIP = Request.ServerVariables("REMOTE_ADDR")
' 只有當直接連線 IP 是受信任代理時,才信任 X-Forwarded-For
If IsTrustedProxy(clientIP, trustedProxies) Then
forwardedFor = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If forwardedFor <> "" Then
ips = Split(forwardedFor, ",")
For i = 0 To UBound(ips)
clientIP = Trim(ips(i))
If IsPublicIP(clientIP) Then Exit For
Next
End If
End If
GetTrustedRealIP = clientIP
End Function
Function IsTrustedProxy(ip, trustedList)
' 實作 CIDR 比對邏輯...
' 簡化:直接清單比對
Dim trusted
For Each trusted In trustedList
If ip = trusted Then IsTrustedProxy = True : Exit Function
Next
IsTrustedProxy = False
End Function
%>
REMOTE_ADDR 可能回傳 IPv6 格式(如 2001:db8::1)VARCHAR(45) 儲存 IPv6<%
Function GetRealIPCached()
Dim cacheKey, cachedIP
cacheKey = "realip_" & Request.ServerVariables("REMOTE_ADDR")
' 簡易 Application 快取(實務建議用 Redis/Memcached)
If Application(cacheKey) <> "" Then
GetRealIPCached = Application(cacheKey)
Exit Function
End If
Dim ip : ip = GetRealIP()
Application.Lock
Application(cacheKey) = ip
Application.UnLock
GetRealIPCached = ip
End Function
%>