address.js 6.5 KB

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