diff --git a/.gitignore b/.gitignore index 9ceba76..c31ed8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules dist -test .vscode .jshintrc jsconfig.json src/parser.js +package-lock.json diff --git a/Readme.md b/Readme.md index 3b81cc5..822e997 100644 --- a/Readme.md +++ b/Readme.md @@ -112,6 +112,15 @@ With the above code, the following is API description. conn is Connection object * conn.getHost() * conn.getUser() +### Login + The login function receives 3 parameters (username: String, password: String, newMethod: Boolean), if the newMethod is set to True, it will use the login method introduced at [v6.43](https://forum.mikrotik.com/viewtopic.php?f=21&t=138995) and made obligatory at [v6.45.1](https://forum.mikrotik.com/viewtopic.php?f=21&t=149786). +```javascript + var MikroNode = require('mikronode'); + var Device =new MikroNode(host,port); + Device.connect().then(([login])=>login('admin','password', true)).then(function(conn) { + var chan=conn.openChannel(); + }); +``` ### Channel diff --git a/package.json b/package.json index 565b234..84dcff8 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,9 @@ "chai": "^3.5.0", "core-decorators": "^0.12.3", "mocha": ">=1.7.4", - "rxjs": "^5.3.0", + "pegjs": "^0.10.0", + "promise": "^8.0.3", + "rxjs": "^5.5.12", "simple-assign": "^0.1.0", "uglify-js": ">=1.2.5", "webpack": "^1.13.1", @@ -55,5 +57,6 @@ "main": "dist/mikronode.js", "engines": { "node": ">= 6" - } + }, + "dependencies": {} } diff --git a/src/index.js b/src/index.js index 6081b07..da749e9 100644 --- a/src/index.js +++ b/src/index.js @@ -158,30 +158,40 @@ class MikroNode { const close=()=>this.sock.getStream().sentence.complete(); - const login=(user,password,cb)=>{ + const login=(user,password,newMethod = false,cb)=>{ this.debug>=DEBUG.DEBUG&&console.log('Logging in'); - stream.write('/login'); - const {promise,resolve,reject}=getUnwrappedPromise(); - // Create a connection handler - this.connection=new Connection( - {...stream,close}, - challenge=>{ - const md5=crypto.createHash('md5'); - md5.update(Buffer.concat([Buffer.from(nullString+password),Buffer.from(challenge)])); - stream.write([ - "/login", - "=name="+user, - "=response=00"+md5.digest("hex") - ]); - },{resolve,reject} - ); - this.connection.setDebug(this.debug); - promise.then(()=>{ - if (cb) cb(null,this.connection); - },err=>{ - if (cb) cb(err,null); - }); - return promise; + // if(!newMethod){ + stream.write('/login'); + // } + const {promise,resolve,reject}=getUnwrappedPromise(); + // Create a connection handler + this.connection=new Connection( + {...stream,close}, + challenge=>{ + if(newMethod){ + stream.write([ + "/login", + "=name="+user, + "=password="+password + ]); + } else { + const md5=crypto.createHash('md5'); + md5.update(Buffer.concat([Buffer.from(nullString+password),Buffer.from(challenge)])); + stream.write([ + "/login", + "=name="+user, + "=response=00"+md5.digest("hex") + ]); + } + },{resolve,reject} + ); + this.connection.setDebug(this.debug); + promise.then(()=>{ + if (cb) cb(null,this.connection); + },err=>{ + if (cb) cb(err,null); + }); + return promise; }; this.debug>=DEBUG.SILLY&&console.log('Creating promise for socket connect'); diff --git a/test/testNewLogin.js b/test/testNewLogin.js new file mode 100644 index 0000000..4ff188e --- /dev/null +++ b/test/testNewLogin.js @@ -0,0 +1,12 @@ + +var api=require('../dist/mikronode.js'); + +var device=new api(/* Host */'127.0.0.1' /*, Port */ /*, Timeout */); +// device.setDebug(api.DEBUG); + +// connect: +device.connect().then(([login])=>login('username','password')).then(function(conn) { + console.log("Logged in"); +},function(err) { + console.log("Error connecting:",err); +});