address.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. var os = require('os');
  3. var fs = require('fs');
  4. var child = require('child_process');
  5. var DEFAULT_RESOLV_FILE = '/etc/resolv.conf';
  6. function getInterfaceName() {
  7. var val = 'eth';
  8. var platform = os.platform();
  9. if (platform === 'darwin') {
  10. val = 'en';
  11. } else if (platform === 'win32') {
  12. val = null;
  13. }
  14. return val;
  15. }
  16. function getIfconfigCMD() {
  17. if (os.platform() === 'win32') {
  18. return 'ipconfig/all';
  19. }
  20. return '/sbin/ifconfig';
  21. }
  22. /**
  23. * Get all addresses.
  24. *
  25. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  26. * @param {Function(err, addr)} callback
  27. * - {Object} addr {
  28. * - {String} ip
  29. * - {String} ipv6
  30. * - {String} mac
  31. * }
  32. */
  33. function address(interfaceName, callback) {
  34. if (typeof interfaceName === 'function') {
  35. callback = interfaceName;
  36. interfaceName = null;
  37. }
  38. var addr = {
  39. ip: address.ip(interfaceName),
  40. ipv6: address.ipv6(interfaceName),
  41. mac: null
  42. };
  43. address.mac(interfaceName, function (err, mac) {
  44. if (mac) {
  45. addr.mac = mac;
  46. }
  47. callback(err, addr);
  48. });
  49. }
  50. address.interface = function (family, name) {
  51. var interfaces = os.networkInterfaces();
  52. var noName = !name;
  53. name = name || getInterfaceName();
  54. family = family || 'IPv4';
  55. for (var i = -1; i < 8; i++) {
  56. var interfaceName = name + (i >= 0 ? i : ''); // support 'lo' and 'lo0'
  57. var items = interfaces[interfaceName];
  58. if (items) {
  59. for (var j = 0; j < items.length; j++) {
  60. var item = items[j];
  61. if (item.family === family) {
  62. return item;
  63. }
  64. }
  65. }
  66. }
  67. if (noName) {
  68. // filter 127.0.0.1, get the first ip
  69. for (var k in interfaces) {
  70. var items = interfaces[k];
  71. for (var i = 0; i < items.length; i++) {
  72. var item = items[i];
  73. if (item.family === family && item.address !== '127.0.0.1') {
  74. return item;
  75. }
  76. }
  77. }
  78. }
  79. return;
  80. };
  81. /**
  82. * Get current machine IPv4
  83. *
  84. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  85. * @return {String} IP address
  86. */
  87. address.ip = function (interfaceName) {
  88. var item = address.interface('IPv4', interfaceName);
  89. return item && item.address;
  90. };
  91. /**
  92. * Get current machine IPv6
  93. *
  94. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  95. * @return {String} IP address
  96. */
  97. address.ipv6 = function (interfaceName) {
  98. var item = address.interface('IPv6', interfaceName);
  99. return item && item.address;
  100. };
  101. // osx start line 'en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500'
  102. // linux start line 'eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 '
  103. var MAC_OSX_START_LINE = /^(\w+)\:\s+flags=/;
  104. var MAC_LINUX_START_LINE = /^(\w+)\s{2,}link encap:\w+/i;
  105. // ether 78:ca:39:b0:e6:7d
  106. // HWaddr 00:16:3E:00:0A:29
  107. var MAC_RE = address.MAC_RE = /(?:ether|HWaddr)\s+((?:[a-z0-9]{2}\:){5}[a-z0-9]{2})/i;
  108. // osx: inet 192.168.2.104 netmask 0xffffff00 broadcast 192.168.2.255
  109. // linux: inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0
  110. var MAC_IP_RE = address.MAC_IP_RE = /inet\s(?:addr\:)?(\d+\.\d+\.\d+\.\d+)/;
  111. function getMAC(content, interfaceName, matchIP) {
  112. var lines = content.split('\n');
  113. for (var i = 0; i < lines.length; i++) {
  114. var line = lines[i].trimRight();
  115. var m = MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line);
  116. if (!m) {
  117. continue;
  118. }
  119. // check interface name
  120. var name = m[1];
  121. if (name.indexOf(interfaceName) !== 0) {
  122. continue;
  123. }
  124. var ip = null;
  125. var mac = null;
  126. var match = MAC_RE.exec(line);
  127. if (match) {
  128. mac = match[1];
  129. }
  130. i++;
  131. while (true) {
  132. line = lines[i];
  133. if (!line || MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line)) {
  134. i--;
  135. break; // hit next interface, handle next interface
  136. }
  137. if (!mac) {
  138. match = MAC_RE.exec(line);
  139. if (match) {
  140. mac = match[1];
  141. }
  142. }
  143. if (!ip) {
  144. match = MAC_IP_RE.exec(line);
  145. if (match) {
  146. ip = match[1];
  147. }
  148. }
  149. i++;
  150. }
  151. if (ip === matchIP) {
  152. return mac;
  153. }
  154. }
  155. }
  156. /**
  157. * Get current machine MAC address
  158. *
  159. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  160. * @param {Function(err, address)} callback
  161. */
  162. address.mac = function (interfaceName, callback) {
  163. if (typeof interfaceName === 'function') {
  164. callback = interfaceName;
  165. interfaceName = null;
  166. }
  167. interfaceName = interfaceName || getInterfaceName();
  168. var item = address.interface('IPv4', interfaceName);
  169. if (!item) {
  170. return callback();
  171. }
  172. // https://github.com/nodejs/node/issues/13581
  173. // bug in node 7.x and <= 8.4.0
  174. if (!process.env.CI && (item.mac === 'ff:00:00:00:00:00' || item.mac === '00:00:00:00:00:00')) {
  175. // wrong address, ignore it
  176. item.mac = '';
  177. }
  178. if (item.mac) {
  179. return callback(null, item.mac);
  180. }
  181. child.exec(getIfconfigCMD(), {timeout: 5000}, function (err, stdout, stderr) {
  182. if (err || !stdout) {
  183. return callback(err);
  184. }
  185. var mac = getMAC(stdout || '', interfaceName, item.address);
  186. callback(null, mac);
  187. });
  188. };
  189. // nameserver 172.24.102.254
  190. var DNS_SERVER_RE = /^nameserver\s+(\d+\.\d+\.\d+\.\d+)$/i;
  191. /**
  192. * Get DNS servers.
  193. *
  194. * @param {String} [filepath] resolv config file path. default is '/etc/resolv.conf'.
  195. * @param {Function(err, servers)} callback
  196. */
  197. address.dns = function (filepath, callback) {
  198. if (typeof filepath === 'function') {
  199. callback = filepath;
  200. filepath = null;
  201. }
  202. filepath = filepath || DEFAULT_RESOLV_FILE;
  203. fs.readFile(filepath, 'utf8', function (err, content) {
  204. if (err) {
  205. return callback(err);
  206. }
  207. var servers = [];
  208. content = content || '';
  209. var lines = content.split('\n');
  210. for (var i = 0; i < lines.length; i++) {
  211. var line = lines[i].trim();
  212. var m = DNS_SERVER_RE.exec(line);
  213. if (m) {
  214. servers.push(m[1]);
  215. }
  216. }
  217. callback(null, servers);
  218. });
  219. };
  220. module.exports = address;