networkService.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const os = require('os');
  2. const { exec } = require('child_process');
  3. const path = require('path');
  4. const constants = require('./constants');
  5. const fs = require('fs');
  6. // 判断是否为物理网卡的函数
  7. function isPhysicalNetworkInterface(name, description) {
  8. // 默认的真实物理网卡名称(如 "以太网")
  9. const defaultPhysicalNames = ["以太网", "eth0", "en0"]; // 可以根据需要扩展
  10. // 如果名称匹配默认的真实物理网卡,直接返回 true
  11. if (defaultPhysicalNames.includes(name)) {
  12. return true;
  13. }
  14. // 排除虚拟网卡的关键词
  15. const virtualKeywords = [
  16. "virtual", "hyper-v", "virtualbox", "vmware", "veth", "virbr", "tunnel",
  17. "docker", "loopback", "vpn", "ppp", "slip", "bridge", "vnic", "vif",
  18. "tap", "vlan", "vswitch", "vxlan", "gre", "ipsec", "vrf", "vport",
  19. "vnet", "vmac", "vxnet"
  20. ];
  21. // 检查网卡描述或名称中是否包含虚拟关键词
  22. const lowerName = name.toLowerCase();
  23. const lowerDescription = (description || '').toLowerCase();
  24. for (const keyword of virtualKeywords) {
  25. if (lowerName.includes(keyword) || lowerDescription.includes(keyword)) {
  26. return false; // 是虚拟网卡
  27. }
  28. }
  29. // 默认认为是物理网卡
  30. return true;
  31. }
  32. // 获取所有网络接口信息
  33. function getNetworkInterfaces() {
  34. // 执行 wmic 命令来获取网卡描述信息
  35. exec('wmic nic get Name,Description,NetConnectionID', (error, stdout, stderr) => {
  36. if (error) {
  37. console.error(`执行命令时出错: ${error.message}`);
  38. return;
  39. }
  40. if (stderr) {
  41. console.error(`命令执行过程中出现错误: ${stderr}`);
  42. return;
  43. }
  44. // 解析命令输出
  45. const lines = stdout.trim().split('\n');
  46. const headers = lines[0].trim().split(/\s+/);
  47. const devices = [];
  48. for (let i = 1; i < lines.length; i++) {
  49. const values = lines[i].trim().split(/\s{2,}/);
  50. if (values.length === headers.length) {
  51. const device = {};
  52. for (let j = 0; j < headers.length; j++) {
  53. device[headers[j]] = values[j];
  54. }
  55. devices.push(device);
  56. }
  57. }
  58. // 获取网络接口信息
  59. const networkInterfaces = os.networkInterfaces();
  60. // 为每个网卡添加 IPv4 地址信息
  61. devices.forEach((device) => {
  62. const netInterface = networkInterfaces[device.NetConnectionID];
  63. if (netInterface) {
  64. const ipv4Info = netInterface.find((info) => info.family === 'IPv4');
  65. if (ipv4Info) {
  66. device.ipv4 = ipv4Info.address;
  67. } else {
  68. device.ipv4 = '未找到 IPv4 地址';
  69. }
  70. } else {
  71. device.ipv4 = '未找到对应的网络接口';
  72. }
  73. });
  74. });
  75. const interfaces = os.networkInterfaces();
  76. //console.log(interfaces);
  77. const result = [];
  78. // 遍历所有网络接口
  79. for (const [name, details] of Object.entries(interfaces)) {
  80. for (const detail of details) {
  81. if (detail.family === 'IPv4' && !detail.internal) {
  82. const isPhysical = isPhysicalNetworkInterface(name, detail.mac);
  83. const networkInfo = {
  84. name: name,
  85. mac: detail.mac,
  86. ip: detail.address,
  87. physical: isPhysical ? 1 : 0
  88. };
  89. result.push(networkInfo);
  90. }
  91. }
  92. }
  93. // 将 physical: 1 的网卡排在前面
  94. result.sort((a, b) => b.physical - a.physical);
  95. // 读取文件内容
  96. let filePath = path.join(constants.serverPath, 'certificate.bat');
  97. filePath == `"${filePath}"`;
  98. //console.log("filePath:", filePath);
  99. fs.readFile(filePath, 'utf8', (err, data) => {
  100. if (err) {
  101. console.error('读取文件失败:', err);
  102. return;
  103. }
  104. // 替换内容
  105. const oldContent = '192.168.8.150';
  106. const newContent = result[0].ip;
  107. const updatedData = data.replace(new RegExp(oldContent, 'g'), newContent);
  108. //console.log("新的文件内容", updatedData);
  109. // 将更新后的内容写回文件
  110. fs.writeFile(filePath, updatedData, 'utf8', (err) => {
  111. if (err) {
  112. console.error('写入文件失败:', err);
  113. return;
  114. }
  115. console.log('文件内容已成功替换!');
  116. });
  117. });
  118. // 输出网卡描述信息和 IPv4 地址
  119. //console.log('网卡描述信息和 IPv4 地址:', result);
  120. result.forEach((device) => {
  121. console.log(`名称: ${device.name}, 是否物理网卡: ${device.physical},mac地址:${device.mac}, IPv4 地址: ${device.ip}`);
  122. });
  123. return result;
  124. }
  125. // 导出模块
  126. module.exports = {
  127. getNetworkInterfaces
  128. };