使用云服务空间在uni-app云函数云对象中调用uniCloud.getClientInfos this.getClientInfo()无法获取真实IP IP防刷无法正常工作

使用云服务空间在uni-app云函数云对象中调用uniCloud.getClientInfos this.getClientInfo()无法获取真实IP IP防刷无法正常工作

示例代码:

async logIp() {  
    const res = this.getClientInfo();  
    let {clientIP} = this.getClientInfo();  
    console.log(res)  
    if(!clientIP) return;  
    const redis = uniCloud.redis();  
    const key = 'iplog:all';  
    const now = Date.now();  

    let data = await redis.hget(key, clientIP);  
    if (data) {  
        try {  
            data = JSON.parse(data);  
        } catch {  
            data = null;  
        }  
    }  
    if (data) {  
        data.count += 1;  
        data.lastVisit = now;  
    } else {  
        data = {  
            count: 1,  
            firstVisit: now,  
            lastVisit: now  
        };  
    }  
    await redis.hset(key, clientIP, JSON.stringify(data));  
    // 可选:设置总key过期时间(如30天),每次访问都会刷新  
    await redis.expire(key, 60 * 60 * 24 * 30);  
}

操作步骤:

任何云函数,云代码调用相关方法,查看日志

预期结果:

返回的是客户端的真实IP

实际结果:

返回的是阿里云的IP,如下面的日志

{
  "args": {
    "method": "logIp",
    "params": []
  },
  "requestId": "ac1cd3981751441461947-92082"
}
[ipAccessLogger/ac1cd3981751441461947-92082/3ms/DEBUG] {
  secretType: 'none',
  asyncRequestId: '',
  RUNTIME_ENV: 'cloud',
  clientIP: '47.92.130.105',
  userAgent: 'node-urllib/2.41.0 Node.js/16.15.1 (Linux 4.19; x64)',
  source: 'function',
  requestId: 'ac1cd3981751441461947-92082'
}
请求响应状态: success

bug描述:

使用阿里云服务空间,在任何云函数,云对象调用

  • uniCloud.getClientInfos
  • this.getClientInfo()

返回的客户端IP均是阿里云服务器的IP,而非客户端真实的IP。导致拿不到客户端真实的IP,即使启用了Redis和IP防刷,设置不了黑名单IP,也没有任何意义


更多关于使用云服务空间在uni-app云函数云对象中调用uniCloud.getClientInfos this.getClientInfo()无法获取真实IP IP防刷无法正常工作的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

非bug,如果在云函数中调用云函数,IP就是云厂商的,但如果是前端调用云函数,ip就是用户的

更多关于使用云服务空间在uni-app云函数云对象中调用uniCloud.getClientInfos this.getClientInfo()无法获取真实IP IP防刷无法正常工作的实战教程也可以访问 https://www.itying.com/category-93-b0.html


意思是这些api实际记录的实际请求者的信息,所以在服务端调用时,记录的就是云厂商的。感谢答复

在uni-app的阿里云服务空间中,getClientInfo()uniCloud.getClientInfos()返回的是云函数运行环境的服务器IP,而非客户端真实IP,这是阿里云服务空间的正常行为。

要获取客户端真实IP,需要通过HTTP请求头中的x-forwarded-for字段获取。在云函数中修改代码如下:

async logIp() {
    const clientInfo = this.getClientInfo();
    // 从请求头获取真实IP
    const realIP = clientInfo.headers['x-forwarded-for'] || 
                   clientInfo.headers['x-real-ip'] || 
                   clientInfo.clientIP;
    
    // 处理x-forwarded-for可能包含多个IP的情况
    const clientIP = realIP ? realIP.split(',')[0].trim() : clientInfo.clientIP;
    
    console.log('客户端真实IP:', clientIP);
    
    if(!clientIP) return;
    
    const redis = uniCloud.redis();
    const key = 'iplog:all';
    const now = Date.now();

    let data = await redis.hget(key, clientIP);
    if (data) {
        try {
            data = JSON.parse(data);
        } catch {
            data = null;
        }
    }
    if (data) {
        data.count += 1;
        data.lastVisit = now;
    } else {
        data = {
            count: 1,
            firstVisit: now,
            lastVisit: now
        };
    }
    await redis.hset(key, clientIP, JSON.stringify(data));
    await redis.expire(key, 60 * 60 * 24 * 30);
}
回到顶部