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,14 +24,8 @@ var s = new SMB2({
, password:'password!'
});
// negotiate the connection with the server
s.connect(function(err){
// if an error happen => throw it
if(err) throw err;
// read the content of the folder
s.readDir('Windows\\System32', function(err, files){
// read the content of the folder
s.readDir('Windows\\System32', function(err, files){
// if an error happen => throw it
if(err) throw err;
......@@ -42,8 +36,6 @@ s.connect(function(err){
// close the connection
s.close();
});
});
```
......
......@@ -76,50 +76,6 @@ var SMB = module.exports = function(opt){
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
* =====
......@@ -150,8 +106,10 @@ proto.close = function(){
proto.readdir = function(path, cb){
var connection = this;
connect(connection, function(err){
if(err) cb(err);
// SMB2 open directory
SMB2Request('open', {path:path}, connection, function(err, file){
else SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 query directory
else SMB2Request('query_directory', file, connection, function(err, files){
......@@ -167,6 +125,8 @@ proto.readdir = function(path, cb){
});
});
});
})
}
......@@ -191,8 +151,10 @@ proto.readFile = function(filename, options, cb){
options = {};
}
connect(connection, function(err){
if(err) cb(err);
// SMB2 open file
SMB2Request('open', {path:filename}, connection, function(err, file){
else SMB2Request('open', {path:filename}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 read file content
else {
......@@ -250,6 +212,7 @@ proto.readFile = function(filename, options, cb){
checkDone();
}
});
});
}
......@@ -355,7 +318,10 @@ proto.writeFile = function(filename, data, options, cb){
checkDone();
}
this.exists(filename, function(err, exists){
connect(connection, function(err){
if(err) cb(err);
else connection.exists(filename, function(err, exists){
if(err) cb && cb(err);
......@@ -376,6 +342,7 @@ proto.writeFile = function(filename, data, options, cb){
}
});
});
}
......@@ -395,12 +362,15 @@ proto.exists = function(path, cb){
var connection = this;
SMB2Request('open', {path:path}, connection, function(err, file){
connect(connection, function(err){
if(err) cb(err);
else SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(null, false);
else SMB2Request('close', file, connection, function(err){
cb && cb(null, true);
});
});
});
}
......@@ -421,7 +391,9 @@ proto.exists = function(path, cb){
proto.unlinkFile = function(path, cb){
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);
......@@ -447,7 +419,44 @@ proto.unlinkFile = function(path, cb){
}
});
});
}
/*
* 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);
}
});
});
});
});
}
......
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