Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. 'use strict';
  2. const {
  3. V4MAPPED,
  4. ADDRCONFIG,
  5. ALL,
  6. promises: {
  7. Resolver: AsyncResolver
  8. },
  9. lookup: dnsLookup
  10. } = require('dns');
  11. const {promisify} = require('util');
  12. const os = require('os');
  13. const kCacheableLookupCreateConnection = Symbol('cacheableLookupCreateConnection');
  14. const kCacheableLookupInstance = Symbol('cacheableLookupInstance');
  15. const kExpires = Symbol('expires');
  16. const supportsALL = typeof ALL === 'number';
  17. const verifyAgent = agent => {
  18. if (!(agent && typeof agent.createConnection === 'function')) {
  19. throw new Error('Expected an Agent instance as the first argument');
  20. }
  21. };
  22. const map4to6 = entries => {
  23. for (const entry of entries) {
  24. if (entry.family === 6) {
  25. continue;
  26. }
  27. entry.address = `::ffff:${entry.address}`;
  28. entry.family = 6;
  29. }
  30. };
  31. const getIfaceInfo = () => {
  32. let has4 = false;
  33. let has6 = false;
  34. for (const device of Object.values(os.networkInterfaces())) {
  35. for (const iface of device) {
  36. if (iface.internal) {
  37. continue;
  38. }
  39. if (iface.family === 'IPv6') {
  40. has6 = true;
  41. } else {
  42. has4 = true;
  43. }
  44. if (has4 && has6) {
  45. return {has4, has6};
  46. }
  47. }
  48. }
  49. return {has4, has6};
  50. };
  51. const isIterable = map => {
  52. return Symbol.iterator in map;
  53. };
  54. const ttl = {ttl: true};
  55. const all = {all: true};
  56. class CacheableLookup {
  57. constructor({
  58. cache = new Map(),
  59. maxTtl = Infinity,
  60. fallbackDuration = 3600,
  61. errorTtl = 0.15,
  62. resolver = new AsyncResolver(),
  63. lookup = dnsLookup
  64. } = {}) {
  65. this.maxTtl = maxTtl;
  66. this.errorTtl = errorTtl;
  67. this._cache = cache;
  68. this._resolver = resolver;
  69. this._dnsLookup = promisify(lookup);
  70. if (this._resolver instanceof AsyncResolver) {
  71. this._resolve4 = this._resolver.resolve4.bind(this._resolver);
  72. this._resolve6 = this._resolver.resolve6.bind(this._resolver);
  73. } else {
  74. this._resolve4 = promisify(this._resolver.resolve4.bind(this._resolver));
  75. this._resolve6 = promisify(this._resolver.resolve6.bind(this._resolver));
  76. }
  77. this._iface = getIfaceInfo();
  78. this._pending = {};
  79. this._nextRemovalTime = false;
  80. this._hostnamesToFallback = new Set();
  81. if (fallbackDuration < 1) {
  82. this._fallback = false;
  83. } else {
  84. this._fallback = true;
  85. const interval = setInterval(() => {
  86. this._hostnamesToFallback.clear();
  87. }, fallbackDuration * 1000);
  88. /* istanbul ignore next: There is no `interval.unref()` when running inside an Electron renderer */
  89. if (interval.unref) {
  90. interval.unref();
  91. }
  92. }
  93. this.lookup = this.lookup.bind(this);
  94. this.lookupAsync = this.lookupAsync.bind(this);
  95. }
  96. set servers(servers) {
  97. this.clear();
  98. this._resolver.setServers(servers);
  99. }
  100. get servers() {
  101. return this._resolver.getServers();
  102. }
  103. lookup(hostname, options, callback) {
  104. if (typeof options === 'function') {
  105. callback = options;
  106. options = {};
  107. } else if (typeof options === 'number') {
  108. options = {
  109. family: options
  110. };
  111. }
  112. if (!callback) {
  113. throw new Error('Callback must be a function.');
  114. }
  115. // eslint-disable-next-line promise/prefer-await-to-then
  116. this.lookupAsync(hostname, options).then(result => {
  117. if (options.all) {
  118. callback(null, result);
  119. } else {
  120. callback(null, result.address, result.family, result.expires, result.ttl);
  121. }
  122. }, callback);
  123. }
  124. async lookupAsync(hostname, options = {}) {
  125. if (typeof options === 'number') {
  126. options = {
  127. family: options
  128. };
  129. }
  130. let cached = await this.query(hostname);
  131. if (options.family === 6) {
  132. const filtered = cached.filter(entry => entry.family === 6);
  133. if (options.hints & V4MAPPED) {
  134. if ((supportsALL && options.hints & ALL) || filtered.length === 0) {
  135. map4to6(cached);
  136. } else {
  137. cached = filtered;
  138. }
  139. } else {
  140. cached = filtered;
  141. }
  142. } else if (options.family === 4) {
  143. cached = cached.filter(entry => entry.family === 4);
  144. }
  145. if (options.hints & ADDRCONFIG) {
  146. const {_iface} = this;
  147. cached = cached.filter(entry => entry.family === 6 ? _iface.has6 : _iface.has4);
  148. }
  149. if (cached.length === 0) {
  150. const error = new Error(`cacheableLookup ENOTFOUND ${hostname}`);
  151. error.code = 'ENOTFOUND';
  152. error.hostname = hostname;
  153. throw error;
  154. }
  155. if (options.all) {
  156. return cached;
  157. }
  158. return cached[0];
  159. }
  160. async query(hostname) {
  161. let cached = await this._cache.get(hostname);
  162. if (!cached) {
  163. const pending = this._pending[hostname];
  164. if (pending) {
  165. cached = await pending;
  166. } else {
  167. const newPromise = this.queryAndCache(hostname);
  168. this._pending[hostname] = newPromise;
  169. try {
  170. cached = await newPromise;
  171. } finally {
  172. delete this._pending[hostname];
  173. }
  174. }
  175. }
  176. cached = cached.map(entry => {
  177. return {...entry};
  178. });
  179. return cached;
  180. }
  181. async _resolve(hostname) {
  182. const wrap = async promise => {
  183. try {
  184. return await promise;
  185. } catch (error) {
  186. if (error.code === 'ENODATA' || error.code === 'ENOTFOUND') {
  187. return [];
  188. }
  189. throw error;
  190. }
  191. };
  192. // ANY is unsafe as it doesn't trigger new queries in the underlying server.
  193. const [A, AAAA] = await Promise.all([
  194. this._resolve4(hostname, ttl),
  195. this._resolve6(hostname, ttl)
  196. ].map(promise => wrap(promise)));
  197. let aTtl = 0;
  198. let aaaaTtl = 0;
  199. let cacheTtl = 0;
  200. const now = Date.now();
  201. for (const entry of A) {
  202. entry.family = 4;
  203. entry.expires = now + (entry.ttl * 1000);
  204. aTtl = Math.max(aTtl, entry.ttl);
  205. }
  206. for (const entry of AAAA) {
  207. entry.family = 6;
  208. entry.expires = now + (entry.ttl * 1000);
  209. aaaaTtl = Math.max(aaaaTtl, entry.ttl);
  210. }
  211. if (A.length > 0) {
  212. if (AAAA.length > 0) {
  213. cacheTtl = Math.min(aTtl, aaaaTtl);
  214. } else {
  215. cacheTtl = aTtl;
  216. }
  217. } else {
  218. cacheTtl = aaaaTtl;
  219. }
  220. return {
  221. entries: [
  222. ...A,
  223. ...AAAA
  224. ],
  225. cacheTtl
  226. };
  227. }
  228. async _lookup(hostname) {
  229. try {
  230. const entries = await this._dnsLookup(hostname, {
  231. all: true
  232. });
  233. return {
  234. entries,
  235. cacheTtl: 0
  236. };
  237. } catch (_) {
  238. return {
  239. entries: [],
  240. cacheTtl: 0
  241. };
  242. }
  243. }
  244. async _set(hostname, data, cacheTtl) {
  245. if (this.maxTtl > 0 && cacheTtl > 0) {
  246. cacheTtl = Math.min(cacheTtl, this.maxTtl) * 1000;
  247. data[kExpires] = Date.now() + cacheTtl;
  248. try {
  249. await this._cache.set(hostname, data, cacheTtl);
  250. } catch (error) {
  251. this.lookupAsync = async () => {
  252. const cacheError = new Error('Cache Error. Please recreate the CacheableLookup instance.');
  253. cacheError.cause = error;
  254. throw cacheError;
  255. };
  256. }
  257. if (isIterable(this._cache)) {
  258. this._tick(cacheTtl);
  259. }
  260. }
  261. }
  262. async queryAndCache(hostname) {
  263. if (this._hostnamesToFallback.has(hostname)) {
  264. return this._dnsLookup(hostname, all);
  265. }
  266. let query = await this._resolve(hostname);
  267. if (query.entries.length === 0 && this._fallback) {
  268. query = await this._lookup(hostname);
  269. if (query.entries.length !== 0) {
  270. // Use `dns.lookup(...)` for that particular hostname
  271. this._hostnamesToFallback.add(hostname);
  272. }
  273. }
  274. const cacheTtl = query.entries.length === 0 ? this.errorTtl : query.cacheTtl;
  275. await this._set(hostname, query.entries, cacheTtl);
  276. return query.entries;
  277. }
  278. _tick(ms) {
  279. const nextRemovalTime = this._nextRemovalTime;
  280. if (!nextRemovalTime || ms < nextRemovalTime) {
  281. clearTimeout(this._removalTimeout);
  282. this._nextRemovalTime = ms;
  283. this._removalTimeout = setTimeout(() => {
  284. this._nextRemovalTime = false;
  285. let nextExpiry = Infinity;
  286. const now = Date.now();
  287. for (const [hostname, entries] of this._cache) {
  288. const expires = entries[kExpires];
  289. if (now >= expires) {
  290. this._cache.delete(hostname);
  291. } else if (expires < nextExpiry) {
  292. nextExpiry = expires;
  293. }
  294. }
  295. if (nextExpiry !== Infinity) {
  296. this._tick(nextExpiry - now);
  297. }
  298. }, ms);
  299. /* istanbul ignore next: There is no `timeout.unref()` when running inside an Electron renderer */
  300. if (this._removalTimeout.unref) {
  301. this._removalTimeout.unref();
  302. }
  303. }
  304. }
  305. install(agent) {
  306. verifyAgent(agent);
  307. if (kCacheableLookupCreateConnection in agent) {
  308. throw new Error('CacheableLookup has been already installed');
  309. }
  310. agent[kCacheableLookupCreateConnection] = agent.createConnection;
  311. agent[kCacheableLookupInstance] = this;
  312. agent.createConnection = (options, callback) => {
  313. if (!('lookup' in options)) {
  314. options.lookup = this.lookup;
  315. }
  316. return agent[kCacheableLookupCreateConnection](options, callback);
  317. };
  318. }
  319. uninstall(agent) {
  320. verifyAgent(agent);
  321. if (agent[kCacheableLookupCreateConnection]) {
  322. if (agent[kCacheableLookupInstance] !== this) {
  323. throw new Error('The agent is not owned by this CacheableLookup instance');
  324. }
  325. agent.createConnection = agent[kCacheableLookupCreateConnection];
  326. delete agent[kCacheableLookupCreateConnection];
  327. delete agent[kCacheableLookupInstance];
  328. }
  329. }
  330. updateInterfaceInfo() {
  331. const {_iface} = this;
  332. this._iface = getIfaceInfo();
  333. if ((_iface.has4 && !this._iface.has4) || (_iface.has6 && !this._iface.has6)) {
  334. this._cache.clear();
  335. }
  336. }
  337. clear(hostname) {
  338. if (hostname) {
  339. this._cache.delete(hostname);
  340. return;
  341. }
  342. this._cache.clear();
  343. }
  344. }
  345. module.exports = CacheableLookup;
  346. module.exports.default = CacheableLookup;