diff options
author | Daniel Baumann <daniel@debian.org> | 2024-11-26 09:28:28 +0100 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-11-26 12:25:58 +0100 |
commit | a1882b67c41fe9901a0cd8059b5cc78a5beadec0 (patch) | |
tree | 2a24507c67aa99a15416707b2f7e645142230ed8 /server/modules | |
parent | Initial commit. (diff) | |
download | uptime-kuma-a1882b67c41fe9901a0cd8059b5cc78a5beadec0.tar.xz uptime-kuma-a1882b67c41fe9901a0cd8059b5cc78a5beadec0.zip |
Adding upstream version 2.0.0~beta.0+dfsg.upstream/2.0.0_beta.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'server/modules')
-rw-r--r-- | server/modules/apicache/apicache.js | 917 | ||||
-rw-r--r-- | server/modules/apicache/index.js | 14 | ||||
-rw-r--r-- | server/modules/apicache/memory-cache.js | 87 | ||||
-rw-r--r-- | server/modules/axios-ntlm/LICENSE | 21 | ||||
-rw-r--r-- | server/modules/axios-ntlm/lib/flags.js | 77 | ||||
-rw-r--r-- | server/modules/axios-ntlm/lib/hash.js | 122 | ||||
-rw-r--r-- | server/modules/axios-ntlm/lib/ntlm.js | 220 | ||||
-rw-r--r-- | server/modules/axios-ntlm/lib/ntlmClient.js | 127 | ||||
-rw-r--r-- | server/modules/dayjs/plugin/timezone.d.ts | 20 | ||||
-rw-r--r-- | server/modules/dayjs/plugin/timezone.js | 115 |
10 files changed, 1720 insertions, 0 deletions
diff --git a/server/modules/apicache/apicache.js b/server/modules/apicache/apicache.js new file mode 100644 index 0000000..41930b2 --- /dev/null +++ b/server/modules/apicache/apicache.js @@ -0,0 +1,917 @@ +let url = require("url"); +let MemoryCache = require("./memory-cache"); + +let t = { + ms: 1, + second: 1000, + minute: 60000, + hour: 3600000, + day: 3600000 * 24, + week: 3600000 * 24 * 7, + month: 3600000 * 24 * 30, +}; + +let instances = []; + +/** + * Does a === b + * @param {any} a + * @returns {function(any): boolean} + */ +let matches = function (a) { + return function (b) { + return a === b; + }; +}; + +/** + * Does a!==b + * @param {any} a + * @returns {function(any): boolean} + */ +let doesntMatch = function (a) { + return function (b) { + return !matches(a)(b); + }; +}; + +/** + * Get log duration + * @param {number} d Time in ms + * @param {string} prefix Prefix for log + * @returns {string} Coloured log string + */ +let logDuration = function (d, prefix) { + let str = d > 1000 ? (d / 1000).toFixed(2) + "sec" : d + "ms"; + return "\x1b[33m- " + (prefix ? prefix + " " : "") + str + "\x1b[0m"; +}; + +/** + * Get safe headers + * @param {Object} res Express response object + * @returns {Object} + */ +function getSafeHeaders(res) { + return res.getHeaders ? res.getHeaders() : res._headers; +} + +/** Constructor for ApiCache instance */ +function ApiCache() { + let memCache = new MemoryCache(); + + let globalOptions = { + debug: false, + defaultDuration: 3600000, + enabled: true, + appendKey: [], + jsonp: false, + redisClient: false, + headerBlacklist: [], + statusCodes: { + include: [], + exclude: [], + }, + events: { + expire: undefined, + }, + headers: { + // 'cache-control': 'no-cache' // example of header overwrite + }, + trackPerformance: false, + respectCacheControl: false, + }; + + let middlewareOptions = []; + let instance = this; + let index = null; + let timers = {}; + let performanceArray = []; // for tracking cache hit rate + + instances.push(this); + this.id = instances.length; + + /** + * Logs a message to the console if the `DEBUG` environment variable is set. + * @param {string} a The first argument to log. + * @param {string} b The second argument to log. + * @param {string} c The third argument to log. + * @param {string} d The fourth argument to log, and so on... (optional) + * + * Generated by Trelent + */ + function debug(a, b, c, d) { + let arr = ["\x1b[36m[apicache]\x1b[0m", a, b, c, d].filter(function (arg) { + return arg !== undefined; + }); + let debugEnv = process.env.DEBUG && process.env.DEBUG.split(",").indexOf("apicache") !== -1; + + return (globalOptions.debug || debugEnv) && console.log.apply(null, arr); + } + + /** + * Returns true if the given request and response should be logged. + * @param {Object} request The HTTP request object. + * @param {Object} response The HTTP response object. + * @param {function(Object, Object):boolean} toggle + * @returns {boolean} + */ + function shouldCacheResponse(request, response, toggle) { + let opt = globalOptions; + let codes = opt.statusCodes; + + if (!response) { + return false; + } + + if (toggle && !toggle(request, response)) { + return false; + } + + if (codes.exclude && codes.exclude.length && codes.exclude.indexOf(response.statusCode) !== -1) { + return false; + } + if (codes.include && codes.include.length && codes.include.indexOf(response.statusCode) === -1) { + return false; + } + + return true; + } + + /** + * Add key to index array + * @param {string} key Key to add + * @param {Object} req Express request object + */ + function addIndexEntries(key, req) { + let groupName = req.apicacheGroup; + + if (groupName) { + debug("group detected \"" + groupName + "\""); + let group = (index.groups[groupName] = index.groups[groupName] || []); + group.unshift(key); + } + + index.all.unshift(key); + } + + /** + * Returns a new object containing only the whitelisted headers. + * @param {Object} headers The original object of header names and + * values. + * @param {string[]} globalOptions.headerWhitelist An array of + * strings representing the whitelisted header names to keep in the + * output object. + * + * Generated by Trelent + */ + function filterBlacklistedHeaders(headers) { + return Object.keys(headers) + .filter(function (key) { + return globalOptions.headerBlacklist.indexOf(key) === -1; + }) + .reduce(function (acc, header) { + acc[header] = headers[header]; + return acc; + }, {}); + } + + /** + * Create a cache object + * @param {Object} headers The response headers to filter. + * @returns {Object} A new object containing only the whitelisted + * response headers. + * + * Generated by Trelent + */ + function createCacheObject(status, headers, data, encoding) { + return { + status: status, + headers: filterBlacklistedHeaders(headers), + data: data, + encoding: encoding, + timestamp: new Date().getTime() / 1000, // seconds since epoch. This is used to properly decrement max-age headers in cached responses. + }; + } + + /** + * Sets a cache value for the given key. + * @param {string} key The cache key to set. + * @param {any} value The cache value to set. + * @param {number} duration How long in milliseconds the cached + * response should be valid for (defaults to 1 hour). + * + * Generated by Trelent + */ + function cacheResponse(key, value, duration) { + let redis = globalOptions.redisClient; + let expireCallback = globalOptions.events.expire; + + if (redis && redis.connected) { + try { + redis.hset(key, "response", JSON.stringify(value)); + redis.hset(key, "duration", duration); + redis.expire(key, duration / 1000, expireCallback || function () {}); + } catch (err) { + debug("[apicache] error in redis.hset()"); + } + } else { + memCache.add(key, value, duration, expireCallback); + } + + // add automatic cache clearing from duration, includes max limit on setTimeout + timers[key] = setTimeout(function () { + instance.clear(key, true); + }, Math.min(duration, 2147483647)); + } + + /** + * Appends content to the response. + * @param {Object} res Express response object + * @param {(string|Buffer)} content The content to append. + * + * Generated by Trelent + */ + function accumulateContent(res, content) { + if (content) { + if (typeof content == "string") { + res._apicache.content = (res._apicache.content || "") + content; + } else if (Buffer.isBuffer(content)) { + let oldContent = res._apicache.content; + + if (typeof oldContent === "string") { + oldContent = !Buffer.from ? new Buffer(oldContent) : Buffer.from(oldContent); + } + + if (!oldContent) { + oldContent = !Buffer.alloc ? new Buffer(0) : Buffer.alloc(0); + } + + res._apicache.content = Buffer.concat( + [oldContent, content], + oldContent.length + content.length + ); + } else { + res._apicache.content = content; + } + } + } + + /** + * Monkeypatches the response object to add cache control headers + * and create a cache object. + * @param {Object} req Express request object + * @param {Object} res Express response object + * @param {function} next Function to call next + * @param {string} key Key to add response as + * @param {number} duration Time to cache response for + * @param {string} strDuration Duration in string form + * @param {function(Object, Object):boolean} toggle + */ + function makeResponseCacheable(req, res, next, key, duration, strDuration, toggle) { + // monkeypatch res.end to create cache object + res._apicache = { + write: res.write, + writeHead: res.writeHead, + end: res.end, + cacheable: true, + content: undefined, + }; + + // append header overwrites if applicable + Object.keys(globalOptions.headers).forEach(function (name) { + res.setHeader(name, globalOptions.headers[name]); + }); + + res.writeHead = function () { + // add cache control headers + if (!globalOptions.headers["cache-control"]) { + if (shouldCacheResponse(req, res, toggle)) { + res.setHeader("cache-control", "max-age=" + (duration / 1000).toFixed(0)); + } else { + res.setHeader("cache-control", "no-cache, no-store, must-revalidate"); + } + } + + res._apicache.headers = Object.assign({}, getSafeHeaders(res)); + return res._apicache.writeHead.apply(this, arguments); + }; + + // patch res.write + res.write = function (content) { + accumulateContent(res, content); + return res._apicache.write.apply(this, arguments); + }; + + // patch res.end + res.end = function (content, encoding) { + if (shouldCacheResponse(req, res, toggle)) { + accumulateContent(res, content); + + if (res._apicache.cacheable && res._apicache.content) { + addIndexEntries(key, req); + let headers = res._apicache.headers || getSafeHeaders(res); + let cacheObject = createCacheObject( + res.statusCode, + headers, + res._apicache.content, + encoding + ); + cacheResponse(key, cacheObject, duration); + + // display log entry + let elapsed = new Date() - req.apicacheTimer; + debug("adding cache entry for \"" + key + "\" @ " + strDuration, logDuration(elapsed)); + debug("_apicache.headers: ", res._apicache.headers); + debug("res.getHeaders(): ", getSafeHeaders(res)); + debug("cacheObject: ", cacheObject); + } + } + + return res._apicache.end.apply(this, arguments); + }; + + next(); + } + + /** + * Send a cached response to client + * @param {Request} request Express request object + * @param {Response} response Express response object + * @param {object} cacheObject Cache object to send + * @param {function(Object, Object):boolean} toggle + * @param {function} next Function to call next + * @param {number} duration Not used + * @returns {boolean|undefined} true if the request should be + * cached, false otherwise. If undefined, defaults to true. + */ + function sendCachedResponse(request, response, cacheObject, toggle, next, duration) { + if (toggle && !toggle(request, response)) { + return next(); + } + + let headers = getSafeHeaders(response); + + // Modified by @louislam, removed Cache-control, since I don't need client side cache! + // Original Source: https://github.com/kwhitley/apicache/blob/0d5686cc21fad353c6dddee646288c2fca3e4f50/src/apicache.js#L254 + Object.assign(headers, filterBlacklistedHeaders(cacheObject.headers || {})); + + // only embed apicache headers when not in production environment + if (process.env.NODE_ENV !== "production") { + Object.assign(headers, { + "apicache-store": globalOptions.redisClient ? "redis" : "memory", + "apicache-version": "1.6.2-modified", + }); + } + + // unstringify buffers + let data = cacheObject.data; + if (data && data.type === "Buffer") { + data = + typeof data.data === "number" ? new Buffer.alloc(data.data) : new Buffer.from(data.data); + } + + // test Etag against If-None-Match for 304 + let cachedEtag = cacheObject.headers.etag; + let requestEtag = request.headers["if-none-match"]; + + if (requestEtag && cachedEtag === requestEtag) { + response.writeHead(304, headers); + return response.end(); + } + + response.writeHead(cacheObject.status || 200, headers); + + return response.end(data, cacheObject.encoding); + } + + /** Sync caching options */ + function syncOptions() { + for (let i in middlewareOptions) { + Object.assign(middlewareOptions[i].options, globalOptions, middlewareOptions[i].localOptions); + } + } + + /** + * Clear key from cache + * @param {string} target Key to clear + * @param {boolean} isAutomatic Is the key being cleared automatically + * @returns {number} + */ + this.clear = function (target, isAutomatic) { + let group = index.groups[target]; + let redis = globalOptions.redisClient; + + if (group) { + debug("clearing group \"" + target + "\""); + + group.forEach(function (key) { + debug("clearing cached entry for \"" + key + "\""); + clearTimeout(timers[key]); + delete timers[key]; + if (!globalOptions.redisClient) { + memCache.delete(key); + } else { + try { + redis.del(key); + } catch (err) { + console.log("[apicache] error in redis.del(\"" + key + "\")"); + } + } + index.all = index.all.filter(doesntMatch(key)); + }); + + delete index.groups[target]; + } else if (target) { + debug("clearing " + (isAutomatic ? "expired" : "cached") + " entry for \"" + target + "\""); + clearTimeout(timers[target]); + delete timers[target]; + // clear actual cached entry + if (!redis) { + memCache.delete(target); + } else { + try { + redis.del(target); + } catch (err) { + console.log("[apicache] error in redis.del(\"" + target + "\")"); + } + } + + // remove from global index + index.all = index.all.filter(doesntMatch(target)); + + // remove target from each group that it may exist in + Object.keys(index.groups).forEach(function (groupName) { + index.groups[groupName] = index.groups[groupName].filter(doesntMatch(target)); + + // delete group if now empty + if (!index.groups[groupName].length) { + delete index.groups[groupName]; + } + }); + } else { + debug("clearing entire index"); + + if (!redis) { + memCache.clear(); + } else { + // clear redis keys one by one from internal index to prevent clearing non-apicache entries + index.all.forEach(function (key) { + clearTimeout(timers[key]); + delete timers[key]; + try { + redis.del(key); + } catch (err) { + console.log("[apicache] error in redis.del(\"" + key + "\")"); + } + }); + } + this.resetIndex(); + } + + return this.getIndex(); + }; + + /** + * Converts a duration string to an integer number of milliseconds. + * @param {(string|number)} duration The string to convert. + * @param {number} defaultDuration The default duration to return if + * can't parse duration + * @returns {number} The converted value in milliseconds, or the + * defaultDuration if it can't be parsed. + */ + function parseDuration(duration, defaultDuration) { + if (typeof duration === "number") { + return duration; + } + + if (typeof duration === "string") { + let split = duration.match(/^([\d\.,]+)\s?(\w+)$/); + + if (split.length === 3) { + let len = parseFloat(split[1]); + let unit = split[2].replace(/s$/i, "").toLowerCase(); + if (unit === "m") { + unit = "ms"; + } + + return (len || 1) * (t[unit] || 0); + } + } + + return defaultDuration; + } + + /** + * Parse duration + * @param {(number|string)} duration + * @returns {number} Duration parsed to a number + */ + this.getDuration = function (duration) { + return parseDuration(duration, globalOptions.defaultDuration); + }; + + /** + * Return cache performance statistics (hit rate). Suitable for + * putting into a route: + * <code> + * app.get('/api/cache/performance', (req, res) => { + * res.json(apicache.getPerformance()) + * }) + * </code> + * @returns {any[]} + */ + this.getPerformance = function () { + return performanceArray.map(function (p) { + return p.report(); + }); + }; + + /** + * Get index of a group + * @param {string} group + * @returns {number} + */ + this.getIndex = function (group) { + if (group) { + return index.groups[group]; + } else { + return index; + } + }; + + /** + * Express middleware + * @param {(string|number)} strDuration Duration to cache responses + * for. + * @param {function(Object, Object):boolean} middlewareToggle + * @param {Object} localOptions Options for APICache + * @returns + */ + this.middleware = function cache(strDuration, middlewareToggle, localOptions) { + let duration = instance.getDuration(strDuration); + let opt = {}; + + middlewareOptions.push({ + options: opt, + }); + + let options = function (localOptions) { + if (localOptions) { + middlewareOptions.find(function (middleware) { + return middleware.options === opt; + }).localOptions = localOptions; + } + + syncOptions(); + + return opt; + }; + + options(localOptions); + + /** + * A Function for non tracking performance + */ + function NOOPCachePerformance() { + this.report = this.hit = this.miss = function () {}; // noop; + } + + /** + * A function for tracking and reporting hit rate. These + * statistics are returned by the getPerformance() call above. + */ + function CachePerformance() { + /** + * Tracks the hit rate for the last 100 requests. If there + * have been fewer than 100 requests, the hit rate just + * considers the requests that have happened. + */ + this.hitsLast100 = new Uint8Array(100 / 4); // each hit is 2 bits + + /** + * Tracks the hit rate for the last 1000 requests. If there + * have been fewer than 1000 requests, the hit rate just + * considers the requests that have happened. + */ + this.hitsLast1000 = new Uint8Array(1000 / 4); // each hit is 2 bits + + /** + * Tracks the hit rate for the last 10000 requests. If there + * have been fewer than 10000 requests, the hit rate just + * considers the requests that have happened. + */ + this.hitsLast10000 = new Uint8Array(10000 / 4); // each hit is 2 bits + + /** + * Tracks the hit rate for the last 100000 requests. If + * there have been fewer than 100000 requests, the hit rate + * just considers the requests that have happened. + */ + this.hitsLast100000 = new Uint8Array(100000 / 4); // each hit is 2 bits + + /** + * The number of calls that have passed through the + * middleware since the server started. + */ + this.callCount = 0; + + /** + * The total number of hits since the server started + */ + this.hitCount = 0; + + /** + * The key from the last cache hit. This is useful in + * identifying which route these statistics apply to. + */ + this.lastCacheHit = null; + + /** + * The key from the last cache miss. This is useful in + * identifying which route these statistics apply to. + */ + this.lastCacheMiss = null; + + /** + * Return performance statistics + * @returns {Object} + */ + this.report = function () { + return { + lastCacheHit: this.lastCacheHit, + lastCacheMiss: this.lastCacheMiss, + callCount: this.callCount, + hitCount: this.hitCount, + missCount: this.callCount - this.hitCount, + hitRate: this.callCount == 0 ? null : this.hitCount / this.callCount, + hitRateLast100: this.hitRate(this.hitsLast100), + hitRateLast1000: this.hitRate(this.hitsLast1000), + hitRateLast10000: this.hitRate(this.hitsLast10000), + hitRateLast100000: this.hitRate(this.hitsLast100000), + }; + }; + + /** + * Computes a cache hit rate from an array of hits and + * misses. + * @param {Uint8Array} array An array representing hits and + * misses. + * @returns {?number} a number between 0 and 1, or null if + * the array has no hits or misses + */ + this.hitRate = function (array) { + let hits = 0; + let misses = 0; + for (let i = 0; i < array.length; i++) { + let n8 = array[i]; + for (let j = 0; j < 4; j++) { + switch (n8 & 3) { + case 1: + hits++; + break; + case 2: + misses++; + break; + } + n8 >>= 2; + } + } + let total = hits + misses; + if (total == 0) { + return null; + } + return hits / total; + }; + + /** + * Record a hit or miss in the given array. It will be + * recorded at a position determined by the current value of + * the callCount variable. + * @param {Uint8Array} array An array representing hits and + * misses. + * @param {boolean} hit true for a hit, false for a miss + * Each element in the array is 8 bits, and encodes 4 + * hit/miss records. Each hit or miss is encoded as to bits + * as follows: 00 means no hit or miss has been recorded in + * these bits 01 encodes a hit 10 encodes a miss + */ + this.recordHitInArray = function (array, hit) { + let arrayIndex = ~~(this.callCount / 4) % array.length; + let bitOffset = (this.callCount % 4) * 2; // 2 bits per record, 4 records per uint8 array element + let clearMask = ~(3 << bitOffset); + let record = (hit ? 1 : 2) << bitOffset; + array[arrayIndex] = (array[arrayIndex] & clearMask) | record; + }; + + /** + * Records the hit or miss in the tracking arrays and + * increments the call count. + * @param {boolean} hit true records a hit, false records a + * miss + */ + this.recordHit = function (hit) { + this.recordHitInArray(this.hitsLast100, hit); + this.recordHitInArray(this.hitsLast1000, hit); + this.recordHitInArray(this.hitsLast10000, hit); + this.recordHitInArray(this.hitsLast100000, hit); + if (hit) { + this.hitCount++; + } + this.callCount++; + }; + + /** + * Records a hit event, setting lastCacheMiss to the given key + * @param {string} key The key that had the cache hit + */ + this.hit = function (key) { + this.recordHit(true); + this.lastCacheHit = key; + }; + + /** + * Records a miss event, setting lastCacheMiss to the given key + * @param {string} key The key that had the cache miss + */ + this.miss = function (key) { + this.recordHit(false); + this.lastCacheMiss = key; + }; + } + + let perf = globalOptions.trackPerformance ? new CachePerformance() : new NOOPCachePerformance(); + + performanceArray.push(perf); + + /** + * Cache a request + * @param {Object} req Express request object + * @param {Object} res Express response object + * @param {function} next Function to call next + * @returns {any} + */ + let cache = function (req, res, next) { + function bypass() { + debug("bypass detected, skipping cache."); + return next(); + } + + // initial bypass chances + if (!opt.enabled) { + return bypass(); + } + if ( + req.headers["x-apicache-bypass"] || + req.headers["x-apicache-force-fetch"] || + (opt.respectCacheControl && req.headers["cache-control"] == "no-cache") + ) { + return bypass(); + } + + // REMOVED IN 0.11.1 TO CORRECT MIDDLEWARE TOGGLE EXECUTE ORDER + // if (typeof middlewareToggle === 'function') { + // if (!middlewareToggle(req, res)) return bypass() + // } else if (middlewareToggle !== undefined && !middlewareToggle) { + // return bypass() + // } + + // embed timer + req.apicacheTimer = new Date(); + + // In Express 4.x the url is ambigious based on where a router is mounted. originalUrl will give the full Url + let key = req.originalUrl || req.url; + + // Remove querystring from key if jsonp option is enabled + if (opt.jsonp) { + key = url.parse(key).pathname; + } + + // add appendKey (either custom function or response path) + if (typeof opt.appendKey === "function") { + key += "$$appendKey=" + opt.appendKey(req, res); + } else if (opt.appendKey.length > 0) { + let appendKey = req; + + for (let i = 0; i < opt.appendKey.length; i++) { + appendKey = appendKey[opt.appendKey[i]]; + } + key += "$$appendKey=" + appendKey; + } + + // attempt cache hit + let redis = opt.redisClient; + let cached = !redis ? memCache.getValue(key) : null; + + // send if cache hit from memory-cache + if (cached) { + let elapsed = new Date() - req.apicacheTimer; + debug("sending cached (memory-cache) version of", key, logDuration(elapsed)); + + perf.hit(key); + return sendCachedResponse(req, res, cached, middlewareToggle, next, duration); + } + + // send if cache hit from redis + if (redis && redis.connected) { + try { + redis.hgetall(key, function (err, obj) { + if (!err && obj && obj.response) { + let elapsed = new Date() - req.apicacheTimer; + debug("sending cached (redis) version of", key, logDuration(elapsed)); + + perf.hit(key); + return sendCachedResponse( + req, + res, + JSON.parse(obj.response), + middlewareToggle, + next, + duration + ); + } else { + perf.miss(key); + return makeResponseCacheable( + req, + res, + next, + key, + duration, + strDuration, + middlewareToggle + ); + } + }); + } catch (err) { + // bypass redis on error + perf.miss(key); + return makeResponseCacheable(req, res, next, key, duration, strDuration, middlewareToggle); + } + } else { + perf.miss(key); + return makeResponseCacheable(req, res, next, key, duration, strDuration, middlewareToggle); + } + }; + + cache.options = options; + + return cache; + }; + + /** + * Process options + * @param {Object} options + * @returns {Object} + */ + this.options = function (options) { + if (options) { + Object.assign(globalOptions, options); + syncOptions(); + + if ("defaultDuration" in options) { + // Convert the default duration to a number in milliseconds (if needed) + globalOptions.defaultDuration = parseDuration(globalOptions.defaultDuration, 3600000); + } + + if (globalOptions.trackPerformance) { + debug("WARNING: using trackPerformance flag can cause high memory usage!"); + } + + return this; + } else { + return globalOptions; + } + }; + + /** Reset the index */ + this.resetIndex = function () { + index = { + all: [], + groups: {}, + }; + }; + + /** + * Create a new instance of ApiCache + * @param {Object} config Config to pass + * @returns {ApiCache} + */ + this.newInstance = function (config) { + let instance = new ApiCache(); + + if (config) { + instance.options(config); + } + + return instance; + }; + + /** Clone this instance */ + this.clone = function () { + return this.newInstance(this.options()); + }; + + // initialize index + this.resetIndex(); +} + +module.exports = new ApiCache(); diff --git a/server/modules/apicache/index.js b/server/modules/apicache/index.js new file mode 100644 index 0000000..b8bb9b3 --- /dev/null +++ b/server/modules/apicache/index.js @@ -0,0 +1,14 @@ +const apicache = require("./apicache"); + +apicache.options({ + headerBlacklist: [ + "cache-control" + ], + headers: { + // Disable client side cache, only server side cache. + // BUG! Not working for the second request + "cache-control": "no-cache", + }, +}); + +module.exports = apicache; diff --git a/server/modules/apicache/memory-cache.js b/server/modules/apicache/memory-cache.js new file mode 100644 index 0000000..a91eee3 --- /dev/null +++ b/server/modules/apicache/memory-cache.js @@ -0,0 +1,87 @@ +function MemoryCache() { + this.cache = {}; + this.size = 0; +} + +/** + * + * @param {string} key Key to store cache as + * @param {any} value Value to store + * @param {number} time Time to store for + * @param {function(any, string)} timeoutCallback Callback to call in + * case of timeout + * @returns {Object} + */ +MemoryCache.prototype.add = function (key, value, time, timeoutCallback) { + let old = this.cache[key]; + let instance = this; + + let entry = { + value: value, + expire: time + Date.now(), + timeout: setTimeout(function () { + instance.delete(key); + return timeoutCallback && typeof timeoutCallback === "function" && timeoutCallback(value, key); + }, time) + }; + + this.cache[key] = entry; + this.size = Object.keys(this.cache).length; + + return entry; +}; + +/** + * Delete a cache entry + * @param {string} key Key to delete + * @returns {null} + */ +MemoryCache.prototype.delete = function (key) { + let entry = this.cache[key]; + + if (entry) { + clearTimeout(entry.timeout); + } + + delete this.cache[key]; + + this.size = Object.keys(this.cache).length; + + return null; +}; + +/** + * Get value of key + * @param {string} key + * @returns {Object} + */ +MemoryCache.prototype.get = function (key) { + let entry = this.cache[key]; + + return entry; +}; + +/** + * Get value of cache entry + * @param {string} key + * @returns {any} + */ +MemoryCache.prototype.getValue = function (key) { + let entry = this.get(key); + + return entry && entry.value; +}; + +/** + * Clear cache + * @returns {boolean} + */ +MemoryCache.prototype.clear = function () { + Object.keys(this.cache).forEach(function (key) { + this.delete(key); + }, this); + + return true; +}; + +module.exports = MemoryCache; diff --git a/server/modules/axios-ntlm/LICENSE b/server/modules/axios-ntlm/LICENSE new file mode 100644 index 0000000..1744ee4 --- /dev/null +++ b/server/modules/axios-ntlm/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 CatButtes + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/server/modules/axios-ntlm/lib/flags.js b/server/modules/axios-ntlm/lib/flags.js new file mode 100644 index 0000000..c16028c --- /dev/null +++ b/server/modules/axios-ntlm/lib/flags.js @@ -0,0 +1,77 @@ +'use strict'; +// Original file https://raw.githubusercontent.com/elasticio/node-ntlm-client/master/lib/flags.js +module.exports.NTLMFLAG_NEGOTIATE_UNICODE = 1 << 0; +/* Indicates that Unicode strings are supported for use in security buffer + data. */ +module.exports.NTLMFLAG_NEGOTIATE_OEM = 1 << 1; +/* Indicates that OEM strings are supported for use in security buffer data. */ +module.exports.NTLMFLAG_REQUEST_TARGET = 1 << 2; +/* Requests that the server's authentication realm be included in the Type 2 + message. */ +/* unknown (1<<3) */ +module.exports.NTLMFLAG_NEGOTIATE_SIGN = 1 << 4; +/* Specifies that authenticated communication between the client and server + should carry a digital signature (message integrity). */ +module.exports.NTLMFLAG_NEGOTIATE_SEAL = 1 << 5; +/* Specifies that authenticated communication between the client and server + should be encrypted (message confidentiality). */ +module.exports.NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE = 1 << 6; +/* Indicates that datagram authentication is being used. */ +module.exports.NTLMFLAG_NEGOTIATE_LM_KEY = 1 << 7; +/* Indicates that the LAN Manager session key should be used for signing and + sealing authenticated communications. */ +module.exports.NTLMFLAG_NEGOTIATE_NETWARE = 1 << 8; +/* unknown purpose */ +module.exports.NTLMFLAG_NEGOTIATE_NTLM_KEY = 1 << 9; +/* Indicates that NTLM authentication is being used. */ +/* unknown (1<<10) */ +module.exports.NTLMFLAG_NEGOTIATE_ANONYMOUS = 1 << 11; +/* Sent by the client in the Type 3 message to indicate that an anonymous + context has been established. This also affects the response fields. */ +module.exports.NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED = 1 << 12; +/* Sent by the client in the Type 1 message to indicate that a desired + authentication realm is included in the message. */ +module.exports.NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED = 1 << 13; +/* Sent by the client in the Type 1 message to indicate that the client + workstation's name is included in the message. */ +module.exports.NTLMFLAG_NEGOTIATE_LOCAL_CALL = 1 << 14; +/* Sent by the server to indicate that the server and client are on the same + machine. Implies that the client may use a pre-established local security + context rather than responding to the challenge. */ +module.exports.NTLMFLAG_NEGOTIATE_ALWAYS_SIGN = 1 << 15; +/* Indicates that authenticated communication between the client and server + should be signed with a "dummy" signature. */ +module.exports.NTLMFLAG_TARGET_TYPE_DOMAIN = 1 << 16; +/* Sent by the server in the Type 2 message to indicate that the target + authentication realm is a domain. */ +module.exports.NTLMFLAG_TARGET_TYPE_SERVER = 1 << 17; +/* Sent by the server in the Type 2 message to indicate that the target + authentication realm is a server. */ +module.exports.NTLMFLAG_TARGET_TYPE_SHARE = 1 << 18; +/* Sent by the server in the Type 2 message to indicate that the target + authentication realm is a share. Presumably, this is for share-level + authentication. Usage is unclear. */ +module.exports.NTLMFLAG_NEGOTIATE_NTLM2_KEY = 1 << 19; +/* Indicates that the NTLM2 signing and sealing scheme should be used for + protecting authenticated communications. */ +module.exports.NTLMFLAG_REQUEST_INIT_RESPONSE = 1 << 20; +/* unknown purpose */ +module.exports.NTLMFLAG_REQUEST_ACCEPT_RESPONSE = 1 << 21; +/* unknown purpose */ +module.exports.NTLMFLAG_REQUEST_NONNT_SESSION_KEY = 1 << 22; +/* unknown purpose */ +module.exports.NTLMFLAG_NEGOTIATE_TARGET_INFO = 1 << 23; +/* Sent by the server in the Type 2 message to indicate that it is including a + Target Information block in the message. */ +/* unknown (1<24) */ +/* unknown (1<25) */ +/* unknown (1<26) */ +/* unknown (1<27) */ +/* unknown (1<28) */ +module.exports.NTLMFLAG_NEGOTIATE_128 = 1 << 29; +/* Indicates that 128-bit encryption is supported. */ +module.exports.NTLMFLAG_NEGOTIATE_KEY_EXCHANGE = 1 << 30; +/* Indicates that the client will provide an encrypted master key in + the "Session Key" field of the Type 3 message. */ +module.exports.NTLMFLAG_NEGOTIATE_56 = 1 << 31; +//# sourceMappingURL=flags.js.map
\ No newline at end of file diff --git a/server/modules/axios-ntlm/lib/hash.js b/server/modules/axios-ntlm/lib/hash.js new file mode 100644 index 0000000..4e5aa26 --- /dev/null +++ b/server/modules/axios-ntlm/lib/hash.js @@ -0,0 +1,122 @@ +'use strict'; +// Original source at https://github.com/elasticio/node-ntlm-client/blob/master/lib/hash.js +var crypto = require('crypto'); +function createLMResponse(challenge, lmhash) { + var buf = new Buffer.alloc(24), pwBuffer = new Buffer.alloc(21).fill(0); + lmhash.copy(pwBuffer); + calculateDES(pwBuffer.slice(0, 7), challenge).copy(buf); + calculateDES(pwBuffer.slice(7, 14), challenge).copy(buf, 8); + calculateDES(pwBuffer.slice(14), challenge).copy(buf, 16); + return buf; +} +function createLMHash(password) { + var buf = new Buffer.alloc(16), pwBuffer = new Buffer.alloc(14), magicKey = new Buffer.from('KGS!@#$%', 'ascii'); + if (password.length > 14) { + buf.fill(0); + return buf; + } + pwBuffer.fill(0); + pwBuffer.write(password.toUpperCase(), 0, 'ascii'); + return Buffer.concat([ + calculateDES(pwBuffer.slice(0, 7), magicKey), + calculateDES(pwBuffer.slice(7), magicKey) + ]); +} +function calculateDES(key, message) { + var desKey = new Buffer.alloc(8); + desKey[0] = key[0] & 0xFE; + desKey[1] = ((key[0] << 7) & 0xFF) | (key[1] >> 1); + desKey[2] = ((key[1] << 6) & 0xFF) | (key[2] >> 2); + desKey[3] = ((key[2] << 5) & 0xFF) | (key[3] >> 3); + desKey[4] = ((key[3] << 4) & 0xFF) | (key[4] >> 4); + desKey[5] = ((key[4] << 3) & 0xFF) | (key[5] >> 5); + desKey[6] = ((key[5] << 2) & 0xFF) | (key[6] >> 6); + desKey[7] = (key[6] << 1) & 0xFF; + for (var i = 0; i < 8; i++) { + var parity = 0; + for (var j = 1; j < 8; j++) { + parity += (desKey[i] >> j) % 2; + } + desKey[i] |= (parity % 2) === 0 ? 1 : 0; + } + var des = crypto.createCipheriv('DES-ECB', desKey, ''); + return des.update(message); +} +function createNTLMResponse(challenge, ntlmhash) { + var buf = new Buffer.alloc(24), ntlmBuffer = new Buffer.alloc(21).fill(0); + ntlmhash.copy(ntlmBuffer); + calculateDES(ntlmBuffer.slice(0, 7), challenge).copy(buf); + calculateDES(ntlmBuffer.slice(7, 14), challenge).copy(buf, 8); + calculateDES(ntlmBuffer.slice(14), challenge).copy(buf, 16); + return buf; +} +function createNTLMHash(password) { + var md4sum = crypto.createHash('md4'); + md4sum.update(new Buffer.from(password, 'ucs2')); + return md4sum.digest(); +} +function createNTLMv2Hash(ntlmhash, username, authTargetName) { + var hmac = crypto.createHmac('md5', ntlmhash); + hmac.update(new Buffer.from(username.toUpperCase() + authTargetName, 'ucs2')); + return hmac.digest(); +} +function createLMv2Response(type2message, username, ntlmhash, nonce, targetName) { + var buf = new Buffer.alloc(24), ntlm2hash = createNTLMv2Hash(ntlmhash, username, targetName), hmac = crypto.createHmac('md5', ntlm2hash); + //server challenge + type2message.challenge.copy(buf, 8); + //client nonce + buf.write(nonce || createPseudoRandomValue(16), 16, 'hex'); + //create hash + hmac.update(buf.slice(8)); + var hashedBuffer = hmac.digest(); + hashedBuffer.copy(buf); + return buf; +} +function createNTLMv2Response(type2message, username, ntlmhash, nonce, targetName) { + var buf = new Buffer.alloc(48 + type2message.targetInfo.buffer.length), ntlm2hash = createNTLMv2Hash(ntlmhash, username, targetName), hmac = crypto.createHmac('md5', ntlm2hash); + //the first 8 bytes are spare to store the hashed value before the blob + //server challenge + type2message.challenge.copy(buf, 8); + //blob signature + buf.writeUInt32BE(0x01010000, 16); + //reserved + buf.writeUInt32LE(0, 20); + //timestamp + //TODO: we are loosing precision here since js is not able to handle those large integers + // maybe think about a different solution here + // 11644473600000 = diff between 1970 and 1601 + var timestamp = ((Date.now() + 11644473600000) * 10000).toString(16); + var timestampLow = Number('0x' + timestamp.substring(Math.max(0, timestamp.length - 8))); + var timestampHigh = Number('0x' + timestamp.substring(0, Math.max(0, timestamp.length - 8))); + buf.writeUInt32LE(timestampLow, 24, false); + buf.writeUInt32LE(timestampHigh, 28, false); + //random client nonce + buf.write(nonce || createPseudoRandomValue(16), 32, 'hex'); + //zero + buf.writeUInt32LE(0, 40); + //complete target information block from type 2 message + type2message.targetInfo.buffer.copy(buf, 44); + //zero + buf.writeUInt32LE(0, 44 + type2message.targetInfo.buffer.length); + hmac.update(buf.slice(8)); + var hashedBuffer = hmac.digest(); + hashedBuffer.copy(buf); + return buf; +} +function createPseudoRandomValue(length) { + var str = ''; + while (str.length < length) { + str += Math.floor(Math.random() * 16).toString(16); + } + return str; +} +module.exports = { + createLMHash: createLMHash, + createNTLMHash: createNTLMHash, + createLMResponse: createLMResponse, + createNTLMResponse: createNTLMResponse, + createLMv2Response: createLMv2Response, + createNTLMv2Response: createNTLMv2Response, + createPseudoRandomValue: createPseudoRandomValue +}; +//# sourceMappingURL=hash.js.map
\ No newline at end of file diff --git a/server/modules/axios-ntlm/lib/ntlm.js b/server/modules/axios-ntlm/lib/ntlm.js new file mode 100644 index 0000000..54490c0 --- /dev/null +++ b/server/modules/axios-ntlm/lib/ntlm.js @@ -0,0 +1,220 @@ +'use strict'; +// Original file https://raw.githubusercontent.com/elasticio/node-ntlm-client/master/lib/ntlm.js +var os = require('os'), flags = require('./flags'), hash = require('./hash'); +var NTLMSIGNATURE = "NTLMSSP\0"; +function createType1Message(workstation, target) { + var dataPos = 32, pos = 0, buf = new Buffer.alloc(1024); + workstation = workstation === undefined ? os.hostname() : workstation; + target = target === undefined ? '' : target; + //signature + buf.write(NTLMSIGNATURE, pos, NTLMSIGNATURE.length, 'ascii'); + pos += NTLMSIGNATURE.length; + //message type + buf.writeUInt32LE(1, pos); + pos += 4; + //flags + buf.writeUInt32LE(flags.NTLMFLAG_NEGOTIATE_OEM | + flags.NTLMFLAG_REQUEST_TARGET | + flags.NTLMFLAG_NEGOTIATE_NTLM_KEY | + flags.NTLMFLAG_NEGOTIATE_NTLM2_KEY | + flags.NTLMFLAG_NEGOTIATE_ALWAYS_SIGN, pos); + pos += 4; + //domain security buffer + buf.writeUInt16LE(target.length, pos); + pos += 2; + buf.writeUInt16LE(target.length, pos); + pos += 2; + buf.writeUInt32LE(target.length === 0 ? 0 : dataPos, pos); + pos += 4; + if (target.length > 0) { + dataPos += buf.write(target, dataPos, 'ascii'); + } + //workstation security buffer + buf.writeUInt16LE(workstation.length, pos); + pos += 2; + buf.writeUInt16LE(workstation.length, pos); + pos += 2; + buf.writeUInt32LE(workstation.length === 0 ? 0 : dataPos, pos); + pos += 4; + if (workstation.length > 0) { + dataPos += buf.write(workstation, dataPos, 'ascii'); + } + return 'NTLM ' + buf.toString('base64', 0, dataPos); +} +function decodeType2Message(str) { + if (str === undefined) { + throw new Error('Invalid argument'); + } + //convenience + if (Object.prototype.toString.call(str) !== '[object String]') { + if (str.hasOwnProperty('headers') && str.headers.hasOwnProperty('www-authenticate')) { + str = str.headers['www-authenticate']; + } + else { + throw new Error('Invalid argument'); + } + } + var ntlmMatch = /^NTLM ([^,\s]+)/.exec(str); + if (ntlmMatch) { + str = ntlmMatch[1]; + } + var buf = new Buffer.from(str, 'base64'), obj = {}; + //check signature + if (buf.toString('ascii', 0, NTLMSIGNATURE.length) !== NTLMSIGNATURE) { + throw new Error('Invalid message signature: ' + str); + } + //check message type + if (buf.readUInt32LE(NTLMSIGNATURE.length) !== 2) { + throw new Error('Invalid message type (no type 2)'); + } + //read flags + obj.flags = buf.readUInt32LE(20); + obj.encoding = (obj.flags & flags.NTLMFLAG_NEGOTIATE_OEM) ? 'ascii' : 'ucs2'; + obj.version = (obj.flags & flags.NTLMFLAG_NEGOTIATE_NTLM2_KEY) ? 2 : 1; + obj.challenge = buf.slice(24, 32); + //read target name + obj.targetName = (function () { + var length = buf.readUInt16LE(12); + //skipping allocated space + var offset = buf.readUInt32LE(16); + if (length === 0) { + return ''; + } + if ((offset + length) > buf.length || offset < 32) { + throw new Error('Bad type 2 message'); + } + return buf.toString(obj.encoding, offset, offset + length); + })(); + //read target info + if (obj.flags & flags.NTLMFLAG_NEGOTIATE_TARGET_INFO) { + obj.targetInfo = (function () { + var info = {}; + var length = buf.readUInt16LE(40); + //skipping allocated space + var offset = buf.readUInt32LE(44); + var targetInfoBuffer = new Buffer.alloc(length); + buf.copy(targetInfoBuffer, 0, offset, offset + length); + if (length === 0) { + return info; + } + if ((offset + length) > buf.length || offset < 32) { + throw new Error('Bad type 2 message'); + } + var pos = offset; + while (pos < (offset + length)) { + var blockType = buf.readUInt16LE(pos); + pos += 2; + var blockLength = buf.readUInt16LE(pos); + pos += 2; + if (blockType === 0) { + //reached the terminator subblock + break; + } + var blockTypeStr = void 0; + switch (blockType) { + case 1: + blockTypeStr = 'SERVER'; + break; + case 2: + blockTypeStr = 'DOMAIN'; + break; + case 3: + blockTypeStr = 'FQDN'; + break; + case 4: + blockTypeStr = 'DNS'; + break; + case 5: + blockTypeStr = 'PARENT_DNS'; + break; + default: + blockTypeStr = ''; + break; + } + if (blockTypeStr) { + info[blockTypeStr] = buf.toString('ucs2', pos, pos + blockLength); + } + pos += blockLength; + } + return { + parsed: info, + buffer: targetInfoBuffer + }; + })(); + } + return obj; +} +function createType3Message(type2Message, username, password, workstation, target) { + var dataPos = 52, buf = new Buffer.alloc(1024); + if (workstation === undefined) { + workstation = os.hostname(); + } + if (target === undefined) { + target = type2Message.targetName; + } + //signature + buf.write(NTLMSIGNATURE, 0, NTLMSIGNATURE.length, 'ascii'); + //message type + buf.writeUInt32LE(3, 8); + if (type2Message.version === 2) { + dataPos = 64; + var ntlmHash = hash.createNTLMHash(password), nonce = hash.createPseudoRandomValue(16), lmv2 = hash.createLMv2Response(type2Message, username, ntlmHash, nonce, target), ntlmv2 = hash.createNTLMv2Response(type2Message, username, ntlmHash, nonce, target); + //lmv2 security buffer + buf.writeUInt16LE(lmv2.length, 12); + buf.writeUInt16LE(lmv2.length, 14); + buf.writeUInt32LE(dataPos, 16); + lmv2.copy(buf, dataPos); + dataPos += lmv2.length; + //ntlmv2 security buffer + buf.writeUInt16LE(ntlmv2.length, 20); + buf.writeUInt16LE(ntlmv2.length, 22); + buf.writeUInt32LE(dataPos, 24); + ntlmv2.copy(buf, dataPos); + dataPos += ntlmv2.length; + } + else { + var lmHash = hash.createLMHash(password), ntlmHash = hash.createNTLMHash(password), lm = hash.createLMResponse(type2Message.challenge, lmHash), ntlm = hash.createNTLMResponse(type2Message.challenge, ntlmHash); + //lm security buffer + buf.writeUInt16LE(lm.length, 12); + buf.writeUInt16LE(lm.length, 14); + buf.writeUInt32LE(dataPos, 16); + lm.copy(buf, dataPos); + dataPos += lm.length; + //ntlm security buffer + buf.writeUInt16LE(ntlm.length, 20); + buf.writeUInt16LE(ntlm.length, 22); + buf.writeUInt32LE(dataPos, 24); + ntlm.copy(buf, dataPos); + dataPos += ntlm.length; + } + //target name security buffer + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? target.length : target.length * 2, 28); + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? target.length : target.length * 2, 30); + buf.writeUInt32LE(dataPos, 32); + dataPos += buf.write(target, dataPos, type2Message.encoding); + //user name security buffer + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? username.length : username.length * 2, 36); + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? username.length : username.length * 2, 38); + buf.writeUInt32LE(dataPos, 40); + dataPos += buf.write(username, dataPos, type2Message.encoding); + //workstation name security buffer + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? workstation.length : workstation.length * 2, 44); + buf.writeUInt16LE(type2Message.encoding === 'ascii' ? workstation.length : workstation.length * 2, 46); + buf.writeUInt32LE(dataPos, 48); + dataPos += buf.write(workstation, dataPos, type2Message.encoding); + if (type2Message.version === 2) { + //session key security buffer + buf.writeUInt16LE(0, 52); + buf.writeUInt16LE(0, 54); + buf.writeUInt32LE(0, 56); + //flags + buf.writeUInt32LE(type2Message.flags, 60); + } + return 'NTLM ' + buf.toString('base64', 0, dataPos); +} +module.exports = { + createType1Message: createType1Message, + decodeType2Message: decodeType2Message, + createType3Message: createType3Message +}; +//# sourceMappingURL=ntlm.js.map
\ No newline at end of file diff --git a/server/modules/axios-ntlm/lib/ntlmClient.js b/server/modules/axios-ntlm/lib/ntlmClient.js new file mode 100644 index 0000000..682de5f --- /dev/null +++ b/server/modules/axios-ntlm/lib/ntlmClient.js @@ -0,0 +1,127 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.NtlmClient = void 0; +var axios_1 = __importDefault(require("axios")); +var ntlm = __importStar(require("./ntlm")); +var https = __importStar(require("https")); +var http = __importStar(require("http")); +var dev_null_1 = __importDefault(require("dev-null")); +/** +* @param credentials An NtlmCredentials object containing the username and password +* @param AxiosConfig The Axios config for the instance you wish to create +* +* @returns This function returns an axios instance configured to use the provided credentials +*/ +function NtlmClient(credentials, AxiosConfig) { + var _this = this; + var config = AxiosConfig !== null && AxiosConfig !== void 0 ? AxiosConfig : {}; + if (!config.httpAgent) { + config.httpAgent = new http.Agent({ keepAlive: true }); + } + if (!config.httpsAgent) { + config.httpsAgent = new https.Agent({ keepAlive: true }); + } + var client = axios_1.default.create(config); + client.interceptors.response.use(function (response) { + return response; + }, function (err) { return __awaiter(_this, void 0, void 0, function () { + var error, t1Msg, t2Msg, t3Msg, stream_1; + var _a; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + error = err.response; + if (!(error && error.status === 401 + && error.headers['www-authenticate'] + && error.headers['www-authenticate'].includes('NTLM'))) return [3 /*break*/, 3]; + // This length check is a hack because SharePoint is awkward and will + // include the Negotiate option when responding with the T2 message + // There is nore we could do to ensure we are processing correctly, + // but this is the easiest option for now + if (error.headers['www-authenticate'].length < 50) { + t1Msg = ntlm.createType1Message(credentials.workstation, credentials.domain); + error.config.headers["Authorization"] = t1Msg; + } + else { + t2Msg = ntlm.decodeType2Message((error.headers['www-authenticate'].match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1]); + t3Msg = ntlm.createType3Message(t2Msg, credentials.username, credentials.password, credentials.workstation, credentials.domain); + error.config.headers["X-retry"] = "false"; + error.config.headers["Authorization"] = t3Msg; + } + if (!(error.config.responseType === "stream")) return [3 /*break*/, 2]; + stream_1 = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data; + if (!(stream_1 && !stream_1.readableEnded)) return [3 /*break*/, 2]; + return [4 /*yield*/, new Promise(function (resolve) { + stream_1.pipe((0, dev_null_1.default)()); + stream_1.once('close', resolve); + })]; + case 1: + _b.sent(); + _b.label = 2; + case 2: return [2 /*return*/, client(error.config)]; + case 3: throw err; + } + }); + }); }); + return client; +} +exports.NtlmClient = NtlmClient; +//# sourceMappingURL=ntlmClient.js.map
\ No newline at end of file diff --git a/server/modules/dayjs/plugin/timezone.d.ts b/server/modules/dayjs/plugin/timezone.d.ts new file mode 100644 index 0000000..d504f69 --- /dev/null +++ b/server/modules/dayjs/plugin/timezone.d.ts @@ -0,0 +1,20 @@ +import { PluginFunc, ConfigType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + tz(timezone?: string, keepLocalTime?: boolean): Dayjs + offsetName(type?: 'short' | 'long'): string | undefined + } + + interface DayjsTimezone { + (date: ConfigType, timezone?: string): Dayjs + (date: ConfigType, format: string, timezone?: string): Dayjs + guess(): string + setDefault(timezone?: string): void + } + + const tz: DayjsTimezone +} diff --git a/server/modules/dayjs/plugin/timezone.js b/server/modules/dayjs/plugin/timezone.js new file mode 100644 index 0000000..de709ae --- /dev/null +++ b/server/modules/dayjs/plugin/timezone.js @@ -0,0 +1,115 @@ +/** + * Copy from node_modules/dayjs/plugin/timezone.js + * Try to fix https://github.com/louislam/uptime-kuma/issues/2318 + * Source: https://github.com/iamkun/dayjs/tree/dev/src/plugin/utc + * License: MIT + */ +!function (t, e) { + // eslint-disable-next-line no-undef + typeof exports == "object" && typeof module != "undefined" ? module.exports = e() : typeof define == "function" && define.amd ? define(e) : (t = typeof globalThis != "undefined" ? globalThis : t || self).dayjs_plugin_timezone = e(); +}(this, (function () { + "use strict"; + let t = { + year: 0, + month: 1, + day: 2, + hour: 3, + minute: 4, + second: 5 + }; + let e = {}; + return function (n, i, o) { + let r; + let a = function (t, n, i) { + void 0 === i && (i = {}); + let o = new Date(t); + let r = function (t, n) { + void 0 === n && (n = {}); + let i = n.timeZoneName || "short"; + let o = t + "|" + i; + let r = e[o]; + return r || (r = new Intl.DateTimeFormat("en-US", { + hour12: !1, + timeZone: t, + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + timeZoneName: i + }), e[o] = r), r; + }(n, i); + return r.formatToParts(o); + }; + let u = function (e, n) { + let i = a(e, n); + let r = []; + let u = 0; + for (; u < i.length; u += 1) { + let f = i[u]; + let s = f.type; + let m = f.value; + let c = t[s]; + c >= 0 && (r[c] = parseInt(m, 10)); + } + let d = r[3]; + let l = d === 24 ? 0 : d; + let v = r[0] + "-" + r[1] + "-" + r[2] + " " + l + ":" + r[4] + ":" + r[5] + ":000"; + let h = +e; + return (o.utc(v).valueOf() - (h -= h % 1e3)) / 6e4; + }; + let f = i.prototype; + f.tz = function (t, e) { + void 0 === t && (t = r); + let n = this.utcOffset(); + let i = this.toDate(); + let a = i.toLocaleString("en-US", { timeZone: t }).replace("\u202f", " "); + let u = Math.round((i - new Date(a)) / 1e3 / 60); + let f = o(a).$set("millisecond", this.$ms).utcOffset(15 * -Math.round(i.getTimezoneOffset() / 15) - u, !0); + if (e) { + let s = f.utcOffset(); + f = f.add(n - s, "minute"); + } + return f.$x.$timezone = t, f; + }, f.offsetName = function (t) { + let e = this.$x.$timezone || o.tz.guess(); + let n = a(this.valueOf(), e, { timeZoneName: t }).find((function (t) { + return t.type.toLowerCase() === "timezonename"; + })); + return n && n.value; + }; + let s = f.startOf; + f.startOf = function (t, e) { + if (!this.$x || !this.$x.$timezone) { + return s.call(this, t, e); + } + let n = o(this.format("YYYY-MM-DD HH:mm:ss:SSS")); + return s.call(n, t, e).tz(this.$x.$timezone, !0); + }, o.tz = function (t, e, n) { + let i = n && e; + let a = n || e || r; + let f = u(+o(), a); + if (typeof t != "string") { + return o(t).tz(a); + } + let s = function (t, e, n) { + let i = t - 60 * e * 1e3; + let o = u(i, n); + if (e === o) { + return [ i, e ]; + } + let r = u(i -= 60 * (o - e) * 1e3, n); + return o === r ? [ i, o ] : [ t - 60 * Math.min(o, r) * 1e3, Math.max(o, r) ]; + }(o.utc(t, i).valueOf(), f, a); + let m = s[0]; + let c = s[1]; + let d = o(m).utcOffset(c); + return d.$x.$timezone = a, d; + }, o.tz.guess = function () { + return Intl.DateTimeFormat().resolvedOptions().timeZone; + }, o.tz.setDefault = function (t) { + r = t; + }; + }; +})); |