Files
tl-rtc-file/utils/request.js
tangshimin ca98ccca4e tl-rtc-file
2021-08-05 16:26:20 +08:00

65 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const os = require('os');
function getLocalIP() {
const osType = os.type(); //系统类型
const netInfo = os.networkInterfaces(); //网络信息
let ip = '';
if (osType === 'Windows_NT') {
for (let dev in netInfo) {
//win7的网络信息中显示为本地连接win10显示为以太网
if (dev === '本地连接' || dev === '以太网') {
for (let j = 0; j < netInfo[dev].length; j++) {
if (netInfo[dev][j].family === 'IPv4') {
ip = netInfo[dev][j].address;
break;
}
}
}
}
} else if (osType === 'Linux') {
for (var dev in netInfo) {    
var iface = netInfo[dev];      
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}  
}
}
return ip;
}
function getClientIP(request){
var ip = request.headers['x-forwarded-for'] ||
request.ip ||
request.connection.remoteAddress ||
request.socket.remoteAddress ||
request.connection.socket.remoteAddress;
if(ip.split(',').length > 0){
ip = ip.split(',')[0]
}
ip = ip.substr(ip.lastIndexOf(':')+1,ip.length);
return ip;
}
function genFlow(req) {
return num = Math.floor(Math.random(100000000)*100000000+1);
}
function genRoom(req) {
return num = Math.floor(Math.random(100000000)*100000000+1);
}
module.exports = {
getLocalIP,
getClientIP,
genFlow,
genRoom
}