const os = require('os')
const ip = require('ip')
// pick the first reasonable looking host.
// this should *just work* when running on a vps.

const isPrivate = ip.isPrivate

function isNonPrivate(e) {
  return !isPrivate(e)
}

function address(inter, filter, details) {
  inter = inter || os.networkInterfaces()
  filter = filter || isNonPrivate
  let score = 0
  let candidate = undefined
  for (const k of Object.keys(inter)) {
    for (const e of inter[k]) {
      e.interface = k
      // Must not be loopback:
      if (e.internal) continue
      // Must pass our filter:
      if (!filter(e.address, e)) continue

      // Prioritize IPv4 wlan:
      if (k.startsWith('wl') && e.family === 'IPv4' && score < 8) {
        score = 8
        candidate = details ? e : e.address
      }
      // Prioritize IPv4 ethernet:
      else if (k.startsWith('en') && e.family === 'IPv4' && score < 7) {
        score = 7
        candidate = details ? e : e.address
      }
      // Prioritize IPv4 OLD ethernet:
      else if (k.startsWith('eth') && e.family === 'IPv4' && score < 6) {
        score = 6
        candidate = details ? e : e.address
      }
      // Prioritize wlan:
      else if (k.startsWith('wl') && e.family === 'IPv6' && score < 5) {
        score = 5
        candidate = details ? e : e.address + '%' + k
      }
      // Prioritize ethernet:
      else if (k.startsWith('en') && e.family === 'IPv6' && score < 4) {
        score = 4
        candidate = details ? e : e.address + '%' + k
      }
      // Prioritize OLD ethernet:
      else if (k.startsWith('eth') && e.family === 'IPv6' && score < 3) {
        score = 3
        candidate = details ? e : e.address + '%' + k
      }
      // Prioritize IPv4 tunnels (VPN):
      else if (k.startsWith('tun') && e.family === 'IPv4' && score < 2) {
        score = 2
        candidate = details ? e : e.address
      }
      // Prioritize tunnels (VPN):
      else if (k.startsWith('tun') && e.family === 'IPv6' && score < 1) {
        score = 1
        candidate = details ? e : e.address + '%' + k
      }
    }
  }
  return candidate
}

function _private(inter) {
  return address(inter, isPrivate)
}

function isV4(e) {
  return e.family === 'IPv4'
}

function isV6(e) {
  return e.family === 'IPv6'
}

// Functions
module.exports = address
module.exports.private = _private

// Values
module.exports.v4 = address(null, (adr, e) => isV4(e) && isNonPrivate(adr))
module.exports.v6 = address(null, (adr, e) => isV6(e) && isNonPrivate(adr))
module.exports.private.v4 = address(null, (adr, e) => isV4(e) && isPrivate(adr))
module.exports.private.v6 = address(null, (adr, e) => isV6(e) && isPrivate(adr))
module.exports.all = {
  public: {
    v4: module.exports.v4,
    v6: module.exports.v6,
  },
  private: {
    v4: module.exports.private.v4,
    v6: module.exports.private.v6,
  },
}

@@ -385,16 +385,258 @@
 defined%0A
+  function setCandidate(e) %7B%0A    if (details) %7B%0A      candidate = e%0A    %7D else %7B%0A      if (e.family === 'IPv6') %7B%0A        candidate = e.address + '%25' + (e.scopeid %7C%7C e.interface)%0A      %7D else %7B%0A        candidate = e.address%0A      %7D%0A    %7D%0A  %7D%0A
   for (c
@@ -663,24 +663,24 @@
 s(inter)) %7B%0A
-
     for (con
@@ -978,33 +978,36 @@
 = 8%0A        
-c
+setC
 andidate
  = details ?
@@ -998,34 +998,11 @@
 date
- = details ? e : e.address
+(e)
 %0A   
@@ -1136,33 +1136,36 @@
 = 7%0A        
-c
+setC
 andidate
  = details ?
@@ -1156,34 +1156,11 @@
 date
- = details ? e : e.address
+(e)
 %0A   
@@ -1299,33 +1299,36 @@
 = 6%0A        
-c
+setC
 andidate
  = details ?
@@ -1319,34 +1319,11 @@
 date
- = details ? e : e.address
+(e)
 %0A   
@@ -1448,33 +1448,36 @@
 = 5%0A        
-c
+setC
 andidate
  = details ?
@@ -1468,44 +1468,11 @@
 date
- = details ? e : e.address + '%25' + k
+(e)
 %0A   
@@ -1601,33 +1601,36 @@
 = 4%0A        
-c
+setC
 andidate
  = details ?
@@ -1621,44 +1621,11 @@
 date
- = details ? e : e.address + '%25' + k
+(e)
 %0A   
@@ -1759,33 +1759,36 @@
 = 3%0A        
-c
+setC
 andidate
  = details ?
@@ -1779,44 +1779,11 @@
 date
- = details ? e : e.address + '%25' + k
+(e)
 %0A   
@@ -1923,33 +1923,36 @@
 = 2%0A        
-c
+setC
 andidate
  = details ?
@@ -1943,34 +1943,11 @@
 date
- = details ? e : e.address
+(e)
 %0A   
@@ -2086,25 +2086,28 @@
         
-c
+setC
 andidate
  = detai
@@ -2102,44 +2102,11 @@
 date
- = details ? e : e.address + '%25' + k
+(e)
 %0A   

use IPv6 scopeid if it's available