Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ var logger = require('./utilities/logging').getLogger(nconf);

class ProtocolConnection {
constructor(config) {
logger.debug("Initialising a ProtocolConnection.")
logger.log('debug',"Initialising a ProtocolConnection.")
this.config = config;
this.setStream(false);
this.clientResponse = function(xml) {
logger.debug("Received: " + xml.toString('utf8'))
logger.log('debug',"Received: " + xml.toString('utf8'))
};
}

Expand All @@ -32,10 +32,10 @@ class ProtocolConnection {
var newStream;
var {host, port} = config;
var options = {
host,
host,
port,
"rejectUnauthorized": false,
"secureProtocol": "TLSv1_method"
// "secureProtocol": "TLSv1_method"
};
if (config.key) {
options.key = fs.readFileSync(config.key);
Expand All @@ -44,27 +44,27 @@ class ProtocolConnection {
options.cert = fs.readFileSync(config.cert);
}

logger.debug("Establishing connection..");
logger.log('debug',"Establishing connection..");
newStream = tls.connect(options, () => {
let message = "Established a secure connection: " + host + ":" + port
logger.info(message, {host, port});
logger.log('info',message, {host, port});
resolve(message)
});
newStream.on('readable', () => {
logger.debug("Read event");
logger.log('debug',"Read event");
this.readStream();
});
newStream.on('clientError', (exception, securePair) => {
logger.error("client error", exception);
logger.log('error',"client error", exception);
reject(exception);
});
newStream.on('end', () => {
logger.error("Got an end event");
logger.log('error',"Got an end event");
process.exit(1);
});
this.setStream(newStream);
} catch (e) {
logger.error("Error in initStream")
logger.log('error',"Error in initStream")
reject(e)
}
} else {
Expand All @@ -84,27 +84,26 @@ class ProtocolConnection {
this.buffer = Buffer.concat([this.buffer, streamBuffer]);
}
var bigEndian = this.buffer.slice(0, 4);
var totalLength = new Buffer(bigEndian).readUIntBE(0, 4);
var totalLength = Buffer.from(bigEndian).readUIntBE(0, 4);
var eppResponseBody = this.buffer.slice(4);
var currentLength = this.buffer.length;
logger.debug("endian length: ", totalLength);
logger.debug("current buffer length", currentLength);
logger.log('debug',"endian length: ", totalLength);
logger.log('debug',"current buffer length", currentLength);
if (this.buffer.length === totalLength || eppResponseBody.length === totalLength) {
this.clientResponse(eppResponseBody);
this.buffer = undefined;
}
}
} catch (e) {
logger.error(e);
logger.log('error',e);
}
}

processBigEndian(xml) {
var xmlBuffer = new Buffer(xml);

var xmlBuffer = Buffer.from(xml); // Changed from new Buffer(xml)
var xmlLength = xmlBuffer.length;
var endianLength = xmlLength + 4;
var b = new Buffer(4);
var b = Buffer.alloc(4); // Changed from new Buffer(4)
b.writeUInt32BE(endianLength, 0);
var preppedXML = Buffer.concat([b, xmlBuffer]);
return preppedXML;
Expand All @@ -114,17 +113,17 @@ class ProtocolConnection {
return new Promise((resolve, reject) => {
// Called in "readStream()" when the stream gets input from EPP server.
this.clientResponse = function(buffer) {
logger.debug("Client responded")
logger.log('debug',"Client responded")
resolve(buffer);
};
try {
var preparedXML = this.processBigEndian(xml);
logger.debug(xml);
logger.log('debug',xml);
this.stream.write(preparedXML, "utf8", function() {
logger.debug("Finished writing to server.");
logger.log('debug',"Finished writing to server.");
});
} catch (e) {
logger.error("Unable to write to stream.");
logger.log('error',"Unable to write to stream.");
reject(e);
}
});
Expand All @@ -133,4 +132,3 @@ class ProtocolConnection {
}

module.exports = ProtocolConnection;

115 changes: 94 additions & 21 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

var ProtocolState = require('./protocol-state');
const moment = require('moment');

var nconf = require('./utilities/config').getConfig();
var logger = require('./utilities/logging').getLogger(nconf);

class Dispatcher {
constructor(registry) {
constructor(registry = 'sonic_registry') {
this.registry = registry;
this.registryConfig = nconf.get('app-config')[registry];
logger.info("Starting dispatcher", {registry, "config": this.registryConfig});
logger.log('info', "Starting dispatcher", { registry, "config": this.registryConfig });
this.state = new ProtocolState(registry, this.registryConfig);
}

Expand All @@ -28,47 +28,47 @@ class Dispatcher {
"password": nconf.get('epp_password')
},
loginTransactionId).then(
function(data) {
logger.info("login data", {data});
return;
},
function(error) {
logger.error("Unable to log in", {error});
throw new Error(error);
}
).catch((error) => {
logger.error("Promise rejected with", error)
});
function (data) {
logger.log('info', "login data", { data });
return;
},
function (error) {
logger.log('error', "Unable to log in", { error });
throw new Error(error);
}
).catch((error) => {
logger.log('error', "Promise rejected with", error)
});
}, 2000);
};
return this.sendMessage(eppCommand)
}

