Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Implementation] Added a newMethod flag to support v6.45.1 #88

Open
wants to merge 3 commits into
base: release
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules
dist
test
.vscode
.jshintrc
jsconfig.json
src/parser.js
package-lock.json
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -55,5 +57,6 @@
"main": "dist/mikronode.js",
"engines": {
"node": ">= 6"
}
},
"dependencies": {}
}
56 changes: 33 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions test/testNewLogin.js
Original file line number Diff line number Diff line change
@@ -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);
});