Commit 83b2fc58 authored by Benjamin Chelli's avatar Benjamin Chelli

Refactor messages to remove duplicate code

parent 7656a83b
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, params){
return new SMB2Message({
headers:{
'Command':'CLOSE'
......@@ -17,19 +18,8 @@ module.exports = {
, request:{
'FileId':params.FileId
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(null);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, params){
var buffer = new Buffer(params.path, 'ucs2');
return new SMB2Message({
headers:{
'Command':'CREATE'
......@@ -25,19 +27,8 @@ module.exports = {
, 'NameOffset':0x0078
, 'CreateContextsOffset':0x007A+buffer.length
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(null, response.getResponse());
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection){
return new SMB2Message({
headers:{
'Command':'NEGOTIATE'
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(null);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, params){
var buffer = new Buffer(params.path, 'ucs2');
return new SMB2Message({
headers:{
'Command':'CREATE'
......@@ -20,19 +22,8 @@ module.exports = {
, 'NameOffset':0x0078
, 'CreateContextsOffset':0x007A+buffer.length
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(null, response.getResponse());
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, params){
return new SMB2Message({
headers:{
'Command':'QUERY_DIRECTORY'
......@@ -18,24 +19,15 @@ module.exports = {
'FileId':params.FileId
, 'Buffer':new Buffer('*', 'ucs2')
}
})
});
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(
null
, parseFiles(response.getResponse().Buffer)
);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
, parseResponse:function(response){
return parseFiles(response.getResponse().Buffer);
}
}
});
......
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, file){
return new SMB2Message({
headers:{
'Command':'READ'
......@@ -19,19 +20,12 @@ module.exports = {
, 'Length':file.Length
, 'Offset':file.Offset
}
})
});
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if (err.code == 'STATUS_SUCCESS'){
cb && cb(null, response.getResponse().Buffer);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
, parseResponse:function(response){
return response.getResponse().Buffer;
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
, ntlm = require('ntlm')
;
module.exports = {
module.exports = message({
generate:function(connection){
return new SMB2Message({
headers:{
'Command':'SESSION_SETUP'
......@@ -19,21 +20,17 @@ module.exports = {
, connection.domain
)
}
})
});
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_MORE_PROCESSING_REQUIRED'){
connection.SessionId = h.SessionId;
connection.nonce = ntlm.decodeType2(response.getResponse().Buffer);
cb && cb(null);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
, successCode: 'STATUS_MORE_PROCESSING_REQUIRED'
, onSuccess:function(connection, response){
var h = response.getHeaders();
connection.SessionId = h.SessionId;
connection.nonce = ntlm.decodeType2(response.getResponse().Buffer);
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
, ntlm = require('ntlm')
;
module.exports = {
module.exports = message({
generate:function(connection){
return new SMB2Message({
headers:{
'Command':'SESSION_SETUP'
......@@ -23,21 +24,8 @@ module.exports = {
, connection.password
)
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if( err.code == 'STATUS_LOGON_FAILURE'){
cb && cb(new Error('Login attempt fail'));
} else if(err.code == 'STATUS_SUCCESS'){
cb && cb(null);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
......@@ -21,8 +20,10 @@ var fileInfoClasses = {
, 'FileValidDataLengthInformation': 39
};
module.exports = {
module.exports = message({
generate:function(connection, params){
return new SMB2Message({
headers:{
'Command':'SET_INFO'
......@@ -34,19 +35,8 @@ module.exports = {
, 'FileId':params.FileId
, 'Buffer':params.Buffer
}
})
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
cb && cb(null, response.getResponse());
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
});
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection){
return new SMB2Message({
headers:{
'Command':'TREE_CONNECT'
......@@ -16,20 +17,14 @@ module.exports = {
, request:{
'Buffer':new Buffer(connection.fullPath, 'ucs2')
}
})
});
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == 'STATUS_SUCCESS'){
connection.TreeId = h.TreeId;
cb && cb(null);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
, onSuccess:function(connection, response){
var h = response.getHeaders();
connection.TreeId = h.TreeId;
}
}
});
var SMB2Message = require('../message')
, MsErref = require('../tools/ms_erref')
, bigint = require('../tools/bigint')
, message = require('../tools/message')
;
module.exports = {
module.exports = message({
generate:function(connection, params){
return new SMB2Message({
headers:{
'Command':'WRITE'
......@@ -19,19 +20,12 @@ module.exports = {
, 'Offset':params.Offset
, 'Buffer':params.Buffer
}
})
});
}
, parse:function(connection, cb){
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if (err.code == 'STATUS_SUCCESS'){
cb && cb(null, response.getResponse().Buffer);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
}
, parseResponse:function(response){
return response.getResponse().Buffer;
}
}
});
var MsErref = require('./ms_erref')
, bigint = require('./bigint')
;
var defaults = {
successCode: 'STATUS_SUCCESS'
, parse:function(connection, cb){
var self = this;
return function(response){
var h = response.getHeaders()
, err = MsErref.getStatus(bigint.fromBuffer(h.Status).toNumber())
;
if(err.code == self.successCode){
self.onSuccess && self.onSuccess(connection, response);
cb && cb(
null
, self.parseResponse && self.parseResponse(response)
);
} else {
cb && cb(new Error(MsErref.getErrorMessage(err)));
}
};
}
, parseResponse:function(response){
return response.getResponse();
}
};
module.exports = function(obj){
for ( var key in defaults ) {
obj[key] = obj[key] || defaults[key];
}
return obj;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment