ya-express-ntlm
Version:
94 lines • 4.23 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NTLMProxy = void 0;
const net_1 = __importDefault(require("net"));
const tls_1 = __importDefault(require("tls"));
const af_color_1 = require("af-color");
const LDAPContext_1 = require("../lib/LDAPContext");
const debug_1 = require("../debug");
const utils_1 = require("../lib/utils");
const constants_1 = require("../lib/constants");
class NTLMProxy {
constructor(opts) {
this.ldapContext = new LDAPContext_1.LDAPContext(); // stub initialization
this.id = opts.id;
this.host = opts.host;
this.port = Number(opts.port) || (opts.tlsOptions ? 636 : 389);
this.tlsOptions = opts.tlsOptions;
this.socket = null;
this.coloredAddress = `${af_color_1.magenta}${this.host}:${this.port}${af_color_1.rs}`;
}
close() {
if (this.socket?.readyState === 'open') {
(0, debug_1.debugNtlmLdapProxy)(`Close connection to ${this.coloredAddress}`);
this.socket?.end();
}
}
openConnection(resolve, reject) {
// @ts-ignore
const isSameConnectionOpened = this.socket?._host === this.host && this.socket?.readyState === 'open';
if (isSameConnectionOpened) {
(0, debug_1.debugNtlmLdapProxy)(`connection to ${this.coloredAddress} already opened`);
}
else {
this.close();
this.socket = this.tlsOptions
? tls_1.default.connect(this.port, this.host, this.tlsOptions)
: net_1.default.createConnection(this.port, this.host);
this.socket.setTimeout(5000);
this.socket.setKeepAlive(true);
(0, debug_1.debugNtlmLdapProxy)(`Opened connection to ${this.coloredAddress}`);
}
this.socket?.once('data', resolve);
this.socket?.once('error', reject);
}
socketWrite(msgBuf, operationType) {
if (!this.socket) {
throw new Error('Transaction on closed socket.');
}
if (debug_1.debugNtlmLdapProxy.enabled) {
(0, debug_1.debugNtlmLdapProxy)(`${constants_1.arrowRR} ${operationType} Send to ${this.coloredAddress}:\t${af_color_1.yellow}${(0, utils_1.sanitizeText)(msgBuf)}`);
}
this.socket.write(msgBuf);
}
async negotiate(messageType1) {
const operationType = `${af_color_1.lBlue}[negotiate]${af_color_1.reset}`;
return new Promise((resolve, reject) => {
this.openConnection((data) => {
try {
const { serverSaslCreds } = this.ldapContext?.parseSessionSetupRESP(data) || {};
resolve(serverSaslCreds);
(0, debug_1.debugNtlmLdapProxy)(`${constants_1.LLarrow} ${operationType} Receive ${this.coloredAddress}:\t${af_color_1.lBlue}${(0, utils_1.sanitizeText)(serverSaslCreds)}`);
}
catch (err) {
reject(err);
}
}, reject);
this.ldapContext = new LDAPContext_1.LDAPContext();
const msg = this.ldapContext.makeSessionSetupREQ(messageType1, 1);
this.socketWrite(msg, operationType);
});
}
async authenticate(messageType3) {
const operationType = `${af_color_1.lBlue}[authenticate]${af_color_1.reset}`;
return new Promise((resolve, reject) => {
this.openConnection((data) => {
try {
const { isOk } = this.ldapContext?.parseSessionSetupRESP(data) || {};
(0, debug_1.debugNtlmLdapProxy)(`${constants_1.LLarrow} ${operationType} Receive ${this.coloredAddress}:\t${af_color_1.lBlue}Authenticated = ${isOk}`);
resolve(isOk);
}
catch (err) {
reject(err);
}
}, reject);
const msg = this.ldapContext?.makeSessionSetupREQ(messageType3, 2);
this.socketWrite(msg, operationType);
});
}
}
exports.NTLMProxy = NTLMProxy;
//# sourceMappingURL=NTLMProxy.js.map