address.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 127.0.0.1, get the first ip
  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. if (matchName(item.family, family) && item.address !== '127.0.0.1') {
  86. return item;
  87. }
  88. }
  89. }
  90. }
  91. return;
  92. };
  93. /**
  94. * Get current machine IPv4
  95. *
  96. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  97. * @return {String} IP address
  98. */
  99. address.ip = function (interfaceName) {
  100. var item = address.interface('IPv4', interfaceName);
  101. return item && item.address;
  102. };
  103. /**
  104. * Get current machine IPv6
  105. *
  106. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  107. * @return {String} IP address
  108. */
  109. address.ipv6 = function (interfaceName) {
  110. var item = address.interface('IPv6', interfaceName);
  111. return item && item.address;
  112. };
  113. // osx start line 'en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500'
  114. // linux start line 'eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 '
  115. var MAC_OSX_START_LINE = /^(\w+)\:\s+flags=/;
  116. var MAC_LINUX_START_LINE = /^(\w+)\s{2,}link encap:\w+/i;
  117. // ether 78:ca:39:b0:e6:7d
  118. // HWaddr 00:16:3E:00:0A:29
  119. var MAC_RE = address.MAC_RE = /(?:ether|HWaddr)\s+((?:[a-z0-9]{2}\:){5}[a-z0-9]{2})/i;
  120. // osx: inet 192.168.2.104 netmask 0xffffff00 broadcast 192.168.2.255
  121. // linux: inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0
  122. var MAC_IP_RE = address.MAC_IP_RE = /inet\s(?:addr\:)?(\d+\.\d+\.\d+\.\d+)/;
  123. function getMAC(content, interfaceName, matchIP) {
  124. var lines = content.split('\n');
  125. for (var i = 0; i < lines.length; i++) {
  126. var line = lines[i].trimRight();
  127. var m = MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line);
  128. if (!m) {
  129. continue;
  130. }
  131. // check interface name
  132. var name = m[1];
  133. if (name.indexOf(interfaceName) !== 0) {
  134. continue;
  135. }
  136. var ip = null;
  137. var mac = null;
  138. var match = MAC_RE.exec(line);
  139. if (match) {
  140. mac = match[1];
  141. }
  142. i++;
  143. while (true) {
  144. line = lines[i];
  145. if (!line || MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line)) {
  146. i--;
  147. break; // hit next interface, handle next interface
  148. }
  149. if (!mac) {
  150. match = MAC_RE.exec(line);
  151. if (match) {
  152. mac = match[1];
  153. }
  154. }
  155. if (!ip) {
  156. match = MAC_IP_RE.exec(line);
  157. if (match) {
  158. ip = match[1];
  159. }
  160. }
  161. i++;
  162. }
  163. if (ip === matchIP) {
  164. return mac;
  165. }
  166. }
  167. }
  168. /**
  169. * Get current machine MAC address
  170. *
  171. * @param {String} [interfaceName] interface name, default is 'eth' on linux, 'en' on mac os.
  172. * @param {Function(err, address)} callback
  173. */
  174. address.mac = function (interfaceName, callback) {
  175. if (typeof interfaceName === 'function') {
  176. callback = interfaceName;
  177. interfaceName = null;
  178. }
  179. interfaceName = interfaceName || getInterfaceName();
  180. var item = address.interface('IPv4', interfaceName);
  181. if (!item) {
  182. return callback();
  183. }
  184. // https://github.com/nodejs/node/issues/13581
  185. // bug in node 7.x and <= 8.4.0
  186. if (!process.env.CI && (item.mac === 'ff:00:00:00:00:00' || item.mac === '00:00:00:00:00:00')) {
  187. // wrong address, ignore it
  188. item.mac = '';
  189. }
  190. if (item.mac) {
  191. return callback(null, item.mac);
  192. }
  193. child.exec(getIfconfigCMD(), {timeout: 5000}, function (err, stdout, stderr) {
  194. if (err || !stdout) {
  195. return callback(err);
  196. }
  197. var mac = getMAC(stdout || '', interfaceName, item.address);
  198. callback(null, mac);
  199. });
  200. };
  201. // nameserver 172.24.102.254
  202. var DNS_SERVER_RE = /^nameserver\s+(\d+\.\d+\.\d+\.\d+)$/i;
  203. /**
  204. * Get DNS servers.
  205. *
  206. * @param {String} [filepath] resolv config file path. default is '/etc/resolv.conf'.
  207. * @param {Function(err, servers)} callback
  208. */
  209. address.dns = function (filepath, callback) {
  210. if (typeof filepath === 'function') {
  211. callback = filepath;
  212. filepath = null;
  213. }
  214. filepath = filepath || DEFAULT_RESOLV_FILE;
  215. fs.readFile(filepath, 'utf8', function (err, content) {
  216. if (err) {
  217. return callback(err);
  218. }
  219. var servers = [];
  220. content = content || '';
  221. var lines = content.split('\n');
  222. for (var i = 0; i < lines.length; i++) {
  223. var line = lines[i].trim();
  224. var m = DNS_SERVER_RE.exec(line);
  225. if (m) {
  226. servers.push(m[1]);
  227. }
  228. }
  229. callback(null, servers);
  230. });
  231. };
  232. module.exports = address;