ya-express-ntlm
Version:
87 lines • 3.66 kB
JavaScript
import net from 'net';
import tls from 'tls';
import { lBlue, magenta, rs, yellow, reset } from 'af-color';
import { LDAPContext } from '../lib/LDAPContext';
import { debugNtlmLdapProxy } from '../debug';
import { sanitizeText } from '../lib/utils';
import { arrowRR, LLarrow } from '../lib/constants';
export class NTLMProxy {
constructor(opts) {
this.ldapContext = new 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 = `${magenta}${this.host}:${this.port}${rs}`;
}
close() {
if (this.socket?.readyState === 'open') {
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) {
debugNtlmLdapProxy(`connection to ${this.coloredAddress} already opened`);
}
else {
this.close();
this.socket = this.tlsOptions
? tls.connect(this.port, this.host, this.tlsOptions)
: net.createConnection(this.port, this.host);
this.socket.setTimeout(5000);
this.socket.setKeepAlive(true);
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 (debugNtlmLdapProxy.enabled) {
debugNtlmLdapProxy(`${arrowRR} ${operationType} Send to ${this.coloredAddress}:\t${yellow}${sanitizeText(msgBuf)}`);
}
this.socket.write(msgBuf);
}
async negotiate(messageType1) {
const operationType = `${lBlue}[negotiate]${reset}`;
return new Promise((resolve, reject) => {
this.openConnection((data) => {
try {
const { serverSaslCreds } = this.ldapContext?.parseSessionSetupRESP(data) || {};
resolve(serverSaslCreds);
debugNtlmLdapProxy(`${LLarrow} ${operationType} Receive ${this.coloredAddress}:\t${lBlue}${sanitizeText(serverSaslCreds)}`);
}
catch (err) {
reject(err);
}
}, reject);
this.ldapContext = new LDAPContext();
const msg = this.ldapContext.makeSessionSetupREQ(messageType1, 1);
this.socketWrite(msg, operationType);
});
}
async authenticate(messageType3) {
const operationType = `${lBlue}[authenticate]${reset}`;
return new Promise((resolve, reject) => {
this.openConnection((data) => {
try {
const { isOk } = this.ldapContext?.parseSessionSetupRESP(data) || {};
debugNtlmLdapProxy(`${LLarrow} ${operationType} Receive ${this.coloredAddress}:\t${lBlue}Authenticated = ${isOk}`);
resolve(isOk);
}
catch (err) {
reject(err);
}
}, reject);
const msg = this.ldapContext?.makeSessionSetupREQ(messageType3, 2);
this.socketWrite(msg, operationType);
});
}
}
//# sourceMappingURL=NTLMProxy.js.map