Commit af5df00f authored by Benjamin Chelli's avatar Benjamin Chelli

remove the connect function and make it called implicitly by every single...

remove the connect function and make it called implicitly by every single function trying to interact with the share
parent 59423dd3
...@@ -24,26 +24,18 @@ var s = new SMB2({ ...@@ -24,26 +24,18 @@ var s = new SMB2({
, password:'password!' , password:'password!'
}); });
// negotiate the connection with the server // read the content of the folder
s.connect(function(err){ s.readDir('Windows\\System32', function(err, files){
// if an error happen => throw it // if an error happen => throw it
if(err) throw err; if(err) throw err;
// read the content of the folder // display content in the console
s.readDir('Windows\\System32', function(err, files){ console.log(files);
// if an error happen => throw it // close the connection
if(err) throw err; s.close();
// display content in the console
console.log(files);
// close the connection
s.close();
});
}); });
``` ```
......
...@@ -76,50 +76,6 @@ var SMB = module.exports = function(opt){ ...@@ -76,50 +76,6 @@ var SMB = module.exports = function(opt){
var proto = SMB.prototype = {}; var proto = SMB.prototype = {};
/*
* connect
* =======
*
* connect you to the SMB2 server:
*
* - open TCP socket
*
* - negotiation SM2 connection
*
* - setup session / ntlm negotiatiation
*
* - setup session / ntlm authetication
*
* - tree connect
*
*/
proto.connect = function(cb){
var connection = this;
// open TCP socket
connection.socket.connect(port, this.ip);
// SMB2 negotiate connection
SMB2Request('negotiate', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / negotiate ntlm
else SMB2Request('session_setup_step1', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / autheticate with ntlm
else SMB2Request('session_setup_step2', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 tree connect
else SMB2Request('tree_connect', {}, connection, function(err){
if(err) cb && cb(err);
else cb && cb(null);
});
});
});
});
}
/* /*
* close * close
* ===== * =====
...@@ -150,23 +106,27 @@ proto.close = function(){ ...@@ -150,23 +106,27 @@ proto.close = function(){
proto.readdir = function(path, cb){ proto.readdir = function(path, cb){
var connection = this; var connection = this;
// SMB2 open directory connect(connection, function(err){
SMB2Request('open', {path:path}, connection, function(err, file){ if(err) cb(err);
if(err) cb && cb(err); // SMB2 open directory
// SMB2 query directory else SMB2Request('open', {path:path}, connection, function(err, file){
else SMB2Request('query_directory', file, connection, function(err, files){
if(err) cb && cb(err); if(err) cb && cb(err);
// SMB2 close directory // SMB2 query directory
else SMB2Request('close', file, connection, function(err){ else SMB2Request('query_directory', file, connection, function(err, files){
cb && cb( if(err) cb && cb(err);
err // SMB2 close directory
, files else SMB2Request('close', file, connection, function(err){
.map(function(v){ return v.Filename }) // get the filename only cb && cb(
.filter(function(v){ return v!='.' && v!='..' }) // remove '.' and '..' values err
); , files
.map(function(v){ return v.Filename }) // get the filename only
.filter(function(v){ return v!='.' && v!='..' }) // remove '.' and '..' values
);
});
}); });
}); });
}); })
} }
...@@ -191,64 +151,67 @@ proto.readFile = function(filename, options, cb){ ...@@ -191,64 +151,67 @@ proto.readFile = function(filename, options, cb){
options = {}; options = {};
} }
// SMB2 open file connect(connection, function(err){
SMB2Request('open', {path:filename}, connection, function(err, file){ if(err) cb(err);
if(err) cb && cb(err); // SMB2 open file
// SMB2 read file content else SMB2Request('open', {path:filename}, connection, function(err, file){
else { if(err) cb && cb(err);
var fileLength = 0 // SMB2 read file content
, offset = new bigint(8) else {
, stop = false var fileLength = 0
, nbRemainingPackets = 0 , offset = new bigint(8)
, maxPacketSize = 0x00010000 , stop = false
; , nbRemainingPackets = 0
// get file length , maxPacketSize = 0x00010000
for(var i=0;i<file.EndofFile.length;i++){ ;
fileLength |= file.EndofFile[i] << (i*8); // get file length
} for(var i=0;i<file.EndofFile.length;i++){
var result = new Buffer(fileLength); fileLength |= file.EndofFile[i] << (i*8);
// callback manager }
function callback(offset){ var result = new Buffer(fileLength);
return function(err, content){ // callback manager
if(stop) return; function callback(offset){
if(err) { return function(err, content){
cb && cb(err); if(stop) return;
stop = true; if(err) {
} else { cb && cb(err);
content.copy(result, offset.toNumber()); stop = true;
nbRemainingPackets--; } else {
checkDone(); content.copy(result, offset.toNumber());
nbRemainingPackets--;
checkDone();
}
} }
} }
} // callback manager
// callback manager function checkDone(){
function checkDone(){ if(stop) return;
if(stop) return; createPackets();
createPackets(); if(nbRemainingPackets==0 && offset.ge(fileLength)) {
if(nbRemainingPackets==0 && offset.ge(fileLength)) { SMB2Request('close', file, connection, function(err){
SMB2Request('close', file, connection, function(err){ cb && cb(err, result.toString());
cb && cb(err, result.toString()); })
}) }
} }
} // create packets
// create packets function createPackets(){
function createPackets(){ while(nbRemainingPackets<connection.packetConcurrency && offset.lt(fileLength)){
while(nbRemainingPackets<connection.packetConcurrency && offset.lt(fileLength)){ // process packet size
// process packet size var rest = offset.sub(fileLength).neg();
var rest = offset.sub(fileLength).neg(); var packetSize = rest.gt(maxPacketSize) ? maxPacketSize : rest.toNumber();
var packetSize = rest.gt(maxPacketSize) ? maxPacketSize : rest.toNumber(); // generate buffer
// generate buffer SMB2Request('read', {
SMB2Request('read', { 'FileId':file.FileId
'FileId':file.FileId , 'Length':packetSize
, 'Length':packetSize , 'Offset':offset.toBuffer()
, 'Offset':offset.toBuffer() }, connection, callback(offset));
}, connection, callback(offset)); offset = offset.add(packetSize);
offset = offset.add(packetSize); nbRemainingPackets++;
nbRemainingPackets++; }
} }
checkDone();
} }
checkDone(); });
}
}); });
} }
...@@ -355,26 +318,30 @@ proto.writeFile = function(filename, data, options, cb){ ...@@ -355,26 +318,30 @@ proto.writeFile = function(filename, data, options, cb){
checkDone(); checkDone();
} }
this.exists(filename, function(err, exists){
if(err) cb && cb(err); connect(connection, function(err){
if(err) cb(err);
else connection.exists(filename, function(err, exists){
else if(!exists){ if(err) cb && cb(err);
else if(!exists){
createFile(function(){ createFile(function(){
setFileSize(function(){ setFileSize(function(){
writeFile(function(){ writeFile(function(){
closeFile(cb); closeFile(cb);
});
}); });
}); });
});
} else { } else {
cb(new Error('File already exists')); cb(new Error('File already exists'));
} }
});
}); });
} }
...@@ -395,10 +362,13 @@ proto.exists = function(path, cb){ ...@@ -395,10 +362,13 @@ proto.exists = function(path, cb){
var connection = this; var connection = this;
SMB2Request('open', {path:path}, connection, function(err, file){ connect(connection, function(err){
if(err) cb && cb(null, false); if(err) cb(err);
else SMB2Request('close', file, connection, function(err){ else SMB2Request('open', {path:path}, connection, function(err, file){
cb && cb(null, true); if(err) cb && cb(null, false);
else SMB2Request('close', file, connection, function(err){
cb && cb(null, true);
});
}); });
}); });
...@@ -421,36 +391,75 @@ proto.exists = function(path, cb){ ...@@ -421,36 +391,75 @@ proto.exists = function(path, cb){
proto.unlinkFile = function(path, cb){ proto.unlinkFile = function(path, cb){
var connection = this; var connection = this;
this.exists(path, function(err, exists){ connect(connection, function(err){
if(err) cb(err);
else connection.exists(path, function(err, exists){
if(err) cb && cb(err); if(err) cb && cb(err);
else if(exists){ else if(exists){
// SMB2 open file // SMB2 open file
SMB2Request('create', {path:path}, connection, function(err, file){ SMB2Request('create', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 query directory
else SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileDispositionInformation',Buffer:(new bigint(1,1)).toBuffer()}, connection, function(err, files){
if(err) cb && cb(err); if(err) cb && cb(err);
// SMB2 close directory // SMB2 query directory
else SMB2Request('close', file, connection, function(err){ else SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileDispositionInformation',Buffer:(new bigint(1,1)).toBuffer()}, connection, function(err, files){
cb && cb(err, files); if(err) cb && cb(err);
// SMB2 close directory
else SMB2Request('close', file, connection, function(err){
cb && cb(err, files);
});
}); });
}); });
});
} else { } else {
cb(new Error('File does not exists')); cb(new Error('File does not exists'));
} }
});
}); });
} }
/*
* PRIVATE FUNCTION TO HANDLE CONNECTION
*/
connect = function(connection, cb){
if(connection.connected){
cb && cb(null);
return;
}
// open TCP socket
connection.socket.connect(port, connection.ip);
// SMB2 negotiate connection
SMB2Request('negotiate', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / negotiate ntlm
else SMB2Request('session_setup_step1', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / autheticate with ntlm
else SMB2Request('session_setup_step2', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 tree connect
else SMB2Request('tree_connect', {}, connection, function(err){
if(err) cb && cb(err);
else {
connection.connected = true;
cb && cb(null);
}
});
});
});
});
}
/* /*
* MESSAGE MANAGMENT * MESSAGE MANAGMENT
*/ */
......
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