kefu/utils/common.js

31 lines
889 B
JavaScript

var http = require('http');
var libqqwry = require('lib-qqwry');
var qqwry = libqqwry() //初始化IP库解析器
qqwry.speed(); //启用急速模式;
function getClientIp(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
};
function getIpLocation(ip, callback) {
return setTimeout(() => {
try {
let data = qqwry.searchIP(ip)
if (!data) {
callback(new Error('get ip exception'), ip);
} else {
let area = data.Area.trim().toLowerCase().replace('cz88.net', '');
callback(false, data.Country + ' ' + area);
}
} catch (e) {
callback(e, ip);
}
}, 10);
}
exports.getClientIp = getClientIp;
exports.getIpLocation = getIpLocation;