get subdomains() { const offset = this.app.subdomainOffset; const hostname = this.hostname; if (net.isIP(hostname)) return []; return hostname .split('.') .reverse() .slice(offset); }
const parse = stdout => { let result; (stdout || "").trim().split("\n").some(line => { const [_, gateway, iface] = /default via (.+?) dev (.+?)( |$)/.exec(line) || []; if (gateway && isIP(gateway)) { result = {gateway, interface: (iface ? iface : null)}; return true; } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
server.listen(() => { jstp.net.connect('app', null, server.address().port, (err, connection) => { test.assertNot(err, 'must connect successfully'); test.assert( net.isIP(connection.remoteAddress), 'remoteAddress must be an IP address' ); connection.close(); server.close(); }); });
const getAddress = async hostname => net.isIP(hostname) ? hostname : (await dnsLookupP(hostname)).address
// Parsing tables like this. The final metric is GatewayCostMetric + IPConnectionMetric // // DefaultIPGateway GatewayCostMetric Index IPConnectionMetric // {"1.2.3.4", "2001:db8::1"} {0, 256} 12 25 // {"2.3.4.5"} {25} 12 55 function parseGwTable(gwTable, family) { let [bestGw, bestMetric, bestId] = [null, null, null]; for (let line of (gwTable || "").trim().split(/\r?\n/).splice(1)) { line = line.trim(); const [_, gwArr, gwCostsArr, id, ipMetric] = /({.+?}) +?({.+?}) +?([0-9]+) +?([0-9]+)/g.exec(line) || []; if (!gwArr) continue; const gateways = (gwArr.match(/"(.+?)"/g) || []).map(match => match.substring(1, match.length - 1)); const gatewayCosts = (gwCostsArr.match(/[0-9]+/g) || []); for (const [index, gateway] of Object.entries(gateways)) { if (!gateway || `v${isIP(gateway)}` !== family) continue; const metric = parseInt(gatewayCosts[index]) + parseInt(ipMetric); if (!bestGw || metric < bestMetric) { [bestGw, bestMetric, bestId] = [gateway, metric, id]; } } } if (bestGw) return [bestGw, bestId]; }
/** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain of * the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting "subdomain offset". * * For example, if the domain is "tobi.ferrets.example.com": * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`. * If "subdomain offset" is 3, req.subdomains is `["tobi"]`. * * @return {Array} * @public */ defineGetter(req, 'subdomains', function subdomains() { var hostname = this.hostname; if (!hostname) return []; var offset = this.app.get('subdomain offset'); var subdomains = !isIP(hostname) ? hostname.split('.').reverse() : [hostname]; return subdomains.slice(offset); });
const parse = stdout => { let result; (stdout || "").trim().split("\n").some(line => { const [target, gateway, _, iface] = line.split(/ +/) || []; if (dests.has(target) && gateway && isIP(gateway)) { result = {gateway, interface: (iface ? iface : null)}; return true; } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
const parse = (stdout, family) => { let result; (stdout || "").trim().split("\n").some(line => { const results = /default( via .+?)?( dev .+?)( |$)/.exec(line) || []; const gateway = (results[1] || "").substring(5); const iface = (results[2] || "").substring(5); if (gateway && isIP(gateway)) { // default via 1.2.3.4 dev en0 result = {gateway, interface: (iface ? iface : null)}; return true; } else if (iface && !gateway) { // default via dev en0 const interfaces = networkInterfaces(); const addresses = interfaces[iface]; if (!addresses || !addresses.length) return; addresses.some(addr => { if (addr.family.substring(2) === family && isIP(addr.address)) { result = {gateway: addr.address, interface: (iface ? iface : null)}; return true; } }); } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
/** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain of * the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting "subdomain offset". * * For example, if the domain is "tobi.ferrets.example.com": * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`. * If "subdomain offset" is 3, req.subdomains is `["tobi"]`. * * @return {Array} * @api public */ defineGetter(MockExpressRequest.prototype, 'subdomains', function subdomains() { var hostname = this.hostname; if (!hostname) { return []; } var offset = this.app['subdomain offset']; var _subdomains = !isIP(hostname) ? hostname.split('.').reverse() : [hostname]; return _subdomains.slice(offset); });
const parse = stdout => { let result; (stdout || "").trim().split("\n").some(line => { const results = line.split(/ +/) || []; const target = results[0]; const gateway = results[1]; const iface = results[5]; if (dests.has(target) && gateway && isIP(gateway)) { result = {gateway, interface: (iface ? iface : null)}; return true; } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
const parse = stdout => { let result; (stdout || "").trim().split("\n").some(line => { const results = line.split(/ +/) || []; const target = results[0]; const gateway = results[1]; const iface = results[7]; if (dests.has(target) && gateway && isIP(gateway)) { result = {gateway, interface: (iface ? iface : null)}; return true; } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
const parse = (stdout, family) => { let result; (stdout || "").trim().split("\n").some(line => { const results = line.split(/ +/) || []; const target = results[0]; const gateway = results[1]; const iface = results[family === "v4" ? v4IfaceColumn : 3]; if (dests.has(target) && gateway && isIP(gateway)) { result = {gateway, interface: (iface ? iface : null)}; return true; } }); if (!result) { throw new Error("Unable to determine default gateway"); } return result; }
/** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain * of the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting `app.subdomainOffset`. * * For example, if the domain is "tobi.ferrets.example.com": * If `app.subdomainOffset` is not set, this.subdomains is * `["ferrets", "tobi"]`. * If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`. * * @return {Array} * @api public */ get subdomains() { const offset = this.app.subdomainOffset; const hostname = this.hostname; if (net.isIP(hostname)) return []; return hostname .split('.') .reverse() .slice(offset); }
/** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain * of the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting `app.subdomainOffset`. * * For example, if the domain is "tobi.ferrets.example.com": * If `app.subdomainOffset` is not set, this.subdomains is * `["ferrets", "tobi"]`. * If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`. * * @return {Array} * @api public */ get subdomains() { const offset = this.app.subdomainOffset; const hostname = this.hostname; if (net.isIP(hostname)) return []; return hostname .split('.') .reverse() .slice(offset); }
/** * Return subdomains as an array. * * Subdomains are the dot-separated parts of the host before the main domain of * the app. By default, the domain of the app is assumed to be the last two * parts of the host. This can be changed by setting "subdomain offset". * * For example, if the domain is "tobi.ferrets.example.com": * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`. * If "subdomain offset" is 3, req.subdomains is `["tobi"]`. * * @return {Array} * @public */ defineGetter(req, 'subdomains', function subdomains() { var hostname = this.hostname; if (!hostname) return []; var offset = this.app.get('subdomain offset'); var subdomains = !isIP(hostname) ? hostname.split('.').reverse() : [hostname]; return subdomains.slice(offset); });