index.js 636 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. var os = require('os');
  3. var type = {
  4. v4: {
  5. def: '127.0.0.1',
  6. family: 'IPv4'
  7. },
  8. v6: {
  9. def: '::1',
  10. family: 'IPv6'
  11. }
  12. };
  13. function internalIp(version) {
  14. var options = type[version];
  15. var ret = options.def;
  16. var interfaces = os.networkInterfaces();
  17. Object.keys(interfaces).forEach(function (el) {
  18. interfaces[el].forEach(function (el2) {
  19. if (!el2.internal && el2.family === options.family) {
  20. ret = el2.address;
  21. }
  22. });
  23. });
  24. return ret;
  25. }
  26. function v4() {
  27. return internalIp('v4');
  28. }
  29. function v6() {
  30. return internalIp('v6');
  31. }
  32. module.exports = v4;
  33. module.exports.v4 = v4;
  34. module.exports.v6 = v6;