ya-express-ntlm
Version:
100 lines • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxyCache = exports.ProxyCache = void 0;
const af_color_1 = require("af-color");
const debug_1 = require("../debug");
const NTLMProxyStub_1 = require("./NTLMProxyStub");
const NTLMProxy_1 = require("./NTLMProxy");
const PROXY_LIVE_TIME_MILLIS = 60000;
const cache = {};
const connectToProxy = async (rsn, id, messageType1) => {
const strategy = rsn.options.getStrategy(rsn);
if (strategy === 'NTLM_STUB') {
const proxy = new NTLMProxyStub_1.NTLMProxyStub(id);
const messageType2Buf = await proxy.negotiate(messageType1);
return { proxy, messageType2Buf };
}
let proxy = exports.proxyCache.getProxy(id);
const tryProxy = async (isNewProxy) => {
if (proxy) {
try {
const messageType2Buf = await proxy.negotiate(messageType1);
if (messageType2Buf) {
return { proxy, messageType2Buf, isNewProxy };
}
}
catch (err) {
proxy.close();
}
}
};
let result = await tryProxy();
if (result) {
return result;
}
const tlsOptions = rsn.options.getTlsOptions(rsn);
const controllers = rsn.options.getDomainControllers(rsn);
for (let i = 0; i < controllers.length; i++) {
const ldapServer = new URL(controllers[i]);
const decodedPath = decodeURI(ldapServer.pathname || '');
(0, debug_1.debugNtlmLdapProxy)(`Choose LDAP server ${af_color_1.blue}${ldapServer.host}${af_color_1.reset}${decodedPath ? ` using base DN "${decodedPath}"` : ''}`);
proxy = new NTLMProxy_1.NTLMProxy({
id,
host: ldapServer.hostname,
port: ldapServer.port,
tlsOptions,
});
result = await tryProxy(true);
if (result) {
return result;
}
}
throw new Error(`None of the Domain Controllers are available: ${JSON.stringify(controllers)}`);
};
class ProxyCache {
clean() {
Object.entries(cache).forEach(([id, cachedProxy]) => {
if (cachedProxy.expire < Date.now()) {
this.remove(id, true);
}
});
exports.proxyCache.info('clean');
}
remove(id, byTimeout) {
const cachedProxy = cache[id];
if (cachedProxy) {
const { proxy } = cachedProxy;
proxy.close();
delete cache[id];
(0, debug_1.debugNtlmLdapProxy)(`Deleted proxy from cache${byTimeout ? ' by timeout' : ''}: id: ${af_color_1.lBlue}${id}${af_color_1.rs} / ${proxy.coloredAddress}`);
}
}
async addOrReplace(rsn, id, messageType1) {
const { proxy, messageType2Buf, isNewProxy } = await connectToProxy(rsn, id, messageType1);
(0, debug_1.debugNtlmLdapProxy)(`${isNewProxy ? 'Inserted proxy to' : 'Used proxy from'} cache: id: ${af_color_1.yellow}${id}${af_color_1.rs} / ${proxy.coloredAddress}`);
cache[id] = { proxy, expire: Date.now() + PROXY_LIVE_TIME_MILLIS };
return messageType2Buf.toString('base64');
}
getProxy(id) {
return cache[id]?.proxy;
}
changeId(oldId, newId) {
const cachedProxy = cache[oldId];
if (cachedProxy) {
cache[newId] = cachedProxy;
cachedProxy.proxy.id = newId;
}
}
info(from = '') {
const { length } = Object.keys(cache);
if (length) {
(0, debug_1.debugNtlmLdapProxy)(`[${from}] In cache ${Object.keys(cache).length} LDAP proxy connections`);
}
}
}
exports.ProxyCache = ProxyCache;
exports.proxyCache = new ProxyCache();
setInterval(() => {
exports.proxyCache.clean();
}, 30000);
//# sourceMappingURL=ProxyCache.js.map