sendMessage(eppCommand) {
try {
logger.debug("Calling epp command.");
logger.log('debug', "Calling epp command.");
return this.state.connection.initStream().then(eppCommand).catch((error) => {
logger.error("Promise rejected with", error)
logger.log('error', "Promise rejected with", error);
});
} catch (e) {
logger.error("Unable to processes EPP request");
logger.error(moment().utc().toString() + ": Dispatcher error: ", e);
logger.log('error', "Unable to processes EPP request");
logger.log('error', moment().utc().toString() + ": Dispatcher error: ", e);
this.state = false;
}
}

command(command, data) {
if (!this.state.loggedIn) {
if (command === 'logout') {
logger.warn("Killing child process.");
logger.log("warn", "Killing child process.");
process.exit(0);
} else if (command !== 'login') {
logger.error("Attempted " + command + " while not logged in.");
logger.log('error', "Attempted " + command + " while not logged in.");
//process.send({"error": "Not logged in."});
return;
}
} else if (command) {
logger.debug("Sending a " + command);
logger.log('debug', "Sending a " + command);
var that = this;
var transactionId = data.transactionId;
if (!transactionId) {
Expand All @@ -82,4 +82,77 @@ class Dispatcher {
}
}


Dispatcher.prototype.processMessage = function processMessage(m) {
var currentState = this.state;
var eppCommand;

if (currentState && currentState.connection && currentState.connection.stream) {
var command = m.command;
var data = m.data;
if (!currentState.loggedIn) {
if (command === 'logout') {
logger.warn("Killing child process.");
process.exit(0);
} else if (command !== 'login') {
logger.log("error","Attempted " + command + " while not logged in.");
process.send({ "error": "Not logged in." });
return;
}
} else if (command) {
var transactionId = data.transactionId;
if (!transactionId) {
transactionId = [command, new Date().getTime(), require('crypto').randomBytes(8).toString('hex')].join('-').toUpperCase();
}
eppCommand = function () {
currentState.command(command, data, transactionId).then(function (responseData) {
process.send(responseData);
},
function (error) {
logger.log("error","Command returned an error state:", error);
throw error;
});
};
}
} else { // Initialise a connection instance with registry configuration.
var registry = m.registry;
var registryConfig = nconf.get('registries')[registry];
currentState = new ProtocolState(registry, registryConfig);
var loginTransactionId = ['login', new Date().getTime(), require('crypto').randomBytes(8).toString('hex')].join('-').toUpperCase();

// Initialise the connection stream. Upon connection, attempt
// to login.
eppCommand = function () {
setTimeout(function () {
currentState.login({
"login": registryConfig.login,
"password": registryConfig.password
},
loginTransactionId).then(
function (data) {
logger.log("Got login data: ", data.toString());
process.send(currentState.loggedIn);
},
function (error) {
logger.log("error","Unable to login: ", error);
process.send(currentState.loggedIn);
});
},
2000);
};
}
try {
logger.log("debug", "Calling epp command.");
currentState.connection.initStream().then(eppCommand);
} catch (e) {
logger.log("error",moment().utc().toString() + ": Dispatcher error: ", e);
process.send({
"msg": "Unable to processes EPP request"
});
this.state = false;
}
this.state = currentState;
};


module.exports = Dispatcher;
12 changes: 6 additions & 6 deletions lib/epp-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EppFactory {
}

static pushExtensionCommandMap(epp, command, extension, extensionFunction) {
logger.debug("Adding " + command + ":" + extension + ":" + extensionFunction + " to epp object ");
logger.log('debug',"Adding " + command + ":" + extension + ":" + extensionFunction + " to epp object ");
if (!epp.extensionCommandMap[command]) {
epp.extensionCommandMap[command] = {};
}
Expand All @@ -27,25 +27,25 @@ class EppFactory {

static generate(registry, config) {
var epp = new EPP(registry, config);
config.extensionClasses && config.extensionClasses.forEach((extensionClass) => {
config?.extensionClasses && config.extensionClasses.forEach((extensionClass) => {
var extension = extensionClass.extension;
var className = extensionClass.className;
var mixer,
mapper;
switch (className) {
case 'SecDnsExtension':
logger.debug("Applying secDNS mixin");
logger.log('debug',"Applying secDNS mixin");
mixer = SecDnsExtension;
mapper = SecDnsExtension.mixinMapper();
break;
case 'HexonetExtension':
mixer = HexonetExtension;
logger.debug("Applying hexonet mixin");
logger.log('debug',"Applying hexonet mixin");
mapper = HexonetExtension.mixinMapper();
break;
case 'AfiliasExtension':
mixer = AfiliasExtension;
logger.debug("Applying afilias mixin");
logger.log('debug',"Applying afilias mixin");
mapper = AfiliasExtension.mixinMapper();
break;
default:
Expand All @@ -63,7 +63,7 @@ class EppFactory {

return epp;
}


}

Expand Down
14 changes: 7 additions & 7 deletions lib/epp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EPP {
this.config = config;
this.extensionCommandMap = {};
}

hello() {
return this.eppWrapper({
"hello": null
Expand Down Expand Up @@ -246,16 +246,16 @@ class EPP {
var preppedCurAddr = {};
var {city, sp, pc, cc, street} = curAddr
if (street && street.length > 0) {
preppedCurAddr["contact:street"] = street;
preppedCurAddr["contact:street"] = street;
}
if (city) {
preppedCurAddr["contact:city"] = city;
preppedCurAddr["contact:city"] = city;
}
if (sp) {
preppedCurAddr["contact:sp"] = sp;
}
if (pc) {
preppedCurAddr["contact:pc"] = pc;
preppedCurAddr["contact:pc"] = pc;
}
if (cc) {
preppedCurAddr["contact:cc"] = cc;
Expand All @@ -280,7 +280,7 @@ class EPP {
/* Try and convert a contact's name into something EPP recognises. EPP expects
* a single "name" field with the contact's first and last name. Most
* homegrown stuff stores it separately as "firstname" and "lastname".
*
*
* */
processContactName(data) {
if (!data.hasOwnProperty('name')) {
Expand Down Expand Up @@ -325,7 +325,7 @@ class EPP {
if (name) {
processedPostal["contact:name"] = name;
}
if (org) {
if (org) {
processedPostal["contact:org"] = org;
}
if (addr) {
Expand Down Expand Up @@ -963,7 +963,7 @@ class EPP {
try {
xml = convert(root, eppData);
} catch (e) {
logger.error("Caught an error while generating EPP: ", e);
logger.log('error',"Caught an error while generating EPP: ", e);
}
return xml;
}
Expand Down
Loading